Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { Addr string // tcp 地址,在客户端使用为需要连接的地址,在服务端使用为监听的地址 Network string // tcp 的网络类型,可选值为 "tcp", "tcp4", "tcp6", "unix" or "unixpacket" MsgPkg }
Config 配置项
type DefaultPkgProto ¶
DefaultPkgProto 一个默认的私有协议实现 协议组成: 4字节(自定义数据长度) + 任意字节(json字符串数据)
type MsgPkg ¶
type MsgPkg interface { Packer([]byte) ([]byte, error) // tcp 数据包的封装函数,传入的数据是需要发送的业务数据,返回发送给 tcp 的数据 Parser(string, []byte) ([][]byte, error) // 将收到的数据包,根据私有协议转换成业务数据,在这里处理粘包,半包等数据包问题,返回处理好的数据包 }
MsgPkg tcp 消息的编解码,处理封包解包
type TCP ¶
type TCP struct { Config *Config // contains filtered or unexported fields }
TCP 适配器
func New ¶
func New(v interface{}) *TCP
New 创建 TCP 适配器,必需指定地址或者配置,使用默认的私有协议解析数据包 默认私有协议包结构: 4byte标识数据长度 + 任意byte 数据
可使用 string 使用 tcp 网络类型指定监听的地址 xtcp.New("127.0.0.1:8520")
使用配置对象配置tcp服务,定义网络和数据包私有协议
xtcp.New(&xtcp.Config{ Addr: "127.0.0.1:8520", Network: "tcpv6", MsgPkg: &yourPrivateProtocol{}, // })
xtcp.New(&xtcp.Config{ Addr: "127.0.0.1:8520", Network: "tcpv6", Packer: func([]byte) ([]byte, error) {}, Parser(string, []byte) ([][]byte, error), })
Click to show internal directories.
Click to hide internal directories.