netService

package
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2020 License: Apache-2.0 Imports: 11 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, receiveHandler util.ITaskWorkerPool, replyHandle util.ITaskWorkerPool, config *Config) netConn.IConnection

创建连接的方法

func NewService

func NewService(config *Config) netConn.IService

实例化TCP服务类

func NewUdpConnection

func NewUdpConnection(server *UdpService, conn *net.UDPConn, connId uint64, remoteAddr net.Addr, receiveHandler util.ITaskWorkerPool, replyHandle util.ITaskWorkerPool, config *Config) netConn.IConnection

Udp连接

func NewUdpService

func NewUdpService(config *Config) netConn.IService

创建UDP服务

Types

type Config

type Config struct {
	Network              string        //网络:tcp,udp
	AddrAry              []string      //监听地址和端口:绑定的IP加端口:["192.168.1.24:7018",...]
	ReceiveWorkerSize    uint          //(上行处理)工作池中工作线程个数
	ReceiveTaskQueueSize uint          //(上行处理)单个工作队列缓存任务大小
	ReplyWorkerSize      uint          //(下行处理)工作池中工作线程个数
	ReplyTaskQueueSize   uint          //(下行处理)单个工作队列缓存任务大小
	BufferSize           uint          //缓存尺寸(字节)
	BufferCount          uint          //缓存池缓存大小
	BytesCacheSize       uint          //分包缓存大小(TCP必填,UDP不需要填写)
	SendOutTime          time.Duration //下行超时时间(秒)
}

配置

func DefaultConfig added in v1.2.0

func DefaultConfig(network string, addrAry []string) *Config

默认配置

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) GetOvertimeConn added in v1.1.1

func (connMgr *ConnManager) GetOvertimeConn(overtimeSecond float64) []netConn.IConnection

获取超时连接(overtimeSecond 秒)

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全局唯一

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

连接结构体

func (*Connection) CallLogHandle added in v1.2.0

func (c *Connection) CallLogHandle(level netConn.ErrLevel, msgAry ...interface{})

调用异常处理

func (*Connection) GetBytesCache

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

获取分包缓存

func (*Connection) GetConnId

func (c *Connection) GetConnId() uint64

获取当前连接ID

func (*Connection) GetHeartTime added in v1.1.1

func (c *Connection) GetHeartTime() time.Time

获取心跳时间

func (*Connection) GetLocalAddr added in v1.2.2

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

获取本地地址信息

func (*Connection) GetNetConn

func (c *Connection) GetNetConn() interface{}

从当前连接获取原始的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 RequestTask added in v1.2.0

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

客户端请求内容

func (*RequestTask) Execute added in v1.2.0

func (r *RequestTask) Execute()

处理数据

func (*RequestTask) GetTaskId added in v1.2.0

func (r *RequestTask) GetTaskId() uint64

获取连接id

type ResponseTask added in v1.2.0

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

发送数据TCP

func (*ResponseTask) Execute added in v1.2.0

func (s *ResponseTask) Execute()

运行

func (*ResponseTask) GetTaskId added in v1.2.0

func (s *ResponseTask) GetTaskId() uint64

获取连接ID

type Service

type Service struct {
	ConnId  uint64               //客户端id
	ConnMgr netConn.IConnManager //当前Server的链接管理器

	CreateReceiver func(conn netConn.IConnection, data []byte) []netConn.IReceiver //数据分包
	BufferPool     *util.BufferPool                                                //缓存管理器
	// contains filtered or unexported fields
}

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

func (*Service) CallLogHandle added in v1.2.0

func (s *Service) CallLogHandle(level netConn.ErrLevel, msgAry ...interface{})

错误消息处理

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) CallOnReplyCompleted added in v1.2.0

func (s *Service) CallOnReplyCompleted(conn netConn.IConnection, data []byte, param interface{}, ok bool, err error)

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) GetTotalQueueCount added in v1.2.0

func (s *Service) GetTotalQueueCount() (receiveCount int32, replyCount int32)

获取队列剩余数

func (*Service) SetCreateReceiver

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

设置创建分包策略方法

func (*Service) SetLogHandle added in v1.2.0

func (s *Service) SetLogHandle(hookFunc func(level netConn.ErrLevel, 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) SetOnReplyCompleted added in v1.2.0

func (s *Service) SetOnReplyCompleted(hookFunc func(netConn.IConnection, []byte, interface{}, bool, error))

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     //客户端地址

	PackCount int64 //包个数

	HeartTime time.Time //心跳时间
	// contains filtered or unexported fields
}

func (*UdpConnection) CallLogHandle added in v1.2.0

func (c *UdpConnection) CallLogHandle(level netConn.ErrLevel, msgAry ...interface{})

调用异常处理

func (*UdpConnection) GetBytesCache

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

获取缓存

func (*UdpConnection) GetConnId

func (c *UdpConnection) GetConnId() uint64

获取当前连接ID

func (*UdpConnection) GetHeartTime added in v1.1.1

func (c *UdpConnection) GetHeartTime() time.Time

获取心跳时间

func (*UdpConnection) GetLocalAddr added in v1.2.2

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

获取本地地址信息

func (*UdpConnection) GetNetConn added in v1.2.2

func (c *UdpConnection) GetNetConn() interface{}

获取网络连接

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 UdpRequestTask added in v1.2.0

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

func (*UdpRequestTask) Execute added in v1.2.0

func (r *UdpRequestTask) Execute()

处理

func (*UdpRequestTask) GetConnection added in v1.2.0

func (r *UdpRequestTask) GetConnection() (netConn.IConnection, bool)

获取请求连接信息

func (*UdpRequestTask) GetTaskId added in v1.2.0

func (r *UdpRequestTask) GetTaskId() uint64

获取连接id

type UdpResponseTask added in v1.2.0

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

Udp响应

func (*UdpResponseTask) Execute added in v1.2.0

func (s *UdpResponseTask) Execute()

运行

func (*UdpResponseTask) GetTaskId added in v1.2.0

func (s *UdpResponseTask) GetTaskId() uint64

获取连接ID

type UdpService

type UdpService struct {
	ConnId  uint64               //客户端id
	ConnMgr netConn.IConnManager //当前Server的链接管理器

	CreateReceiver func(conn netConn.IConnection, data []byte) []netConn.IReceiver //数据分包
	BufferPool     *util.BufferPool                                                //缓存管理器
	ConnectionsMap sync.Map                                                        //连接地址映射
	// contains filtered or unexported fields
}

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

func (*UdpService) CallLogHandle added in v1.2.0

func (s *UdpService) CallLogHandle(level netConn.ErrLevel, msgAry ...interface{})

错误消息处理

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) CallOnReplyCompleted added in v1.2.0

func (s *UdpService) CallOnReplyCompleted(conn netConn.IConnection, data []byte, param interface{}, ok bool, err error)

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) GetTotalQueueCount added in v1.2.0

func (s *UdpService) GetTotalQueueCount() (receiveCount int32, replyCount int32)

获取队列剩余数

func (*UdpService) SetCreateReceiver

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

设置创建分包策略方法

func (*UdpService) SetLogHandle added in v1.2.0

func (s *UdpService) SetLogHandle(hookFunc func(level netConn.ErrLevel, 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) SetOnReplyCompleted added in v1.2.0

func (s *UdpService) SetOnReplyCompleted(hookFunc func(netConn.IConnection, []byte, interface{}, bool, error))

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