network

package
v0.0.0-...-4450499 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// 对应proto文件里的package导出名
	ProtoPackageName = "gserver"
)

Variables

View Source
var (
	ClientConnectionConfig = gnet.ConnectionConfig{
		SendPacketCacheCap: 256,
		SendBufferSize:     8 * 1024,
		RecvBufferSize:     8 * 1024,
		MaxPacketSize:      16 * 1024,
		RecvTimeout:        20,
		WriteTimeout:       10,
	}

	WebSocketClientConnectionConfig = gnet.ConnectionConfig{
		SendPacketCacheCap: 256,
		SendBufferSize:     8 * 1024,
		RecvBufferSize:     8 * 1024,
		MaxPacketSize:      16 * 1024,
		RecvTimeout:        20,
		WriteTimeout:       10,
		Path:               "/ws",
	}

	GateConnectionConfig = gnet.ConnectionConfig{
		SendPacketCacheCap: 512,
		SendBufferSize:     512 * 1024,
		RecvBufferSize:     512 * 1024,
		MaxPacketSize:      1024 * 1024,
		RecvTimeout:        5,
		WriteTimeout:       5,
		HeartBeatInterval:  2,
	}

	ServerConnectionConfig = gnet.ConnectionConfig{
		SendPacketCacheCap: 512,
		SendBufferSize:     512 * 1024,
		RecvBufferSize:     512 * 1024,
		MaxPacketSize:      1024 * 1024,
		RecvTimeout:        5,
		WriteTimeout:       5,
		HeartBeatInterval:  2,
	}
)

Functions

func GetCommandByProto

func GetCommandByProto(protoMessage proto.Message) int32

func GetFullMessageName

func GetFullMessageName(packageName, messageName string) string

func GetResCommand

func GetResCommand(reqCommand int32) int32

本项目的request和response的消息号规范: XxxReq XxxRes

func InitCommandMappingFromFile

func InitCommandMappingFromFile(file string)

func IsGatePacket

func IsGatePacket(packet Packet) bool

func ListenClient

func ListenClient(listenAddr string, listenerHandler gnet.ListenerHandler, packetRegister func(handler *gnet.DefaultConnectionHandler)) gnet.Listener

非网关服务器监听普通TCP客户端

func ListenGate

func ListenGate(listenAddr string, packetRegister func(handler *gnet.DefaultConnectionHandler)) gnet.Listener

其他服务器监听网关

func ListenGateClient

func ListenGateClient(listenAddr string, listenerHandler gnet.ListenerHandler, packetRegister func(handler *gnet.DefaultConnectionHandler)) gnet.Listener

网关服务器监听普通TCP客户端

func ListenWebSocketClient

func ListenWebSocketClient(listenAddr string, listenerHandler gnet.ListenerHandler, packetRegister func(handler *gnet.DefaultConnectionHandler)) gnet.Listener

网关服务器监听WebSocket客户端

func NewMessageByName

func NewMessageByName(messageName string) proto.Message

func NewPacket

func NewPacket(message proto.Message) *gnet.ProtoPacket

func RegisterPacketHandler

func RegisterPacketHandler(register gnet.PacketHandlerRegister, protoMessage proto.Message, handler gnet.PacketHandler)

func SendPacketAdapt

func SendPacketAdapt(connection Connection, reqPacket Packet, sendMessage proto.Message) bool

根据请求消息的类型,自动适配不同的发消息接口

func SendPacketAdaptWithError

func SendPacketAdaptWithError(connection Connection, reqPacket Packet, sendMessage proto.Message, errorCode int32) bool

Types

type ClientCodec

type ClientCodec 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
}

Tcp客户端和gate之间的编解码

func NewClientCodec

func NewClientCodec() *ClientCodec

func (*ClientCodec) DecodePacket

func (this *ClientCodec) DecodePacket(connection Connection, packetHeader PacketHeader, packetData []byte) Packet

func (*ClientCodec) EncodePacket

func (this *ClientCodec) EncodePacket(connection Connection, packet Packet) ([][]byte, uint8)

func (*ClientCodec) Register

func (this *ClientCodec) Register(command PacketCommand, protoMessage proto.Message)

注册消息和proto.Message的映射

protoMessage can be nil

type ClientData

type ClientData struct {
	ConnId       uint32
	AccountId    int64
	PlayerId     int64
	GameServerId int32
}

客户端绑定数据

type GateCodec

type GateCodec 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
}

gate和其他服务器之间的编解码

func NewGateCodec

func NewGateCodec(protoMessageTypeMap map[PacketCommand]reflect.Type) *GateCodec

func (*GateCodec) DecodePacket

func (this *GateCodec) DecodePacket(connection Connection, packetHeader PacketHeader, packetData []byte) Packet

func (*GateCodec) EncodePacket

func (this *GateCodec) EncodePacket(connection Connection, packet Packet) ([][]byte, uint8)

func (*GateCodec) Register

func (this *GateCodec) Register(command PacketCommand, protoMessage proto.Message)

注册消息和proto.Message的映射

protoMessage can be nil

type GatePacket

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

func NewGatePacket

func NewGatePacket(playerId int64, command PacketCommand, message proto.Message) *GatePacket

func NewGatePacketWithData

func NewGatePacketWithData(playerId int64, command PacketCommand, data []byte) *GatePacket

func (*GatePacket) Clone

func (this *GatePacket) Clone() Packet

deep copy

func (*GatePacket) Command

func (this *GatePacket) Command() PacketCommand

func (*GatePacket) ErrorCode

func (this *GatePacket) ErrorCode() uint32

func (*GatePacket) GetStreamData

func (this *GatePacket) GetStreamData() []byte

某些特殊需求会直接使用序列化好的数据

support stream data

func (*GatePacket) Message

func (this *GatePacket) Message() proto.Message

func (*GatePacket) PlayerId

func (this *GatePacket) PlayerId() int64

func (*GatePacket) RpcCallId

func (this *GatePacket) RpcCallId() uint32

func (*GatePacket) SetErrorCode

func (this *GatePacket) SetErrorCode(code uint32) *GatePacket

func (*GatePacket) SetPlayerId

func (this *GatePacket) SetPlayerId(playerId int64)

func (*GatePacket) SetRpcCallId

func (this *GatePacket) SetRpcCallId(rpcCallId uint32)

func (*GatePacket) ToProtoPacket

func (this *GatePacket) ToProtoPacket() *ProtoPacket

func (*GatePacket) WithRpc

func (this *GatePacket) WithRpc(arg any) *GatePacket

func (*GatePacket) WithStreamData

func (this *GatePacket) WithStreamData(streamData []byte) *GatePacket

type WsClientCodec

type WsClientCodec struct {
	SimpleProtoCodec
}

WebSocket客户端和gate之间的编解码

func NewWsClientCodec

func NewWsClientCodec() *WsClientCodec

func (*WsClientCodec) Decode

func (this *WsClientCodec) Decode(connection Connection, data []byte) (newPacket Packet, err error)

Jump to

Keyboard shortcuts

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