Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Network package

Grigorij Eleskin edited this page Aug 13, 2017 · 2 revisions

Instruction about the network package in the emulator.

Structure of folders in package:

  • errors
  • in
  • opcodes
  • out

Detailed information about each the internal package:

  • errors package -> describes errors which client accepted in packets, there we are set number of a error and an internal message for logger
  • in package -> describes structure of packets with functions which accepted by IIn interface, it's Opcode() and Unpack(pr *barrel.Processor). These packets parsed from client and will send to the actors.
  • opcodes package -> describes all of opcodes as enums
  • out package -> describes structure of packets with functions which accepted by IOut interface, it's Opcode() and Pack(pr *barrel.Processor). These packets formed and will send to the client.

Okay, we figured out with notions, next things will be about how we can write In and Out packets.

Let's pick an abstract packet like Move, it must have Request and Response form.

Request form (It's in package)

type Move struct {
}

func (m Move) Opcode() uint16 {
	return opcodes.OP_CLIENT_MOVE
}

func (m *Move) Unpack(pr *barrel.Processor) {
}

in Unpack method we should use pr variable to get an information from packet to structure. You can find docs about exists methods in https://godoc.org/github.com/Nyarum/barrel, basically here we need methods which starting with Read*.

For example we have fields like:

StartX uint16
StartY uint16
EndX uint16
EndY uint16

Then we can transform our struct to this form:

type Move struct {
	StartX uint16
	StartY uint16
	EndX uint16
	EndY uint16
}

func (m Move) Opcode() uint16 {
	return opcodes.OP_CLIENT_MOVE
}

func (m *Move) Unpack(pr *barrel.Processor) {
	pr.ReadUint16(&m.StartX)
	pr.ReadUint16(&m.StartY)
	pr.ReadUint16(&m.EndX)
	pr.ReadUint16(&m.EndY)
}

Okay, we figured out with describe of packet

Now we must add this packet to a general network.go file

There are you can find a code like in NetNetwork() method:

packets := make(map[uint16]in.IIn)
packets[(in.Ping{}).Opcode()] = &in.Ping{}
packets[(in.Auth{}).Opcode()] = &in.Auth{}
packets[(in.Exit{}).Opcode()] = &in.Exit{}

We must to add a new field to the map that parser can parse the packet from client

packets[(in.Move{}).Opcode()] = &in.Move{}

And all, we are done with that :)

Response form (It's out package)

type Move struct {
}

func (m Move) Opcode() uint16 {
	return opcodes.OP_SERVER_MOVE
}

func (m *Move) Pack(pr *barrel.Processor) {
}

We do an identical steps like in Request form but exclude the step about add a packet to the accept map And instead Read* methods we must to use Write* methods that we can compose fields to []byte view

Finally response form will view like:

type Move struct {
	StartX uint16
	StartY uint16
	EndX uint16
	EndY uint16
}

func (m Move) Opcode() uint16 {
	return opcodes.OP_SERVER_MOVE
}

func (m *Move) Pack(pr *barrel.Processor) {
	pr.WriteUint16(m.StartX)
	pr.WriteUint16(m.StartY)
	pr.WriteUint16(m.EndX)
	pr.WriteUint16(m.EndY)
}

Description about each packet you can take from official source files. Structs above it's just example.

Clone this wiki locally