netService

package
v1.7.7 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2021 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConnManager

func NewConnManager() netInterface.IConnManager

实例化管理

func NewConnection

func NewConnection(server *Service, conn net.Conn, connId uint64, receiveHandler pools.ITaskWorkerPool, replyHandle pools.ITaskWorkerPool, config *Config) netInterface.IConnection

创建连接的方法

func NewService

func NewService(config *Config) netInterface.IService

实例化TCP服务类

func NewUdpService

func NewUdpService(config *Config) netInterface.IService

创建UDP服务

Types

type AcceptTask added in v1.4.1

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

连接接入任务

func (*AcceptTask) CallOutTime added in v1.4.1

func (a *AcceptTask) CallOutTime()

超时回调

func (*AcceptTask) GetDuration added in v1.6.7

func (a *AcceptTask) GetDuration() time.Duration

func (*AcceptTask) GetTaskId added in v1.4.1

func (a *AcceptTask) GetTaskId() uint64

获取连接id

func (*AcceptTask) RunTask added in v1.4.1

func (a *AcceptTask) RunTask()

处理数据

type AddrConfig added in v1.5.0

type AddrConfig struct {
	Addr      string //地址端口:192.168.1.24:7018
	CertFile  string //server.crt 文件路径:TCP支持Tls
	KeyFile   string //server.key 文件路径:TCP支持Tls
	ClientCer string //配置客户端证书【加入证书会开启客户端验证】
	IsTls     bool   //是否TLS连接
}

Tls配置

type Config

type Config struct {
	Network              string        //网络:tcp,udp
	AddrAry              []*AddrConfig //[普通/Tls]监听地址和端口:绑定的IP加端口:["192.168.1.24:7018",...]
	ReceiveWorkerSize    uint          //(上行处理)工作池中工作线程个数,必须2的N次方
	ReceiveTaskQueueSize uint          //(上行处理)单个工作队列缓存任务大小
	ReceiveOutTime       time.Duration //(上行处理)处理任务超时时间 ,IsOutTime=true 时生效
	ReplyWorkerSize      uint          //(下行处理)工作池中工作线程个数,必须2的N次方
	ReplyTaskQueueSize   uint          //(下行处理)单个工作队列缓存任务大小
	ReplyOutTime         time.Duration //(下行处理)超时时间(完整任务) ,IsOutTime=true 时生效
	AcceptWorkerSize     uint          //(连接接入处理)工作池中工作线程个数[tcp使用],必须2的N次方
	AcceptTaskQueueSize  uint          //(连接接入处理)单个工作队列缓存任务大小[tcp使用]
	AcceptOutTime        time.Duration //(连接接入处理)超时时间,IsOutTime=true 时生效
	BufferSize           uint          //缓存尺寸(字节)
	BytesCacheSize       uint          //分包缓存大小
	HDataCache           bool          //是否启用处理数据缓存优化[注意:启用后SetOnReceive 处理方法使用协程会导致数据错乱]
	SendOutTime          time.Duration //(下行处理网络)超时时间
	IsOutTime            bool          //是否需要支持超时
	OverflowDiscard      bool          //接收,处理,回复溢出是否丢弃【true:丢弃,false:等待处理】
}

配置

func DefaultConfig added in v1.2.0

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

普通网络配置

func NewConfig added in v1.5.0

func NewConfig(network string, addrAry []*AddrConfig) *Config

普通、Tls 网络都支持配置

type ConnManager

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

连接管理模块

func (*ConnManager) Add

func (c *ConnManager) Add(conn netInterface.IConnection)

添加链接

func (*ConnManager) ClearConn

func (c *ConnManager) ClearConn()

清除并停止所有连接

func (*ConnManager) Count added in v1.4.1

func (c *ConnManager) Count() int

获取个数

func (*ConnManager) Get

func (c *ConnManager) Get(connId uint64) (netInterface.IConnection, bool)

利用ConnID获取链接

func (*ConnManager) Range added in v1.3.1

func (c *ConnManager) Range(hFunc func(connId uint64, value netInterface.IConnection) bool)

遍历连接

func (*ConnManager) Remove

func (c *ConnManager) Remove(conn netInterface.IConnection)

删除连接

func (*ConnManager) RemoveById added in v1.7.5

func (c *ConnManager) RemoveById(connId uint64)

删除连接

type Connection

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

连接结构体

func (*Connection) CallLogHandle added in v1.2.0

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

