netService

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2020 License: MPL-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConnManager

func NewConnManager() netConn.IConnManager

实例化管理

func NewConnection

func NewConnection(server *Service, conn net.Conn, connId uint64, msgHandler netConn.IMsgHandle, config *Config) netConn.IConnection

创建连接的方法

func NewMsgHandle

func NewMsgHandle(workerSize int, taskQueueSize int) netConn.IMsgHandle

创建消息处理队列

func NewService

func NewService(config *Config) netConn.IService

实例化TCP服务类

func NewUdpConnection

func NewUdpConnection(server *UdpService, conn *net.UDPConn, connId uint64, remoteAddr net.Addr, msgHandler netConn.IMsgHandle, config *Config) netConn.IConnection

Udp连接

func NewUdpService

func NewUdpService(config *Config) netConn.IService

创建一个服务器句柄 name:服务名称 network:网络类型 "udp", "udp4", "udp6" port:端口 workerSize:工作池 workerPoolSize:单工作线程缓存个数 bufferSize:单个缓存大小 bufferCount:缓存数量

Types

type CallSendHandle

type CallSendHandle struct {
	Buf      []byte                                              //发送数据
	CallFunc func(netConn.IConnection, bool, interface{}, error) //回调方法
	Param    interface{}                                         //参数
}

数据发送回调处理

type Config

type Config struct {
	Network       string   //网络
	AddrAry       []string //监听地址和端口:绑定的IP加端口:["192.168.1.24:7018",...]
	WorkerSize    int      //工作池中工作线程个数
	TaskQueueSize int      //单个工作队列缓存任务大小
	BufferSize    uint     //缓存尺寸(字节)
	BufferCount   uint     //缓存池缓存大小
	SendDataCount int      //单个连接待发送数据个数
}

配置

type ConnManager

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

连接管理模块

func (*ConnManager) Add

func (connMgr *ConnManager) Add(conn netConn.IConnection)

添加链接

func (*ConnManager) ClearConn

func (connMgr *ConnManager) ClearConn()

清除并停止所有连接

func (*ConnManager) Get

func (connMgr *ConnManager) Get(connID uint64) (netConn.IConnection, bool)

利用ConnID获取链接

func (*ConnManager) Len

func (connMgr *ConnManager) Len() int

获取个数

func (*ConnManager) Remove

func (connMgr *ConnManager) Remove(conn netConn.IConnection)

删除连接

type Connection

type Connection struct {
	Server *Service //当前conn属于哪个server,在conn初始化的时候添加即可
	Conn   net.Conn //当前连接的socket 套接字
	ConnId uint64   //当前连接的ID 也可以称作为SessionID,ID全局唯一

	ExitBuffChan chan bool //告知该链接已经退出/停止的channel(管道)

	MsgHandler netConn.IMsgHandle  //消息处理
	HeartTime  time.Time           //心跳时间
	Receiver   []netConn.IReceiver //分包器
	PackCount  int64               //包个数
	// contains filtered or unexported fields
}

连接结构体

func (*Connection) GetBytesCache

func (c *Connection) GetBytesCache() *bytes.Buffer

获取缓存

func (*Connection) GetConnId

func (c *Connection) GetConnId() uint64

获取当前连接ID

func (*Connection) GetNetConn

func (c *Connection) GetNetConn() net.Conn

从当前连接获取原始的socket TCPConn

func (*Connection) GetPackCount

func (c *Connection) GetPackCount() int64

获取包个数

func (*Connection) GetProperty

func (c *Connection) GetProperty(key string) (interface{}, error)

获取链接属性

func (*Connection) GetReceiver

func (c *Connection) GetReceiver() []netConn.IReceiver

获取分包器

func (*Connection) GetRemoteAddr

func (c *Connection) GetRemoteAddr() net.Addr

获取远程客户端地址信息

func (*Connection) OnCompleted

func (c *Connection) OnCompleted(data []byte, offset int, count int)

数据上传处理

func (*Connection) OnReceiveCompleted

func (c *Connection) OnReceiveCompleted(data []byte)

数据上传了完整一包的回调

func (*Connection) RemoveProperty

func (c *Connection) RemoveProperty(key string)

移除链接属性

func (*Connection) SendData

func (c *Connection) SendData(data []byte) error

直接将Message数据发送数据给远程的TCP客户端

func (*Connection) SendDataCall

func (c *Connection) SendDataCall(data []byte, param interface{}, callFunc func(netConn.IConnection, bool, interface{}, error)) error

直接将Message数据发送数据给远程的TCP客户端(带参数和回调)

func (*Connection) SetProperty

func (c *Connection) SetProperty(key string, value interface{})

设置链接属性

func (*Connection) Start

func (c *Connection) Start()

启动连接,让当前连接开始工作

func (*Connection) Stop

func (c *Connection) Stop()

停止连接,结束当前连接状态M

