gnet

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 26, 2024 License: MIT Imports: 20 Imported by: 14

README

gnet

Go Report Card Go Reference codecov Mentioned in Awesome Go

中文说明

High performance network library,especially for game servers

Features

  • MultiThread, nonblocking
  • Default support protobuf
  • Optimize receiving and dispatching using lockless RingBuffer, which can improve performance by 5x for some cases
  • Easy to implement custom encoding and decoding
  • Support Tcp,WebSocket(ws and wss)

Core module

Listener(https://github.com/fish-tennis/gnet/blob/main/listener.go)

Listen to a certain port, start a listening goroutine, and manage the connected connections

create a Listener:

netMgr.NewListener("127.0.0.1:10001", connectionConfig, codec, &echoServerHandler{}, &echoListenerHandler{})
Connection(https://github.com/fish-tennis/gnet/blob/main/connection.go)

There are two types of connection:

  • connector(call Connect() to connect the server)
  • accept by listener(the server call accept() to accept a new connection)

create a Connector:

netMgr.NewConnector("127.0.0.1:10001", connectionConfig, codec, &echoClientHandler{}, nil)
Packet(https://github.com/fish-tennis/gnet/blob/main/packet.go)

The common practices in game servers,the packet consists of a message number and a proto message,meanwhile gnet reserve a binary interface

Encoding and decoding(https://github.com/fish-tennis/gnet/blob/main/codec.go)

gnet divide TCP stream based decoding into three layers

Layer1:subcontracting stream, format:|Length|Data|,after receiving full packet content, hand it over to the next layer for processing

Layer2:Decoding the data from Layer1,such as decryption,decompression,etc

Layer3:protobuf deserialize,generate proto.Message

encode

decode

Handler(https://github.com/fish-tennis/gnet/blob/main/handler.go)

ListenerHandler:when the server accept a new connection or the accepted connection disconnected

ConnectionHandler:when the connection connected,disconnected,receive packet

gnet provided a default ConnectionHandler:

handler := NewDefaultConnectionHandler(codec)
// register packet and process function
handler.Register(123, OnTest, new(pb.TestMessage))
func OnTest(conn Connection, packet Packet) {
    testMessage := packet.Message().(*pb.TestMessage)
    // do something
}
Use RingBuffer to increase performance

ringbuffer-performance

goroutine

connection_goroutine

Examples

echo with protobuf

echo without protobuf

custom packet struct

simulate performance testing of a simple game server scenario

tcp without ringbuffer

websocket

Client Connector Library

C#: gnet_csharp

Project

game db&cache framework

distributed game server framework

gnet is also used in our commercial online game projects

Documentation

Index

Constants

View Source
const (
	DebugLevel int8 = iota - 1
	InfoLevel
	WarnLevel
	ErrorLevel
)

日志级别,参考zap

log level
View Source
const (
	// 默认包头长度
	// the default packet header size
	DefaultPacketHeaderSize = int(unsafe.Sizeof(DefaultPacketHeader{}))
	// 数据包长度限制(16M)
	// the default packet data size limit
	MaxPacketDataSize = 0x00FFFFFF
)
View Source
const (
	SimplePacketHeaderSize = 6
)

Variables

View Source
var (
	ErrBufferFull   = errors.New("buffer is full")
	ErrNotSupport   = errors.New("not support")
	ErrPacketLength = errors.New("packet length error")
	// 数据包长度超出设置
	ErrPacketLengthExceed = errors.New("packet length exceed")
	ErrReadRemainPacket   = errors.New("read remain packet data error")
)

Functions

func GetCurrentTimeStamp

func GetCurrentTimeStamp() int64

获取当前时间戳(秒)

get current timestamp

func LogStack

func LogStack()

func NewConnectionId added in v0.1.1

func NewConnectionId() uint32

func SetLogLevel

func SetLogLevel(level int8)

func SetLogger

func SetLogger(w Logger, 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) Clone

func (this *DataPacket) Clone() Packet

deep copy

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 GetLogger

func GetLogger() Logger

func NewStdLogger added in v0.0.12

func NewStdLogger(callDepth int) Logger

type NetMgr

type NetMgr struct {
	// contains filtered or unexported fields
}

网络管理类,提供对外接口

manager class

func GetNetMgr

func GetNetMgr() *NetMgr

单例模式,在调用的时候才会执行初始化一次

singleton mode, init once

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 (this *NetMgr) NewListener(ctx context.Context, address string, listenerConfig *ListenerConfig) Listener

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

func (this *NetMgr) NewWsListener(ctx context.Context, address string, listenerConfig *ListenerConfig) Listener

func (*NetMgr) Shutdown

func (this *NetMgr) Shutdown(waitForAllNetGoroutine bool)

waitForAllNetGoroutine:是否阻塞等待所有网络协程结束

waitForAllNetGoroutine: wait blocks until all goroutine end

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 PacketCommand

type PacketCommand uint16

消息号[0,65535]

type PacketCreator added in v1.0.5

type PacketCreator func() Packet

Packet ctor func

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 ProtoMessageCreator

type ProtoMessageCreator func() proto.Message

proto.Message ctor func

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

func (this *ProtoPacket) Clone() Packet

deep copy

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

func (this *RingBuffer) GetBuffer() []byte

Buffer

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

func (this *RingBuffer) UnReadLength() int

未被读取的长度

func (*RingBuffer) Write

func (this *RingBuffer) Write(p []byte) (n int, err error)

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
}

func (*StdLogger) Debug added in v0.0.12

func (s *StdLogger) Debug(format string, args ...interface{})

func (*StdLogger) Error added in v0.0.12

func (s *StdLogger) Error(format string, args ...interface{})

func (*StdLogger) Info added in v0.0.12

func (s *StdLogger) Info(format string, args ...interface{})

func (*StdLogger) Warn added in v0.0.12

func (s *StdLogger) Warn(format string, args ...interface{})

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

func (this *TcpConnection) GetCodec() Codec

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

func (this *TcpConnection) SetCodec(codec Codec)

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

func (*TcpListener) Start

func (this *TcpListener) Start(ctx context.Context, listenAddress string) bool

start goroutine

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) GetCodec added in v1.1.0

func (this *WsConnection) GetCodec() Codec

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) SetCodec added in v1.1.0

func (this *WsConnection) SetCodec(codec Codec)

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

func (*WsListener) Start added in v1.1.0

func (this *WsListener) Start(ctx context.Context, listenAddress string) bool

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

Directories

Path Synopsis
example
pb

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL