refactor: 更新到1.18,interface{} 替换为 any

main v1.1.0
NoahLan 1 year ago
parent 1cd2b96c52
commit 73af4fce09

@ -68,8 +68,8 @@ type (
} }
PendingMessage struct { PendingMessage struct {
header interface{} header any
payload interface{} payload any
} }
) )
@ -109,7 +109,7 @@ func NewConnection(
return r return r
} }
func (r *Connection) Send(header, payload interface{}) (err error) { func (r *Connection) Send(header, payload any) (err error) {
defer func() { defer func() {
if e := recover(); e != nil { if e := recover(); e != nil {
err = ErrBrokenPipe err = ErrBrokenPipe

@ -5,7 +5,7 @@ import (
) )
type ( type (
Func func(c *Connection, v interface{}) error Func func(c *Connection, v any) error
// Pipeline 消息管道 // Pipeline 消息管道
Pipeline interface { Pipeline interface {
@ -20,7 +20,7 @@ type (
Channel interface { Channel interface {
PushFront(h Func) PushFront(h Func)
PushBack(h Func) PushBack(h Func)
Process(c *Connection, v interface{}) error Process(c *Connection, v any) error
} }
pipelineChannel struct { pipelineChannel struct {
@ -65,7 +65,7 @@ func (p *pipelineChannel) PushBack(h Func) {
} }
// Process 处理所有的pipeline方法 // 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 { if len(p.handlers) < 1 {
return nil return nil
} }

@ -12,15 +12,15 @@ var (
type ( type (
// IPacket 数据帧 // IPacket 数据帧
IPacket interface { IPacket interface {
GetHeader() interface{} // 数据帧头部 Header GetHeader() any // 数据帧头部 Header
GetLen() uint64 // 数据帧长度 8bytes根据实际情况进行转换 GetLen() uint64 // 数据帧长度 8bytes根据实际情况进行转换
GetBody() []byte // 数据 Body GetBody() []byte // 数据 Body
} }
// Packer 数据帧 封包/解包 // Packer 数据帧 封包/解包
Packer interface { Packer interface {
// Pack 封包,将原始数据构造为二进制流数据帧 // Pack 封包,将原始数据构造为二进制流数据帧
Pack(header interface{}, data []byte) ([]byte, error) Pack(header any, data []byte) ([]byte, error)
// Unpack 解包 // Unpack 解包
Unpack(data []byte) ([]IPacket, error) Unpack(data []byte) ([]IPacket, error)

@ -14,7 +14,7 @@ type OnReadyFunc func()
func WithNNetClientPipeline(onReady OnReadyFunc, packer packet.Packer) nnet.RunOption { func WithNNetClientPipeline(onReady OnReadyFunc, packer packet.Packer) nnet.RunOption {
return func(ngin *nnet.Engine) { 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) pkg, ok := v.(*Packet)
if !ok { if !ok {
return packet.ErrWrongPacketType return packet.ErrWrongPacketType

@ -22,7 +22,7 @@ type (
ClientSecret string `json:"clientSecret"` // 客户端密钥,服务器以此判定客户端是否可用 ClientSecret string `json:"clientSecret"` // 客户端密钥,服务器以此判定客户端是否可用
// 透传信息 // 透传信息
Payload interface{} `json:"payload,optional,omitempty"` Payload any `json:"payload,optional,omitempty"`
} }
HandshakeResp struct { HandshakeResp struct {
@ -32,7 +32,7 @@ type (
*RouteMap *RouteMap
// 透传信息 // 透传信息
Payload interface{} `json:"payload,optional,omitempty"` Payload any `json:"payload,optional,omitempty"`
} }
) )

@ -56,7 +56,7 @@ func (d *Packer) invalidType(t MsgType) bool {
return t < Request || t > Push 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) h, ok := header.(Header)
if !ok { if !ok {
return nil, packet.ErrWrongPacketType return nil, packet.ErrWrongPacketType

@ -76,7 +76,7 @@ func newPacket(typ Type) *Packet {
} }
} }
func (p *Packet) GetHeader() interface{} { func (p *Packet) GetHeader() any {
return p.Header return p.Header
} }

@ -12,7 +12,7 @@ import (
type ( type (
HandshakeValidatorFunc func(*HandshakeReq) error HandshakeValidatorFunc func(*HandshakeReq) error
HandshakeAckPayloadFunc func() interface{} HandshakeAckPayloadFunc func() any
) )
func withNNetPipeline( func withNNetPipeline(
@ -21,7 +21,7 @@ func withNNetPipeline(
packer packet.Packer, packer packet.Packer,
) nnet.RunOption { ) nnet.RunOption {
return func(ngin *nnet.Engine) { 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) pkg, ok := v.(*Packet)
if !ok { if !ok {
return packet.ErrWrongPacketType return packet.ErrWrongPacketType

@ -60,7 +60,7 @@ func (r *nRouter) Handle(conn *connection.Connection, p packet.IPacket) {
handler.Handle(conn, p) 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) match, ok := matches.(Match)
if !ok { if !ok {
return errors.New(fmt.Sprintf("the type of matches must be %T", Match{})) return errors.New(fmt.Sprintf("the type of matches must be %T", Match{}))

@ -23,7 +23,7 @@ func NewPacker() *Packer {
func (d *Packer) resetFlags() { 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)) buf := make([]byte, len(data))
copy(buf, data) copy(buf, data)
return buf, nil return buf, nil

@ -13,7 +13,7 @@ func newPacket() *Packet {
return &Packet{} return &Packet{}
} }
func (p *Packet) GetHeader() interface{} { func (p *Packet) GetHeader() any {
return nil return nil
} }

@ -8,7 +8,7 @@ import (
func withPipeline() nnet.RunOption { func withPipeline() nnet.RunOption {
return func(ngin *nnet.Engine) { 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) _, ok := v.(*Packet)
if !ok { if !ok {
return packet.ErrWrongPacketType return packet.ErrWrongPacketType

@ -33,7 +33,7 @@ func (r *Router) Handle(conn *connection.Connection, pkg packet.IPacket) {
r.plainHandler.Handle(conn, p) 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 r.plainHandler = handler
return nil return nil
} }

@ -16,13 +16,13 @@ type (
Middleware func(next HandlerFunc) HandlerFunc Middleware func(next HandlerFunc) HandlerFunc
Route struct { Route struct {
Matches interface{} // 用于匹配的关键字段 Matches any // 用于匹配的关键字段
Handler HandlerFunc // 处理方法 Handler HandlerFunc // 处理方法
} }
Router interface { Router interface {
Handler Handler
Register(matches interface{}, handler Handler) error Register(matches any, handler Handler) error
SetNotFoundHandler(handler Handler) SetNotFoundHandler(handler Handler)
} }
@ -101,7 +101,7 @@ func (p *plainRouter) Handle(c *connection.Connection, pkg packet.IPacket) {
p.handler.Handle(c, pkg) p.handler.Handle(c, pkg)
} }
func (p *plainRouter) Register(_ interface{}, handler Handler) error { func (p *plainRouter) Register(_ any, handler Handler) error {
p.handler = handler p.handler = handler
return nil return nil
} }

@ -11,10 +11,10 @@ func NewSerializer() ndef.Serializer {
return &Serializer{} return &Serializer{}
} }
func (s *Serializer) Marshal(i interface{}) ([]byte, error) { func (s *Serializer) Marshal(i any) ([]byte, error) {
return json.Marshal(i) 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) return json.Unmarshal(bytes, i)
} }

@ -15,7 +15,7 @@ func NewSerializer() ndef.Serializer {
return &Serializer{} return &Serializer{}
} }
func (s *Serializer) Marshal(v interface{}) ([]byte, error) { func (s *Serializer) Marshal(v any) ([]byte, error) {
pb, ok := v.(proto.Message) pb, ok := v.(proto.Message)
if !ok { if !ok {
return nil, ErrWrongValueType return nil, ErrWrongValueType
@ -23,7 +23,7 @@ func (s *Serializer) Marshal(v interface{}) ([]byte, error) {
return proto.Marshal(pb) 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) pb, ok := v.(proto.Message)
if !ok { if !ok {
return ErrWrongValueType return ErrWrongValueType

@ -7,16 +7,16 @@ import (
type Session struct { type Session struct {
sync.RWMutex // 数据锁 sync.RWMutex // 数据锁
id int64 // Session全局唯一ID id int64 // Session全局唯一ID
uid string // 用户ID不绑定的情况下与sid一致 uid string // 用户ID不绑定的情况下与sid一致
data map[string]interface{} // session数据存储内存 data map[string]any // session数据存储内存
} }
func NewSession(id int64) *Session { func NewSession(id int64) *Session {
return &Session{ return &Session{
id: id, id: id,
uid: "", uid: "",
data: make(map[string]interface{}), data: make(map[string]any),
} }
} }
@ -36,7 +36,7 @@ func (s *Session) Bind(uid string) {
} }
// Attribute 获取指定key对应参数 // Attribute 获取指定key对应参数
func (s *Session) Attribute(key string) interface{} { func (s *Session) Attribute(key string) any {
s.RLock() s.RLock()
defer s.RUnlock() defer s.RUnlock()
@ -65,7 +65,7 @@ func (s *Session) Exists(key string) bool {
} }
// Attributes 获取所有参数 // Attributes 获取所有参数
func (s *Session) Attributes() map[string]interface{} { func (s *Session) Attributes() map[string]any {
s.RLock() s.RLock()
defer s.RUnlock() defer s.RUnlock()
@ -81,7 +81,7 @@ func (s *Session) RemoveAttribute(key string) {
} }
// SetAttribute 设置参数 // SetAttribute 设置参数
func (s *Session) SetAttribute(key string, value interface{}) { func (s *Session) SetAttribute(key string, value any) {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
@ -95,7 +95,7 @@ func (s *Session) Invalidate() {
s.id = 0 s.id = 0
s.uid = "" s.uid = ""
s.data = make(map[string]interface{}) s.data = make(map[string]any)
} }
// Close 关闭 // Close 关闭

Loading…
Cancel
Save