From 6ec0070ebe25d154a8ca1454f486ce1d3c763a7b Mon Sep 17 00:00:00 2001 From: NorthLan <6995syu@163.com> Date: Mon, 31 Oct 2022 23:49:38 +0800 Subject: [PATCH] =?UTF-8?q?wip:=20=E9=87=8D=E5=BC=84=E4=B8=80=E4=B8=8B?= =?UTF-8?q?=E7=BC=96=E7=A0=81=E8=A7=A3=E7=A0=81=EF=BC=8C=E5=B0=81=E5=8C=85?= =?UTF-8?q?=E8=A7=A3=E5=8C=85=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 5 ++--- go.sum | 10 ++++----- nnet/handler.go | 2 +- packet/packer.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++ packet/packet.go | 21 ++++++------------ session/session.go | 1 + 6 files changed, 68 insertions(+), 24 deletions(-) create mode 100644 packet/packer.go diff --git a/go.mod b/go.mod index 7f88823..f9567bd 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module git.noahlan.cn/northlan/nnet go 1.19 require ( - github.com/gorilla/websocket v1.5.0 // indirect - github.com/panjf2000/ants/v2 v2.6.0 // indirect - google.golang.org/protobuf v1.28.1 // indirect + github.com/gorilla/websocket v1.5.0 + github.com/panjf2000/ants/v2 v2.6.0 ) diff --git a/go.sum b/go.sum index 7e0a492..d8816c7 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,8 @@ -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 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/panjf2000/ants/v2 v2.6.0 h1:xOSpw42m+BMiJ2I33we7h6fYzG4DAlpE1xyI7VS2gxU= 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= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= -google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= diff --git a/nnet/handler.go b/nnet/handler.go index 7250120..a6f7d9e 100644 --- a/nnet/handler.go +++ b/nnet/handler.go @@ -45,5 +45,5 @@ func (h *Handler) register(comp component.Component, opts []component.Option) er } func (h *Handler) handle(request *Request) { - + buf := make([]byte, 3) } diff --git a/packet/packer.go b/packet/packer.go new file mode 100644 index 0000000..d467d26 --- /dev/null +++ b/packet/packer.go @@ -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 +} diff --git a/packet/packet.go b/packet/packet.go index 8f2da8c..801501e 100644 --- a/packet/packet.go +++ b/packet/packet.go @@ -1,8 +1,6 @@ package packet -import ( - "fmt" -) +import "errors" // Type 数据帧类型,如:握手,心跳,数据等 type Type byte @@ -27,14 +25,13 @@ const ( Kick = 0x05 ) -type Packet struct { - Type Type // 数据帧 类型 - - HeaderLen uint32 // 数据帧头 长度 - HeaderRaw []byte // 头原始数据 +// ErrWrongPacketType represents a wrong packet type. +var ErrWrongPacketType = errors.New("wrong packet type") - DataLen uint32 // 数据长度 - DataRaw []byte // 原始数据 +type Packet struct { + Type Type // 数据帧 类型 + Length uint32 // 数据长度 + Data []byte // 原始数据 } func New() *Packet { @@ -42,7 +39,3 @@ func New() *Packet { 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)) -} diff --git a/session/session.go b/session/session.go index 7719ba1..2772b36 100644 --- a/session/session.go +++ b/session/session.go @@ -1,6 +1,7 @@ package session import ( + "git.noahlan.cn/northlan/nnet/nface" "sync" "sync/atomic" )