|
|
@ -1,4 +1,4 @@
|
|
|
|
package protocol
|
|
|
|
package nnet
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"bytes"
|
|
|
@ -7,7 +7,7 @@ import (
|
|
|
|
"git.noahlan.cn/noahlan/nnet/packet"
|
|
|
|
"git.noahlan.cn/noahlan/nnet/packet"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type NNetPacker struct {
|
|
|
|
type Packer struct {
|
|
|
|
buf *bytes.Buffer
|
|
|
|
buf *bytes.Buffer
|
|
|
|
size int // 最近一次 length
|
|
|
|
size int // 最近一次 length
|
|
|
|
typ byte // 最近一次 packet type
|
|
|
|
typ byte // 最近一次 packet type
|
|
|
@ -31,13 +31,10 @@ var (
|
|
|
|
ErrWrongMessageType = errors.New("wrong message type")
|
|
|
|
ErrWrongMessageType = errors.New("wrong message type")
|
|
|
|
ErrRouteInfoNotFound = errors.New("route info not found in dictionary")
|
|
|
|
ErrRouteInfoNotFound = errors.New("route info not found in dictionary")
|
|
|
|
ErrWrongMessage = errors.New("wrong message")
|
|
|
|
ErrWrongMessage = errors.New("wrong message")
|
|
|
|
|
|
|
|
|
|
|
|
// ErrWrongPacketType represents a wrong packet type.
|
|
|
|
|
|
|
|
ErrWrongPacketType = errors.New("wrong packet type")
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func NewNNetPacker(routeMap *RouteMap) *NNetPacker {
|
|
|
|
func NewPacker(routeMap *RouteMap) *Packer {
|
|
|
|
p := &NNetPacker{
|
|
|
|
p := &Packer{
|
|
|
|
buf: bytes.NewBuffer(nil),
|
|
|
|
buf: bytes.NewBuffer(nil),
|
|
|
|
routeMap: routeMap,
|
|
|
|
routeMap: routeMap,
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -45,29 +42,29 @@ func NewNNetPacker(routeMap *RouteMap) *NNetPacker {
|
|
|
|
return p
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (d *NNetPacker) resetFlags() {
|
|
|
|
func (d *Packer) resetFlags() {
|
|
|
|
d.size = -1
|
|
|
|
d.size = -1
|
|
|
|
d.typ = byte(Unknown)
|
|
|
|
d.typ = byte(Unknown)
|
|
|
|
d.flag = 0x00
|
|
|
|
d.flag = 0x00
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (d *NNetPacker) routable(t MsgType) bool {
|
|
|
|
func (d *Packer) routable(t MsgType) bool {
|
|
|
|
return t == Request || t == Notify || t == Push
|
|
|
|
return t == Request || t == Notify || t == Push
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (d *NNetPacker) invalidType(t MsgType) bool {
|
|
|
|
func (d *Packer) invalidType(t MsgType) bool {
|
|
|
|
return t < Request || t > Push
|
|
|
|
return t < Request || t > Push
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (d *NNetPacker) Pack(header interface{}, data []byte) ([]byte, error) {
|
|
|
|
func (d *Packer) Pack(header interface{}, data []byte) ([]byte, error) {
|
|
|
|
h, ok := header.(Header)
|
|
|
|
h, ok := header.(Header)
|
|
|
|
if !ok {
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrWrongPacketType
|
|
|
|
return nil, packet.ErrWrongPacketType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
typ := h.PacketType
|
|
|
|
typ := h.PacketType
|
|
|
|
|
|
|
|
|
|
|
|
if typ < Handshake || typ > Kick {
|
|
|
|
if typ < Handshake || typ > Kick {
|
|
|
|
return nil, ErrWrongPacketType
|
|
|
|
return nil, packet.ErrWrongPacketType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if d.invalidType(h.MsgType) {
|
|
|
|
if d.invalidType(h.MsgType) {
|
|
|
@ -123,7 +120,7 @@ func (d *NNetPacker) Pack(header interface{}, data []byte) ([]byte, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Encode packet data length to bytes(Big end)
|
|
|
|
// Encode packet data length to bytes(Big end)
|
|
|
|
func (d *NNetPacker) intToBytes(n uint32) []byte {
|
|
|
|
func (d *Packer) intToBytes(n uint32) []byte {
|
|
|
|
buf := make([]byte, 3)
|
|
|
|
buf := make([]byte, 3)
|
|
|
|
buf[0] = byte((n >> 16) & 0xFF)
|
|
|
|
buf[0] = byte((n >> 16) & 0xFF)
|
|
|
|
buf[1] = byte((n >> 8) & 0xFF)
|
|
|
|
buf[1] = byte((n >> 8) & 0xFF)
|
|
|
@ -131,7 +128,7 @@ func (d *NNetPacker) intToBytes(n uint32) []byte {
|
|
|
|
return buf
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (d *NNetPacker) Unpack(data []byte) ([]packet.IPacket, error) {
|
|
|
|
func (d *Packer) Unpack(data []byte) ([]packet.IPacket, error) {
|
|
|
|
d.buf.Write(data) // copy
|
|
|
|
d.buf.Write(data) // copy
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
var (
|
|
|
@ -219,11 +216,11 @@ func (d *NNetPacker) Unpack(data []byte) ([]packet.IPacket, error) {
|
|
|
|
return packets, nil
|
|
|
|
return packets, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (d *NNetPacker) readHeader() error {
|
|
|
|
func (d *Packer) readHeader() error {
|
|
|
|
header := d.buf.Next(headLength)
|
|
|
|
header := d.buf.Next(headLength)
|
|
|
|
d.typ = header[0]
|
|
|
|
d.typ = header[0]
|
|
|
|
if d.typ < Handshake || d.typ > Kick {
|
|
|
|
if d.typ < Handshake || d.typ > Kick {
|
|
|
|
return ErrWrongPacketType
|
|
|
|
return packet.ErrWrongPacketType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.size = d.bytesToInt(header[1 : len(header)-1])
|
|
|
|
d.size = d.bytesToInt(header[1 : len(header)-1])
|
|
|
|
d.flag = header[len(header)-1]
|
|
|
|
d.flag = header[len(header)-1]
|
|
|
@ -236,7 +233,7 @@ func (d *NNetPacker) readHeader() error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Decode packet data length byte to int(Big end)
|
|
|
|
// Decode packet data length byte to int(Big end)
|
|
|
|
func (d *NNetPacker) bytesToInt(b []byte) int {
|
|
|
|
func (d *Packer) bytesToInt(b []byte) int {
|
|
|
|
result := 0
|
|
|
|
result := 0
|
|
|
|
for _, v := range b {
|
|
|
|
for _, v := range b {
|
|
|
|
result = result<<8 + int(v)
|
|
|
|
result = result<<8 + int(v)
|