server_on_windows.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //go:build windows
  2. package yu_hpsocket
  3. import (
  4. "sync"
  5. "syscall"
  6. )
  7. func init() {
  8. server_callback_OnPrepareListen_ptr = syscall.NewCallback(server_callback_OnPrepareListen)
  9. server_callback_OnAccept_ptr = syscall.NewCallback(server_callback_OnAccept)
  10. server_callback_OnHandShake_ptr = syscall.NewCallback(server_callback_OnHandShake)
  11. server_callback_OnSend_ptr = syscall.NewCallback(server_callback_OnSend)
  12. server_callback_OnReceive_ptr = syscall.NewCallback(server_callback_OnReceive)
  13. server_callback_OnClose_ptr = syscall.NewCallback(server_callback_OnClose)
  14. server_callback_OnShutdown_ptr = syscall.NewCallback(server_callback_OnShutdown)
  15. }
  16. var (
  17. server_callback_OnPrepareListen_ptr uintptr
  18. server_callback_OnAccept_ptr uintptr
  19. server_callback_OnHandShake_ptr uintptr
  20. server_callback_OnSend_ptr uintptr
  21. server_callback_OnReceive_ptr uintptr
  22. server_callback_OnClose_ptr uintptr
  23. server_callback_OnShutdown_ptr uintptr
  24. )
  25. func server_callback_OnPrepareListen(Sender, Listen uintptr) HR {
  26. switch j_item := _server_on_get(Sender); j_item._type {
  27. case _server_type_TcpServer:
  28. t := j_item.t.(*TcpServer)
  29. return t.onPrepareListen(t, Sender, Listen)
  30. case _server_type_TcpPackServer:
  31. t := j_item.t.(*TcpPackServer)
  32. return t.onPrepareListen(t, Sender, Listen)
  33. case _server_type_HttpServer:
  34. t := j_item.t.(*HttpServer)
  35. return t.onPrepareListen(t, Sender, Listen)
  36. }
  37. return HR_OK
  38. }
  39. func server_callback_OnAccept(Sender, ConnID, Client uintptr) HR {
  40. switch j_item := _server_on_get(Sender); j_item._type {
  41. case _server_type_TcpServer:
  42. t := j_item.t.(*TcpServer)
  43. return t.onAccept(t, Sender, ConnID, Client)
  44. case _server_type_TcpPackServer:
  45. t := j_item.t.(*TcpPackServer)
  46. return t.onAccept(t, Sender, ConnID, Client)
  47. case _server_type_HttpServer:
  48. t := j_item.t.(*HttpServer)
  49. return t.onAccept(t, Sender, ConnID, Client)
  50. }
  51. return HR_OK
  52. }
  53. func server_callback_OnHandShake(Sender, ConnID uintptr) HR {
  54. switch j_item := _server_on_get(Sender); j_item._type {
  55. case _server_type_TcpServer:
  56. t := j_item.t.(*TcpServer)
  57. return t.onHandShake(t, Sender, ConnID)
  58. case _server_type_TcpPackServer:
  59. t := j_item.t.(*TcpPackServer)
  60. return t.onHandShake(t, Sender, ConnID)
  61. case _server_type_HttpServer:
  62. t := j_item.t.(*HttpServer)
  63. return t.onHandShake(t, Sender, ConnID)
  64. }
  65. return HR_OK
  66. }
  67. func server_callback_OnSend(Sender, ConnID, Data uintptr, Length uintptr) HR {
  68. switch j_item := _server_on_get(Sender); j_item._type {
  69. case _server_type_TcpServer:
  70. t := j_item.t.(*TcpServer)
  71. return t.onSend(t, Sender, ConnID, Data, Length)
  72. case _server_type_TcpPackServer:
  73. t := j_item.t.(*TcpPackServer)
  74. return t.onSend(t, Sender, ConnID, Data, Length)
  75. case _server_type_HttpServer:
  76. t := j_item.t.(*HttpServer)
  77. return t.onSend(t, Sender, ConnID, Data, Length)
  78. }
  79. return HR_OK
  80. }
  81. func server_callback_OnReceive(Sender, ConnID, Data uintptr, Length uintptr) HR {
  82. switch j_item := _server_on_get(Sender); j_item._type {
  83. case _server_type_TcpServer:
  84. t := j_item.t.(*TcpServer)
  85. return t.onReceive(t, Sender, ConnID, Data, Length)
  86. case _server_type_TcpPackServer:
  87. t := j_item.t.(*TcpPackServer)
  88. return t.onReceive(t, Sender, ConnID, Data, Length)
  89. case _server_type_HttpServer:
  90. t := j_item.t.(*HttpServer)
  91. return t.onReceive(t, Sender, ConnID, Data, Length)
  92. }
  93. return HR_OK
  94. }
  95. func server_callback_OnClose(Sender, ConnID uintptr, Operation SO, ErrorCode int) HR {
  96. switch j_item := _server_on_get(Sender); j_item._type {
  97. case _server_type_TcpServer:
  98. t := j_item.t.(*TcpServer)
  99. return t.onClose(t, Sender, ConnID, Operation, ErrorCode)
  100. case _server_type_TcpPackServer:
  101. t := j_item.t.(*TcpPackServer)
  102. return t.onClose(t, Sender, ConnID, Operation, ErrorCode)
  103. case _server_type_HttpServer:
  104. t := j_item.t.(*HttpServer)
  105. return t.onClose(t, Sender, ConnID, Operation, ErrorCode)
  106. }
  107. return HR_OK
  108. }
  109. func server_callback_OnShutdown(Sender uintptr) HR {
  110. switch j_item := _server_on_get(Sender); j_item._type {
  111. case _server_type_TcpServer:
  112. t := j_item.t.(*TcpServer)
  113. return t.onShutdown(t, Sender)
  114. case _server_type_TcpPackServer:
  115. t := j_item.t.(*TcpPackServer)
  116. return t.onShutdown(t, Sender)
  117. case _server_type_HttpServer:
  118. t := j_item.t.(*HttpServer)
  119. return t.onShutdown(t, Sender)
  120. }
  121. return HR_OK
  122. }
  123. // ---------------------------------------------------------------------------------------------------------------------
  124. var _server_on_lock sync.Mutex
  125. var _server_on = make(map[uintptr]any)
  126. const (
  127. _server_type_Server = iota
  128. _server_type_TcpServer
  129. _server_type_TcpPackServer
  130. _server_type_HttpServer
  131. )
  132. type _server_item struct {
  133. _type byte
  134. t any
  135. }
  136. func _server_on_set(Sender uintptr, v *_server_item) {
  137. _server_on_lock.Lock()
  138. defer _server_on_lock.Unlock()
  139. _server_on[Sender] = v
  140. }
  141. func _server_on_get(Sender uintptr) *_server_item {
  142. _server_on_lock.Lock()
  143. defer _server_on_lock.Unlock()
  144. if j_v, j_is := _server_on[Sender]; j_is {
  145. return j_v.(*_server_item)
  146. }
  147. return nil
  148. }
  149. func _server_on_del(Sender uintptr) {
  150. _server_on_lock.Lock()
  151. defer _server_on_lock.Unlock()
  152. delete(_server_on, Sender)
  153. }