move command types to commands package
parent
11a6dd52de
commit
9e865e83bb
16
client.go
16
client.go
|
@ -16,7 +16,7 @@ type Client struct {
|
|||
PeerID uint16
|
||||
sph *packet.SplitpacketHandler
|
||||
netrx chan []byte
|
||||
listeners []chan packet.Command
|
||||
listeners []chan commands.Command
|
||||
listener_lock *sync.RWMutex
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@ func NewClient(host string, port int) *Client {
|
|||
Port: port,
|
||||
sph: packet.NewSplitPacketHandler(),
|
||||
netrx: make(chan []byte, 1000),
|
||||
listeners: make([]chan packet.Command, 0),
|
||||
listeners: make([]chan commands.Command, 0),
|
||||
listener_lock: &sync.RWMutex{},
|
||||
}
|
||||
}
|
||||
|
@ -58,13 +58,13 @@ func (c *Client) Init() error {
|
|||
return c.Send(peerInit)
|
||||
}
|
||||
|
||||
func (c *Client) AddListener(ch chan packet.Command) {
|
||||
func (c *Client) AddListener(ch chan commands.Command) {
|
||||
c.listener_lock.Lock()
|
||||
defer c.listener_lock.Unlock()
|
||||
c.listeners = append(c.listeners, ch)
|
||||
}
|
||||
|
||||
func (c *Client) emitCommand(cmd packet.Command) {
|
||||
func (c *Client) emitCommand(cmd commands.Command) {
|
||||
c.listener_lock.RLock()
|
||||
defer c.listener_lock.RUnlock()
|
||||
|
||||
|
@ -76,10 +76,10 @@ func (c *Client) emitCommand(cmd packet.Command) {
|
|||
}
|
||||
}
|
||||
|
||||
func (c *Client) SendOriginalCommand(cmd packet.Command) error {
|
||||
func (c *Client) SendOriginalCommand(cmd commands.Command) error {
|
||||
//fmt.Printf("Sending original command: %s\n", cmd)
|
||||
|
||||
payload, err := packet.CreatePayload(cmd)
|
||||
payload, err := commands.CreatePayload(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -88,10 +88,10 @@ func (c *Client) SendOriginalCommand(cmd packet.Command) error {
|
|||
return c.Send(pkg)
|
||||
}
|
||||
|
||||
func (c *Client) SendCommand(cmd packet.Command) error {
|
||||
func (c *Client) SendCommand(cmd commands.Command) error {
|
||||
//fmt.Printf("Sending command: %s\n", cmd)
|
||||
|
||||
payload, err := packet.CreatePayload(cmd)
|
||||
payload, err := commands.CreatePayload(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ type ClientHandler struct {
|
|||
}
|
||||
|
||||
func (ch *ClientHandler) HandlerLoop() {
|
||||
cmd_chan := make(chan packet.Command, 500)
|
||||
cmd_chan := make(chan commands.Command, 500)
|
||||
ch.Client.AddListener(cmd_chan)
|
||||
|
||||
for o := range cmd_chan {
|
||||
|
|
|
@ -3,14 +3,13 @@ package commands
|
|||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"minetest_client/packet"
|
||||
)
|
||||
|
||||
func Parse(payload []byte) (packet.Command, error) {
|
||||
func Parse(payload []byte) (Command, error) {
|
||||
commandId := binary.BigEndian.Uint16(payload[0:])
|
||||
commandPayload := payload[2:]
|
||||
|
||||
var cmd packet.Command
|
||||
var cmd Command
|
||||
|
||||
switch commandId {
|
||||
case ServerCommandSetPeer:
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package commands
|
||||
|
||||
import "encoding/binary"
|
||||
|
||||
func CreatePayload(cmd Command) ([]byte, error) {
|
||||
inner_payload, err := cmd.MarshalPacket()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payload := make([]byte, len(inner_payload)+2)
|
||||
copy(payload[2:], inner_payload)
|
||||
|
||||
binary.BigEndian.PutUint16(payload[0:], cmd.GetCommandId())
|
||||
|
||||
return payload, nil
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package packet
|
||||
package commands
|
||||
|
||||
type Serializeable interface {
|
||||
MarshalPacket() ([]byte, error)
|
|
@ -51,20 +51,6 @@ func NextSequenceNr() uint16 {
|
|||
return seqNr
|
||||
}
|
||||
|
||||
func CreatePayload(cmd Command) ([]byte, error) {
|
||||
inner_payload, err := cmd.MarshalPacket()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payload := make([]byte, len(inner_payload)+2)
|
||||
copy(payload[2:], inner_payload)
|
||||
|
||||
binary.BigEndian.PutUint16(payload[0:], cmd.GetCommandId())
|
||||
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func CreateReliable(peerId uint16, payload []byte) *Packet {
|
||||
return &Packet{
|
||||
PacketType: Reliable,
|
||||
|
|
Loading…
Reference in New Issue