libnet.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. --[[
  2. @module libnet
  3. @summary libnet 在socket库基础上的同步阻塞api,socket库本身是异步非阻塞api
  4. @version 1.0
  5. @date 2023.03.16
  6. @author lisiqi
  7. ]]
  8. local libnet = {}
  9. --[[
  10. 阻塞等待网卡的网络连接上,只能用于sysplus.taskInitEx创建的任务函数中
  11. @api libnet.waitLink(taskName,timeout,...)
  12. @string 任务标志
  13. @int 超时时间,如果==0或者空,则没有超时一致等待
  14. @... 其他参数和socket.linkup一致
  15. @return boolean 失败或者超时返回false 成功返回true
  16. ]]
  17. function libnet.waitLink(taskName, timeout, ...)
  18. local succ, result = socket.linkup(...)
  19. if not succ then
  20. return false
  21. end
  22. if not result then
  23. result = sysplus.waitMsg(taskName, socket.LINK, timeout)
  24. else
  25. return true
  26. end
  27. if type(result) == 'table' and result[2] == 0 then
  28. return true
  29. else
  30. return false
  31. end
  32. end
  33. --[[
  34. 阻塞等待IP或者域名连接上,如果加密连接还要等握手完成,只能用于sysplus.taskInitEx创建的任务函数中
  35. @api libnet.connect(taskName,timeout,...)
  36. @string 任务标志
  37. @int 超时时间,如果==0或者空,则没有超时一致等待
  38. @... 其他参数和socket.connect一致
  39. @return boolean 失败或者超时返回false 成功返回true
  40. ]]
  41. function libnet.connect(taskName,timeout, ... )
  42. local succ, result = socket.connect(...)
  43. if not succ then
  44. return false
  45. end
  46. if not result then
  47. result = sysplus.waitMsg(taskName, socket.ON_LINE, timeout)
  48. else
  49. return true
  50. end
  51. if type(result) == 'table' and result[2] == 0 then
  52. return true
  53. else
  54. return false
  55. end
  56. end
  57. --[[
  58. 阻塞等待客户端连接上,只能用于sysplus.taskInitEx创建的任务函数中
  59. @api libnet.listen(taskName,timeout,...)
  60. @string 任务标志
  61. @int 超时时间,如果==0或者空,则没有超时一致等待
  62. @... 其他参数和socket.listen一致
  63. @return boolean 失败或者超时返回false 成功返回true
  64. ]]
  65. function libnet.listen(taskName,timeout, ... )
  66. local succ, result = socket.listen(...)
  67. if not succ then
  68. return false
  69. end
  70. if not result then
  71. result = sysplus.waitMsg(taskName, socket.ON_LINE, timeout)
  72. else
  73. return true
  74. end
  75. if type(result) == 'table' and result[2] == 0 then
  76. return true
  77. else
  78. return false
  79. end
  80. end
  81. --[[
  82. 阻塞等待数据发送完成,只能用于sysplus.taskInitEx创建的任务函数中
  83. @api libnet.tx(taskName,timeout,...)
  84. @string 任务标志
  85. @int 超时时间,如果==0或者空,则没有超时一直等待
  86. @... 其他参数和socket.tx一致
  87. @return boolean 失败或者超时返回false,缓冲区满了或者成功返回true
  88. @return boolean 缓存区是否满了
  89. ]]
  90. function libnet.tx(taskName,timeout, ...)
  91. local succ, is_full, result = socket.tx(...)
  92. if not succ then
  93. return false, is_full
  94. end
  95. if is_full then
  96. return true, true
  97. end
  98. if not result then
  99. result = sysplus.waitMsg(taskName, socket.TX_OK, timeout)
  100. else
  101. return true, is_full
  102. end
  103. if type(result) == 'table' and result[2] == 0 then
  104. return true, false
  105. else
  106. return false, is_full
  107. end
  108. end
  109. --[[
  110. 阻塞等待新的网络事件,只能用于sysplus.taskInitEx创建的任务函数中,可以通过sysplus.sendMsg(taskName,socket.EVENT,0)或者sys_send(taskName,socket.EVENT,0)强制退出
  111. @api libnet.wait(taskName,timeout, netc)
  112. @string 任务标志
  113. @int 超时时间,如果==0或者空,则没有超时一致等待
  114. @userdata socket.create返回的netc
  115. @return boolean 网络异常返回false,其他返回true
  116. @return boolean 超时返回false,有新的网络事件到返回true
  117. ]]
  118. function libnet.wait(taskName,timeout, netc)
  119. local succ, result = socket.wait(netc)
  120. if not succ then
  121. return false,false
  122. end
  123. if not result then
  124. result = sysplus.waitMsg(taskName, socket.EVENT, timeout)
  125. else
  126. return true,true
  127. end
  128. if type(result) == 'table' then
  129. if result[2] == 0 then
  130. return true, true
  131. else
  132. return false, false
  133. end
  134. else
  135. return true, false
  136. end
  137. end
  138. --[[
  139. 阻塞等待网络断开连接,只能用于sysplus.taskInitEx创建的任务函数中
  140. @api libnet.close(taskName,timeout, netc)
  141. @string 任务标志
  142. @int 超时时间,如果==0或者空,则没有超时一致等待
  143. @userdata socket.create返回的netc
  144. ]]
  145. function libnet.close(taskName,timeout, netc)
  146. local succ, result = socket.discon(netc)
  147. if not succ then
  148. socket.close(netc)
  149. return
  150. end
  151. if not result then
  152. result = sysplus.waitMsg(taskName, socket.CLOSED, timeout)
  153. else
  154. socket.close(netc)
  155. return
  156. end
  157. socket.close(netc)
  158. end
  159. return libnet