Documentation ¶
Index ¶
- Constants
- Variables
- func GetCurrentTimeStamp() int64
- func LogStack()
- func NewConnectionId() uint32
- func SetLogLevel(level int8)
- func SetLogger(w Logger, level int8)
- type AcceptConnectionCreator
- type Codec
- type Connection
- type ConnectionConfig
- type ConnectionCreator
- type ConnectionHandler
- type DataPacket
- type DefaultCodec
- type DefaultConnectionHandler
- func (this *DefaultConnectionHandler) CreateHeartBeatPacket(connection Connection) Packet
- func (this *DefaultConnectionHandler) GetCodec() Codec
- func (this *DefaultConnectionHandler) GetPacketHandler(packetCommand PacketCommand) PacketHandler
- func (this *DefaultConnectionHandler) OnConnected(connection Connection, success bool)
- func (this *DefaultConnectionHandler) OnDisconnected(connection Connection)
- func (this *DefaultConnectionHandler) OnRecvPacket(connection Connection, packet Packet)
- func (this *DefaultConnectionHandler) Register(packetCommand PacketCommand, handler PacketHandler, protoMessage proto.Message)
- func (this *DefaultConnectionHandler) RegisterHeartBeat(heartBeatPacketCreator PacketCreator)
- func (this *DefaultConnectionHandler) SetOnConnectedFunc(onConnectedFunc func(connection Connection, success bool))
- func (this *DefaultConnectionHandler) SetOnDisconnectedFunc(onDisconnectedFunc func(connection Connection))
- func (this *DefaultConnectionHandler) SetUnRegisterHandler(unRegisterHandler PacketHandler)
- type DefaultPacketHeader
- type Listener
- type ListenerConfig
- type ListenerHandler
- type Logger
- type NetMgr
- func (this *NetMgr) NewConnector(ctx context.Context, address string, connectionConfig *ConnectionConfig, ...) Connection
- func (this *NetMgr) NewConnectorCustom(ctx context.Context, address string, connectionConfig *ConnectionConfig, ...) Connection
- func (this *NetMgr) NewListener(ctx context.Context, address string, listenerConfig *ListenerConfig) Listener
- func (this *NetMgr) NewWsConnector(ctx context.Context, address string, connectionConfig *ConnectionConfig, ...) Connection
- func (this *NetMgr) NewWsListener(ctx context.Context, address string, listenerConfig *ListenerConfig) Listener
- func (this *NetMgr) Shutdown(waitForAllNetGoroutine bool)
- type Packet
- type PacketCommand
- type PacketCreator
- type PacketHandler
- type PacketHandlerRegister
- type PacketHeader
- type ProtoCodec
- type ProtoMessageCreator
- type ProtoPacket
- type ProtoRegister
- type RingBuffer
- func (this *RingBuffer) GetBuffer() []byte
- func (this *RingBuffer) ReadBuffer() []byte
- func (this *RingBuffer) ReadFull(readLen int) []byte
- func (this *RingBuffer) SetReaded(readedLength int)
- func (this *RingBuffer) SetWrote(wroteLength int)
- func (this *RingBuffer) Size() int
- func (this *RingBuffer) UnReadLength() int
- func (this *RingBuffer) Write(p []byte) (n int, err error)
- func (this *RingBuffer) WriteBuffer() []byte
- type RingBufferCodec
- func (this *RingBufferCodec) CreatePacketHeader(connection Connection, packet Packet, packetData []byte) PacketHeader
- func (this *RingBufferCodec) Decode(connection Connection, data []byte) (newPacket Packet, err error)
- func (this *RingBufferCodec) Encode(connection Connection, packet Packet) []byte
- func (this *RingBufferCodec) PacketHeaderSize() uint32
- type SimplePacketHeader
- type SimpleProtoCodec
- func (this *SimpleProtoCodec) CreatePacketHeader(connection Connection, packet Packet, packetData []byte) PacketHeader
- func (this *SimpleProtoCodec) Decode(connection Connection, data []byte) (newPacket Packet, err error)
- func (this *SimpleProtoCodec) Encode(connection Connection, packet Packet) []byte
- func (this *SimpleProtoCodec) PacketHeaderSize() uint32
- func (this *SimpleProtoCodec) Register(command PacketCommand, protoMessage proto.Message)
- type StdLogger
- type TcpConnection
- func (this *TcpConnection) Close()
- func (this *TcpConnection) Connect(address string) bool
- func (this *TcpConnection) GetCodec() Codec
- func (this *TcpConnection) GetConnectionId() uint32
- func (this *TcpConnection) GetHandler() ConnectionHandler
- func (this *TcpConnection) GetSendPacketChanLen() int
- func (this *TcpConnection) GetTag() interface{}
- func (this *TcpConnection) IsConnected() bool
- func (this *TcpConnection) IsConnector() bool
- func (this *TcpConnection) LocalAddr() net.Addr
- func (this *TcpConnection) RemoteAddr() net.Addr
- func (this *TcpConnection) Send(command PacketCommand, message proto.Message) bool
- func (this *TcpConnection) SendPacket(packet Packet) bool
- func (this *TcpConnection) SetCodec(codec Codec)
- func (this *TcpConnection) SetTag(tag interface{})
- func (this *TcpConnection) Start(ctx context.Context, netMgrWg *sync.WaitGroup, ...)
- func (this *TcpConnection) TrySendPacket(packet Packet, timeout time.Duration) bool
- type TcpConnectionSimple
- func (this *TcpConnectionSimple) Close()
- func (this *TcpConnectionSimple) Connect(address string) bool
- func (this *TcpConnectionSimple) GetCodec() Codec
- func (this *TcpConnectionSimple) GetConnectionId() uint32
- func (this *TcpConnectionSimple) GetHandler() ConnectionHandler
- func (this *TcpConnectionSimple) GetSendPacketChanLen() int
- func (this *TcpConnectionSimple) GetTag() interface{}
- func (this *TcpConnectionSimple) IsConnected() bool
- func (this *TcpConnectionSimple) IsConnector() bool
- func (this *TcpConnectionSimple) LocalAddr() net.Addr
- func (this *TcpConnectionSimple) RemoteAddr() net.Addr
- func (this *TcpConnectionSimple) Send(command PacketCommand, message proto.Message) bool
- func (this *TcpConnectionSimple) SendPacket(packet Packet) bool
- func (this *TcpConnectionSimple) SetCodec(codec Codec)
- func (this *TcpConnectionSimple) SetTag(tag interface{})
- func (this *TcpConnectionSimple) Start(ctx context.Context, netMgrWg *sync.WaitGroup, ...)
- func (this *TcpConnectionSimple) TrySendPacket(packet Packet, timeout time.Duration) bool
- type TcpListener
- func (this *TcpListener) Addr() net.Addr
- func (this *TcpListener) Broadcast(packet Packet)
- func (this *TcpListener) Close()
- func (this *TcpListener) GetConnection(connectionId uint32) Connection
- func (this *TcpListener) GetListenerId() uint32
- func (this *TcpListener) IsRunning() bool
- func (this *TcpListener) RangeConnections(f func(conn Connection) bool)
- func (this *TcpListener) Start(ctx context.Context, listenAddress string) bool
- type WsConnection
- func (this *WsConnection) Close()
- func (this *WsConnection) Connect(address string) bool
- func (this *WsConnection) GetCodec() Codec
- func (this *WsConnection) GetConn() *websocket.Conn
- func (this *WsConnection) GetConnectionId() uint32
- func (this *WsConnection) GetHandler() ConnectionHandler
- func (this *WsConnection) GetSendPacketChanLen() int
- func (this *WsConnection) GetTag() interface{}
- func (this *WsConnection) IsConnected() bool
- func (this *WsConnection) IsConnector() bool
- func (this *WsConnection) LocalAddr() net.Addr
- func (this *WsConnection) RemoteAddr() net.Addr
- func (this *WsConnection) Send(command PacketCommand, message proto.Message) bool
- func (this *WsConnection) SendPacket(packet Packet) bool
- func (this *WsConnection) SetCodec(codec Codec)
- func (this *WsConnection) SetTag(tag interface{})
- func (this *WsConnection) Start(ctx context.Context, netMgrWg *sync.WaitGroup, ...)
- func (this *WsConnection) TrySendPacket(packet Packet, timeout time.Duration) bool
- type WsListener
- func (this *WsListener) Addr() net.Addr
- func (this *WsListener) Broadcast(packet Packet)
- func (this *WsListener) Close()
- func (this *WsListener) GetConnection(connectionId uint32) Connection
- func (this *WsListener) GetListenerId() uint32
- func (this *WsListener) IsRunning() bool
- func (this *WsListener) RangeConnections(f func(conn Connection) bool)
- func (this *WsListener) Start(ctx context.Context, listenAddress string) bool
- type XorProtoCodec
Constants ¶
const ( DebugLevel int8 = iota - 1 InfoLevel WarnLevel ErrorLevel )
日志级别,参考zap
log level
const ( // 默认包头长度 // the default packet header size DefaultPacketHeaderSize = int(unsafe.Sizeof(DefaultPacketHeader{})) // 数据包长度限制(16M) // the default packet data size limit MaxPacketDataSize = 0x00FFFFFF )
const (
SimplePacketHeaderSize = 6
)
Variables ¶
Functions ¶
func NewConnectionId ¶ added in v0.1.1
func NewConnectionId() uint32
func SetLogLevel ¶
func SetLogLevel(level int8)
Types ¶
type AcceptConnectionCreator ¶ added in v0.1.1
type AcceptConnectionCreator func(conn net.Conn, config *ConnectionConfig) Connection
type Codec ¶
type Codec interface { // 包头长度 // 应用层可以自己扩展包头长度 // the packet header size (without packet data) PacketHeaderSize() uint32 // 创建消息头 // generate a packet header // packet can be nil // packetData is the data after encode,can be nil CreatePacketHeader(connection Connection, packet Packet, packetData []byte) PacketHeader // encoding Encode(connection Connection, packet Packet) []byte // decoding Decode(connection Connection, data []byte) (newPacket Packet, err error) }
连接的编解码接口 interface for encoding and decoding
type Connection ¶
type Connection interface { // unique id GetConnectionId() uint32 // is connector IsConnector() bool // send a packet(proto.Message) // NOTE: 调用Send(command,message)之后,不要再对message进行读写! // NOTE: do not read or modify message after call Send Send(command PacketCommand, message proto.Message) bool // send a packet(Packet) // NOTE:调用SendPacket(packet)之后,不要再对packet进行读写! // NOTE: do not read or modify Packet after call SendPacket SendPacket(packet Packet) bool // 超时发包,超时未发送则丢弃,适用于某些允许丢弃的数据包 // try send a packet with timeout TrySendPacket(packet Packet, timeout time.Duration) bool // is connected IsConnected() bool // codec for this connection GetCodec() Codec // set codec SetCodec(codec Codec) // handler for this connection GetHandler() ConnectionHandler // LocalAddr returns the local network address. LocalAddr() net.Addr // RemoteAddr returns the remote network address. RemoteAddr() net.Addr // close this connection Close() // 获取关联数据 // get the associated tag GetTag() interface{} // 设置关联数据 // set the associated tag SetTag(tag interface{}) // connect to target server // address format ip:port Connect(address string) bool // 开启读写协程 // start the read&write goroutine Start(ctx context.Context, netMgrWg *sync.WaitGroup, onClose func(connection Connection)) }
interface for Connection
type ConnectionConfig ¶
type ConnectionConfig struct { // 发包缓存chan大小(缓存数据包chan容量) // capacity for send packet chan SendPacketCacheCap uint32 // 发包Buffer大小(byte) // size of send RingBuffer (byte) SendBufferSize uint32 // 收包Buffer大小(byte) // size of recv RingBuffer (byte) RecvBufferSize uint32 // 最大包体大小设置(byte),不包含PacketHeader // 允许该值大于SendBufferSize和RecvBufferSize // max size of packet (byte), not include PacketHeader's size // allow MaxPacketSize lager than SendBufferSize and RecvBufferSize MaxPacketSize uint32 // 收包超时设置(秒) // if the connection dont recv packet for RecvTimeout seconds,the connection will close // if RecvTimeout is zero,it will not check timeout RecvTimeout uint32 // 心跳包发送间隔(秒),对connector有效 // heartbeat packet sending interval(seconds) // only valid for connector HeartBeatInterval uint32 // 发包超时设置(秒) // net.Conn.SetWriteDeadline WriteTimeout uint32 Codec Codec Handler ConnectionHandler // ws或wss的http路径,如"/ws"或"/wss" Path string // "ws"或"wss" Scheme string }
connection options
type ConnectionCreator ¶ added in v0.1.1
type ConnectionCreator func(config *ConnectionConfig) Connection
type ConnectionHandler ¶
type ConnectionHandler interface { // 连接成功或失败 // after connect OnConnected(connection Connection, success bool) // 断开连接 // when disconnected OnDisconnected(connection Connection) // 收到一个完整数据包 // 在收包协程中调用 // after recv a full packet, calling in the read goroutine OnRecvPacket(connection Connection, packet Packet) // 创建一个心跳包(只对connector有效) // 在connector的发包协程中调用 // generate a heartbeat packet, calling int the connector's write goroutine CreateHeartBeatPacket(connection Connection) Packet }
handler for Connection
type DataPacket ¶
type DataPacket struct {
// contains filtered or unexported fields
}
只包含一个[]byte的数据包
packet which only have a byte array
func NewDataPacket ¶
func NewDataPacket(data []byte) *DataPacket
func (*DataPacket) Command ¶
func (this *DataPacket) Command() PacketCommand
func (*DataPacket) GetStreamData ¶
func (this *DataPacket) GetStreamData() []byte
func (*DataPacket) Message ¶
func (this *DataPacket) Message() proto.Message
type DefaultCodec ¶
type DefaultCodec struct {
RingBufferCodec
}
默认编解码,只做长度和数据的解析 default codec, which format is length + data
func NewDefaultCodec ¶
func NewDefaultCodec() *DefaultCodec
type DefaultConnectionHandler ¶
type DefaultConnectionHandler struct { // 注册消息的处理函数map // registered map of PacketCommand and PacketHandler PacketHandlers map[PacketCommand]PacketHandler // 未注册消息的处理函数 // packetHandler for unregistered PacketCommand UnRegisterHandler PacketHandler // contains filtered or unexported fields }
default ConnectionHandler for Proto
func NewDefaultConnectionHandler ¶
func NewDefaultConnectionHandler(protoCodec Codec) *DefaultConnectionHandler
func (*DefaultConnectionHandler) CreateHeartBeatPacket ¶
func (this *DefaultConnectionHandler) CreateHeartBeatPacket(connection Connection) Packet
func (*DefaultConnectionHandler) GetCodec ¶
func (this *DefaultConnectionHandler) GetCodec() Codec
func (*DefaultConnectionHandler) GetPacketHandler ¶
func (this *DefaultConnectionHandler) GetPacketHandler(packetCommand PacketCommand) PacketHandler
func (*DefaultConnectionHandler) OnConnected ¶
func (this *DefaultConnectionHandler) OnConnected(connection Connection, success bool)
func (*DefaultConnectionHandler) OnDisconnected ¶
func (this *DefaultConnectionHandler) OnDisconnected(connection Connection)
func (*DefaultConnectionHandler) OnRecvPacket ¶
func (this *DefaultConnectionHandler) OnRecvPacket(connection Connection, packet Packet)
func (*DefaultConnectionHandler) Register ¶
func (this *DefaultConnectionHandler) Register(packetCommand PacketCommand, handler PacketHandler, protoMessage proto.Message)
注册消息号和消息回调,proto.Message的映射 handler在TcpConnection的read协程中被调用
register PacketCommand,PacketHandler,proto.Message
func (*DefaultConnectionHandler) RegisterHeartBeat ¶
func (this *DefaultConnectionHandler) RegisterHeartBeat(heartBeatPacketCreator PacketCreator)
注册心跳包(只对connector有效)
register heartBeatPacketCreator, only valid for connector
func (*DefaultConnectionHandler) SetOnConnectedFunc ¶
func (this *DefaultConnectionHandler) SetOnConnectedFunc(onConnectedFunc func(connection Connection, success bool))
set connected callback
func (*DefaultConnectionHandler) SetOnDisconnectedFunc ¶
func (this *DefaultConnectionHandler) SetOnDisconnectedFunc(onDisconnectedFunc func(connection Connection))
set disconnected callback
func (*DefaultConnectionHandler) SetUnRegisterHandler ¶
func (this *DefaultConnectionHandler) SetUnRegisterHandler(unRegisterHandler PacketHandler)
未注册消息的处理函数 unRegisterHandler在TcpConnection的read协程中被调用
register the PacketHandler for unRegister PacketCommand
type DefaultPacketHeader ¶ added in v0.1.1
type DefaultPacketHeader struct { // (flags << 24) | len // flags [0,255) // len [0,16M) LenAndFlags uint32 }
默认包头,支持小于16M的数据包
default packet header
func NewDefaultPacketHeader ¶ added in v0.1.1
func NewDefaultPacketHeader(len uint32, flags uint8) *DefaultPacketHeader
func (*DefaultPacketHeader) Flags ¶ added in v0.1.1
func (this *DefaultPacketHeader) Flags() uint8
标记 [0,0xFF]
func (*DefaultPacketHeader) Len ¶ added in v0.1.1
func (this *DefaultPacketHeader) Len() uint32
包体长度,不包含包头的长度
packet body length (without packet header's length) [0,0x00FFFFFF]
func (*DefaultPacketHeader) ReadFrom ¶ added in v0.1.1
func (this *DefaultPacketHeader) ReadFrom(packetHeaderData []byte)
从字节流读取数据,len(messageHeaderData)>=MessageHeaderSize 使用小端字节序
parse LenAndFlags from stream data
func (*DefaultPacketHeader) WriteTo ¶ added in v0.1.1
func (this *DefaultPacketHeader) WriteTo(packetHeaderData []byte)
写入字节流,使用小端字节序
write LenAndFlags to stream data
type Listener ¶
type Listener interface { GetListenerId() uint32 GetConnection(connectionId uint32) Connection // 广播消息 // broadcast packet to accepted connections Broadcast(packet Packet) // Addr returns the listener's network address. Addr() net.Addr Close() }
interface for Listener
type ListenerConfig ¶ added in v1.1.0
type ListenerConfig struct { AcceptConfig ConnectionConfig AcceptConnectionCreator AcceptConnectionCreator ListenerHandler ListenerHandler // ws或wss的http监听路径,如"/ws"或"/wss" Path string // 签名cert文件,wss专用 CertFile string // 签名key文件,wss专用 KeyFile string }
type ListenerHandler ¶
type ListenerHandler interface { // accept a new connection OnConnectionConnected(listener Listener, acceptedConnection Connection) // a connection disconnect OnConnectionDisconnect(listener Listener, connection Connection) }
handler for Listener
type Logger ¶
type Logger interface { Debug(format string, args ...interface{}) Info(format string, args ...interface{}) Warn(format string, args ...interface{}) Error(format string, args ...interface{}) }
func NewStdLogger ¶ added in v0.0.12
type NetMgr ¶
type NetMgr struct {
// contains filtered or unexported fields
}
网络管理类,提供对外接口
manager class
func (*NetMgr) NewConnector ¶
func (this *NetMgr) NewConnector(ctx context.Context, address string, connectionConfig *ConnectionConfig, tag interface{}) Connection
create a new TcpConnection
func (*NetMgr) NewConnectorCustom ¶ added in v0.1.1
func (this *NetMgr) NewConnectorCustom(ctx context.Context, address string, connectionConfig *ConnectionConfig, tag interface{}, connectionCreator ConnectionCreator) Connection
create a new Connection, with custom connectionCreator
func (*NetMgr) NewListener ¶
func (*NetMgr) NewWsConnector ¶ added in v1.1.0
func (this *NetMgr) NewWsConnector(ctx context.Context, address string, connectionConfig *ConnectionConfig, tag interface{}) Connection
func (*NetMgr) NewWsListener ¶ added in v1.1.0
type Packet ¶
type Packet interface { // 消息号 // 没有把消息号放在PacketHeader里,因为对TCP网络层来说,只需要知道每个数据包的分割长度就可以了, // 至于数据包具体的格式,不该是网络层关心的事情 // 消息号也不是必须放在这里的,但是游戏项目一般都是用消息号,为了减少封装层次,就放这里了 // packet command number Command() PacketCommand // default protobuf Message() proto.Message // 提供一个二进制数据的接口,支持外部直接传入序列化的字节流数据 // support stream data, outside can direct pass the serialized data GetStreamData() []byte // deep copy Clone() Packet }
interface for packet
type PacketHandler ¶
type PacketHandler func(connection Connection, packet Packet)
handler for Packet
type PacketHandlerRegister ¶ added in v0.0.13
type PacketHandlerRegister interface {
Register(packetCommand PacketCommand, handler PacketHandler, protoMessage proto.Message)
}
type PacketHeader ¶
type PacketHeader interface { Len() uint32 ReadFrom(packetHeaderData []byte) WriteTo(packetHeaderData []byte) }
interface for PacketHeader
type ProtoCodec ¶
type ProtoCodec struct { RingBufferCodec // 在proto序列化后的数据,再做一层编码 // encoder after proto.Message serialize ProtoPacketBytesEncoder func(protoPacketBytes [][]byte) [][]byte // 在proto反序列化之前,先做一层解码 // decoder before proto.Message deserialize ProtoPacketBytesDecoder func(packetData []byte) []byte // 消息号和proto.Message type的映射表 MessageCreatorMap map[PacketCommand]reflect.Type }
codec for proto.Message
func NewProtoCodec ¶
func NewProtoCodec(protoMessageTypeMap map[PacketCommand]reflect.Type) *ProtoCodec
func (*ProtoCodec) DecodePacket ¶
func (this *ProtoCodec) DecodePacket(connection Connection, packetHeader PacketHeader, packetData []byte) Packet
func (*ProtoCodec) EncodePacket ¶
func (this *ProtoCodec) EncodePacket(connection Connection, packet Packet) [][]byte
func (*ProtoCodec) Register ¶
func (this *ProtoCodec) Register(command PacketCommand, protoMessage proto.Message)
注册消息和proto.Message的映射
protoMessage can be nil
type ProtoPacket ¶
type ProtoPacket struct {
// contains filtered or unexported fields
}
packet for proto.Message
func NewProtoPacket ¶
func NewProtoPacket(command PacketCommand, message proto.Message) *ProtoPacket
func NewProtoPacketWithData ¶ added in v0.0.13
func NewProtoPacketWithData(command PacketCommand, data []byte) *ProtoPacket
func (*ProtoPacket) Command ¶
func (this *ProtoPacket) Command() PacketCommand
func (*ProtoPacket) GetStreamData ¶
func (this *ProtoPacket) GetStreamData() []byte
某些特殊需求会直接使用序列化好的数据
support stream data
func (*ProtoPacket) Message ¶
func (this *ProtoPacket) Message() proto.Message
type ProtoRegister ¶
type ProtoRegister interface {
Register(command PacketCommand, protoMessage proto.Message)
}
type RingBuffer ¶
type RingBuffer struct {
// contains filtered or unexported fields
}
环形buffer,专为TcpConnection定制,在收发包时,可以减少内存分配和拷贝 NOTE:不支持多线程,不具备通用性
optimize for TcpConnection, reduce memory alloc and copy not thread safe
func NewRingBuffer ¶
func NewRingBuffer(size int) *RingBuffer
指定大小的RingBuffer,不支持动态扩容
fix size, not support dynamic expansion
func (*RingBuffer) ReadBuffer ¶
func (this *RingBuffer) ReadBuffer() []byte
返回可读取的连续buffer(不产生copy) NOTE:调用ReadBuffer之前,需要先确保UnReadLength()>0
continuous buffer can read
func (*RingBuffer) ReadFull ¶ added in v0.0.13
func (this *RingBuffer) ReadFull(readLen int) []byte
读取指定长度的数据
read data of a specified length
func (*RingBuffer) SetReaded ¶
func (this *RingBuffer) SetReaded(readedLength int)
设置已读取长度
set readed length
func (*RingBuffer) SetWrote ¶ added in v1.0.2
func (this *RingBuffer) SetWrote(wroteLength int)
设置已写入长度
set wrote length
func (*RingBuffer) Size ¶
func (this *RingBuffer) Size() int
func (*RingBuffer) WriteBuffer ¶
func (this *RingBuffer) WriteBuffer() []byte
返回可写入的连续buffer
continuous buffer can write
type RingBufferCodec ¶
type RingBufferCodec struct { // 包头的编码接口,包头长度不能变 // encoder for packer header HeaderEncoder func(connection Connection, packet Packet, headerData []byte) // 包体的编码接口 // NOTE:返回值允许返回多个[]byte,如ProtoPacket在编码时,可以分别返回command和proto.Message的序列化[]byte // 如果只返回一个[]byte,就需要把command和proto.Message序列化的[]byte再合并成一个[]byte,造成性能损失 // encoder for packer data // allow return multiple []byte,for example:when encode ProtoPacket, return two []byte(serialized data of command and proto.Message) DataEncoder func(connection Connection, packet Packet) [][]byte // 包头的解码接口,包头长度不能变 // decoder for packer header HeaderDecoder func(connection Connection, headerData []byte) // 包体的解码接口 // decoder for packer data DataDecoder func(connection Connection, packetHeader PacketHeader, packetData []byte) Packet }
用了RingBuffer的连接的编解码接口 a codec with RingBuffer stream format: Length+Data process divided into two layers layer1: retrieve the original package data from RingBuffer layer2: encode or decode of the original package data NOTE: only support TcpConnection
func (*RingBufferCodec) CreatePacketHeader ¶ added in v0.1.1
func (this *RingBufferCodec) CreatePacketHeader(connection Connection, packet Packet, packetData []byte) PacketHeader
func (*RingBufferCodec) Decode ¶
func (this *RingBufferCodec) Decode(connection Connection, data []byte) (newPacket Packet, err error)
func (*RingBufferCodec) Encode ¶
func (this *RingBufferCodec) Encode(connection Connection, packet Packet) []byte
TcpConnection做了优化,在Encode的过程中就直接写入sendBuffer 返回值:未能写入sendBuffer的数据(sendBuffer写满了的情况) encode packet and write encoded data to sendBuffer return the remain encoded data not wrote to sendBuffer(when sendBuffer is full)
func (*RingBufferCodec) PacketHeaderSize ¶
func (this *RingBufferCodec) PacketHeaderSize() uint32
type SimplePacketHeader ¶ added in v1.0.2
type SimplePacketHeader struct { // (flags << 24) | len // flags [0,255) // len [0,16M) LenAndFlags uint32 Command uint16 }
a simple packet header for TcpConnectionSimple contains packet len and packet command
func NewSimplePacketHeader ¶ added in v1.0.2
func NewSimplePacketHeader(len uint32, flags uint8, command PacketCommand) *SimplePacketHeader
func (*SimplePacketHeader) Flags ¶ added in v1.0.2
func (this *SimplePacketHeader) Flags() uint8
标记 [0,0xFF]
func (*SimplePacketHeader) Len ¶ added in v1.0.2
func (this *SimplePacketHeader) Len() uint32
包体长度,不包含包头的长度
packet body length (without packet header's length) [0,0x00FFFFFF]
func (*SimplePacketHeader) ReadFrom ¶ added in v1.0.2
func (this *SimplePacketHeader) ReadFrom(packetHeaderData []byte)
从字节流读取数据,len(messageHeaderData)>=MessageHeaderSize 使用小端字节序
parse LenAndFlags,Command from stream data
func (*SimplePacketHeader) WriteTo ¶ added in v1.0.2
func (this *SimplePacketHeader) WriteTo(packetHeaderData []byte)
写入字节流,使用小端字节序
write LenAndFlags,Command to stream data
type SimpleProtoCodec ¶ added in v1.0.2
type SimpleProtoCodec struct { // 消息号和proto.Message type的映射表 MessageCreatorMap map[PacketCommand]reflect.Type }
a simple protobuf codec for TcpConnectionSimple, without RingBuffer use SimplePacketHeader as PacketHeader
func NewSimpleProtoCodec ¶ added in v1.0.2
func NewSimpleProtoCodec() *SimpleProtoCodec
func (*SimpleProtoCodec) CreatePacketHeader ¶ added in v1.0.2
func (this *SimpleProtoCodec) CreatePacketHeader(connection Connection, packet Packet, packetData []byte) PacketHeader
func (*SimpleProtoCodec) Decode ¶ added in v1.0.2
func (this *SimpleProtoCodec) Decode(connection Connection, data []byte) (newPacket Packet, err error)
func (*SimpleProtoCodec) Encode ¶ added in v1.0.2
func (this *SimpleProtoCodec) Encode(connection Connection, packet Packet) []byte
func (*SimpleProtoCodec) PacketHeaderSize ¶ added in v1.0.2
func (this *SimpleProtoCodec) PacketHeaderSize() uint32
func (*SimpleProtoCodec) Register ¶ added in v1.0.2
func (this *SimpleProtoCodec) Register(command PacketCommand, protoMessage proto.Message)
注册消息和proto.Message的映射
protoMessage can be nil
type StdLogger ¶ added in v0.0.12
type StdLogger struct {
// contains filtered or unexported fields
}
type TcpConnection ¶
type TcpConnection struct {
// contains filtered or unexported fields
}
TcpConnection use RingBuffer to optimize
func NewTcpConnectionAccept ¶
func NewTcpConnectionAccept(conn net.Conn, config *ConnectionConfig) *TcpConnection
func NewTcpConnector ¶
func NewTcpConnector(config *ConnectionConfig) *TcpConnection
func (*TcpConnection) Close ¶
func (this *TcpConnection) Close()
func (*TcpConnection) Connect ¶
func (this *TcpConnection) Connect(address string) bool
func (*TcpConnection) GetConnectionId ¶
func (this *TcpConnection) GetConnectionId() uint32
unique id
func (*TcpConnection) GetHandler ¶ added in v0.1.1
func (this *TcpConnection) GetHandler() ConnectionHandler
func (*TcpConnection) GetSendPacketChanLen ¶ added in v0.0.13
func (this *TcpConnection) GetSendPacketChanLen() int
func (*TcpConnection) GetTag ¶
func (this *TcpConnection) GetTag() interface{}
获取关联数据
get the associated tag
func (*TcpConnection) IsConnected ¶
func (this *TcpConnection) IsConnected() bool
func (*TcpConnection) IsConnector ¶
func (this *TcpConnection) IsConnector() bool
func (*TcpConnection) LocalAddr ¶
func (this *TcpConnection) LocalAddr() net.Addr
LocalAddr returns the local network address.
func (*TcpConnection) RemoteAddr ¶
func (this *TcpConnection) RemoteAddr() net.Addr
RemoteAddr returns the remote network address.
func (*TcpConnection) Send ¶
func (this *TcpConnection) Send(command PacketCommand, message proto.Message) bool
异步发送proto包 NOTE:调用Send(command,message)之后,不要再对message进行读写!
asynchronous send (write to chan, not send immediately)
func (*TcpConnection) SendPacket ¶
func (this *TcpConnection) SendPacket(packet Packet) bool
异步发送数据 NOTE:调用SendPacket(packet)之后,不要再对packet进行读写!
asynchronous send (write to chan, not send immediately)
func (*TcpConnection) SetTag ¶
func (this *TcpConnection) SetTag(tag interface{})
设置关联数据
set the associated tag
func (*TcpConnection) Start ¶
func (this *TcpConnection) Start(ctx context.Context, netMgrWg *sync.WaitGroup, onClose func(connection Connection))
start read&write goroutine
func (*TcpConnection) TrySendPacket ¶
func (this *TcpConnection) TrySendPacket(packet Packet, timeout time.Duration) bool
超时发包,超时未发送则丢弃,适用于某些允许丢弃的数据包 可以防止某些"不重要的"数据包造成chan阻塞,比如游戏项目常见的聊天广播
asynchronous send with timeout (write to chan, not send immediately) if return false, means not write to chan
type TcpConnectionSimple ¶ added in v0.1.1
type TcpConnectionSimple struct {
// contains filtered or unexported fields
}
不使用RingBuffer的TcpConnection 需要搭配对应的codec
TcpConnection without RingBuffer
func NewTcpConnectionSimple ¶ added in v0.1.1
func NewTcpConnectionSimple(config *ConnectionConfig) *TcpConnectionSimple
func NewTcpConnectionSimpleAccept ¶ added in v0.1.1
func NewTcpConnectionSimpleAccept(conn net.Conn, config *ConnectionConfig) *TcpConnectionSimple
func (*TcpConnectionSimple) Close ¶ added in v0.1.1
func (this *TcpConnectionSimple) Close()
func (*TcpConnectionSimple) Connect ¶ added in v0.1.1
func (this *TcpConnectionSimple) Connect(address string) bool
func (*TcpConnectionSimple) GetCodec ¶ added in v0.1.1
func (this *TcpConnectionSimple) GetCodec() Codec
func (*TcpConnectionSimple) GetConnectionId ¶ added in v0.1.1
func (this *TcpConnectionSimple) GetConnectionId() uint32
unique id
func (*TcpConnectionSimple) GetHandler ¶ added in v0.1.1
func (this *TcpConnectionSimple) GetHandler() ConnectionHandler
func (*TcpConnectionSimple) GetSendPacketChanLen ¶ added in v0.1.1
func (this *TcpConnectionSimple) GetSendPacketChanLen() int
func (*TcpConnectionSimple) GetTag ¶ added in v0.1.1
func (this *TcpConnectionSimple) GetTag() interface{}
获取关联数据
get the associated tag
func (*TcpConnectionSimple) IsConnected ¶ added in v0.1.1
func (this *TcpConnectionSimple) IsConnected() bool
func (*TcpConnectionSimple) IsConnector ¶ added in v0.1.1
func (this *TcpConnectionSimple) IsConnector() bool
func (*TcpConnectionSimple) LocalAddr ¶ added in v0.1.1
func (this *TcpConnectionSimple) LocalAddr() net.Addr
LocalAddr returns the local network address.
func (*TcpConnectionSimple) RemoteAddr ¶ added in v0.1.1
func (this *TcpConnectionSimple) RemoteAddr() net.Addr
RemoteAddr returns the remote network address.
func (*TcpConnectionSimple) Send ¶ added in v0.1.1
func (this *TcpConnectionSimple) Send(command PacketCommand, message proto.Message) bool
异步发送proto包 NOTE:调用Send(command,message)之后,不要再对message进行读写!
asynchronous send (write to chan, not send immediately)
func (*TcpConnectionSimple) SendPacket ¶ added in v0.1.1
func (this *TcpConnectionSimple) SendPacket(packet Packet) bool
异步发送数据 NOTE:调用SendPacket(packet)之后,不要再对packet进行读写!
asynchronous send (write to chan, not send immediately)
func (*TcpConnectionSimple) SetCodec ¶ added in v0.1.1
func (this *TcpConnectionSimple) SetCodec(codec Codec)
func (*TcpConnectionSimple) SetTag ¶ added in v0.1.1
func (this *TcpConnectionSimple) SetTag(tag interface{})
设置关联数据
set the associated tag
func (*TcpConnectionSimple) Start ¶ added in v0.1.1
func (this *TcpConnectionSimple) Start(ctx context.Context, netMgrWg *sync.WaitGroup, onClose func(connection Connection))
start read&write goroutine
func (*TcpConnectionSimple) TrySendPacket ¶ added in v0.1.1
func (this *TcpConnectionSimple) TrySendPacket(packet Packet, timeout time.Duration) bool
超时发包,超时未发送则丢弃,适用于某些允许丢弃的数据包 可以防止某些"不重要的"数据包造成chan阻塞,比如游戏项目常见的聊天广播
asynchronous send with timeout (write to chan, not send immediately) if return false, means not write to chan
type TcpListener ¶
type TcpListener struct {
// contains filtered or unexported fields
}
tcp Listener
func NewTcpListener ¶
func NewTcpListener(listenerConfig *ListenerConfig) *TcpListener
func (*TcpListener) Addr ¶
func (this *TcpListener) Addr() net.Addr
Addr returns the listener's network address.
func (*TcpListener) Broadcast ¶
func (this *TcpListener) Broadcast(packet Packet)
广播消息
broadcast packet to accepted connections
func (*TcpListener) Close ¶
func (this *TcpListener) Close()
关闭监听,并关闭管理的连接
close listen, close the accepted connections
func (*TcpListener) GetConnection ¶
func (this *TcpListener) GetConnection(connectionId uint32) Connection
func (*TcpListener) GetListenerId ¶
func (this *TcpListener) GetListenerId() uint32
func (*TcpListener) IsRunning ¶ added in v1.0.2
func (this *TcpListener) IsRunning() bool
func (*TcpListener) RangeConnections ¶ added in v1.0.2
func (this *TcpListener) RangeConnections(f func(conn Connection) bool)
range for accepted connections
type WsConnection ¶ added in v1.1.0
type WsConnection struct {
// contains filtered or unexported fields
}
WsConnection WebSocket
func NewWsConnection ¶ added in v1.1.0
func NewWsConnection(config *ConnectionConfig) *WsConnection
func NewWsConnectionAccept ¶ added in v1.1.0
func NewWsConnectionAccept(conn *websocket.Conn, config *ConnectionConfig, codec Codec, handler ConnectionHandler) *WsConnection
func (*WsConnection) Close ¶ added in v1.1.0
func (this *WsConnection) Close()
func (*WsConnection) Connect ¶ added in v1.1.0
func (this *WsConnection) Connect(address string) bool
func (*WsConnection) GetConn ¶ added in v1.1.0
func (this *WsConnection) GetConn() *websocket.Conn
func (*WsConnection) GetConnectionId ¶ added in v1.1.0
func (this *WsConnection) GetConnectionId() uint32
unique id
func (*WsConnection) GetHandler ¶ added in v1.1.0
func (this *WsConnection) GetHandler() ConnectionHandler
func (*WsConnection) GetSendPacketChanLen ¶ added in v1.1.0
func (this *WsConnection) GetSendPacketChanLen() int
func (*WsConnection) GetTag ¶ added in v1.1.0
func (this *WsConnection) GetTag() interface{}
获取关联数据
get the associated tag
func (*WsConnection) IsConnected ¶ added in v1.1.0
func (this *WsConnection) IsConnected() bool
func (*WsConnection) IsConnector ¶ added in v1.1.0
func (this *WsConnection) IsConnector() bool
func (*WsConnection) LocalAddr ¶ added in v1.1.0
func (this *WsConnection) LocalAddr() net.Addr
func (*WsConnection) RemoteAddr ¶ added in v1.1.0
func (this *WsConnection) RemoteAddr() net.Addr
func (*WsConnection) Send ¶ added in v1.1.0
func (this *WsConnection) Send(command PacketCommand, message proto.Message) bool
func (*WsConnection) SendPacket ¶ added in v1.1.0
func (this *WsConnection) SendPacket(packet Packet) bool
func (*WsConnection) SetTag ¶ added in v1.1.0
func (this *WsConnection) SetTag(tag interface{})
设置关联数据
set the associated tag
func (*WsConnection) Start ¶ added in v1.1.0
func (this *WsConnection) Start(ctx context.Context, netMgrWg *sync.WaitGroup, onClose func(connection Connection))
func (*WsConnection) TrySendPacket ¶ added in v1.1.0
func (this *WsConnection) TrySendPacket(packet Packet, timeout time.Duration) bool
type WsListener ¶ added in v1.1.0
type WsListener struct {
// contains filtered or unexported fields
}
func NewWsListener ¶ added in v1.1.0
func NewWsListener(listenerConfig *ListenerConfig) *WsListener
func (*WsListener) Addr ¶ added in v1.1.0
func (this *WsListener) Addr() net.Addr
func (*WsListener) Broadcast ¶ added in v1.1.0
func (this *WsListener) Broadcast(packet Packet)
func (*WsListener) Close ¶ added in v1.1.0
func (this *WsListener) Close()
func (*WsListener) GetConnection ¶ added in v1.1.0
func (this *WsListener) GetConnection(connectionId uint32) Connection
func (*WsListener) GetListenerId ¶ added in v1.1.0
func (this *WsListener) GetListenerId() uint32
func (*WsListener) IsRunning ¶ added in v1.1.0
func (this *WsListener) IsRunning() bool
func (*WsListener) RangeConnections ¶ added in v1.1.0
func (this *WsListener) RangeConnections(f func(conn Connection) bool)
range for accepted connections
type XorProtoCodec ¶
type XorProtoCodec struct { *ProtoCodec // contains filtered or unexported fields }
codec for proto.Message and xor
func NewXorProtoCodec ¶
func NewXorProtoCodec(xorKey []byte, protoMessageTypeMap map[PacketCommand]reflect.Type) *XorProtoCodec