server_tcp_windows.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //go:build windows
  2. package yu_hpsocket
  3. // TcpServer
  4. // @Description: TcpServerListener 对象,请自己创建和销毁,目的是保证安全
  5. type TcpServer struct {
  6. Server[TcpServer]
  7. }
  8. // NewTcpServer
  9. //
  10. // @Description: 创建 TcpServer 对象
  11. func NewTcpServer() (t *TcpServer) {
  12. t = &TcpServer{}
  13. return
  14. }
  15. // Create
  16. //
  17. // @Description: 创建 TcpServer 环境
  18. func (t *TcpServer) Create(SSL bool) bool {
  19. t.Close()
  20. if t.hListener, _, _ = Create_HP_TcpServerListener.Call(); t.hListener == 0 {
  21. return false
  22. }
  23. if t.SSL = SSL; t.SSL {
  24. t.hServer, _, _ = Create_HP_SSLServer.Call(t.hListener)
  25. } else {
  26. t.hServer, _, _ = Create_HP_TcpServer.Call(t.hListener)
  27. }
  28. if t.hServer == 0 {
  29. Destroy_HP_TcpServerListener.Call(t.hListener)
  30. t.hListener = 0
  31. return false
  32. }
  33. _server_on_set(t.hServer, &_server_item{
  34. _type: _server_type_TcpServer,
  35. t: t,
  36. })
  37. return true
  38. }
  39. // Close
  40. //
  41. // @Description: 销毁 TcpServer 环境
  42. func (t *TcpServer) Close() {
  43. t.Stop()
  44. t.Wait(-1)
  45. if t.hServer != 0 {
  46. if t.SSL {
  47. Destroy_HP_SSLServer.Call(t.hServer)
  48. } else {
  49. Destroy_HP_TcpServer.Call(t.hServer)
  50. }
  51. _server_on_del(t.hServer)
  52. t.hServer = 0
  53. }
  54. if t.hListener != 0 {
  55. Destroy_HP_TcpServerListener.Call(t.hListener)
  56. t.hListener = 0
  57. }
  58. }
  59. // SetSocketListenQueue
  60. //
  61. // @Description: 设置监听 Socket 的等候队列大小(根据并发连接数量调整设置)
  62. func (t *TcpServer) SetSocketListenQueue(SocketListenQueue int) *TcpServer {
  63. if !t.isValid() {
  64. return t
  65. }
  66. HP_TcpServer_SetSocketListenQueue.Call(t.hServer, SocketListenQueue)
  67. return t
  68. }
  69. // SetAcceptSocketCount
  70. //
  71. // @Description: 设置 Accept 预投递数量(根据负载调整设置,Accept 预投递数量越大则支持的并发连接请求越多
  72. func (t *TcpServer) SetAcceptSocketCount(AcceptSocketCount int) *TcpServer {
  73. if !t.isValid() {
  74. return t
  75. }
  76. HP_TcpServer_SetAcceptSocketCount.Call(t.hServer, AcceptSocketCount)
  77. return t
  78. }
  79. // SetSocketBufferSize
  80. //
  81. // @Description: 设置通信数据缓冲区大小(根据平均通信数据包大小调整设置,通常设置为:(N * 1024) - sizeof(TBufferObj))
  82. func (t *TcpServer) SetSocketBufferSize(SocketBufferSize int) *TcpServer {
  83. if !t.isValid() {
  84. return t
  85. }
  86. HP_TcpServer_SetSocketBufferSize.Call(t.hServer, SocketBufferSize)
  87. return t
  88. }
  89. // SetKeepAliveTime
  90. //
  91. // @Description: 设置正常心跳包间隔(毫秒,0 则不发送心跳包,默认:60 * 1000)
  92. func (t *TcpServer) SetKeepAliveTime(KeepAliveTime int) *TcpServer {
  93. if !t.isValid() {
  94. return t
  95. }
  96. HP_TcpServer_SetKeepAliveTime.Call(t.hServer, KeepAliveTime)
  97. return t
  98. }
  99. // SetKeepAliveInterval
  100. //
  101. // @Description: 设置异常心跳包间隔(毫秒,0 不发送心跳包,,默认:20 * 1000,如果超过若干次 [默认:WinXP 5 次, Win7 10 次] 检测不到心跳确认包则认为已断线)
  102. func (t *TcpServer) SetKeepAliveInterval(KeepAliveInterval int) *TcpServer {
  103. if !t.isValid() {
  104. return t
  105. }
  106. HP_TcpServer_SetKeepAliveInterval.Call(t.hServer, KeepAliveInterval)
  107. return t
  108. }
  109. // SetNoDelay
  110. //
  111. // @Description: 设置是否开启 nodelay 模式(默认:FALSE,不开启)
  112. func (t *TcpServer) SetNoDelay(NoDelay bool) *TcpServer {
  113. if !t.isValid() {
  114. return t
  115. }
  116. HP_TcpServer_SetNoDelay.Call(t.hServer, NoDelay)
  117. return t
  118. }
  119. // GetSocketListenQueue
  120. //
  121. // @Description: 获取监听 Socket 的等候队列大小
  122. func (t *TcpServer) GetSocketListenQueue() int {
  123. if !t.isValid() {
  124. return 0
  125. }
  126. return HP_TcpServer_GetSocketListenQueue.CallInt(t.hServer)
  127. }
  128. // GetAcceptSocketCount
  129. //
  130. // @Description: 获取 Accept 预投递数量
  131. func (t *TcpServer) GetAcceptSocketCount() int {
  132. if !t.isValid() {
  133. return 0
  134. }
  135. return HP_TcpServer_GetAcceptSocketCount.CallInt(t.hServer)
  136. }
  137. // GetSocketBufferSize
  138. //
  139. // @Description: 获取通信数据缓冲区大小
  140. func (t *TcpServer) GetSocketBufferSize() int {
  141. if !t.isValid() {
  142. return 0
  143. }
  144. return HP_TcpServer_GetSocketBufferSize.CallInt(t.hServer)
  145. }
  146. // GetKeepAliveTime
  147. //
  148. // @Description: 获取正常心跳包间隔
  149. func (t *TcpServer) GetKeepAliveTime() int {
  150. if !t.isValid() {
  151. return 0
  152. }
  153. return HP_TcpServer_GetKeepAliveTime.CallInt(t.hServer)
  154. }
  155. // GetKeepAliveInterval
  156. //
  157. // @Description: 获取异常心跳包间隔
  158. func (t *TcpServer) GetKeepAliveInterval() int {
  159. if !t.isValid() {
  160. return 0
  161. }
  162. return HP_TcpServer_GetKeepAliveInterval.CallInt(t.hServer)
  163. }
  164. // IsNoDelay
  165. //
  166. // @Description: 检查是否开启 nodelay 模式
  167. func (t *TcpServer) IsNoDelay() bool {
  168. if !t.isValid() {
  169. return false
  170. }
  171. return HP_TcpServer_IsNoDelay.CallBool(t.hServer)
  172. }