1 Star 0 Fork 0

siliworks / common-package

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
options.go 3.92 KB
一键复制 编辑 原始数据 按行查看 历史
545403892 提交于 2023-06-06 00:08 . init
package server
import (
"gitee.com/siliworks/common-package/collector/module/define"
"gitee.com/micro-tools/wf/net/gtcp"
"gitee.com/micro-tools/wf/net/gudp"
"gitee.com/micro-tools/wf/os/gtime"
"net"
"time"
)
type Client struct {
Address string `json:"address"`
HeartBeatAt *gtime.Time `json:"heartBeatAt"`
ConnectAt *gtime.Time `json:"connectAt"`
Conn *gtcp.Conn
ReceiveCount int64 `json:"receiveData"`
SendCount int64 `json:"sendData"`
}
type UdpClient struct {
Address string `json:"address"`
HeartBeatAt *gtime.Time `json:"heartBeatAt"`
ConnectAt *gtime.Time `json:"connectAt"`
Conn *gudp.Conn
ReceiveCount int64 `json:"receiveData"`
SendCount int64 `json:"sendData"`
}
// Handle 接口
type Handle interface {
Send(key string, data []byte) error
GetClient(key string) *Client
RemoveClientFromBlacklist(key string) error
CloseClient(key string) error
CloseAllClient() error
ClientList() []*Client
Run() error
}
// OnConnectHandler 客户端连接成功后的回调函数
type OnConnectHandler func(Handle, *net.Conn)
// OnDisconnectHandler 客户端断开后的回调函数
type OnDisconnectHandler func(Handle, error)
// OnDataHandler 客户端收到数据后的回调函数
type OnDataHandler func(Handle, map[string]interface{}, string)
// OnAuthenticationHandler 认证回调函数
type OnAuthenticationHandler func(Handle, define.AgreementData) bool
type Options struct {
Address string `json:"address"`
KeepAlive time.Duration `json:"keepalive"`
AuthenticationEnable bool `json:"authenticationEnable"`
ClientConnectCap int `json:"clientConnectCap"`
ClientSameIPCap int `json:"clientSameIPCap"`
BlacklistDuration int64 `json:"blacklistDuration"`
AuthenticationFailedCap int `json:"authenticationFailedCap"`
OnConnect OnConnectHandler
OnDisconnect OnDisconnectHandler
OnData OnDataHandler
OnAuthentication OnAuthenticationHandler
}
// SetAddress sets the address to be used by the client to connect.
func (o *Options) SetAddress(address string) *Options {
o.Address = address
return o
}
// SetAuthenticationEnable sets the function to be called when the client connected and for the first time receives data.
func (o *Options) SetAuthenticationEnable(authenticationEnable bool) *Options {
o.AuthenticationEnable = authenticationEnable
return o
}
// SetClientConnectCap sets the maximum number of concurrent connections.
func (o *Options) SetClientConnectCap(cap int) *Options {
o.ClientConnectCap = cap
return o
}
// SetClientSameIPCap sets the maximum number of concurrent connections from the same IP.
func (o *Options) SetClientSameIPCap(cap int) *Options {
o.ClientSameIPCap = cap
return o
}
// SetBlacklistDuration sets the duration of the blacklist.
func (o *Options) SetBlacklistDuration(duration int64) *Options {
o.BlacklistDuration = duration
return o
}
// SetAuthenticationFailedCap sets the maximum number of authentication failures.
func (o *Options) SetAuthenticationFailedCap(cap int) *Options {
o.AuthenticationFailedCap = cap
return o
}
// SetOnConnectHandler sets the function to be called when the client is connected. Both
func (o *Options) SetOnConnectHandler(onConn OnConnectHandler) *Options {
o.OnConnect = onConn
return o
}
// SetOnDisconnectHandler sets the function to be called when the client is disconnected.
func (o *Options) SetOnDisconnectHandler(onLost OnDisconnectHandler) *Options {
o.OnDisconnect = onLost
return o
}
// SetOnDataHandler sets the function to be called when the client receives data.
func (o *Options) SetOnDataHandler(onData OnDataHandler) *Options {
o.OnData = onData
return o
}
// SetOnAuthenticationHandler sets the function to be called when the client receives data.
func (o *Options) SetOnAuthenticationHandler(onAuth OnAuthenticationHandler) *Options {
o.OnAuthentication = onAuth
return o
}
1
https://gitee.com/siliworks/common-package.git
git@gitee.com:siliworks/common-package.git
siliworks
common-package
common-package
v1.0.3

搜索帮助