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

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

@ -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

@ -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
}

@ -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)

@ -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

@ -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"`
}
)

@ -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

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

@ -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

@ -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{}))

@ -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

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

@ -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

@ -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
}

@ -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
}

@ -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)
}

@ -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

@ -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 关闭

Loading…
Cancel
Save