network

package
v0.0.0-...-677ea5f Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2021 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCodec

func NewCodec(encode FuncEncode, decode FuncDecode) *poolCodec

func NewGate

func NewGate(cap int) *gate

func NewJsonCodec

func NewJsonCodec() *poolCodec

func NewMsgPackCodec

func NewMsgPackCodec() *poolCodec

func NewPbCodec

func NewPbCodec() *poolCodec

func NewRouter

func NewRouter() *router

func NewTcpDialer

func NewTcpDialer(name, addr string, deadline time.Duration, receiver FNetReceiver, onClosed FAgent) *tcpDialer

func NewTcpListener

func NewTcpListener(addr string, onConn func(conn net.Conn)) *tcpListener

func NewWebsocketDialer

func NewWebsocketDialer(url, protocol, origin string, deadline time.Duration, receiver FNetReceiver, onClosed FAgent) *websocketDialer

func NewWebsocketListener

func NewWebsocketListener(addr string, onConn func(conn net.Conn)) *websocketListener

Types

type FAgent

type FAgent func(agent IAgent)

type FAgentChecker

type FAgentChecker func(data utils.IData) bool

type FNetReceiver

type FNetReceiver func(agent IAgent, data []byte)

@Description: 网络消息接收函数 @param agent @param data

type FServiceHandle

type FServiceHandle func(agent IAgent, data []byte)

type FuncDecode

type FuncDecode func(data []byte, pkt interface{}) error

type FuncEncode

type FuncEncode func(pkt interface{}) ([]byte, error)

type FuncTestConn

type FuncTestConn func(conn net.Conn) bool

验证是否允许连接

type IAgent

type IAgent interface {
	// @Description: 远程地址
	// @return string
	Addr() string
	// @Description: 连接接口
	// @return net.Conn
	Conn() net.Conn
	// @Description: 设置消息接收函数
	// @param receiver
	// @return error
	SetReceiver(receiver FNetReceiver) error
	// @Description: 发送数据
	// @param data
	// @return error
	Send(data []byte) error
	// @Description: 启动,内部开启发送和接收消息的协程
	Start()
	// @Description: 停止,内部停止发送和接收消息的协程
	Stop()
	// @Description: 连接持有的临时数据
	Data() utils.IData
}

@Description: 连接代理接口

func NewTcpAgent

func NewTcpAgent(addr string, deadline time.Duration, conn net.Conn, onClose FAgent, receiver FNetReceiver, data map[string]interface{}) IAgent

func NewWebsocketAgent

func NewWebsocketAgent(addr string, deadline time.Duration, conn net.Conn, onClose FAgent, receiver FNetReceiver, data map[string]interface{}) IAgent

type ICodec

type ICodec interface {
	// @Description: 编码
	// @param pkt
	// @return []byte
	// @return error
	Encode(pkt interface{}) ([]byte, error)
	// @Description: 解码
	// @param data
	// @param pkt
	// @return error
	Decode(data []byte, pkt interface{}) error
}

@Description: 编解码器

type IDialer

type IDialer interface {
	Id() string
	// @Description: 连接远程服务器
	// @param data 初始数据,不需要可设置nil
	// @return error
	Connect(data map[string]interface{}) error
	// @Description: 连接代理
	// @return IAgent
	Agent() IAgent
	// @Description: 关闭连接
	Close()
}

@Description: 拨号器接口

type IGate

type IGate interface {
	// @Description: 添加连接代理
	// @param agent
	Add(agent IAgent)
	// @Description: 移除连接代理
	// @param agent
	Remove(agent string)
	// @Description: 获取连接代理
	// @param agent
	// @return IAgent
	// @return bool
	Get(agent string) (IAgent, bool)
	// @Description: 发送消息到指定代理
	// @param id
	// @param data
	// @return error
	Send(id string, data []byte) error
	// @Description: 广播消息到所有代理
	// @param data
	SendAll(data []byte)
	// @Description: 发送消息到多个代理
	// @param agents
	// @param data
	SendMulti(agents []string, data []byte)
	// @Description: 发送除指定代理外的其他代理
	// @param agents
	// @param data
	SendExclude(agents []string, data []byte)
	// @Description: 使用过滤器发送指定代理
	// @param filter
	// @param data
	SendFilter(filter func(IAgent) bool, data []byte)
	// @Description: 关闭所有代理
	CloseAll()
	// @Description: 当前代理的数量
	// @return uint16
	Len() uint16
}

@Description: 网关

type IListener

type IListener interface {
	// @Description: 开始监听
	// @return error
	Listen() error
	// @Description: 关闭监听
	Close()
}

网络监听

type IPoolCodec

type IPoolCodec interface {
	ICodec
	// @Description: 编码请求包,使用对象池对象,用完自动回收
	// @param code
	// @param setReq 设置请求包的数据
	// @return []byte
	// @return error
	EncodeReq(code uint32, setReq utils.ActionObj) ([]byte, error)
	// @Description: 编码响应包,使用对象池对象,用完自动回收
	// @param code
	// @param setRes 设置响应包的数据
	// @return []byte
	// @return error
	EncodeRes(code uint32, setRes utils.ActionObj) ([]byte, error)
	// @Description: 解码请求包,需要手动调用RecycleReq回收
	// @param code
	// @param data
	// @return interface{}
	// @return error
	DecodeReq(code uint32, data []byte) (interface{}, error)
	// @Description: 解码响应包,需要手动调用RecycleRes回收
	// @param code
	// @param data
	// @return interface{}
	// @return error
	DecodeRes(code uint32, data []byte) (interface{}, error)
	// @Description: 从对象池生成一个请求包
	// @param code
	// @return interface{}
	// @return error
	SpawnReq(code uint32) (interface{}, error)
	// @Description: 回收请求包
	// @param code
	// @param packet
	// @return error
	RecycleReq(code uint32, packet interface{}) error
	// @Description: 从对象池生成一个响应包
	// @param code
	// @return interface{}
	// @return error
	SpawnRes(code uint32) (interface{}, error)
	// @Description: 回收响应包
	// @param code
	// @param packet
	// @return error
	RecycleRes(code uint32, packet interface{}) error
	// @Description: 绑定编解码函数
	// @param code
	// @param requestNewFunc
	// @param responseNewFunc
	BindPool(code uint32, requestNewFunc func() interface{}, responseNewFunc func() interface{})
}

@Description: 使用对象池的消息包编解码器

type IRouter

type IRouter interface {
	Bind(head uint8, handle FServiceHandle, checker FAgentChecker)
	BindWithMap(m map[uint8]FServiceHandle, checker FAgentChecker)
	Receive(agent IAgent, data []byte)
}

Jump to

Keyboard shortcuts

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