Browse Source

feat: Customizable implementation package

master
zhengchong 4 years ago
parent
commit
be72f57786
  1. 8
      ziface/ipacket.go
  2. 1
      ziface/iserver.go
  3. 10
      znet/connection.go
  4. 6
      znet/datapack.go
  5. 12
      znet/options.go
  6. 14
      znet/server.go

8
ziface/ipacket.go

@ -0,0 +1,8 @@
package ziface
type Packet interface {
Unpack(binaryData []byte) (IMessage, error)
Pack(msg IMessage) ([]byte, error)
GetHeadLen() uint32
}

1
ziface/iserver.go

@ -24,4 +24,5 @@ type IServer interface {
SetOnConnStop(func(IConnection)) //设置该Server的连接断开时的Hook函数
CallOnConnStart(conn IConnection) //调用连接OnConnStart Hook函数
CallOnConnStop(conn IConnection) //调用连接OnConnStop Hook函数
Packet() Packet
}

10
znet/connection.go

@ -96,16 +96,14 @@ func (c *Connection) StartReader() {
defer c.Stop()
// 创建拆包解包的对象
dp := NewDataPack()
for {
select {
case <-c.ctx.Done():
return
default:
//读取客户端的Msg head
headData := make([]byte, dp.GetHeadLen())
headData := make([]byte, c.TCPServer.Packet().GetHeadLen())
if _, err := io.ReadFull(c.Conn, headData); err != nil {
fmt.Println("read msg head error ", err)
return
@ -113,7 +111,7 @@ func (c *Connection) StartReader() {
//fmt.Printf("read headData %+v\n", headData)
//拆包,得到msgID 和 datalen 放在msg中
msg, err := dp.Unpack(headData)
msg, err := c.TCPServer.Packet().Unpack(headData)
if err != nil {
fmt.Println("unpack error ", err)
return
@ -214,7 +212,7 @@ func (c *Connection) SendMsg(msgID uint32, data []byte) error {
}
//将data封包,并且发送
dp := NewDataPack()
dp := c.TCPServer.Packet()
msg, err := dp.Pack(NewMsgPackage(msgID, data))
if err != nil {
fmt.Println("Pack error msg ID = ", msgID)
@ -236,7 +234,7 @@ func (c *Connection) SendBuffMsg(msgID uint32, data []byte) error {
}
//将data封包,并且发送
dp := NewDataPack()
dp := c.TCPServer.Packet()
msg, err := dp.Pack(NewMsgPackage(msgID, data))
if err != nil {
fmt.Println("Pack error msg ID = ", msgID)

6
znet/datapack.go

@ -9,18 +9,20 @@ import (
"github.com/aceld/zinx/ziface"
)
var defaultHeaderLen uint32 = 8
//DataPack 封包拆包类实例,暂时不需要成员
type DataPack struct{}
//NewDataPack 封包拆包实例初始化方法
func NewDataPack() *DataPack {
func NewDataPack() ziface.Packet {
return &DataPack{}
}
//GetHeadLen 获取包头长度方法
func (dp *DataPack) GetHeadLen() uint32 {
//ID uint32(4字节) + DataLen uint32(4字节)
return 8
return defaultHeaderLen
}
//Pack 封包方法(压缩数据)

12
znet/options.go

@ -0,0 +1,12 @@
package znet
import "github.com/aceld/zinx/ziface"
type Option func(s *Server)
// 只要实现Packet 接口可自由实现数据包解析格式,如果没有则使用默认解析格式
func WithPacket(pack ziface.Packet) Option {
return func(s *Server) {
s.packet = pack
}
}

14
znet/server.go

@ -39,10 +39,12 @@ type Server struct {
OnConnStart func(conn ziface.IConnection)
//该Server的连接断开时的Hook函数
OnConnStop func(conn ziface.IConnection)
packet ziface.Packet
}
//NewServer 创建一个服务器句柄
func NewServer() ziface.IServer {
func NewServer(opts ...Option) ziface.IServer {
printLogo()
s := &Server{
@ -52,7 +54,13 @@ func NewServer() ziface.IServer {
Port: utils.GlobalObject.TCPPort,
msgHandler: NewMsgHandle(),
ConnMgr: NewConnManager(),
packet: NewDataPack(),
}
for _, opt := range opts {
opt(s)
}
return s
}
@ -168,6 +176,10 @@ func (s *Server) CallOnConnStop(conn ziface.IConnection) {
}
}
func (s *Server) Packet() ziface.Packet {
return s.packet
}
func printLogo() {
fmt.Println(zinxLogo)
fmt.Println(topLine)

Loading…
Cancel
Save