client_tcp_pack_windows.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //go:build windows
  2. package yu_hpsocket
  3. import (
  4. yu_proxy "gogs.qqck.cn/s/tools/proxy"
  5. "time"
  6. )
  7. // TcpPackClient
  8. // @Description: TcpPackClientListener 对象,请自己创建和销毁,目的是保证安全
  9. type TcpPackClient struct {
  10. Client[TcpPackClient]
  11. TcpClient
  12. }
  13. // NewTcpPackClient
  14. //
  15. // @Description: 创建 TcpPackClient 对象
  16. func NewTcpPackClient() (t *TcpPackClient) {
  17. t = &TcpPackClient{}
  18. t.proxy_info = yu_proxy.NewInfo()
  19. return
  20. }
  21. // Create
  22. //
  23. // @Description: 创建 TcpPackClient 环境
  24. func (t *TcpPackClient) Create(SSL bool) bool {
  25. t.Close()
  26. if t.hListener, _, _ = Create_HP_TcpPackClientListener.Call(); t.hListener == 0 {
  27. return false
  28. }
  29. if t.SSL = SSL; t.SSL {
  30. t.hClient, _, _ = Create_HP_SSLPackClient.Call(t.hListener)
  31. } else {
  32. t.hClient, _, _ = Create_HP_TcpPackClient.Call(t.hListener)
  33. }
  34. if t.hClient == 0 {
  35. Destroy_HP_TcpPackClientListener.Call(t.hListener)
  36. t.hListener = 0
  37. return false
  38. }
  39. _client_on_set(t.hClient, &_client_item{
  40. _type: _client_type_TcpPackClient,
  41. t: t,
  42. })
  43. return true
  44. }
  45. // Close
  46. //
  47. // @Description: 销毁 TcpPackClient 环境
  48. func (t *TcpPackClient) Close() {
  49. for t.GetPendingDataLength() > 0 {
  50. time.Sleep(time.Microsecond)
  51. }
  52. t.Stop()
  53. t.Wait(-1)
  54. if t.hClient != 0 {
  55. if t.SSL {
  56. Destroy_HP_SSLPackClient.Call(t.hClient)
  57. } else {
  58. Destroy_HP_TcpPackClient.Call(t.hClient)
  59. }
  60. _client_on_del(t.hClient)
  61. t.hClient = 0
  62. }
  63. if t.hListener != 0 {
  64. Destroy_HP_TcpPackClientListener.Call(t.hListener)
  65. t.hListener = 0
  66. }
  67. }
  68. // SetMaxPackSize
  69. //
  70. // @Description: 设置数据包最大长度(有效数据包最大长度不能超过 4194303/0x3FFFFF 字节,默认:262144/0x40000)
  71. func (t *TcpPackClient) SetMaxPackSize(MaxPackSize int) *TcpPackClient {
  72. if !t.isValid() {
  73. return t
  74. }
  75. HP_TcpPackClient_SetMaxPackSize.Call(t.hClient, MaxPackSize)
  76. return t
  77. }
  78. // SetPackHeaderFlag
  79. //
  80. // @Description: 设置包头标识(有效包头标识取值范围 0 ~ 1023/0x3FF,当包头标识为 0 时不校验包头,默认:0)
  81. func (t *TcpPackClient) SetPackHeaderFlag(PackHeaderFlag int) *TcpPackClient {
  82. if !t.isValid() {
  83. return t
  84. }
  85. HP_TcpPackClient_SetPackHeaderFlag.Call(t.hClient, PackHeaderFlag)
  86. return t
  87. }
  88. // GetMaxPackSize
  89. //
  90. // @Description: 获取数据包最大长度
  91. func (t *TcpPackClient) GetMaxPackSize() int {
  92. if !t.isValid() {
  93. return 0
  94. }
  95. return HP_TcpPackClient_GetMaxPackSize.CallInt(t.hClient)
  96. }
  97. // GetPackHeaderFlag
  98. //
  99. // @Description: 获取包头标识
  100. func (t *TcpPackClient) GetPackHeaderFlag() int {
  101. if !t.isValid() {
  102. return 0
  103. }
  104. return HP_TcpPackClient_GetPackHeaderFlag.CallInt(t.hClient)
  105. }