diff --git a/examples/LTVDecoder.go b/examples/LTVDecoder.go new file mode 100644 index 00000000..4eff355c --- /dev/null +++ b/examples/LTVDecoder.go @@ -0,0 +1,95 @@ +// TLV,即Tag(Type)—Length—Value,是一种简单实用的数据传输方案。 +//在TLV的定义中,可以知道它包括三个域,分别为:标签域(Tag),长度域(Length),内容域(Value)。这里的长度域的值实际上就是内容域的长度。 +// +//解码前 (20 bytes) 解码后 (20 bytes) +//+------------+------------+-----------------+ +------------+------------+-----------------+ +//| Tag | Length | Value |----->| Tag | Length | Value | +//| 0x00000001 | 0x0000000C | "HELLO, WORLD" | | 0x00000001 | 0x0000000C | "HELLO, WORLD" | +//+------------+------------+-----------------+ +------------+------------+-----------------+ +// Tag: uint32类型,占4字节,Tag作为MsgId,暂定为1 +// Length:uint32类型,占4字节,Length标记Value长度12(hex:0x0000000C) +// Value: 共12个字符,占12字节 +// +// 说明: +// lengthFieldOffset = 4 (Length的字节位索引下标是4) 长度字段的偏差 +// lengthFieldLength = 4 (Length是4个byte) 长度字段占的字节数 +// lengthAdjustment = 0 (Length只表示Value长度,程序只会读取Length个字节就结束,后面没有来,故为0,若Value后面还有crc占2字节的话,那么此处就是2。若Length标记的是Tag+Length+Value总长度,那么此处是-8) +// initialBytesToStrip = 0 (这个0表示返回完整的协议内容Tag+Length+Value,如果只想返回Value内容,去掉Tag的4字节和Length的4字节,此处就是8) 从解码帧中第一次去除的字节数 +// maxFrameLength = 2^32 + 4 + 4 (Length为uint类型,故2^32次方表示Value最大长度,此外Tag和Length各占4字节) + +package examples + +import ( + "bytes" + "encoding/binary" + "encoding/hex" + "github.com/aceld/zinx/ziface" + "github.com/aceld/zinx/zlog" + "math" + "unsafe" +) + +const TLV_HEADER_SIZE = 8 //表示TLV空包长度 + +type LtvData struct { + Tag uint32 + Length uint32 + Value string +} + +type LTVDecoder struct { +} + +func (this *LTVDecoder) GetLengthField() ziface.LengthField { + // +---------------+---------------+---------------+ + // | Length | Tag | Value | + // | uint32(4byte) | uint32(4byte) | n byte | + // +---------------+---------------+---------------+ + // Length:uint32类型,占4字节,Length标记Value长度 + // Tag: uint32类型,占4字节 + // Value: 占n字节 + // + //说明: + // lengthFieldOffset = 0 (Length的字节位索引下标是0) 长度字段的偏差 + // lengthFieldLength = 4 (Length是4个byte) 长度字段占的字节数 + // lengthAdjustment = 4 (Length只表示Value长度,程序只会读取Length个字节就结束,后面没有来,故为0,若Value后面还有crc占2字节的话,那么此处就是2。若Length标记的是Tag+Length+Value总长度,那么此处是-8) + // initialBytesToStrip = 0 (这个0表示返回完整的协议内容Tag+Length+Value,如果只想返回Value内容,去掉Tag的4字节和Length的4字节,此处就是8) 从解码帧中第一次去除的字节数 + // maxFrameLength = 2^32 + 4 + 4 (Length为uint32类型,故2^32次方表示Value最大长度,此外Tag和Length各占4字节) + //默认使用TLV封包方式 + return ziface.LengthField{ + MaxFrameLength: math.MaxUint32 + 4 + 4, + LengthFieldOffset: 0, + LengthFieldLength: 4, + LengthAdjustment: 4, + InitialBytesToStrip: 0, + Order: binary.LittleEndian, //好吧,我看了代码,使用的是小端😂 + } +} + +func (this *LTVDecoder) Intercept(chain ziface.Chain) ziface.Response { + request := chain.Request() + if request != nil { + switch request.(type) { + case ziface.IRequest: + iRequest := request.(ziface.IRequest) + iMessage := iRequest.GetMessage() + if iMessage != nil { + data := iMessage.GetData() + zlog.Ins().DebugF("TLV-RawData size:%d data:%s\n", len(data), hex.EncodeToString(data)) + datasize := len(data) + _data := LtvData{} + if datasize >= TLV_HEADER_SIZE { + _data.Length = binary.LittleEndian.Uint32(data[0:4]) + _data.Tag = binary.LittleEndian.Uint32(data[4:8]) + value := make([]byte, _data.Length) + binary.Read(bytes.NewBuffer(data[8:8+_data.Length]), binary.LittleEndian, value) + _data.Value = string(value) + iMessage.SetMsgID(_data.Tag) + iRequest.SetResponse(_data) + zlog.Ins().DebugF("TLV-DecodeData size:%d data:%+v\n", unsafe.Sizeof(data), _data) + } + } + } + } + return chain.Proceed(chain.Request()) +} diff --git a/examples/zinx_client/main.go b/examples/zinx_client/main.go index b3df5af5..83da595b 100644 --- a/examples/zinx_client/main.go +++ b/examples/zinx_client/main.go @@ -8,6 +8,7 @@ package main import ( "fmt" + "github.com/aceld/zinx/examples" "github.com/aceld/zinx/examples/zinx_client/c_router" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/zlog" @@ -20,7 +21,7 @@ import ( "time" ) -//客户端自定义业务 +// 客户端自定义业务 func business(conn ziface.IConnection) { for { @@ -35,7 +36,7 @@ func business(conn ziface.IConnection) { } } -//创建连接的时候执行 +// 创建连接的时候执行 func DoClientConnectedBegin(conn ziface.IConnection) { zlog.Debug("DoConnecionBegin is Called ... ") @@ -46,7 +47,7 @@ func DoClientConnectedBegin(conn ziface.IConnection) { go business(conn) } -//连接断开的时候执行 +// 连接断开的时候执行 func DoClientConnectedLost(conn ziface.IConnection) { //在连接销毁之前,查询conn的Name,Home属性 if name, err := conn.GetProperty("Name"); err == nil { @@ -72,6 +73,10 @@ func main() { client.AddRouter(2, &c_router.PingRouter{}) client.AddRouter(3, &c_router.HelloRouter{}) + tlvDecoder := examples.LTVDecoder{} + client.SetLengthField(tlvDecoder.GetLengthField()) + client.AddInterceptor(&tlvDecoder) //TVL协议解码器 + //启动客户端client client.Start() @@ -83,7 +88,7 @@ func main() { } /* - 模拟客户端, 不使用client模块方式 +模拟客户端, 不使用client模块方式 */ func main_old() { conn, err := net.Dial("tcp", "127.0.0.1:8999") diff --git a/examples/zinx_decoder/decode/htlvcrcdecoder.go b/examples/zinx_decoder/decode/htlvcrcdecoder.go index b289139b..57dbce6d 100644 --- a/examples/zinx_decoder/decode/htlvcrcdecoder.go +++ b/examples/zinx_decoder/decode/htlvcrcdecoder.go @@ -25,6 +25,7 @@ import ( "github.com/aceld/zinx/examples/zinx_decoder/bili/utils" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/zlog" + "math" "unsafe" ) @@ -42,6 +43,30 @@ type HtlvCrcData struct { type HtlvCrcDecoder struct { } +func (this *HtlvCrcDecoder) GetLengthField() ziface.LengthField { + //+------+-------+---------+--------+--------+ + //| 头码 | 功能码 | 数据长度 | 数据内容 | CRC校验 | + //| 1字节 | 1字节 | 1字节 | N字节 | 2字节 | + //+------+-------+---------+--------+--------+ + //头码 功能码 数据长度 Body CRC + //A2 10 0E 0102030405060708091011121314 050B + //说明: + // 1.数据长度len是14(0E),这里的len仅仅指Body长度; + // + // lengthFieldOffset = 2 (len的索引下标是2,下标从0开始) 长度字段的偏差 + // lengthFieldLength = 1 (len是1个byte) 长度字段占的字节数 + // lengthAdjustment = 2 (len只表示Body长度,程序只会读取len个字节就结束,但是CRC还有2byte没读呢,所以为2) + // initialBytesToStrip = 0 (这个0表示完整的协议内容,如果不想要A2,那么这里就是1) 从解码帧中第一次去除的字节数 + // maxFrameLength = 255 + 4(起始码、功能码、CRC) (len是1个byte,所以最大长度是无符号1个byte的最大值) + return ziface.LengthField{ + MaxFrameLength: math.MaxInt8 + 4, + LengthFieldOffset: 2, + LengthFieldLength: 1, + LengthAdjustment: 2, + InitialBytesToStrip: 0, + } +} + func (this *HtlvCrcDecoder) Intercept(chain ziface.Chain) ziface.Response { request := chain.Request() if request != nil { diff --git a/examples/zinx_decoder/decode/tlvdecoder.go b/examples/zinx_decoder/decode/tlvdecoder.go index fb5c0808..5ff4e2d4 100644 --- a/examples/zinx_decoder/decode/tlvdecoder.go +++ b/examples/zinx_decoder/decode/tlvdecoder.go @@ -24,6 +24,7 @@ import ( "encoding/hex" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/zlog" + "math" "unsafe" ) @@ -38,6 +39,31 @@ type TlvData struct { type TLVDecoder struct { } +func (this *TLVDecoder) GetLengthField() ziface.LengthField { + // +---------------+---------------+---------------+ + // | Tag | Length | Value | + // | uint32(4byte) | uint32(4byte) | n byte | + // +---------------+---------------+---------------+ + // Tag: uint32类型,占4字节 + // Length:uint32类型,占4字节,Length标记Value长度 + // Value: 占n字节 + // + //说明: + // lengthFieldOffset = 4 (Length的字节位索引下标是4) 长度字段的偏差 + // lengthFieldLength = 4 (Length是4个byte) 长度字段占的字节数 + // lengthAdjustment = 0 (Length只表示Value长度,程序只会读取Length个字节就结束,后面没有来,故为0,若Value后面还有crc占2字节的话,那么此处就是2。若Length标记的是Tag+Length+Value总长度,那么此处是-8) + // initialBytesToStrip = 0 (这个0表示返回完整的协议内容Tag+Length+Value,如果只想返回Value内容,去掉Tag的4字节和Length的4字节,此处就是8) 从解码帧中第一次去除的字节数 + // maxFrameLength = 2^32 + 4 + 4 (Length为uint32类型,故2^32次方表示Value最大长度,此外Tag和Length各占4字节) + //默认使用TLV封包方式 + return ziface.LengthField{ + MaxFrameLength: math.MaxUint32 + 4 + 4, + LengthFieldOffset: 4, + LengthFieldLength: 4, + LengthAdjustment: 0, + InitialBytesToStrip: 0, + } +} + func (this *TLVDecoder) Intercept(chain ziface.Chain) ziface.Response { request := chain.Request() if request != nil { diff --git a/examples/zinx_decoder/server.go b/examples/zinx_decoder/server.go index bd13d981..fc8474c7 100644 --- a/examples/zinx_decoder/server.go +++ b/examples/zinx_decoder/server.go @@ -25,11 +25,15 @@ func main() { s.SetOnConnStop(DoConnectionLost) //处理TLV协议数据 - s.AddInterceptor(&decode.TLVDecoder{}) //TVL协议解码器 + tlvDecoder := decode.TLVDecoder{} + s.SetLengthField(tlvDecoder.GetLengthField()) + s.AddInterceptor(&tlvDecoder) //TVL协议解码器 s.AddRouter(0x00000001, &router.TLVBusinessRouter{}) //TLV协议对应业务功能 //处理HTLVCRC协议数据 - //s.AddInterceptor(&decode.HtlvCrcDecoder{}) //TVL协议解码器 + //htlvDecoder := decode.HtlvCrcDecoder{} + //s.SetLengthField(htlvDecoder.GetLengthField()) + //s.AddInterceptor(&htlvDecoder) //TVL协议解码器 //s.AddRouter(0x10, &router.HtlvCrcBusinessRouter{}) //TLV协议对应业务功能,因为client.go中模拟数据funcode字段为0x10 //开启服务 diff --git a/examples/zinx_heartbeat/client.go b/examples/zinx_heartbeat/client.go index c87252a6..1ef98a51 100644 --- a/examples/zinx_heartbeat/client.go +++ b/examples/zinx_heartbeat/client.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/aceld/zinx/examples" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/znet" "os" @@ -9,12 +10,12 @@ import ( "time" ) -//ping test 自定义路由 +// ping test 自定义路由 type PingRouter struct { znet.BaseRouter } -//Ping Handle +// Ping Handle func (this *PingRouter) Handle(request ziface.IRequest) { fmt.Println("Call PingRouter Handle") //先读取客户端的数据,再回写ping...ping...ping @@ -27,7 +28,7 @@ func (this *PingRouter) Handle(request ziface.IRequest) { */ } -//客户端自定义业务 +// 客户端自定义业务 func business(conn ziface.IConnection) { for { @@ -41,7 +42,7 @@ func business(conn ziface.IConnection) { } } -//创建连接的时候执行 +// 创建连接的时候执行 func DoClientConnectedBegin(conn ziface.IConnection) { fmt.Println("DoConnecionBegin is Called ... ") @@ -52,7 +53,7 @@ func DoClientConnectedBegin(conn ziface.IConnection) { go business(conn) } -//连接断开的时候执行 +// 连接断开的时候执行 func DoClientConnectedLost(conn ziface.IConnection) { //在连接销毁之前,查询conn的Name,Home属性 if name, err := conn.GetProperty("Name"); err == nil { @@ -77,6 +78,10 @@ func main() { //注册收到服务器消息业务路由 client.AddRouter(0, &PingRouter{}) + tlvDecoder := examples.LTVDecoder{} + client.SetLengthField(tlvDecoder.GetLengthField()) + client.AddInterceptor(&tlvDecoder) //TVL协议解码器 + //启动心跳检测 client.StartHeartBeat(5 * time.Second) diff --git a/examples/zinx_heartbeat/server.go b/examples/zinx_heartbeat/server.go index 8f8986c8..4bd31d2b 100644 --- a/examples/zinx_heartbeat/server.go +++ b/examples/zinx_heartbeat/server.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/aceld/zinx/examples" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/znet" "time" @@ -11,7 +12,7 @@ type TestRouter struct { znet.BaseRouter } -//Handle - +// Handle - func (t *TestRouter) Handle(req ziface.IRequest) { fmt.Println("--> Call Handle, reveived msg: ", string(req.GetData()), " msgID: ", req.GetMsgID(), " connID: ", req.GetConnection().GetConnID()) @@ -25,6 +26,10 @@ func main() { s.AddRouter(1, &TestRouter{}) + tlvDecoder := examples.LTVDecoder{} + s.SetLengthField(tlvDecoder.GetLengthField()) + s.AddInterceptor(&tlvDecoder) //LTV协议解码器 + //启动心跳检测 s.StartHeartBeat(5 * time.Second) diff --git a/examples/zinx_logger/client.go b/examples/zinx_logger/client.go index 98ed8006..aa7a3c19 100644 --- a/examples/zinx_logger/client.go +++ b/examples/zinx_logger/client.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/aceld/zinx/examples" "github.com/aceld/zinx/examples/zinx_client/c_router" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/zlog" @@ -11,7 +12,7 @@ import ( "time" ) -//客户端自定义业务 +// 客户端自定义业务 func business(conn ziface.IConnection) { for { @@ -26,7 +27,7 @@ func business(conn ziface.IConnection) { } } -//创建连接的时候执行 +// 创建连接的时候执行 func DoClientConnectedBegin(conn ziface.IConnection) { zlog.Debug("DoConnecionBegin is Called ... ") @@ -37,7 +38,7 @@ func DoClientConnectedBegin(conn ziface.IConnection) { go business(conn) } -//连接断开的时候执行 +// 连接断开的时候执行 func DoClientConnectedLost(conn ziface.IConnection) { //在连接销毁之前,查询conn的Name,Home属性 if name, err := conn.GetProperty("Name"); err == nil { @@ -61,6 +62,9 @@ func main() { //注册收到服务器消息业务路由 client.AddRouter(0, &c_router.PingRouter{}) + tlvDecoder := examples.LTVDecoder{} + client.SetLengthField(tlvDecoder.GetLengthField()) + client.AddInterceptor(&tlvDecoder) //LTV协议解码器 //启动客户端client client.Start() diff --git a/examples/zinx_logger/server.go b/examples/zinx_logger/server.go index aad8da36..3c927720 100644 --- a/examples/zinx_logger/server.go +++ b/examples/zinx_logger/server.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/aceld/zinx/examples" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/zlog" "github.com/aceld/zinx/znet" @@ -12,7 +13,7 @@ type TestRouter struct { znet.BaseRouter } -//PreHandle - +// PreHandle - func (t *TestRouter) PreHandle(req ziface.IRequest) { //使用场景模拟 完整路由计时 start := time.Now() @@ -25,7 +26,7 @@ func (t *TestRouter) PreHandle(req ziface.IRequest) { fmt.Println("该路由组执行完成耗时:", elapsed) } -//Handle - +// Handle - func (t *TestRouter) Handle(req ziface.IRequest) { fmt.Println("--> Call Handle") @@ -34,7 +35,7 @@ func (t *TestRouter) Handle(req ziface.IRequest) { } } -//PostHandle - +// PostHandle - func (t *TestRouter) PostHandle(req ziface.IRequest) { fmt.Println("--> Call PostHandle") if err := req.GetConnection().SendMsg(0, []byte("test3")); err != nil { @@ -45,6 +46,9 @@ func (t *TestRouter) PostHandle(req ziface.IRequest) { func main() { s := znet.NewServer() s.AddRouter(1, &TestRouter{}) + tlvDecoder := examples.LTVDecoder{} + s.SetLengthField(tlvDecoder.GetLengthField()) + s.AddInterceptor(&tlvDecoder) //LTV协议解码器 zlog.SetLogger(new(MyLogger)) s.Serve() } diff --git a/examples/zinx_new_router/server.go b/examples/zinx_new_router/server.go index 9cfb5a55..02d524fa 100644 --- a/examples/zinx_new_router/server.go +++ b/examples/zinx_new_router/server.go @@ -3,6 +3,7 @@ package main import ( "errors" "fmt" + "github.com/aceld/zinx/examples" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/znet" "time" @@ -12,7 +13,7 @@ type TestRouter struct { znet.BaseRouter } -//PreHandle - +// PreHandle - func (t *TestRouter) PreHandle(req ziface.IRequest) { //使用场景模拟 完整路由计时 start := time.Now() @@ -25,7 +26,7 @@ func (t *TestRouter) PreHandle(req ziface.IRequest) { fmt.Println("该路由组执行完成耗时:", elapsed) } -//Handle - +// Handle - func (t *TestRouter) Handle(req ziface.IRequest) { fmt.Println("--> Call Handle") @@ -50,7 +51,7 @@ func (t *TestRouter) Handle(req ziface.IRequest) { time.Sleep(1 * time.Millisecond) //模拟函数计时 } -//PostHandle - +// PostHandle - func (t *TestRouter) PostHandle(req ziface.IRequest) { fmt.Println("--> Call PostHandle") if err := req.GetConnection().SendMsg(0, []byte("test3")); err != nil { @@ -67,5 +68,8 @@ func Err() error { func main() { s := znet.NewServer() s.AddRouter(1, &TestRouter{}) + tlvDecoder := examples.LTVDecoder{} + s.SetLengthField(tlvDecoder.GetLengthField()) + s.AddInterceptor(&tlvDecoder) //LTV协议解码器 s.Serve() } diff --git a/examples/zinx_server/main.go b/examples/zinx_server/main.go index 0eea76f4..936c8d08 100644 --- a/examples/zinx_server/main.go +++ b/examples/zinx_server/main.go @@ -7,13 +7,14 @@ package main import ( + "github.com/aceld/zinx/examples" "github.com/aceld/zinx/examples/zinx_server/s_router" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/zlog" "github.com/aceld/zinx/znet" ) -//创建连接的时候执行 +// 创建连接的时候执行 func DoConnectionBegin(conn ziface.IConnection) { zlog.Ins().InfoF("DoConnecionBegin is Called ...") @@ -27,7 +28,7 @@ func DoConnectionBegin(conn ziface.IConnection) { } } -//连接断开的时候执行 +// 连接断开的时候执行 func DoConnectionLost(conn ziface.IConnection) { //在连接销毁之前,查询conn的Name,Home属性 if name, err := conn.GetProperty("Name"); err == nil { @@ -53,6 +54,10 @@ func main() { s.AddRouter(0, &s_router.PingRouter{}) s.AddRouter(1, &s_router.HelloZinxRouter{}) + tlvDecoder := examples.LTVDecoder{} + s.SetLengthField(tlvDecoder.GetLengthField()) + s.AddInterceptor(&tlvDecoder) //TVL协议解码器 + //开启服务 s.Serve() } diff --git a/examples/zinx_version_ex/ZinxV0.10Test/Server.go b/examples/zinx_version_ex/ZinxV0.10Test/Server.go index 29642813..4564e58d 100644 --- a/examples/zinx_version_ex/ZinxV0.10Test/Server.go +++ b/examples/zinx_version_ex/ZinxV0.10Test/Server.go @@ -2,16 +2,17 @@ package main import ( "fmt" + "github.com/aceld/zinx/examples" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/znet" ) -//ping test 自定义路由 +// ping test 自定义路由 type PingRouter struct { znet.BaseRouter } -//Ping Handle +// Ping Handle func (this *PingRouter) Handle(request ziface.IRequest) { fmt.Println("Call PingRouter Handle") //先读取客户端的数据,再回写ping...ping...ping @@ -27,7 +28,7 @@ type HelloZinxRouter struct { znet.BaseRouter } -//HelloZinxRouter Handle +// HelloZinxRouter Handle func (this *HelloZinxRouter) Handle(request ziface.IRequest) { fmt.Println("Call HelloZinxRouter Handle") //先读取客户端的数据,再回写ping...ping...ping @@ -39,7 +40,7 @@ func (this *HelloZinxRouter) Handle(request ziface.IRequest) { } } -//创建连接的时候执行 +// 创建连接的时候执行 func DoConnectionBegin(conn ziface.IConnection) { fmt.Println("DoConnecionBegin is Called ... ") @@ -54,7 +55,7 @@ func DoConnectionBegin(conn ziface.IConnection) { } } -//连接断开的时候执行 +// 连接断开的时候执行 func DoConnectionLost(conn ziface.IConnection) { //在连接销毁之前,查询conn的Name,Home属性 if name, err := conn.GetProperty("Name"); err == nil { @@ -79,6 +80,9 @@ func main() { //配置路由 s.AddRouter(0, &PingRouter{}) s.AddRouter(1, &HelloZinxRouter{}) + tlvDecoder := examples.LTVDecoder{} + s.SetLengthField(tlvDecoder.GetLengthField()) + s.AddInterceptor(&tlvDecoder) //LTV协议解码器 //开启服务 s.Serve() diff --git a/ziface/iclient.go b/ziface/iclient.go index 13ab5438..7b3eeffd 100644 --- a/ziface/iclient.go +++ b/ziface/iclient.go @@ -33,4 +33,5 @@ type IClient interface { StartHeartBeatWithOption(time.Duration, *HeartBeatOption) //启动心跳检测(自定义回调) AddInterceptor(interceptor Interceptor) //添加协议解析拦截器 GetLengthField() LengthField + SetLengthField(LengthField) } diff --git a/ziface/iserver.go b/ziface/iserver.go index 2c53fff0..2fda8c15 100644 --- a/ziface/iserver.go +++ b/ziface/iserver.go @@ -36,4 +36,5 @@ type IServer interface { StartHeartBeatWithOption(time.Duration, *HeartBeatOption) //启动心跳检测(自定义回调) AddInterceptor(interceptor Interceptor) //添加协议解析拦截器 GetLengthField() LengthField + SetLengthField(LengthField) } diff --git a/znet/client.go b/znet/client.go index 1dbbd645..93275809 100644 --- a/znet/client.go +++ b/znet/client.go @@ -74,6 +74,10 @@ func (this *Client) AddInterceptor(interceptor ziface.Interceptor) { this.msgHandler.AddInterceptor(interceptor) } +func (this *Client) SetLengthField(field ziface.LengthField) { + this.LengthField = field +} + func (this *Client) GetLengthField() ziface.LengthField { return this.LengthField } diff --git a/znet/server.go b/znet/server.go index d986591c..a34fc8fb 100644 --- a/znet/server.go +++ b/znet/server.go @@ -54,10 +54,6 @@ type Server struct { LengthField ziface.LengthField } -func (this *Server) GetLengthField() ziface.LengthField { - return this.LengthField -} - // NewServer 创建一个服务器句柄 func NewServer(opts ...Option) ziface.IServer { printLogo() @@ -144,6 +140,14 @@ func NewUserConfServer(config *utils.Config, opts ...Option) ziface.IServer { //============== 实现 ziface.IServer 里的全部接口方法 ======== +func (this *Server) SetLengthField(field ziface.LengthField) { + this.LengthField = field +} + +func (this *Server) GetLengthField() ziface.LengthField { + return this.LengthField +} + func (this *Server) AddInterceptor(interceptor ziface.Interceptor) { this.msgHandler.AddInterceptor(interceptor) }