From 73af4fce097a1fef1df487ffb4ddab1d922ad51a Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Wed, 21 Jun 2023 11:18:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=96=B0=E5=88=B01.18?= =?UTF-8?q?=EF=BC=8Cinterface{}=20=E6=9B=BF=E6=8D=A2=E4=B8=BA=20any?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- connection/connection.go | 6 +++--- connection/pipeline.go | 6 +++--- packet/packet.go | 8 ++++---- protocol/nnet/client_pipeline_nnet.go | 2 +- protocol/nnet/nnet.go | 4 ++-- protocol/nnet/packer_nnet.go | 2 +- protocol/nnet/packet_nnet.go | 2 +- protocol/nnet/pipeline_nnet.go | 4 ++-- protocol/nnet/router_nnet.go | 2 +- protocol/plain/packer_plain.go | 2 +- protocol/plain/packet_plain.go | 2 +- protocol/plain/pipeline_plain.go | 2 +- protocol/plain/router_plain.go | 2 +- router/router.go | 6 +++--- serialize/json/json.go | 4 ++-- serialize/protobuf/protobuf.go | 4 ++-- session/session.go | 16 ++++++++-------- 17 files changed, 37 insertions(+), 37 deletions(-) diff --git a/connection/connection.go b/connection/connection.go index 3eb72ba..b2dd7db 100644 --- a/connection/connection.go +++ b/connection/connection.go @@ -68,8 +68,8 @@ type ( } PendingMessage struct { - header interface{} - payload interface{} + header any + payload any } ) @@ -109,7 +109,7 @@ func NewConnection( return r } -func (r *Connection) Send(header, payload interface{}) (err error) { +func (r *Connection) Send(header, payload any) (err error) { defer func() { if e := recover(); e != nil { err = ErrBrokenPipe diff --git a/connection/pipeline.go b/connection/pipeline.go index 17de2d9..ab55bf7 100644 --- a/connection/pipeline.go +++ b/connection/pipeline.go @@ -5,7 +5,7 @@ import ( ) type ( - Func func(c *Connection, v interface{}) error + Func func(c *Connection, v any) error // Pipeline 消息管道 Pipeline interface { @@ -20,7 +20,7 @@ type ( Channel interface { PushFront(h Func) PushBack(h Func) - Process(c *Connection, v interface{}) error + Process(c *Connection, v any) error } pipelineChannel struct { @@ -65,7 +65,7 @@ func (p *pipelineChannel) PushBack(h Func) { } // Process 处理所有的pipeline方法 -func (p *pipelineChannel) Process(c *Connection, v interface{}) error { +func (p *pipelineChannel) Process(c *Connection, v any) error { if len(p.handlers) < 1 { return nil } diff --git a/packet/packet.go b/packet/packet.go index b0ff85c..7cf3c21 100644 --- a/packet/packet.go +++ b/packet/packet.go @@ -12,15 +12,15 @@ var ( type ( // IPacket 数据帧 IPacket interface { - GetHeader() interface{} // 数据帧头部 Header - GetLen() uint64 // 数据帧长度 8bytes,根据实际情况进行转换 - GetBody() []byte // 数据 Body + GetHeader() any // 数据帧头部 Header + GetLen() uint64 // 数据帧长度 8bytes,根据实际情况进行转换 + GetBody() []byte // 数据 Body } // Packer 数据帧 封包/解包 Packer interface { // Pack 封包,将原始数据构造为二进制流数据帧 - Pack(header interface{}, data []byte) ([]byte, error) + Pack(header any, data []byte) ([]byte, error) // Unpack 解包 Unpack(data []byte) ([]IPacket, error) diff --git a/protocol/nnet/client_pipeline_nnet.go b/protocol/nnet/client_pipeline_nnet.go index a381a44..8d4054b 100644 --- a/protocol/nnet/client_pipeline_nnet.go +++ b/protocol/nnet/client_pipeline_nnet.go @@ -14,7 +14,7 @@ type OnReadyFunc func() func WithNNetClientPipeline(onReady OnReadyFunc, packer packet.Packer) nnet.RunOption { return func(ngin *nnet.Engine) { - ngin.Pipeline().Inbound().PushFront(func(conn *connection.Connection, v interface{}) error { + ngin.Pipeline().Inbound().PushFront(func(conn *connection.Connection, v any) error { pkg, ok := v.(*Packet) if !ok { return packet.ErrWrongPacketType diff --git a/protocol/nnet/nnet.go b/protocol/nnet/nnet.go index 37a8c15..fe9f72d 100644 --- a/protocol/nnet/nnet.go +++ b/protocol/nnet/nnet.go @@ -22,7 +22,7 @@ type ( ClientSecret string `json:"clientSecret"` // 客户端密钥,服务器以此判定客户端是否可用 // 透传信息 - Payload interface{} `json:"payload,optional,omitempty"` + Payload any `json:"payload,optional,omitempty"` } HandshakeResp struct { @@ -32,7 +32,7 @@ type ( *RouteMap // 透传信息 - Payload interface{} `json:"payload,optional,omitempty"` + Payload any `json:"payload,optional,omitempty"` } ) diff --git a/protocol/nnet/packer_nnet.go b/protocol/nnet/packer_nnet.go index a57fcec..231e6c9 100644 --- a/protocol/nnet/packer_nnet.go +++ b/protocol/nnet/packer_nnet.go @@ -56,7 +56,7 @@ func (d *Packer) invalidType(t MsgType) bool { return t < Request || t > Push } -func (d *Packer) Pack(header interface{}, data []byte) ([]byte, error) { +func (d *Packer) Pack(header any, data []byte) ([]byte, error) { h, ok := header.(Header) if !ok { return nil, packet.ErrWrongPacketType diff --git a/protocol/nnet/packet_nnet.go b/protocol/nnet/packet_nnet.go index c13f193..f93177c 100644 --- a/protocol/nnet/packet_nnet.go +++ b/protocol/nnet/packet_nnet.go @@ -76,7 +76,7 @@ func newPacket(typ Type) *Packet { } } -func (p *Packet) GetHeader() interface{} { +func (p *Packet) GetHeader() any { return p.Header } diff --git a/protocol/nnet/pipeline_nnet.go b/protocol/nnet/pipeline_nnet.go index f2ac1ab..5b10b4a 100644 --- a/protocol/nnet/pipeline_nnet.go +++ b/protocol/nnet/pipeline_nnet.go @@ -12,7 +12,7 @@ import ( type ( HandshakeValidatorFunc func(*HandshakeReq) error - HandshakeAckPayloadFunc func() interface{} + HandshakeAckPayloadFunc func() any ) func withNNetPipeline( @@ -21,7 +21,7 @@ func withNNetPipeline( packer packet.Packer, ) nnet.RunOption { return func(ngin *nnet.Engine) { - ngin.Pipeline().Inbound().PushFront(func(conn *connection.Connection, v interface{}) error { + ngin.Pipeline().Inbound().PushFront(func(conn *connection.Connection, v any) error { pkg, ok := v.(*Packet) if !ok { return packet.ErrWrongPacketType diff --git a/protocol/nnet/router_nnet.go b/protocol/nnet/router_nnet.go index 7b6e7ad..850b17a 100644 --- a/protocol/nnet/router_nnet.go +++ b/protocol/nnet/router_nnet.go @@ -60,7 +60,7 @@ func (r *nRouter) Handle(conn *connection.Connection, p packet.IPacket) { handler.Handle(conn, p) } -func (r *nRouter) Register(matches interface{}, handler rt.Handler) error { +func (r *nRouter) Register(matches any, handler rt.Handler) error { match, ok := matches.(Match) if !ok { return errors.New(fmt.Sprintf("the type of matches must be %T", Match{})) diff --git a/protocol/plain/packer_plain.go b/protocol/plain/packer_plain.go index b959d2e..78b5ccc 100644 --- a/protocol/plain/packer_plain.go +++ b/protocol/plain/packer_plain.go @@ -23,7 +23,7 @@ func NewPacker() *Packer { func (d *Packer) resetFlags() { } -func (d *Packer) Pack(_ interface{}, data []byte) ([]byte, error) { +func (d *Packer) Pack(_ any, data []byte) ([]byte, error) { buf := make([]byte, len(data)) copy(buf, data) return buf, nil diff --git a/protocol/plain/packet_plain.go b/protocol/plain/packet_plain.go index f771ce0..1d68898 100644 --- a/protocol/plain/packet_plain.go +++ b/protocol/plain/packet_plain.go @@ -13,7 +13,7 @@ func newPacket() *Packet { return &Packet{} } -func (p *Packet) GetHeader() interface{} { +func (p *Packet) GetHeader() any { return nil } diff --git a/protocol/plain/pipeline_plain.go b/protocol/plain/pipeline_plain.go index f27ede3..3c20cb1 100644 --- a/protocol/plain/pipeline_plain.go +++ b/protocol/plain/pipeline_plain.go @@ -8,7 +8,7 @@ import ( func withPipeline() nnet.RunOption { return func(ngin *nnet.Engine) { - ngin.Pipeline().Inbound().PushFront(func(conn *connection.Connection, v interface{}) error { + ngin.Pipeline().Inbound().PushFront(func(conn *connection.Connection, v any) error { _, ok := v.(*Packet) if !ok { return packet.ErrWrongPacketType diff --git a/protocol/plain/router_plain.go b/protocol/plain/router_plain.go index a108b1a..ac8954e 100644 --- a/protocol/plain/router_plain.go +++ b/protocol/plain/router_plain.go @@ -33,7 +33,7 @@ func (r *Router) Handle(conn *connection.Connection, pkg packet.IPacket) { r.plainHandler.Handle(conn, p) } -func (r *Router) Register(_ interface{}, handler router.Handler) error { +func (r *Router) Register(_ any, handler router.Handler) error { r.plainHandler = handler return nil } diff --git a/router/router.go b/router/router.go index 0b22606..ffe91d9 100644 --- a/router/router.go +++ b/router/router.go @@ -16,13 +16,13 @@ type ( Middleware func(next HandlerFunc) HandlerFunc Route struct { - Matches interface{} // 用于匹配的关键字段 + Matches any // 用于匹配的关键字段 Handler HandlerFunc // 处理方法 } Router interface { Handler - Register(matches interface{}, handler Handler) error + Register(matches any, handler Handler) error SetNotFoundHandler(handler Handler) } @@ -101,7 +101,7 @@ func (p *plainRouter) Handle(c *connection.Connection, pkg packet.IPacket) { p.handler.Handle(c, pkg) } -func (p *plainRouter) Register(_ interface{}, handler Handler) error { +func (p *plainRouter) Register(_ any, handler Handler) error { p.handler = handler return nil } diff --git a/serialize/json/json.go b/serialize/json/json.go index c48a798..101215d 100644 --- a/serialize/json/json.go +++ b/serialize/json/json.go @@ -11,10 +11,10 @@ func NewSerializer() ndef.Serializer { return &Serializer{} } -func (s *Serializer) Marshal(i interface{}) ([]byte, error) { +func (s *Serializer) Marshal(i any) ([]byte, error) { return json.Marshal(i) } -func (s *Serializer) Unmarshal(bytes []byte, i interface{}) error { +func (s *Serializer) Unmarshal(bytes []byte, i any) error { return json.Unmarshal(bytes, i) } diff --git a/serialize/protobuf/protobuf.go b/serialize/protobuf/protobuf.go index 0a5cccb..aaa7cc8 100644 --- a/serialize/protobuf/protobuf.go +++ b/serialize/protobuf/protobuf.go @@ -15,7 +15,7 @@ func NewSerializer() ndef.Serializer { return &Serializer{} } -func (s *Serializer) Marshal(v interface{}) ([]byte, error) { +func (s *Serializer) Marshal(v any) ([]byte, error) { pb, ok := v.(proto.Message) if !ok { return nil, ErrWrongValueType @@ -23,7 +23,7 @@ func (s *Serializer) Marshal(v interface{}) ([]byte, error) { return proto.Marshal(pb) } -func (s *Serializer) Unmarshal(data []byte, v interface{}) error { +func (s *Serializer) Unmarshal(data []byte, v any) error { pb, ok := v.(proto.Message) if !ok { return ErrWrongValueType diff --git a/session/session.go b/session/session.go index 8745820..daa4c22 100644 --- a/session/session.go +++ b/session/session.go @@ -7,16 +7,16 @@ import ( type Session struct { sync.RWMutex // 数据锁 - id int64 // Session全局唯一ID - uid string // 用户ID,不绑定的情况下与sid一致 - data map[string]interface{} // session数据存储(内存) + id int64 // Session全局唯一ID + uid string // 用户ID,不绑定的情况下与sid一致 + data map[string]any // session数据存储(内存) } func NewSession(id int64) *Session { return &Session{ id: id, uid: "", - data: make(map[string]interface{}), + data: make(map[string]any), } } @@ -36,7 +36,7 @@ func (s *Session) Bind(uid string) { } // Attribute 获取指定key对应参数 -func (s *Session) Attribute(key string) interface{} { +func (s *Session) Attribute(key string) any { s.RLock() defer s.RUnlock() @@ -65,7 +65,7 @@ func (s *Session) Exists(key string) bool { } // Attributes 获取所有参数 -func (s *Session) Attributes() map[string]interface{} { +func (s *Session) Attributes() map[string]any { s.RLock() defer s.RUnlock() @@ -81,7 +81,7 @@ func (s *Session) RemoveAttribute(key string) { } // SetAttribute 设置参数 -func (s *Session) SetAttribute(key string, value interface{}) { +func (s *Session) SetAttribute(key string, value any) { s.Lock() defer s.Unlock() @@ -95,7 +95,7 @@ func (s *Session) Invalidate() { s.id = 0 s.uid = "" - s.data = make(map[string]interface{}) + s.data = make(map[string]any) } // Close 关闭