type MsgHandle

type MsgHandle struct {
	TaskQueue   []chan netConn.IRequest //Worker负责取任务的消息队列
	ExitMsgChan []chan bool             //告知该链接已经退出/停止的channel(管道)
	// contains filtered or unexported fields
}

消息处理队列

func (*MsgHandle) DoMsgHandler

func (mh *MsgHandle) DoMsgHandler(request netConn.IRequest)

处理消息

func (*MsgHandle) SendMsgToTaskQueue

func (mh *MsgHandle) SendMsgToTaskQueue(request netConn.IRequest)

将消息交给TaskQueue,由worker进行处理

func (*MsgHandle) StartOneWorker

func (mh *MsgHandle) StartOneWorker(workerID int, taskQueue chan netConn.IRequest)

启动一个Worker工作流程

func (*MsgHandle) StartWorkerPool

func (mh *MsgHandle) StartWorkerPool()

启动worker工作池

func (*MsgHandle) StopWorkerPool

func (mh *MsgHandle) StopWorkerPool()

停止worker工作池[关闭程序强制退出]

type Request

type Request struct {
	Conn  netConn.IConnection //已经和客户端建立好的 链接
	Data  []byte              //客户端请求的数据
	Count int                 //数据长度
}

客户端请求内容

func (*Request) GetConnection

func (r *Request) GetConnection() netConn.IConnection

获取请求连接信息

func (*Request) GetCount

func (r *Request) GetCount() int

获取请求消息的长度

func (*Request) GetData

func (r *Request) GetData() []byte

获取请求消息的数据

type Service

type Service struct {
	ConnId uint64 //客户端id

	ConnMgr            netConn.IConnManager                                            //当前Server的链接管理器
	OnErrorHandle      func(level string, msg string)                                  //设置异常处理
	OnConnStart        func(conn netConn.IConnection)                                  //该Server的连接创建时Hook函数
	OnConnStop         func(conn netConn.IConnection)                                  //该Server的连接断开时的Hook函数
	OnReceiveCompleted func(conn netConn.IConnection, data []byte)                     //数据上传完成
	ExitServiceChan    chan bool                                                       //告知该链接已经退出/停止的channel(管道)
	CreateReceiver     func(conn netConn.IConnection, data []byte) []netConn.IReceiver //数据分包
	BufferPool         *util.BufferPool                                                //缓存管理器
	// contains filtered or unexported fields
}

IServer 接口实现,定义一个Server服务类

func (*Service) CallErrorhandle

func (s *Service) CallErrorhandle(level string, msg ...string)

错误消息处理

func (*Service) CallOnConnStart

func (s *Service) CallOnConnStart(conn netConn.IConnection)

调用连接OnConnStart Hook函数

func (*Service) CallOnConnStop

func (s *Service) CallOnConnStop(conn netConn.IConnection)

调用连接OnConnStop Hook函数

func (*Service) CallOnReceiveCompleted

func (s *Service) CallOnReceiveCompleted(conn netConn.IConnection, data []byte)

数据上传完成回调

func (*Service) GetConn

func (s *Service) GetConn(connId uint64) (netConn.IConnection, bool)

func (*Service) GetConnId

func (s *Service) GetConnId() uint64

获取自增ID

func (*Service) GetConnMgr

func (s *Service) GetConnMgr() netConn.IConnManager

得到链接管理

func (*Service) SetCreateReceiver

func (s *Service) SetCreateReceiver(hookFunc func(netConn.IConnection, []byte) []netConn.IReceiver)

设置创建分包策略方法

func (*Service) SetErrorHandle

func (s *Service) SetErrorHandle(hookFunc func(level string, msg string))

func (*Service) SetOnConnStart

func (s *Service) SetOnConnStart(hookFunc func(netConn.IConnection))

设置该Server的连接创建时Hook函数

func (*Service) SetOnConnStop

func (s *Service) SetOnConnStop(hookFunc func(netConn.IConnection))

设置该Server的连接断开时的Hook函数

func (*Service) SetOnReceiveCompleted

func (s *Service) SetOnReceiveCompleted(hookFunc func(netConn.IConnection, []byte))

数据上传完成处理函数[分包后]

func (*Service) Start

func (s *Service) Start()

开启网络服务

func (*Service) Stop

func (s *Service) Stop()

停止服务

type UdpConnection

type UdpConnection struct {
	Server     *UdpService  //当前conn属于哪个server,在conn初始化的时候添加即可
	Conn       *net.UDPConn //当前连接的socket 套接字
	ConnId     uint64       //当前连接的ID 也可以称作为SessionID,ID全局唯一
	RemoteAddr net.Addr     //客户端地址

	ExitBuffChan chan bool //告知该链接已经退出/停止的channel(管道)

	PackCount int64 //包个数

	MsgHandler netConn.IMsgHandle //消息处理
	HeartTime  time.Time          //心跳时间
	// contains filtered or unexported fields
}