调用异常处理

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) GetIsClosed added in v1.4.1

func (c *Connection) GetIsClosed() bool

获取的状态(ture:关闭状态,false:未关闭)

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) GetNetwork added in v1.6.6

func (c *Connection) GetNetwork() string

GetNetwork 获取网络类型

func (*Connection) GetProperty

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

获取链接属性

func (*Connection) GetPropertyKeys added in v1.6.3

func (c *Connection) GetPropertyKeys() []string

获取所有属性key

func (*Connection) GetRecInfo added in v1.4.1

func (c *Connection) GetRecInfo() (count, byteSize uint64)

上行当前处理的包总数(处理前,1开始),总大小(字节)

func (*Connection) GetRemoteAddr

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

获取远程客户端地址信息

func (*Connection) GetRepInfo added in v1.4.1

func (c *Connection) GetRepInfo() (count, byteSize, errCount uint64)

下行当前处理的包总数(处理后),总大小(字节)

func (*Connection) GetStartTime added in v1.4.1

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

获取连接启动时间

func (*Connection) OnCompleted

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

数据上传处理

func (*Connection) OnReceive added in v1.6.1

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

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

func (*Connection) RemoveProperty

func (c *Connection) RemoveProperty(key string)

移除链接属性

func (*Connection) SendData

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

直接将数据发送数据给远程的TCP客户端 data 下发数据 cmdCode 指令标识[如: rep 普通回复, cmd 用户操作下发 。。]

func (*Connection) SendDataCall

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

直接将数据发送数据给远程的TCP客户端(带参数和回调) data 下发数据 param 下发需要回调携带参数 cmdCode 指令标识[如: rep 普通回复, cmd 用户操作下发 。。] callFunc 下发后回调函数

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 ReceiveTask added in v1.4.1

type ReceiveTask struct {
	OnCompleted func(data []byte, count int) //接收到一次数据(未分包开始处理不确定是否完成)
	// contains filtered or unexported fields
}

客户端请求内容

func (*ReceiveTask) CallOutTime added in v1.4.1

func (r *ReceiveTask) CallOutTime()

任务超时

func (*ReceiveTask) GetDuration added in v1.6.7

func (r *ReceiveTask) GetDuration() time.Duration

func (*ReceiveTask) GetTaskId added in v1.4.1

func (r *ReceiveTask) GetTaskId() uint64

获取连接id

func (*ReceiveTask) RunTask added in v1.4.1

func (r *ReceiveTask) RunTask()

处理数据

type ReplyTask added in v1.4.1

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

发送数据TCP

func (*ReplyTask) CallOutTime added in v1.4.1

func (r *ReplyTask) CallOutTime()

任务超时

func (*ReplyTask) GetDuration added in v1.6.7

func (r *ReplyTask) GetDuration() time.Duration

func (*ReplyTask) GetTaskId added in v1.4.1

func (r *ReplyTask) GetTaskId() uint64

获取连接ID

func (*ReplyTask) RunTask added in v1.4.1

func (r *ReplyTask) RunTask()

运行

type Service

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

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

func (*Service) AddConnId added in v1.4.1

func (s *Service) AddConnId() uint64

累加并获取自增ID

func (*Service) CallLogHandle added in v1.2.0

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

错误消息处理

func (*Service) CallOnConnStart

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

调用连接OnConnStart Hook函数

func (*Service) CallOnConnStop

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

调用连接OnConnStop Hook函数

func (*Service) CallOnReceive added in v1.6.1

func (s *Service) CallOnReceive(conn netInterface.IConnection, data []byte)

CallOnReceive 数据上传完成回调

func (*Service) CallOnReply added in v1.6.1

func (s *Service) CallOnReply(conn netInterface.IConnection, data []byte, isOk bool, cmdCode string, param interface{}, err error)

下发后回调

func (*Service) GetAcceptWorkerPool added in v1.7.5

func (s *Service) GetAcceptWorkerPool() pools.ITaskWorkerPool

[TCP特有]连接接收处理器(工作池)

func (*Service) GetConn

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

获取连接

func (*Service) GetConnMgr

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

得到链接管理

func (*Service) GetReceiveWorkerPool added in v1.7.5

func (s *Service) GetReceiveWorkerPool() pools.ITaskWorkerPool

消息处理模块(工作池)

func (*Service) GetReplyWorkerPool added in v1.7.5

