client_on_windows.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //go:build windows
  2. package yu_hpsocket
  3. import (
  4. "sync"
  5. "syscall"
  6. )
  7. func init() {
  8. client_callback_OnPrepareConnect_ptr = syscall.NewCallback(client_callback_OnPrepareConnect)
  9. client_callback_OnConnect_ptr = syscall.NewCallback(client_callback_OnConnect)
  10. client_callback_OnHandShake_ptr = syscall.NewCallback(client_callback_OnHandShake)
  11. client_callback_OnSend_ptr = syscall.NewCallback(client_callback_OnSend)
  12. client_callback_OnReceive_ptr = syscall.NewCallback(client_callback_OnReceive)
  13. client_callback_OnClose_ptr = syscall.NewCallback(client_callback_OnClose)
  14. }
  15. var (
  16. client_callback_OnPrepareConnect_ptr uintptr
  17. client_callback_OnConnect_ptr uintptr
  18. client_callback_OnHandShake_ptr uintptr
  19. client_callback_OnSend_ptr uintptr
  20. client_callback_OnReceive_ptr uintptr
  21. client_callback_OnClose_ptr uintptr
  22. )
  23. func client_callback_OnPrepareConnect(Sender, ConnID, socket uintptr) HR {
  24. switch j_item := _client_on_get(Sender); j_item._type {
  25. case _client_type_TcpClient:
  26. t := j_item.t.(*TcpClient)
  27. t.socket = socket
  28. return t.onPrepareConnect(t, Sender, ConnID, socket)
  29. case _client_type_TcpPackClient:
  30. t := j_item.t.(*TcpPackClient)
  31. t.socket = socket
  32. return t.onPrepareConnect(t, Sender, ConnID, socket)
  33. }
  34. return HR_OK
  35. }
  36. func client_callback_OnConnect(Sender, ConnID uintptr) HR {
  37. switch j_item := _client_on_get(Sender); j_item._type {
  38. case _client_type_TcpClient:
  39. t := j_item.t.(*TcpClient)
  40. if t.proxy_info.IsValid() && !t.connect_proxy() {
  41. return HR_ERROR
  42. }
  43. return t.onConnect(t, Sender, ConnID)
  44. case _client_type_TcpPackClient:
  45. t := j_item.t.(*TcpPackClient)
  46. if t.proxy_info.IsValid() && !t.connect_proxy() {
  47. return HR_ERROR
  48. }
  49. return t.onConnect(t, Sender, ConnID)
  50. }
  51. return HR_OK
  52. }
  53. func client_callback_OnHandShake(Sender, ConnID uintptr) HR {
  54. switch j_item := _client_on_get(Sender); j_item._type {
  55. case _client_type_TcpClient:
  56. t := j_item.t.(*TcpClient)
  57. return t.onHandShake(t, Sender, ConnID)
  58. case _client_type_TcpPackClient:
  59. t := j_item.t.(*TcpPackClient)
  60. return t.onHandShake(t, Sender, ConnID)
  61. }
  62. return HR_OK
  63. }
  64. func client_callback_OnSend(Sender, ConnID, Data uintptr, Length uintptr) HR {
  65. switch j_item := _client_on_get(Sender); j_item._type {
  66. case _client_type_TcpClient:
  67. t := j_item.t.(*TcpClient)
  68. return t.onSend(t, Sender, ConnID, Data, Length)
  69. case _client_type_TcpPackClient:
  70. t := j_item.t.(*TcpPackClient)
  71. return t.onSend(t, Sender, ConnID, Data, Length)
  72. }
  73. return HR_OK
  74. }
  75. func client_callback_OnReceive(Sender, ConnID, Data uintptr, Length uintptr) HR {
  76. switch j_item := _client_on_get(Sender); j_item._type {
  77. case _client_type_TcpClient:
  78. t := j_item.t.(*TcpClient)
  79. return t.onReceive(t, Sender, ConnID, Data, Length)
  80. case _client_type_TcpPackClient:
  81. t := j_item.t.(*TcpPackClient)
  82. return t.onReceive(t, Sender, ConnID, Data, Length)
  83. }
  84. return HR_OK
  85. }
  86. func client_callback_OnClose(Sender, ConnID uintptr, Operation SO, ErrorCode int) HR {
  87. switch j_item := _client_on_get(Sender); j_item._type {
  88. case _client_type_TcpClient:
  89. t := j_item.t.(*TcpClient)
  90. return t.onClose(t, Sender, ConnID, Operation, ErrorCode)
  91. case _client_type_TcpPackClient:
  92. t := j_item.t.(*TcpPackClient)
  93. return t.onClose(t, Sender, ConnID, Operation, ErrorCode)
  94. }
  95. return HR_OK
  96. }
  97. // ---------------------------------------------------------------------------------------------------------------------
  98. var _client_on_lock sync.Mutex
  99. var _client_on = make(map[uintptr]any)
  100. const (
  101. _client_type_Client = iota
  102. _client_type_TcpClient
  103. _client_type_TcpPackClient
  104. )
  105. type _client_item struct {
  106. _type byte
  107. t any
  108. }
  109. func _client_on_set(Sender uintptr, v *_client_item) {
  110. _client_on_lock.Lock()
  111. defer _client_on_lock.Unlock()
  112. _client_on[Sender] = v
  113. }
  114. func _client_on_get(Sender uintptr) *_client_item {
  115. _client_on_lock.Lock()
  116. defer _client_on_lock.Unlock()
  117. if j_v, j_is := _client_on[Sender]; j_is {
  118. return j_v.(*_client_item)
  119. }
  120. return nil
  121. }
  122. func _client_on_del(Sender uintptr) {
  123. _client_on_lock.Lock()
  124. defer _client_on_lock.Unlock()
  125. delete(_client_on, Sender)
  126. }