func (*UdpConnection) GetBytesCache

func (c *UdpConnection) GetBytesCache() *bytes.Buffer

获取缓存

func (*UdpConnection) GetConnId

func (c *UdpConnection) GetConnId() uint64

获取当前连接ID

func (*UdpConnection) GetPackCount

func (c *UdpConnection) GetPackCount() int64

获取包个数

func (*UdpConnection) GetProperty

func (c *UdpConnection) GetProperty(key string) (interface{}, error)

获取链接属性

func (*UdpConnection) GetRemoteAddr

func (c *UdpConnection) GetRemoteAddr() net.Addr

获取远程客户端地址信息

func (*UdpConnection) OnCompleted

func (c *UdpConnection) OnCompleted(data []byte, offset int, count int)

数据上次一包(未分包开始处理不确定是否完成)

func (*UdpConnection) OnReceiveCompleted

func (c *UdpConnection) OnReceiveCompleted(data []byte)

数据上传完整的一包处理

func (*UdpConnection) RemoveProperty

func (c *UdpConnection) RemoveProperty(key string)

移除链接属性

func (*UdpConnection) SendData

func (c *UdpConnection) SendData(data []byte) error

发送消息到客户端

func (*UdpConnection) SendDataCall

func (c *UdpConnection) SendDataCall(data []byte, param interface{}, callFunc func(netConn.IConnection, bool, interface{}, error)) error

发送消息到客户端带回调

func (*UdpConnection) SetProperty

func (c *UdpConnection) SetProperty(key string, value interface{})

设置链接属性

func (*UdpConnection) Start

func (c *UdpConnection) Start()

启动连接,让当前连接开始工作

func (*UdpConnection) Stop

func (c *UdpConnection) Stop()

停止连接,结束当前连接状态M

type UdpService

type UdpService struct {
	ConnId uint64 //客户端id

	ConnMgr            netConn.IConnManager                                            //当前Server的链接管理器
	OnErrorHandle      func(level string, msg string)                                  //设置异常处理
	OnConnStart        func(conn netConn.IConnection)                                  //该Server的连接创建时Hook函数
	OnConnStop         func(conn netConn.IConnection)                                  //该Server的连接断开时的Hook函数
	OnReceiveCompleted func(conn netConn.IConnection, data []byte)                     //数据上传完成
	ExitServiceChan    chan bool                                                       //告知该链接已经退出/停止的channel(管道)
	CreateReceiver     func(conn netConn.IConnection, data []byte) []netConn.IReceiver //数据分包
	BufferPool         *util.BufferPool                                                //缓存管理器
	ConnectionsMap     map[string]uint64                                               //连接地址映射
	// contains filtered or unexported fields
}

IServer 接口实现,定义一个Server服务类

func (*UdpService) CallErrorhandle

func (s *UdpService) CallErrorhandle(level string, msgs ...string)

错误消息处理

func (*UdpService) CallOnConnStart

func (s *UdpService) CallOnConnStart(conn netConn.IConnection)

调用连接OnConnStart Hook函数

func (*UdpService) CallOnConnStop

func (s *UdpService) CallOnConnStop(conn netConn.IConnection)

调用连接OnConnStop Hook函数

func (*UdpService) CallOnReceiveCompleted

func (s *UdpService) CallOnReceiveCompleted(conn netConn.IConnection, data []byte)

数据上传完成回调

func (*UdpService) GetConn

func (s *UdpService) GetConn(connId uint64) (netConn.IConnection, bool)

func (*UdpService) GetConnId

func (s *UdpService) GetConnId() uint64

获取自增ID

func (*UdpService) GetConnMgr

func (s *UdpService) GetConnMgr() netConn.IConnManager

得到链接管理

func (*UdpService) SetCreateReceiver

func (s *UdpService) SetCreateReceiver(hookFunc func(netConn.IConnection, []byte) []netConn.IReceiver)

设置创建分包策略方法

func (*UdpService) SetErrorHandle

func (s *UdpService) SetErrorHandle(hookFunc func(level string, msg string))

func (*UdpService) SetOnConnStart

func (s *UdpService) SetOnConnStart(hookFunc func(netConn.IConnection))

设置该Server的连接创建时Hook函数

func (*UdpService) SetOnConnStop

func (s *UdpService) SetOnConnStop(hookFunc func(netConn.IConnection))

设置该Server的连接断开时的Hook函数

func (*UdpService) SetOnReceiveCompleted

func (s *UdpService) SetOnReceiveCompleted(hookFunc func(netConn.IConnection, []byte))

数据上传完成处理函数[分包后]

func (*UdpService) Start

func (s *UdpService) Start()

开启网络服务

func (*UdpService) Stop

func (s *UdpService) Stop()

停止服务

Jump to

Keyboard shortcuts

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