func (s *Service) GetReplyWorkerPool() pools.ITaskWorkerPool

消息发送处理器(工作池)

func (*Service) GetStartTime added in v1.4.1

func (s *Service) GetStartTime() time.Time

获取连接启动时间

func (*Service) SetCreateReceiver

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

设置创建分包策略方法

func (*Service) SetLogHandle added in v1.2.0

func (s *Service) SetLogHandle(hookFunc func(level netInterface.ErrLevel, msg ...interface{}))

设置日志处理

func (*Service) SetOnConnStart

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

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

func (*Service) SetOnConnStop

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

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

func (*Service) SetOnReceive added in v1.6.1

func (s *Service) SetOnReceive(hookFunc func(netInterface.IConnection, []byte))

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

func (*Service) SetOnReply added in v1.6.1

func (s *Service) SetOnReply(hookFunc func(netInterface.IConnection, []byte, bool, string, interface{}, error))

func (*Service) Start

func (s *Service) Start()

开启网络服务

func (*Service) Stop

func (s *Service) Stop()

停止服务

type UdpConnection

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

func NewUdpConnection

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

Udp连接

func (*UdpConnection) CallLogHandle added in v1.2.0

func (u *UdpConnection) CallLogHandle(level netInterface.ErrLevel, msgAry ...interface{})

调用异常处理

func (*UdpConnection) GetConnId

func (u *UdpConnection) GetConnId() uint64

获取当前连接ID

func (*UdpConnection) GetHeartTime added in v1.1.1

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

获取心跳时间

func (*UdpConnection) GetIsClosed added in v1.4.1

func (u *UdpConnection) GetIsClosed() bool

获取的状态(ture:关闭状态,false:未关闭)

func (*UdpConnection) GetLocalAddr added in v1.2.2

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

获取本地地址信息

func (*UdpConnection) GetNetConn added in v1.2.2

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

获取网络连接

func (*UdpConnection) GetNetwork added in v1.6.6

func (u *UdpConnection) GetNetwork() string

GetNetwork 获取网络类型

func (*UdpConnection) GetProperty

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

获取链接属性

func (*UdpConnection) GetPropertyKeys added in v1.6.3

func (u *UdpConnection) GetPropertyKeys() []string

获取所有属性key

func (*UdpConnection) GetRecInfo added in v1.4.1

func (u *UdpConnection) GetRecInfo() (count, byteSize uint64)

上行当前处理的包总数(处理前,1开始),总大小(字节)

func (*UdpConnection) GetRemoteAddr

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

获取远程客户端地址信息

func (*UdpConnection) GetRepInfo added in v1.4.1

func (u *UdpConnection) GetRepInfo() (count, byteSize, errCount uint64)

下行当前处理的包总数(处理后),总大小(字节)

func (*UdpConnection) GetStartTime added in v1.4.1

func (u *UdpConnection) GetStartTime() time.Time

获取连接启动时间

func (*UdpConnection) OnCompleted

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

Udp 不需要分包

func (*UdpConnection) OnReceive added in v1.6.1

func (u *UdpConnection) OnReceive(data []byte)

数据上传完整的一包处理

func (*UdpConnection) RemoveProperty

func (u *UdpConnection) RemoveProperty(key string)

移除链接属性

func (*UdpConnection) SendData

func (u *UdpConnection) SendData(data []byte, cmdCode string) error

直接将数据发送数据给远程的TCP客户端 data 下发数据 cmdCode 指令标识[如: rep 普通回复, cmd 用户操作下发 。。]

func (*UdpConnection) SendDataCall

func (u *UdpConnection) SendDataCall(data []byte, cmdCode string, param interface{}, callFunc func(netInterface.IConnection, []byte, bool, string, interface{}, error)) error

直接将数据发送数据给远程的TCP客户端(带参数和回调) data 下发数据 param 下发需要回调携带参数 cmdCode 指令标识[如: rep 普通回复, cmd 用户操作下发 。。] callFunc 下发后回调函数

func (*UdpConnection) SetProperty

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

设置链接属性

func (*UdpConnection) Start

func (u *UdpConnection) Start()

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

func (*UdpConnection) Stop

func (u *UdpConnection) Stop()

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

type UdpReceiveTask added in v1.4.1

type UdpReceiveTask struct {
	OnCompleted func(data []byte, count int) //接收到一次数据(未分包开始处理不确定是否完成)
	// contains filtered or unexported fields
}

