client_tcp_windows.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //go:build windows
  2. package yu_hpsocket
  3. import yu_proxy "gogs.qqck.cn/s/tools/proxy"
  4. // TcpClient
  5. // @Description: TcpClientListener 对象,请自己创建和销毁,目的是保证安全
  6. type TcpClient struct {
  7. Client[TcpClient]
  8. }
  9. // NewTcpClient
  10. //
  11. // @Description: 创建 TcpClient 对象
  12. func NewTcpClient() (t *TcpClient) {
  13. t = &TcpClient{}
  14. t.proxy_info = yu_proxy.NewInfo()
  15. return
  16. }
  17. // Create
  18. //
  19. // @Description: 创建 TcpClient 环境
  20. func (t *TcpClient) Create(SSL bool) bool {
  21. t.Close()
  22. if t.hListener, _, _ = Create_HP_TcpClientListener.Call(); t.hListener == 0 {
  23. return false
  24. }
  25. if t.SSL = SSL; t.SSL {
  26. t.hClient, _, _ = Create_HP_SSLClient.Call(t.hListener)
  27. } else {
  28. t.hClient, _, _ = Create_HP_TcpClient.Call(t.hListener)
  29. }
  30. if t.hClient == 0 {
  31. Destroy_HP_TcpClientListener.Call(t.hListener)
  32. t.hListener = 0
  33. return false
  34. }
  35. _client_on_set(t.hClient, &_client_item{
  36. _type: _client_type_TcpClient,
  37. t: t,
  38. })
  39. return true
  40. }
  41. // Close
  42. //
  43. // @Description: 销毁 TcpClient 环境
  44. func (t *TcpClient) Close() {
  45. t.Stop()
  46. t.Wait(-1)
  47. if t.hClient != 0 {
  48. if t.SSL {
  49. Destroy_HP_SSLClient.Call(t.hClient)
  50. } else {
  51. Destroy_HP_TcpClient.Call(t.hClient)
  52. }
  53. _client_on_del(t.hClient)
  54. t.hClient = 0
  55. }
  56. if t.hListener != 0 {
  57. Destroy_HP_TcpClientListener.Call(t.hListener)
  58. t.hListener = 0
  59. }
  60. }
  61. // SetSocketBufferSize
  62. //
  63. // @Description: 设置通信数据缓冲区大小(根据平均通信数据包大小调整设置,通常设置为:(N * 1024) - sizeof(TBufferObj))
  64. func (t *TcpClient) SetSocketBufferSize(SocketBufferSize int) *TcpClient {
  65. if !t.isValid() {
  66. return t
  67. }
  68. HP_TcpClient_SetSocketBufferSize.Call(t.hClient, SocketBufferSize)
  69. return t
  70. }
  71. // SetKeepAliveTime
  72. //
  73. // @Description: 设置正常心跳包间隔(毫秒,0 则不发送心跳包,默认:60 * 1000)
  74. func (t *TcpClient) SetKeepAliveTime(KeepAliveTime int) *TcpClient {
  75. if !t.isValid() {
  76. return t
  77. }
  78. HP_TcpClient_SetKeepAliveTime.Call(t.hClient, KeepAliveTime)
  79. return t
  80. }
  81. // SetKeepAliveInterval
  82. //
  83. // @Description: 设置异常心跳包间隔(毫秒,0 不发送心跳包,,默认:20 * 1000,如果超过若干次 [默认:WinXP 5 次, Win7 10 次] 检测不到心跳确认包则认为已断线)
  84. func (t *TcpClient) SetKeepAliveInterval(KeepAliveInterval int) *TcpClient {
  85. if !t.isValid() {
  86. return t
  87. }
  88. HP_TcpClient_SetKeepAliveInterval.Call(t.hClient, KeepAliveInterval)
  89. return t
  90. }
  91. // SetNoDelay
  92. //
  93. // @Description: 设置是否开启 nodelay 模式(默认:false,不开启)
  94. func (t *TcpClient) SetNoDelay(NoDelay bool) *TcpClient {
  95. if !t.isValid() {
  96. return t
  97. }
  98. HP_TcpClient_SetNoDelay.Call(t.hClient, NoDelay)
  99. return t
  100. }
  101. // GetSocketBufferSize
  102. //
  103. // @Description: 获取通信数据缓冲区大小
  104. func (t *TcpClient) GetSocketBufferSize() int {
  105. if !t.isValid() {
  106. return 0
  107. }
  108. return HP_TcpClient_GetSocketBufferSize.CallInt(t.hClient)
  109. }
  110. // GetKeepAliveTime
  111. //
  112. // @Description: 获取正常心跳包间隔
  113. func (t *TcpClient) GetKeepAliveTime() int {
  114. if !t.isValid() {
  115. return 0
  116. }
  117. return HP_TcpClient_GetKeepAliveTime.CallInt(t.hClient)
  118. }
  119. // GetKeepAliveInterval
  120. //
  121. // @Description: 获取异常心跳包间隔
  122. func (t *TcpClient) GetKeepAliveInterval() int {
  123. if !t.isValid() {
  124. return 0
  125. }
  126. return HP_TcpClient_GetKeepAliveInterval.CallInt(t.hClient)
  127. }
  128. // IsNoDelay
  129. //
  130. // @Description: 检查是否开启 nodelay 模式
  131. func (t *TcpClient) IsNoDelay() bool {
  132. if !t.isValid() {
  133. return false
  134. }
  135. return HP_TcpClient_IsNoDelay.CallBool(t.hClient)
  136. }