wip: 重弄一下编码解码,封包解包。

main
NorthLan 2 years ago
parent 5779bb7989
commit 6ec0070ebe

@ -3,7 +3,6 @@ module git.noahlan.cn/northlan/nnet
go 1.19 go 1.19
require ( require (
github.com/gorilla/websocket v1.5.0 // indirect github.com/gorilla/websocket v1.5.0
github.com/panjf2000/ants/v2 v2.6.0 // indirect github.com/panjf2000/ants/v2 v2.6.0
google.golang.org/protobuf v1.28.1 // indirect
) )

@ -1,10 +1,8 @@
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/panjf2000/ants/v2 v2.6.0 h1:xOSpw42m+BMiJ2I33we7h6fYzG4DAlpE1xyI7VS2gxU= github.com/panjf2000/ants/v2 v2.6.0 h1:xOSpw42m+BMiJ2I33we7h6fYzG4DAlpE1xyI7VS2gxU=
github.com/panjf2000/ants/v2 v2.6.0/go.mod h1:cU93usDlihJZ5CfRGNDYsiBYvoilLvBF5Qp/BT2GNRE= github.com/panjf2000/ants/v2 v2.6.0/go.mod h1:cU93usDlihJZ5CfRGNDYsiBYvoilLvBF5Qp/BT2GNRE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=

@ -45,5 +45,5 @@ func (h *Handler) register(comp component.Component, opts []component.Option) er
} }
func (h *Handler) handle(request *Request) { func (h *Handler) handle(request *Request) {
buf := make([]byte, 3)
} }

@ -0,0 +1,53 @@
package packet
type Packer interface {
// Pack 从原始raw bytes创建一个用于网络传输的 packet.Packet 结构
Pack(typ Type, data []byte) ([]byte, error)
// Unpack 解包
Unpack(data []byte) (*Packet, error)
}
type DefaultPacker struct {
}
// Codec constants.
const (
headLength = 4
maxPacketSize = 64 * 1024
)
func NewDefaultPacker() Packer {
return &DefaultPacker{}
}
func (d *DefaultPacker) Pack(typ Type, data []byte) ([]byte, error) {
if typ < Handshake || typ > Kick {
return nil, ErrWrongPacketType
}
p := &Packet{Type: typ, Length: uint32(len(data))}
buf := make([]byte, p.Length+headLength)
// header
buf[0] = byte(p.Type)
copy(buf[1:headLength], d.intToBytes(p.Length))
// body
copy(buf[headLength:], data)
return buf, nil
}
// Encode packet data length to bytes(Big end)
func (d *DefaultPacker) intToBytes(n uint32) []byte {
buf := make([]byte, 3)
buf[0] = byte((n >> 16) & 0xFF)
buf[1] = byte((n >> 8) & 0xFF)
buf[2] = byte(n & 0xFF)
return buf
}
func (d *DefaultPacker) Unpack(data []byte) (*Packet, error) {
// header
}

@ -1,8 +1,6 @@
package packet package packet
import ( import "errors"
"fmt"
)
// Type 数据帧类型,如:握手,心跳,数据等 // Type 数据帧类型,如:握手,心跳,数据等
type Type byte type Type byte
@ -27,14 +25,13 @@ const (
Kick = 0x05 Kick = 0x05
) )
// ErrWrongPacketType represents a wrong packet type.
var ErrWrongPacketType = errors.New("wrong packet type")
type Packet struct { type Packet struct {
Type Type // 数据帧 类型 Type Type // 数据帧 类型
Length uint32 // 数据长度
HeaderLen uint32 // 数据帧头 长度 Data []byte // 原始数据
HeaderRaw []byte // 头原始数据
DataLen uint32 // 数据长度
DataRaw []byte // 原始数据
} }
func New() *Packet { func New() *Packet {
@ -42,7 +39,3 @@ func New() *Packet {
Type: Default, Type: Default,
} }
} }
func (p *Packet) String() string {
return fmt.Sprintf("Type: %d, HeaderLen: %d, DataLen: %d, Header: %s, Data: %s", p.Type, p.HeaderLen, p.DataLen, string(p.HeaderRaw), string(p.DataRaw))
}

@ -1,6 +1,7 @@
package session package session
import ( import (
"git.noahlan.cn/northlan/nnet/nface"
"sync" "sync"
"sync/atomic" "sync/atomic"
) )

Loading…
Cancel
Save