nettcp

package
v0.0.0-...-c0334d6 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MsgLenMax         int = 4096 // 单条消息长度限制.(暂不做硬性限制, 逻辑层控制.)
	MsgHeadLen        int = 8 // 消息头长度.
	MsgLenIndexE      int = 2 // 消息长度信息在消息头[]byte中的索引结束位置.起始位置固定为0
	MsgCryptFlagIndex int = 2 // 消息加密标识在消息头[]byte中的索引.
	MsgZipFlagIndex   int = 3 // 消息压缩标识在消息头[]byte中的索引.
	MsgMsgIDIndexS    int = 4 // 消息ID在消息头[]byte中的索引起始位置.
	MsgMsgIDIndexE    int = 8 // 消息ID在消息头[]byte中的索引结束位置.
)

消息结构相关常量.

View Source
const (
	MsgCryptFlag byte = 1 // 消息加密标识符.
	MsgZipFlag   byte = 1 // 消息压缩标识符.
)

加密, 压缩标识.

View Source
const BufReadMax int = 1024

BufReadMax 单次接收消息长度.

View Source
const HeartBeatTimeTick int64 = 300

HeartBeatTimeTick 心跳检测频率.(单位:秒)

View Source
const MsgChanMax int32 = 100

MsgChanMax message channel size.

Variables

This section is empty.

Functions

func Marshal

func Marshal(msg *Msg) ([]byte, error)

Marshal 消息编码. 参数: 1-消息结构体. 返回: 1-消息体二进制数据, 2-错误信息.

func ReadMsgLen

func ReadMsgLen(buf []byte) (uint16, error)

ReadMsgLen 获得消息长度. 参数: 1-长度为2字节的消息头信息. 返回: 1-消息长度, 2-错误信息.

Types

type Client

type Client struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Client struct.

func (*Client) Close

func (owner *Client) Close()

Close connection by external function.

func (*Client) ConnID

func (owner *Client) ConnID() int32

ConnID get connection id.

func (*Client) Run

func (owner *Client) Run(id int32)

Run ready to recv data.

func (*Client) Send

func (owner *Client) Send(msg interface{})

Send data to connection.

type ClientMngr

type ClientMngr struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

ClientMngr struct.

func (*ClientMngr) AddUID

func (owner *ClientMngr) AddUID(id int32, uid uint64)

AddUID add hashUIDs data. (login verify success, logic call this function.)

func (*ClientMngr) CheckConnection

func (owner *ClientMngr) CheckConnection(id int32) error

CheckConnection function.

func (*ClientMngr) DelUID

func (owner *ClientMngr) DelUID(id int32)

DelUID del hashUIDs data. (logic call this function.)

func (*ClientMngr) GetUIDByConnection

func (owner *ClientMngr) GetUIDByConnection(id int32) uint64

GetUIDByConnection function.

func (*ClientMngr) RefreshHBTime

func (owner *ClientMngr) RefreshHBTime(id int32)

RefreshHBTime function.

func (*ClientMngr) SendToAll

func (owner *ClientMngr) SendToAll(msg *Msg)

SendToAll function.

func (*ClientMngr) SendToClient

func (owner *ClientMngr) SendToClient(id int32, msg *Msg)

SendToClient function.

type Config

type Config struct {
	Flag int32
	Type string
	Host string
	Port int32
	Max  int32
}

Config struct.

func (*Config) String

func (owner *Config) String() string

type ConnEventCallback

type ConnEventCallback func(id int32, uid uint64)

ConnEventCallback type callback

type Event

type Event struct {
	Connid   int32
	Msgid    uint32
	MetaData []byte
}

Event struct.

type IHandler

type IHandler interface {
	Handle(ev *Event)
}

IHandler interface.

type Msg

type Msg struct {
	Len      uint16
	Crypt    byte
	Zip      byte
	ID       uint32
	MetaData []byte
}

Msg struct.

func Unmarshal

func Unmarshal(buf []byte) (*Msg, error)

Unmarshal 消息解码. 参数: 1-消息体二进制数据.(包含8字节的消息头) 返回: 1-格式化后的消息结构体, 2-错误信息.

type Router

type Router struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Router struct.

func (*Router) RegHandler

func (owner *Router) RegHandler(msgid uint32, handler interface{})

RegHandler 注册消息处理函数. 参数: 1-消息ID, 2-消息处理器结构体实例.

func (*Router) Route

func (owner *Router) Route(connid int32, msgid uint32, metadata []byte)

Route 解析后的消息放入路由中的事件队列进行分发.

type Service

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

Service struct.

func NewService

func NewService(conf Config) *Service

NewService 创建新的服务.

func (*Service) Close

func (owner *Service) Close()

Close network service.

func (*Service) GetClientMngr

func (owner *Service) GetClientMngr() *ClientMngr

GetClientMngr get TcpConnMngr Instance.

func (*Service) GetRouter

func (owner *Service) GetRouter() *Router

GetRouter get router Instance.

func (*Service) RegHandler

func (owner *Service) RegHandler(msgid uint32, handler interface{})

RegHandler register net message handler.

func (*Service) SetConnCallback

func (owner *Service) SetConnCallback(cbAdd, cbDel ConnEventCallback)

SetConnCallback register connection close event callback.

func (*Service) Start

func (owner *Service) Start()

Start network service.

Jump to

Keyboard shortcuts

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