123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890 |
- //go:build windows
- package yu_hpsocket
- import yu_sys "gogs.qqck.cn/s/tools/sys"
- // Server
- // @Description: Server 系列组件底层实现
- type Server[T any] struct {
- hListener uintptr
- hServer uintptr
- SSL bool
- Extra any // 附加数据
- // 事件----------------------------------
- onPrepareListen func(t *T, Sender, Listen uintptr) HR
- onAccept func(t *T, Sender, ConnID, Client uintptr) HR
- onHandShake func(t *T, Sender, ConnID uintptr) HR
- onSend func(t *T, Sender, ConnID, Data uintptr, Length uintptr) HR
- onReceive func(t *T, Sender, ConnID, Data uintptr, Length uintptr) HR
- onClose func(t *T, Sender, ConnID uintptr, Operation SO, ErrorCode int) HR
- onShutdown func(t *T, Sender uintptr) HR
- }
- // isValid
- //
- // @Description: 对象是否有效
- func (t *Server[T]) isValid() bool {
- return t.hListener != 0 && t.hServer != 0
- }
- // Start 名称:启动通信组件
- //
- // @Description: 启动服务端通信组件,启动完成后可开始接收客户端连接并收发数据
- // @param BindAddress 监听地址
- // @param Port 监听端口
- // @return bool 是否启动成功
- func (t *Server[T]) Start(BindAddress string, Port int) bool {
- if !t.isValid() {
- return false
- }
- return HP_Server_Start.CallBool(t.hServer, yu_sys.S{yu_sys.Gbk, BindAddress}, Port)
- }
- // Stop 名称:关闭通信组件
- //
- // @Description: 关闭服务端通信组件,关闭完成后断开所有客户端连接并释放所有资源
- // @return bool 是否成功关闭
- func (t *Server[T]) Stop() bool {
- if !t.isValid() {
- return false
- }
- return HP_Server_Stop.CallBool(t.hServer)
- }
- // Wait
- //
- // @Description: 等待通信组件停止运行
- // @param Milliseconds 超时时间(毫秒,默认:-1,永不超时)
- // @return bool 超时返回 false
- func (t *Server[T]) Wait(Milliseconds int) bool {
- if !t.isValid() {
- return false
- }
- return HP_Server_Wait.CallBool(t.hServer, Milliseconds)
- }
- // Send
- //
- // @Description: 向指定连接发送数据
- // @param ConnID 连接 ID
- // @param buf 欲发送的数据
- // @return bool 是否发送成功
- func (t *Server[T]) Send(ConnID uintptr, buf []byte) bool {
- if !t.isValid() {
- return false
- }
- if len(buf) == 0 {
- return true
- }
- return HP_Server_Send.CallBool(t.hServer, ConnID, buf, len(buf))
- }
- // SendS
- //
- // @Description: 向指定连接发送数据
- // @param ConnID 连接 ID
- // @param buf 欲发送的数据
- // @return bool 是否发送成功
- func (t *Server[T]) SendS(ConnID uintptr, buf string) bool {
- if !t.isValid() {
- return false
- }
- if len(buf) == 0 {
- return true
- }
- return HP_Server_Send.CallBool(t.hServer, ConnID, buf, len(buf))
- }
- // IsSecure
- //
- // @Description: 检测是否为安全连接(SSL/HTTPS)
- func (t *Server[T]) IsSecure() bool {
- if !t.isValid() {
- return false
- }
- return HP_Server_IsSecure.CallBool(t.hServer)
- }
- // Disconnect
- //
- // @Description: 断开与某个客户端的连接
- // @param Force 是否强制断开连接
- func (t *Server[T]) Disconnect(ConnID uintptr, Force bool) bool {
- if !t.isValid() {
- return false
- }
- return HP_Server_Disconnect.CallBool(t.hServer, ConnID, Force)
- }
- // DisconnectLongConnections
- //
- // @Description: 断开超过指定时长的连接
- // @param Period 时长(毫秒)
- // @param Force 是否强制断开连接
- func (t *Server[T]) DisconnectLongConnections(Period uintptr, Force bool) bool {
- if !t.isValid() {
- return false
- }
- return HP_Server_DisconnectLongConnections.CallBool(t.hServer, Period, Force)
- }
- // DisconnectSilenceConnections
- //
- // @Description: 断开静默连接
- // @param Period 时长(毫秒)
- // @param Force 是否强制断开连接
- func (t *Server[T]) DisconnectSilenceConnections(Period uintptr, Force bool) bool {
- if !t.isValid() {
- return false
- }
- return HP_Server_DisconnectSilenceConnections.CallBool(t.hServer, Period, Force)
- }
- // SetExtra
- //
- // @Description: 设置连接的附加数据
- // @param ConnID 连接 ID
- // @param Extra 附加数据
- func (t *Server[T]) SetExtra(ConnID, Extra uintptr) (success bool) {
- if !t.isValid() {
- return
- }
- return HP_Server_SetConnectionExtra.CallBool(t.hServer, ConnID, Extra)
- }
- // GetExtra
- //
- // @Description: 获取连接的附加数据
- // @param ConnID 连接 ID
- func (t *Server[T]) GetExtra(ConnID uintptr) (Extra uintptr) {
- if !t.isValid() {
- return
- }
- HP_Server_GetConnectionExtra.Call(t.hServer, ConnID, &Extra)
- return
- }
- // HasStarted
- //
- // @Description: 检查通信组件是否已启动
- func (t *Server[T]) HasStarted() bool {
- if !t.isValid() {
- return false
- }
- return HP_Server_HasStarted.CallBool(t.hServer)
- }
- // GetState
- //
- // @Description: 查看通信组件当前状态,SS_ 开头常量
- func (t *Server[T]) GetState() SS {
- if !t.isValid() {
- return SS_STOPPED
- }
- return SS(HP_Server_GetState.CallUintptr(t.hServer))
- }
- // GetLastError
- //
- // @Description: 获取最近一次失败操作的错误代码
- func (t *Server[T]) GetLastError() uintptr {
- if !t.isValid() {
- return 0
- }
- return HP_Server_GetLastError.CallUintptr(t.hServer)
- }
- // GetLastErrorDesc
- //
- // @Description: 获取最近一次失败操作的错误描述
- func (t *Server[T]) GetLastErrorDesc() string {
- if !t.isValid() {
- return "Not Object"
- }
- return HP_Server_GetLastErrorDesc.CallGbkToUtf8(t.hServer)
- }
- // GetPendingDataLength
- //
- // @Description: 获取连接中未发出数据的长度
- // @param ConnID 连接 ID
- func (t *Server[T]) GetPendingDataLength(ConnID uintptr) (Pending int) {
- if !t.isValid() {
- return
- }
- HP_Server_GetPendingDataLength.Call(t.hServer, ConnID, &Pending)
- return
- }
- // GetConnectionCount
- //
- // @Description: 获取客户端连接数
- func (t *Server[T]) GetConnectionCount() int {
- if !t.isValid() {
- return 0
- }
- return HP_Server_GetConnectionCount.CallInt(t.hServer)
- }
- // GetAllConnectionIDs
- //
- // @Description: 获取所有连接的 HP_CONNID
- // @return IDs
- func (t *Server[T]) GetAllConnectionIDs() (IDs []uintptr) {
- if !t.isValid() {
- return
- }
- var j_count uintptr
- HP_Server_GetAllConnectionIDs.Call(t.hServer, 0, &j_count)
- if j_count == 0 {
- return
- }
- IDs = make([]uintptr, j_count)
- HP_Server_GetAllConnectionIDs.Call(t.hServer, IDs, &j_count)
- return
- }
- // GetConnectPeriod
- //
- // @Description: 获取某个客户端连接时长(毫秒)
- // @param ConnID 连接 ID
- func (t *Server[T]) GetConnectPeriod(ConnID uintptr) (Pending int) {
- if !t.isValid() {
- return
- }
- HP_Server_GetConnectPeriod.Call(t.hServer, ConnID, &Pending)
- return
- }
- // GetSilencePeriod
- //
- // @Description: 获取某个连接静默时间(毫秒)
- // @param ConnID 连接 ID
- func (t *Server[T]) GetSilencePeriod(ConnID uintptr) (Period int) {
- if !t.isValid() {
- return
- }
- HP_Server_GetSilencePeriod.Call(t.hServer, ConnID, &Period)
- return
- }
- // GetListenAddress
- //
- // @Description: 获取监听 Socket 的地址信息
- func (t *Server[T]) GetListenAddress() (address string, port int) {
- if !t.isValid() {
- return
- }
- j_address_len := 128
- j_address := make([]byte, 64)
- if !HP_Server_GetListenAddress.CallBool(t.hServer, j_address, &j_address_len, &port) {
- return
- }
- j_address_len -= 1 // 有结束符 {0}
- j_unicode_len := j_address_len * 2
- j_unicode := make([]byte, j_unicode_len)
- yu_sys.MultiByteToWideChar.Call(936, 0, j_address, j_address_len, j_unicode, j_unicode_len)
- j_utf8_size := j_unicode_len * 2
- j_utf8 := make([]byte, j_utf8_size)
- address = string(j_utf8[:yu_sys.WideCharToMultiByte.CallInt(65001, 0, j_unicode, j_address_len, j_utf8, j_utf8_size, 0, 0)])
- return
- }
- // GetLocalAddress
- //
- // @Description: 获取某个连接的本地地址信息
- // @param ConnID 连接 ID
- func (t *Server[T]) GetLocalAddress(ConnID uintptr) (address string, port int) {
- if !t.isValid() {
- return
- }
- j_address_len := 128
- j_address := make([]byte, 64)
- if !HP_Server_GetLocalAddress.CallBool(t.hServer, ConnID, j_address, &j_address_len, &port) {
- return
- }
- j_address_len -= 1 // 有结束符 {0}
- j_unicode_len := j_address_len * 2
- j_unicode := make([]byte, j_unicode_len)
- yu_sys.MultiByteToWideChar.Call(936, 0, j_address, j_address_len, j_unicode, j_unicode_len)
- j_utf8_size := j_unicode_len * 2
- j_utf8 := make([]byte, j_utf8_size)
- address = string(j_utf8[:yu_sys.WideCharToMultiByte.CallInt(65001, 0, j_unicode, j_address_len, j_utf8, j_utf8_size, 0, 0)])
- return
- }
- // GetRemoteAddress
- //
- // @Description: 获取某个连接的远程地址信息
- // @param ConnID 连接 ID
- func (t *Server[T]) GetRemoteAddress(ConnID uintptr) (address string, port int) {
- if !t.isValid() {
- return
- }
- j_address_len := 128
- j_address := make([]byte, 64)
- if !HP_Server_GetRemoteAddress.CallBool(t.hServer, ConnID, j_address, &j_address_len, &port) {
- return
- }
- j_address_len -= 1 // 有结束符 {0}
- j_unicode_len := j_address_len * 2
- j_unicode := make([]byte, j_unicode_len)
- yu_sys.MultiByteToWideChar.Call(936, 0, j_address, j_address_len, j_unicode, j_unicode_len)
- j_utf8_size := j_unicode_len * 2
- j_utf8 := make([]byte, j_utf8_size)
- address = string(j_utf8[:yu_sys.WideCharToMultiByte.CallInt(65001, 0, j_unicode, j_address_len, j_utf8, j_utf8_size, 0, 0)])
- return
- }
- // SetSendPolicy
- //
- // @Description: 设置数据发送策略
- // @param SendPolicy SP_ 开头常量
- func (t *Server[T]) SetSendPolicy(SendPolicy SP) *Server[T] {
- if !t.isValid() {
- return t
- }
- HP_Server_SetSendPolicy.Call(t.hServer, uintptr(SendPolicy))
- return t
- }
- // SetOnSendSyncPolicy
- //
- // @Description: 设置 OnSend 事件同步策略(默认:OSSP_NONE,不同步)
- // @param SyncPolicy OSSP_ 开头常量
- func (t *Server[T]) SetOnSendSyncPolicy(SyncPolicy OSSP) *Server[T] {
- if !t.isValid() {
- return t
- }
- HP_Server_SetOnSendSyncPolicy.Call(t.hServer, uintptr(SyncPolicy))
- return t
- }
- // SetFreeSocketObjLockTime
- //
- // @Description: 设置 Socket 缓存对象锁定时间(毫秒,在锁定期间该 Socket 缓存对象不能被获取使用)
- func (t *Server[T]) SetFreeSocketObjLockTime(FreeSocketObjLockTime int) *Server[T] {
- if !t.isValid() {
- return t
- }
- HP_Server_SetFreeSocketObjLockTime.Call(t.hServer, FreeSocketObjLockTime)
- return t
- }
- // SetFreeSocketObjPool
- //
- // @Description: 设置 Socket 缓存池大小(通常设置为平均并发连接数量的 1/3 - 1/2)
- func (t *Server[T]) SetFreeSocketObjPool(FreeSocketObjPool int) *Server[T] {
- if !t.isValid() {
- return t
- }
- HP_Server_SetFreeSocketObjPool.Call(t.hServer, FreeSocketObjPool)
- return t
- }
- // SetFreeBufferObjPool
- //
- // @Description: 设置内存块缓存池大小(通常设置为 Socket 缓存池大小的 2 - 3 倍)
- func (t *Server[T]) SetFreeBufferObjPool(FreeBufferObjPool int) *Server[T] {
- if !t.isValid() {
- return t
- }
- HP_Server_SetFreeBufferObjPool.Call(t.hServer, FreeBufferObjPool)
- return t
- }
- // SetFreeSocketObjHold
- //
- // @Description: 设置 Socket 缓存池回收阀值(通常设置为 Socket 缓存池大小的 3 倍)
- func (t *Server[T]) SetFreeSocketObjHold(FreeSocketObjHold int) *Server[T] {
- if !t.isValid() {
- return t
- }
- HP_Server_SetFreeSocketObjHold.Call(t.hServer, FreeSocketObjHold)
- return t
- }
- // SetFreeBufferObjHold
- //
- // @Description: 设置内存块缓存池回收阀值(通常设置为内存块缓存池大小的 3 倍)
- func (t *Server[T]) SetFreeBufferObjHold(FreeBufferObjHold int) *Server[T] {
- if !t.isValid() {
- return t
- }
- HP_Server_SetFreeBufferObjHold.Call(t.hServer, FreeBufferObjHold)
- return t
- }
- // SetMaxConnectionCount
- //
- // @Description: 设置最大连接数(组件会根据设置值预分配内存,因此需要根据实际情况设置,不宜过大)
- func (t *Server[T]) SetMaxConnectionCount(MaxConnectionCount int) *Server[T] {
- if !t.isValid() {
- return t
- }
- HP_Server_SetMaxConnectionCount.Call(t.hServer, MaxConnectionCount)
- return t
- }
- // SetWorkerThreadCount
- //
- // @Description: 设置工作线程数量(通常设置为 2 * CPU + 2)
- func (t *Server[T]) SetWorkerThreadCount(WorkerThreadCount int) *Server[T] {
- if !t.isValid() {
- return t
- }
- HP_Server_SetWorkerThreadCount.Call(t.hServer, WorkerThreadCount)
- return t
- }
- // SetMarkSilence
- //
- // @Description: 设置是否标记静默时间(设置为 TRUE 时 DisconnectSilenceConnections() 和 GetSilencePeriod() 才有效,默认:TRUE)
- func (t *Server[T]) SetMarkSilence(MarkSilence bool) *Server[T] {
- if !t.isValid() {
- return t
- }
- HP_Server_SetMarkSilence.Call(t.hServer, MarkSilence)
- return t
- }
- // GetSendPolicy
- //
- // @Description: 获取数据发送策略
- func (t *Server[T]) GetSendPolicy() SP {
- if !t.isValid() {
- return 0
- }
- return SP(HP_Server_GetSendPolicy.CallUintptr(t.hServer))
- }
- // GetOnSendSyncPolicy
- //
- // @Description: 获取 OnSend 事件同步策略,返回 OSSP_ 开头常量
- func (t *Server[T]) GetOnSendSyncPolicy() OSSP {
- if !t.isValid() {
- return 0
- }
- return OSSP(HP_Server_GetOnSendSyncPolicy.CallUintptr(t.hServer))
- }
- // GetFreeSocketObjLockTime
- //
- // @Description: 获取 Socket 缓存对象锁定时间
- func (t *Server[T]) GetFreeSocketObjLockTime() int {
- if !t.isValid() {
- return 0
- }
- return HP_Server_GetFreeSocketObjLockTime.CallInt(t.hServer)
- }
- // GetFreeSocketObjPool
- //
- // @Description: 获取 Socket 缓存池大小
- func (t *Server[T]) GetFreeSocketObjPool() int {
- if !t.isValid() {
- return 0
- }
- return HP_Server_GetFreeSocketObjPool.CallInt(t.hServer)
- }
- // GetFreeBufferObjPool
- //
- // @Description: 获取内存块缓存池大小
- func (t *Server[T]) GetFreeBufferObjPool() int {
- if !t.isValid() {
- return 0
- }
- return HP_Server_GetFreeBufferObjPool.CallInt(t.hServer)
- }
- // GetFreeSocketObjHold
- //
- // @Description: 获取 Socket 缓存池回收阀值
- func (t *Server[T]) GetFreeSocketObjHold() int {
- if !t.isValid() {
- return 0
- }
- return HP_Server_GetFreeSocketObjHold.CallInt(t.hServer)
- }
- // GetFreeBufferObjHold
- //
- // @Description: 获取内存块缓存池回收阀值
- func (t *Server[T]) GetFreeBufferObjHold() int {
- if !t.isValid() {
- return 0
- }
- return HP_Server_GetFreeBufferObjHold.CallInt(t.hServer)
- }
- // GetMaxConnectionCount
- //
- // @Description: 获取最大连接数
- func (t *Server[T]) GetMaxConnectionCount() int {
- if !t.isValid() {
- return 0
- }
- return HP_Server_GetMaxConnectionCount.CallInt(t.hServer)
- }
- // GetWorkerThreadCount
- //
- // @Description: 获取工作线程数量
- func (t *Server[T]) GetWorkerThreadCount() int {
- if !t.isValid() {
- return 0
- }
- return HP_Server_GetWorkerThreadCount.CallInt(t.hServer)
- }
- // IsMarkSilence
- //
- // @Description: 检测是否标记静默时间
- func (t *Server[T]) IsMarkSilence() bool {
- if !t.isValid() {
- return false
- }
- return HP_Server_IsMarkSilence.CallBool(t.hServer)
- }
- // SetupSSLContext
- //
- // @Description: 初始化通信组件SSL环境参数 必须在 SSL 通信组件启动前完成初始化,否则启动失败
- // @param VerifyMode SSL_VM_ 开头常量, SSL验证模式
- // @param PemCertFile 证书文件
- // @param PemKeyFile 私钥文件
- // @param KeyPasswod 私钥密码(没有密码则为空)
- // @param CAPemCertFileOrPath CA 证书文件或目录(单向验证或客户端可选)
- func (t *Server[T]) SetupSSLContext(VerifyMode SSL_VM, PemCertFile, PemKeyFile, KeyPasswod, CAPemCertFileOrPath string) bool {
- if !t.isValid() {
- return false
- }
- return HP_SSLServer_SetupSSLContext.CallBool(t.hServer, uintptr(VerifyMode),
- yu_sys.S{yu_sys.Gbk, PemCertFile}, yu_sys.S{yu_sys.Gbk, PemKeyFile},
- yu_sys.S{yu_sys.Gbk, KeyPasswod}, yu_sys.S{yu_sys.Gbk, CAPemCertFileOrPath},
- 0, // fnServerNameCallback SNI 回调函数指针(可选)int (__HP_CALL *Fn_SNI_ServerNameCallback)(LPCTSTR lpszServerName, PVOID pContext)
- )
- }
- // SetupSSLContextByMemory 名称:
- //
- // @Description: 初始化通信组件SSL环境参数 必须在SSL通信组件启动前完成初始化,否则启动失败
- // @param VerifyMode SSL_VM_ 开头常量, SSL验证模式
- // @param PemCert 证书内容
- // @param PemKey 私钥内容
- // @param KeyPasswod 私钥密码(没有密码则为空)
- // @param CAPemCert CA 证书内容(单向验证可选)
- func (t *Server[T]) SetupSSLContextByMemory(VerifyMode SSL_VM, PemCert, PemKey, KeyPasswod, CAPemCert string) bool {
- if !t.isValid() {
- return false
- }
- return HP_SSLServer_SetupSSLContextByMemory.CallBool(t.hServer, uintptr(VerifyMode),
- yu_sys.S{yu_sys.Gbk, PemCert}, yu_sys.S{yu_sys.Gbk, PemKey},
- yu_sys.S{yu_sys.Gbk, KeyPasswod}, yu_sys.S{yu_sys.Gbk, CAPemCert},
- 0, // fnServerNameCallback SNI 回调函数指针(可选)int (__HP_CALL *Fn_SNI_ServerNameCallback)(LPCTSTR lpszServerName, PVOID pContext)
- )
- }
- // AddSSLContext 名称:增加 SNI 主机证书
- //
- // @Description: 增加 SNI 主机证书 SSL服务端在 SetupSSLContext() 成功后可以调用本方法增加多个 SNI 主机证书,正数-成功,并返回 SNI 主机证书对应的索引,该索引用于在 SNI 回调函数中定位 SNI 主机,负数-失败
- // @param VerifyMode SSL_VM_ 开头常量, SSL验证模式
- // @param PemCertFile 证书文件
- // @param PemKeyFile 私钥文件
- // @param KeyPasswod 私钥密码(没有密码则为空)
- // @param CAPemCertFileOrPath CA 证书文件或目录(单向验证或客户端可选)
- func (t *Server[T]) AddSSLContext(VerifyMode SSL_VM, PemCertFile, PemKeyFile, KeyPasswod, CAPemCertFileOrPath string) int {
- if !t.isValid() {
- return -1
- }
- return HP_SSLServer_AddSSLContext.CallInt(t.hServer, uintptr(VerifyMode),
- yu_sys.S{yu_sys.Gbk, PemCertFile}, yu_sys.S{yu_sys.Gbk, PemKeyFile},
- yu_sys.S{yu_sys.Gbk, KeyPasswod}, yu_sys.S{yu_sys.Gbk, CAPemCertFileOrPath},
- )
- }
- // AddSSLContextByMemory 名称:增加 SNI 主机证书
- //
- // @Description: 增加 SNI 主机证书 SSL服务端在 SetupSSLContext() 成功后可以调用本方法增加多个 SNI 主机证书,正数-成功,并返回 SNI 主机证书对应的索引,该索引用于在 SNI 回调函数中定位 SNI 主机,负数-失败
- // @param VerifyMode SSL_VM_ 开头常量, SSL验证模式
- // @param PemCert 证书文件
- // @param PemKey 私钥文件
- // @param KeyPasswod 私钥密码(没有密码则为空)
- // @param CAPemCert CA 证书内容(单向验证可选)
- func (t *Server[T]) AddSSLContextByMemory(VerifyMode SSL_VM, PemCert, PemKey, KeyPasswod, CAPemCert string) int {
- if !t.isValid() {
- return -1
- }
- return HP_SSLServer_AddSSLContextByMemory.CallInt(t.hServer, uintptr(VerifyMode),
- yu_sys.S{yu_sys.Gbk, PemCert}, yu_sys.S{yu_sys.Gbk, PemKey},
- yu_sys.S{yu_sys.Gbk, KeyPasswod}, yu_sys.S{yu_sys.Gbk, CAPemCert},
- )
- }
- // BindSSLServerName 名称:绑定 SNI 主机域名
- //
- // @Description: 绑定 SNI 主机域名 SSL服务端在 AddSSLContext() 成功后可以调用本方法绑定主机域名到 SNI 主机证书
- // @param DomainName 主机域名
- // @param ContextIndex SNI 主机证书对应的索引
- func (t *Server[T]) BindSSLServerName(DomainName string, ContextIndex uintptr) bool {
- if !t.isValid() {
- return false
- }
- return HP_SSLServer_BindSSLServerName.CallBool(t.hServer, yu_sys.S{yu_sys.Gbk, DomainName}, ContextIndex)
- }
- // StartSSLHandShake
- //
- // @Description: 启动 SSL 握手,当通信组件设置为非自动握手时,需要调用本方法启动 SSL 握手
- // @param ConnID 连接 ID
- func (t *Server[T]) StartSSLHandShake(ConnID uintptr) bool {
- if !t.isValid() {
- return false
- }
- return HP_SSLServer_StartSSLHandShake.CallBool(t.hServer, ConnID)
- }
- // SetSSLAutoHandShake
- //
- // @Description: 设置通信组件握手方式(默认:真,自动握手)
- func (t *Server[T]) SetSSLAutoHandShake(AutoHandShake bool) *Server[T] {
- if !t.isValid() {
- return t
- }
- HP_SSLServer_SetSSLAutoHandShake.Call(t.hServer, AutoHandShake)
- return t
- }
- // IsSSLAutoHandShake
- //
- // @Description: 获取通信组件握手方式
- func (t *Server[T]) IsSSLAutoHandShake() bool {
- if !t.isValid() {
- return false
- }
- return HP_SSLServer_IsSSLAutoHandShake.CallBool(t.hServer)
- }
- // SetSSLCipherList
- //
- // @Description: 设置 SSL加密算法列表
- func (t *Server[T]) SetSSLCipherList(CipherList string) *Server[T] {
- if !t.isValid() {
- return t
- }
- HP_SSLServer_SetSSLCipherList.Call(t.hServer, yu_sys.S{yu_sys.Gbk, CipherList})
- return t
- }
- // GetSSLCipherList
- //
- // @Description: 获取 SSL加密算法列表
- func (t *Server[T]) GetSSLCipherList() string {
- if !t.isValid() {
- return ""
- }
- return HP_SSLServer_GetSSLCipherList.CallGbkToUtf8(t.hServer)
- }
- // GetSSLSessionInfo 名称:
- //
- // @Description: 获取指定类型的 SSLSession 信息(输出类型参考:SSL_SSI_)
- // @param info SSL_SSI_ 开头常量
- func (t *Server[T]) GetSSLSessionInfo(info SSL_SSI) (Info uintptr) {
- if !t.isValid() {
- return
- }
- HP_SSLServer_GetSSLSessionInfo.Call(t.hServer, uintptr(info), &Info)
- return
- }
- // IsPauseReceive
- //
- // @Description: 获取连接的数据接收状态
- // @param ConnID 连接 ID
- func (t *Server[T]) IsPauseReceive(ConnID uintptr) (Paused bool) {
- if !t.isValid() {
- return
- }
- HP_Server_IsPauseReceive.Call(t.hServer, ConnID, &Paused)
- return
- }
- // IsConnected
- //
- // @Description: 检测是否有效连接
- // @param ConnID 连接 ID
- func (t *Server[T]) IsConnected(ConnID uintptr) bool {
- if !t.isValid() {
- return false
- }
- return HP_Server_IsConnected.CallBool(t.hServer, ConnID)
- }
- // PauseReceive
- //
- // @Description: 暂停/恢复某个连接的数据接收工作
- // @param ConnID 连接 ID
- func (t *Server[T]) PauseReceive(ConnID uintptr, Paused bool) bool {
- if !t.isValid() {
- return false
- }
- return HP_Server_PauseReceive.CallBool(t.hServer, ConnID, Paused)
- }
- // CleanupSSLContext
- //
- // @Description: 清理通信组件 SSL运行环境,回收 SSL相关内存,无,/当要重新设置通信组件 SSL环境参数时,需要先调用本方法清理原先的环境参数
- func (t *Server[T]) CleanupSSLContext() *Server[T] {
- if !t.isValid() {
- return t
- }
- HP_SSLServer_CleanupSSLContext.Call(t.hServer)
- return t
- }
- // OnPrepareListen
- //
- // @Description: 【可选】绑定准备监听事件
- // @param call->return HR_ 开头常量
- func (t *Server[T]) OnPrepareListen(call func(t *T, Sender, Listen uintptr) HR) *Server[T] {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_Server_OnPrepareListen.Call(t.hListener, 0)
- } else {
- t.onPrepareListen = call
- HP_Set_FN_Server_OnPrepareListen.Call(t.hListener, server_callback_OnPrepareListen_ptr)
- }
- return t
- }
- // OnAccept
- //
- // @Description: 【可选】绑定接受事件
- // @param call->return HR_ 开头常量
- func (t *Server[T]) OnAccept(call func(t *T, Sender, ConnID, Client uintptr) HR) *Server[T] {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_Server_OnAccept.Call(t.hListener, 0)
- } else {
- t.onAccept = call
- HP_Set_FN_Server_OnAccept.Call(t.hListener, server_callback_OnAccept_ptr)
- }
- return t
- }
- // OnHandShake
- //
- // @Description: 【可选】绑定握手事件
- // @param call->return HR_ 开头常量
- func (t *Server[T]) OnHandShake(call func(t *T, Sender, ConnID uintptr) HR) *Server[T] {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_Server_OnHandShake.Call(t.hListener, 0)
- } else {
- t.onHandShake = call
- HP_Set_FN_Server_OnHandShake.Call(t.hListener, server_callback_OnHandShake_ptr)
- }
- return t
- }
- // OnSend
- //
- // @Description: 【可选】绑定发送事件
- // @param call->return HR_ 开头常量
- func (t *Server[T]) OnSend(call func(t *T, Sender, ConnID, Data uintptr, Length uintptr) HR) *Server[T] {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_Server_OnSend.Call(t.hListener, 0)
- } else {
- t.onSend = call
- HP_Set_FN_Server_OnSend.Call(t.hListener, server_callback_OnSend_ptr)
- }
- return t
- }
- // OnReceive
- //
- // @Description: 【必选】绑定数据接收事件
- // @param call->return HR_ 开头常量
- func (t *Server[T]) OnReceive(call func(t *T, Sender, ConnID, Data uintptr, Length uintptr) HR) *Server[T] {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_Server_OnReceive.Call(t.hListener, 0)
- } else {
- t.onReceive = call
- HP_Set_FN_Server_OnReceive.Call(t.hListener, server_callback_OnReceive_ptr)
- }
- return t
- }
- // OnClose
- //
- // @Description: 【必选】绑定断开事件
- // @param call->Operation 通过该参数标识是哪种操作导致的错误 SO_ 开头常量
- // @param call->return HR_ 开头常量
- func (t *Server[T]) OnClose(call func(t *T, Sender, ConnID uintptr, Operation SO, ErrorCode int) HR) *Server[T] {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_Server_OnClose.Call(t.hListener, 0)
- } else {
- t.onClose = call
- HP_Set_FN_Server_OnClose.Call(t.hListener, server_callback_OnClose_ptr)
- }
- return t
- }
- // OnShutdown
- //
- // @Description: 【可选】绑定关闭事件
- // @param call->return HR_ 开头常量
- func (t *Server[T]) OnShutdown(call func(t *T, Sender uintptr) HR) *Server[T] {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_Server_OnShutdown.Call(t.hListener, 0)
- } else {
- t.onShutdown = call
- HP_Set_FN_Server_OnShutdown.Call(t.hListener, server_callback_OnShutdown_ptr)
- }
- return t
- }
- // OnDefault
- //
- // @Description: 【可选】绑定所有【必选】事件
- func (t *Server[T]) OnDefault() *Server[T] {
- if !t.isValid() {
- return t
- }
- if t.onReceive == nil {
- t.OnReceive(func(t *T, Sender, ConnID, Data uintptr, Length uintptr) HR {
- return HR_OK
- })
- }
- if t.onClose == nil {
- t.OnClose(func(t *T, Sender, ConnID uintptr, Operation SO, ErrorCode int) HR {
- return HR_OK
- })
- }
- return t
- }
|