libnet.lua 3.9 KB

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