func (*UdpReceiveTask) CallOutTime added in v1.4.1

func (u *UdpReceiveTask) CallOutTime()

任务超时

func (*UdpReceiveTask) GetDuration added in v1.6.7

func (u *UdpReceiveTask) GetDuration() time.Duration

func (*UdpReceiveTask) GetTaskId added in v1.4.1

func (u *UdpReceiveTask) GetTaskId() uint64

获取连接id

func (*UdpReceiveTask) RunTask added in v1.4.1

func (u *UdpReceiveTask) RunTask()

处理

type UdpReplyTask added in v1.4.1

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

Udp响应

func (*UdpReplyTask) CallOutTime added in v1.4.1

func (u *UdpReplyTask) CallOutTime()

任务超时

func (*UdpReplyTask) GetDuration added in v1.6.7

func (u *UdpReplyTask) GetDuration() time.Duration

func (*UdpReplyTask) GetTaskId added in v1.4.1

func (u *UdpReplyTask) GetTaskId() uint64

获取连接ID

func (*UdpReplyTask) RunTask added in v1.4.1

func (u *UdpReplyTask) RunTask()

运行

type UdpService

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

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

func (*UdpService) AddConnId added in v1.4.1

func (u *UdpService) AddConnId() uint64

累加并获取自增ID

func (*UdpService) CallLogHandle added in v1.2.0

func (u *UdpService) CallLogHandle(level netInterface.ErrLevel, msgAry ...interface{})

错误消息处理

func (*UdpService) CallOnConnStart

func (u *UdpService) CallOnConnStart(conn netInterface.IConnection)

调用连接OnConnStart Hook函数

func (*UdpService) CallOnConnStop

func (u *UdpService) CallOnConnStop(conn netInterface.IConnection)

调用连接OnConnStop Hook函数

func (*UdpService) CallOnReceive added in v1.6.1

func (u *UdpService) CallOnReceive(conn netInterface.IConnection, data []byte)

数据上传完成回调

func (*UdpService) CallOnReply added in v1.6.1

func (u *UdpService) CallOnReply(conn netInterface.IConnection, data []byte, isOk bool, cmdCode string, param interface{}, err error)

下发后回调

func (*UdpService) GetAcceptWorkerPool added in v1.7.5

func (u *UdpService) GetAcceptWorkerPool() pools.ITaskWorkerPool

[TCP特有]连接接收处理器(工作池)

func (*UdpService) GetConn

func (u *UdpService) GetConn(connId uint64) (netInterface.IConnection, bool)

func (*UdpService) GetConnMgr

func (u *UdpService) GetConnMgr() netInterface.IConnManager

得到链接管理

func (*UdpService) GetReceiveWorkerPool added in v1.7.5

func (u *UdpService) GetReceiveWorkerPool() pools.ITaskWorkerPool

消息处理模块(工作池)

func (*UdpService) GetReplyWorkerPool added in v1.7.5

func (u *UdpService) GetReplyWorkerPool() pools.ITaskWorkerPool

消息发送处理器(工作池)

func (*UdpService) GetStartTime added in v1.4.1

func (u *UdpService) GetStartTime() time.Time

获取连接启动时间

func (*UdpService) SetCreateReceiver

func (u *UdpService) SetCreateReceiver(hookFunc func(netInterface.IConnection, []byte) []netInterface.IReceiver)

设置创建分包策略方法

func (*UdpService) SetLogHandle added in v1.2.0

func (u *UdpService) SetLogHandle(hookFunc func(level netInterface.ErrLevel, msg ...interface{}))

func (*UdpService) SetOnConnStart

func (u *UdpService) SetOnConnStart(hookFunc func(netInterface.IConnection))

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

func (*UdpService) SetOnConnStop

func (u *UdpService) SetOnConnStop(hookFunc func(netInterface.IConnection))

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

func (*UdpService) SetOnReceive added in v1.6.1

func (u *UdpService) SetOnReceive(hookFunc func(netInterface.IConnection, []byte))

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

func (*UdpService) SetOnReply added in v1.6.1

func (u *UdpService) SetOnReply(hookFunc func(netInterface.IConnection, []byte, bool, string, interface{}, error))

func (*UdpService) Start

func (u *UdpService) Start()

开启网络服务

func (*UdpService) Stop

func (u *UdpService) Stop()

停止服务

Jump to

Keyboard shortcuts

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