uws-react-native 0.0.1-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (315) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +84 -0
  3. package/UwsReactNative.podspec +71 -0
  4. package/android/build.gradle.kts +138 -0
  5. package/android/src/main/AndroidManifest.xml +2 -0
  6. package/android/src/main/java/id/sufeni/oss/uwsreactnative/UwsReactNativePackage.kt +18 -0
  7. package/apple/OnLoad.mm +17 -0
  8. package/cpp/CMakeLists.txt +154 -0
  9. package/cpp/UwsReactNativeModule.cpp +191 -0
  10. package/cpp/UwsReactNativeModule.h +30 -0
  11. package/cpp/app/AppHost.h +37 -0
  12. package/cpp/app/AppRunner.h +88 -0
  13. package/cpp/app/CMakeLists.txt +39 -0
  14. package/cpp/app/HttpRequestObject.h +173 -0
  15. package/cpp/app/HttpResponseObject.h +623 -0
  16. package/cpp/app/RecognizedString.h +71 -0
  17. package/cpp/app/TemplatedAppObject.h +472 -0
  18. package/cpp/bridging/ArrayBuffer.h +150 -0
  19. package/cpp/bridging/CMakeLists.txt +34 -0
  20. package/cpp/jsi/Buffer.h +91 -0
  21. package/cpp/jsi/CMakeLists.txt +34 -0
  22. package/cpp/uSockets/bsd.c +767 -0
  23. package/cpp/uSockets/context.c +524 -0
  24. package/cpp/uSockets/crypto/openssl.c +858 -0
  25. package/cpp/uSockets/crypto/sni_tree.cpp +218 -0
  26. package/cpp/uSockets/eventing/asio.cpp +475 -0
  27. package/cpp/uSockets/eventing/epoll_kqueue.c +458 -0
  28. package/cpp/uSockets/eventing/gcd.c +278 -0
  29. package/cpp/uSockets/eventing/libuv.c +305 -0
  30. package/cpp/uSockets/internal/eventing/asio.h +45 -0
  31. package/cpp/uSockets/internal/eventing/epoll_kqueue.h +67 -0
  32. package/cpp/uSockets/internal/eventing/gcd.h +40 -0
  33. package/cpp/uSockets/internal/eventing/libuv.h +45 -0
  34. package/cpp/uSockets/internal/internal.h +224 -0
  35. package/cpp/uSockets/internal/loop_data.h +38 -0
  36. package/cpp/uSockets/internal/networking/bsd.h +109 -0
  37. package/cpp/uSockets/io_uring/internal.h +98 -0
  38. package/cpp/uSockets/io_uring/io_context.c +302 -0
  39. package/cpp/uSockets/io_uring/io_loop.c +422 -0
  40. package/cpp/uSockets/io_uring/io_socket.c +139 -0
  41. package/cpp/uSockets/libusockets.h +350 -0
  42. package/cpp/uSockets/loop.c +396 -0
  43. package/cpp/uSockets/quic.c +1071 -0
  44. package/cpp/uSockets/quic.h +68 -0
  45. package/cpp/uSockets/socket.c +231 -0
  46. package/cpp/uSockets/udp.c +151 -0
  47. package/cpp/uWebSockets/uWebSockets/App.h +676 -0
  48. package/cpp/uWebSockets/uWebSockets/AsyncSocket.h +376 -0
  49. package/cpp/uWebSockets/uWebSockets/AsyncSocketData.h +94 -0
  50. package/cpp/uWebSockets/uWebSockets/BloomFilter.h +83 -0
  51. package/cpp/uWebSockets/uWebSockets/CachingApp.h +115 -0
  52. package/cpp/uWebSockets/uWebSockets/ChunkedEncoding.h +237 -0
  53. package/cpp/uWebSockets/uWebSockets/ClientApp.h +36 -0
  54. package/cpp/uWebSockets/uWebSockets/Http3App.h +140 -0
  55. package/cpp/uWebSockets/uWebSockets/Http3Context.h +157 -0
  56. package/cpp/uWebSockets/uWebSockets/Http3ContextData.h +21 -0
  57. package/cpp/uWebSockets/uWebSockets/Http3Request.h +22 -0
  58. package/cpp/uWebSockets/uWebSockets/Http3Response.h +120 -0
  59. package/cpp/uWebSockets/uWebSockets/Http3ResponseData.h +25 -0
  60. package/cpp/uWebSockets/uWebSockets/HttpContext.h +521 -0
  61. package/cpp/uWebSockets/uWebSockets/HttpContextData.h +60 -0
  62. package/cpp/uWebSockets/uWebSockets/HttpErrors.h +53 -0
  63. package/cpp/uWebSockets/uWebSockets/HttpParser.h +747 -0
  64. package/cpp/uWebSockets/uWebSockets/HttpResponse.h +605 -0
  65. package/cpp/uWebSockets/uWebSockets/HttpResponseData.h +123 -0
  66. package/cpp/uWebSockets/uWebSockets/HttpRouter.h +384 -0
  67. package/cpp/uWebSockets/uWebSockets/LocalCluster.h +64 -0
  68. package/cpp/uWebSockets/uWebSockets/Loop.h +238 -0
  69. package/cpp/uWebSockets/uWebSockets/LoopData.h +113 -0
  70. package/cpp/uWebSockets/uWebSockets/MessageParser.h +64 -0
  71. package/cpp/uWebSockets/uWebSockets/MoveOnlyFunction.h +389 -0
  72. package/cpp/uWebSockets/uWebSockets/Multipart.h +231 -0
  73. package/cpp/uWebSockets/uWebSockets/PerMessageDeflate.h +303 -0
  74. package/cpp/uWebSockets/uWebSockets/ProxyParser.h +187 -0
  75. package/cpp/uWebSockets/uWebSockets/QueryParser.h +120 -0
  76. package/cpp/uWebSockets/uWebSockets/TopicTree.h +364 -0
  77. package/cpp/uWebSockets/uWebSockets/Utilities.h +66 -0
  78. package/cpp/uWebSockets/uWebSockets/WebSocket.h +407 -0
  79. package/cpp/uWebSockets/uWebSockets/WebSocketContext.h +436 -0
  80. package/cpp/uWebSockets/uWebSockets/WebSocketContextData.h +109 -0
  81. package/cpp/uWebSockets/uWebSockets/WebSocketData.h +86 -0
  82. package/cpp/uWebSockets/uWebSockets/WebSocketExtensions.h +256 -0
  83. package/cpp/uWebSockets/uWebSockets/WebSocketHandshake.h +145 -0
  84. package/cpp/uWebSockets/uWebSockets/WebSocketProtocol.h +525 -0
  85. package/lib/commonjs/_internal/native-modules/NativeReactNativeUws.js +12 -0
  86. package/lib/commonjs/_internal/native-modules/NativeReactNativeUws.js.map +1 -0
  87. package/lib/commonjs/index.js +28 -0
  88. package/lib/commonjs/index.js.map +1 -0
  89. package/lib/commonjs/modules/App.js +17 -0
  90. package/lib/commonjs/modules/App.js.map +1 -0
  91. package/lib/commonjs/modules/CompressOptions.js +103 -0
  92. package/lib/commonjs/modules/CompressOptions.js.map +1 -0
  93. package/lib/commonjs/modules/EnvironmentVariables.js +17 -0
  94. package/lib/commonjs/modules/EnvironmentVariables.js.map +1 -0
  95. package/lib/commonjs/modules/ListenOptions.js +12 -0
  96. package/lib/commonjs/modules/ListenOptions.js.map +1 -0
  97. package/lib/commonjs/modules/SSLApp.js +15 -0
  98. package/lib/commonjs/modules/SSLApp.js.map +1 -0
  99. package/lib/commonjs/modules/getParts.js +28 -0
  100. package/lib/commonjs/modules/getParts.js.map +1 -0
  101. package/lib/commonjs/modules/index.js +61 -0
  102. package/lib/commonjs/modules/index.js.map +1 -0
  103. package/lib/commonjs/modules/us_listen_socket_close.js +21 -0
  104. package/lib/commonjs/modules/us_listen_socket_close.js.map +1 -0
  105. package/lib/commonjs/modules/us_socket_local_port.js +22 -0
  106. package/lib/commonjs/modules/us_socket_local_port.js.map +1 -0
  107. package/lib/commonjs/package.json +1 -0
  108. package/lib/commonjs/types/AppDescriptor.js +2 -0
  109. package/lib/commonjs/types/AppDescriptor.js.map +1 -0
  110. package/lib/commonjs/types/AppOptions.js +2 -0
  111. package/lib/commonjs/types/AppOptions.js.map +1 -0
  112. package/lib/commonjs/types/HttpRequest.js +6 -0
  113. package/lib/commonjs/types/HttpRequest.js.map +1 -0
  114. package/lib/commonjs/types/HttpResponse.js +6 -0
  115. package/lib/commonjs/types/HttpResponse.js.map +1 -0
  116. package/lib/commonjs/types/HttpRouterOptions.js +2 -0
  117. package/lib/commonjs/types/HttpRouterOptions.js.map +1 -0
  118. package/lib/commonjs/types/MultipartField.js +2 -0
  119. package/lib/commonjs/types/MultipartField.js.map +1 -0
  120. package/lib/commonjs/types/RecognizedString.js +2 -0
  121. package/lib/commonjs/types/RecognizedString.js.map +1 -0
  122. package/lib/commonjs/types/TemplatedApp.js +6 -0
  123. package/lib/commonjs/types/TemplatedApp.js.map +1 -0
  124. package/lib/commonjs/types/WebSocket.js +6 -0
  125. package/lib/commonjs/types/WebSocket.js.map +1 -0
  126. package/lib/commonjs/types/WebSocketBehaviour.js +139 -0
  127. package/lib/commonjs/types/WebSocketBehaviour.js.map +1 -0
  128. package/lib/commonjs/types/index.js +2 -0
  129. package/lib/commonjs/types/index.js.map +1 -0
  130. package/lib/commonjs/types/us_listen_socket.js +2 -0
  131. package/lib/commonjs/types/us_listen_socket.js.map +1 -0
  132. package/lib/commonjs/types/us_socket.js +2 -0
  133. package/lib/commonjs/types/us_socket.js.map +1 -0
  134. package/lib/commonjs/types/us_socket_context_t.js +2 -0
  135. package/lib/commonjs/types/us_socket_context_t.js.map +1 -0
  136. package/lib/module/_internal/native-modules/NativeReactNativeUws.js +10 -0
  137. package/lib/module/_internal/native-modules/NativeReactNativeUws.js.map +1 -0
  138. package/lib/module/index.js +5 -0
  139. package/lib/module/index.js.map +1 -0
  140. package/lib/module/modules/App.js +12 -0
  141. package/lib/module/modules/App.js.map +1 -0
  142. package/lib/module/modules/CompressOptions.js +103 -0
  143. package/lib/module/modules/CompressOptions.js.map +1 -0
  144. package/lib/module/modules/EnvironmentVariables.js +13 -0
  145. package/lib/module/modules/EnvironmentVariables.js.map +1 -0
  146. package/lib/module/modules/ListenOptions.js +8 -0
  147. package/lib/module/modules/ListenOptions.js.map +1 -0
  148. package/lib/module/modules/SSLApp.js +15 -0
  149. package/lib/module/modules/SSLApp.js.map +1 -0
  150. package/lib/module/modules/getParts.js +23 -0
  151. package/lib/module/modules/getParts.js.map +1 -0
  152. package/lib/module/modules/index.js +11 -0
  153. package/lib/module/modules/index.js.map +1 -0
  154. package/lib/module/modules/us_listen_socket_close.js +17 -0
  155. package/lib/module/modules/us_listen_socket_close.js.map +1 -0
  156. package/lib/module/modules/us_socket_local_port.js +18 -0
  157. package/lib/module/modules/us_socket_local_port.js.map +1 -0
  158. package/lib/module/package.json +1 -0
  159. package/lib/module/types/AppDescriptor.js +2 -0
  160. package/lib/module/types/AppDescriptor.js.map +1 -0
  161. package/lib/module/types/AppOptions.js +2 -0
  162. package/lib/module/types/AppOptions.js.map +1 -0
  163. package/lib/module/types/HttpRequest.js +4 -0
  164. package/lib/module/types/HttpRequest.js.map +1 -0
  165. package/lib/module/types/HttpResponse.js +4 -0
  166. package/lib/module/types/HttpResponse.js.map +1 -0
  167. package/lib/module/types/HttpRouterOptions.js +2 -0
  168. package/lib/module/types/HttpRouterOptions.js.map +1 -0
  169. package/lib/module/types/MultipartField.js +2 -0
  170. package/lib/module/types/MultipartField.js.map +1 -0
  171. package/lib/module/types/RecognizedString.js +2 -0
  172. package/lib/module/types/RecognizedString.js.map +1 -0
  173. package/lib/module/types/TemplatedApp.js +4 -0
  174. package/lib/module/types/TemplatedApp.js.map +1 -0
  175. package/lib/module/types/WebSocket.js +4 -0
  176. package/lib/module/types/WebSocket.js.map +1 -0
  177. package/lib/module/types/WebSocketBehaviour.js +139 -0
  178. package/lib/module/types/WebSocketBehaviour.js.map +1 -0
  179. package/lib/module/types/index.js +2 -0
  180. package/lib/module/types/index.js.map +1 -0
  181. package/lib/module/types/us_listen_socket.js +2 -0
  182. package/lib/module/types/us_listen_socket.js.map +1 -0
  183. package/lib/module/types/us_socket.js +2 -0
  184. package/lib/module/types/us_socket.js.map +1 -0
  185. package/lib/module/types/us_socket_context_t.js +2 -0
  186. package/lib/module/types/us_socket_context_t.js.map +1 -0
  187. package/lib/typescript/commonjs/_internal/native-modules/NativeReactNativeUws.d.ts +22 -0
  188. package/lib/typescript/commonjs/_internal/native-modules/NativeReactNativeUws.d.ts.map +1 -0
  189. package/lib/typescript/commonjs/index.d.ts +3 -0
  190. package/lib/typescript/commonjs/index.d.ts.map +1 -0
  191. package/lib/typescript/commonjs/modules/App.d.ts +7 -0
  192. package/lib/typescript/commonjs/modules/App.d.ts.map +1 -0
  193. package/lib/typescript/commonjs/modules/CompressOptions.d.ts +1 -0
  194. package/lib/typescript/commonjs/modules/CompressOptions.d.ts.map +1 -0
  195. package/lib/typescript/commonjs/modules/EnvironmentVariables.d.ts +16 -0
  196. package/lib/typescript/commonjs/modules/EnvironmentVariables.d.ts.map +1 -0
  197. package/lib/typescript/commonjs/modules/ListenOptions.d.ts +5 -0
  198. package/lib/typescript/commonjs/modules/ListenOptions.d.ts.map +1 -0
  199. package/lib/typescript/commonjs/modules/SSLApp.d.ts +1 -0
  200. package/lib/typescript/commonjs/modules/SSLApp.d.ts.map +1 -0
  201. package/lib/typescript/commonjs/modules/getParts.d.ts +6 -0
  202. package/lib/typescript/commonjs/modules/getParts.d.ts.map +1 -0
  203. package/lib/typescript/commonjs/modules/index.d.ts +6 -0
  204. package/lib/typescript/commonjs/modules/index.d.ts.map +1 -0
  205. package/lib/typescript/commonjs/modules/us_listen_socket_close.d.ts +5 -0
  206. package/lib/typescript/commonjs/modules/us_listen_socket_close.d.ts.map +1 -0
  207. package/lib/typescript/commonjs/modules/us_socket_local_port.d.ts +5 -0
  208. package/lib/typescript/commonjs/modules/us_socket_local_port.d.ts.map +1 -0
  209. package/lib/typescript/commonjs/package.json +1 -0
  210. package/lib/typescript/commonjs/types/AppDescriptor.d.ts +8 -0
  211. package/lib/typescript/commonjs/types/AppDescriptor.d.ts.map +1 -0
  212. package/lib/typescript/commonjs/types/AppOptions.d.ts +17 -0
  213. package/lib/typescript/commonjs/types/AppOptions.d.ts.map +1 -0
  214. package/lib/typescript/commonjs/types/HttpRequest.d.ts +54 -0
  215. package/lib/typescript/commonjs/types/HttpRequest.d.ts.map +1 -0
  216. package/lib/typescript/commonjs/types/HttpResponse.d.ts +169 -0
  217. package/lib/typescript/commonjs/types/HttpResponse.d.ts.map +1 -0
  218. package/lib/typescript/commonjs/types/HttpRouterOptions.d.ts +24 -0
  219. package/lib/typescript/commonjs/types/HttpRouterOptions.d.ts.map +1 -0
  220. package/lib/typescript/commonjs/types/MultipartField.d.ts +7 -0
  221. package/lib/typescript/commonjs/types/MultipartField.d.ts.map +1 -0
  222. package/lib/typescript/commonjs/types/RecognizedString.d.ts +10 -0
  223. package/lib/typescript/commonjs/types/RecognizedString.d.ts.map +1 -0
  224. package/lib/typescript/commonjs/types/TemplatedApp.d.ts +108 -0
  225. package/lib/typescript/commonjs/types/TemplatedApp.d.ts.map +1 -0
  226. package/lib/typescript/commonjs/types/WebSocket.d.ts +96 -0
  227. package/lib/typescript/commonjs/types/WebSocket.d.ts.map +1 -0
  228. package/lib/typescript/commonjs/types/WebSocketBehaviour.d.ts +1 -0
  229. package/lib/typescript/commonjs/types/WebSocketBehaviour.d.ts.map +1 -0
  230. package/lib/typescript/commonjs/types/index.d.ts +6 -0
  231. package/lib/typescript/commonjs/types/index.d.ts.map +1 -0
  232. package/lib/typescript/commonjs/types/us_listen_socket.d.ts +7 -0
  233. package/lib/typescript/commonjs/types/us_listen_socket.d.ts.map +1 -0
  234. package/lib/typescript/commonjs/types/us_socket.d.ts +7 -0
  235. package/lib/typescript/commonjs/types/us_socket.d.ts.map +1 -0
  236. package/lib/typescript/commonjs/types/us_socket_context_t.d.ts +7 -0
  237. package/lib/typescript/commonjs/types/us_socket_context_t.d.ts.map +1 -0
  238. package/lib/typescript/module/_internal/native-modules/NativeReactNativeUws.d.ts +22 -0
  239. package/lib/typescript/module/_internal/native-modules/NativeReactNativeUws.d.ts.map +1 -0
  240. package/lib/typescript/module/index.d.ts +3 -0
  241. package/lib/typescript/module/index.d.ts.map +1 -0
  242. package/lib/typescript/module/modules/App.d.ts +7 -0
  243. package/lib/typescript/module/modules/App.d.ts.map +1 -0
  244. package/lib/typescript/module/modules/CompressOptions.d.ts +1 -0
  245. package/lib/typescript/module/modules/CompressOptions.d.ts.map +1 -0
  246. package/lib/typescript/module/modules/EnvironmentVariables.d.ts +16 -0
  247. package/lib/typescript/module/modules/EnvironmentVariables.d.ts.map +1 -0
  248. package/lib/typescript/module/modules/ListenOptions.d.ts +5 -0
  249. package/lib/typescript/module/modules/ListenOptions.d.ts.map +1 -0
  250. package/lib/typescript/module/modules/SSLApp.d.ts +1 -0
  251. package/lib/typescript/module/modules/SSLApp.d.ts.map +1 -0
  252. package/lib/typescript/module/modules/getParts.d.ts +6 -0
  253. package/lib/typescript/module/modules/getParts.d.ts.map +1 -0
  254. package/lib/typescript/module/modules/index.d.ts +6 -0
  255. package/lib/typescript/module/modules/index.d.ts.map +1 -0
  256. package/lib/typescript/module/modules/us_listen_socket_close.d.ts +5 -0
  257. package/lib/typescript/module/modules/us_listen_socket_close.d.ts.map +1 -0
  258. package/lib/typescript/module/modules/us_socket_local_port.d.ts +5 -0
  259. package/lib/typescript/module/modules/us_socket_local_port.d.ts.map +1 -0
  260. package/lib/typescript/module/package.json +1 -0
  261. package/lib/typescript/module/types/AppDescriptor.d.ts +8 -0
  262. package/lib/typescript/module/types/AppDescriptor.d.ts.map +1 -0
  263. package/lib/typescript/module/types/AppOptions.d.ts +17 -0
  264. package/lib/typescript/module/types/AppOptions.d.ts.map +1 -0
  265. package/lib/typescript/module/types/HttpRequest.d.ts +54 -0
  266. package/lib/typescript/module/types/HttpRequest.d.ts.map +1 -0
  267. package/lib/typescript/module/types/HttpResponse.d.ts +169 -0
  268. package/lib/typescript/module/types/HttpResponse.d.ts.map +1 -0
  269. package/lib/typescript/module/types/HttpRouterOptions.d.ts +24 -0
  270. package/lib/typescript/module/types/HttpRouterOptions.d.ts.map +1 -0
  271. package/lib/typescript/module/types/MultipartField.d.ts +7 -0
  272. package/lib/typescript/module/types/MultipartField.d.ts.map +1 -0
  273. package/lib/typescript/module/types/RecognizedString.d.ts +10 -0
  274. package/lib/typescript/module/types/RecognizedString.d.ts.map +1 -0
  275. package/lib/typescript/module/types/TemplatedApp.d.ts +108 -0
  276. package/lib/typescript/module/types/TemplatedApp.d.ts.map +1 -0
  277. package/lib/typescript/module/types/WebSocket.d.ts +96 -0
  278. package/lib/typescript/module/types/WebSocket.d.ts.map +1 -0
  279. package/lib/typescript/module/types/WebSocketBehaviour.d.ts +1 -0
  280. package/lib/typescript/module/types/WebSocketBehaviour.d.ts.map +1 -0
  281. package/lib/typescript/module/types/index.d.ts +6 -0
  282. package/lib/typescript/module/types/index.d.ts.map +1 -0
  283. package/lib/typescript/module/types/us_listen_socket.d.ts +7 -0
  284. package/lib/typescript/module/types/us_listen_socket.d.ts.map +1 -0
  285. package/lib/typescript/module/types/us_socket.d.ts +7 -0
  286. package/lib/typescript/module/types/us_socket.d.ts.map +1 -0
  287. package/lib/typescript/module/types/us_socket_context_t.d.ts +7 -0
  288. package/lib/typescript/module/types/us_socket_context_t.d.ts.map +1 -0
  289. package/lib/typescript/tsconfig.tsbuildinfo +1 -0
  290. package/package.json +62 -0
  291. package/src/_internal/native-modules/NativeUwsReactNative.ts +40 -0
  292. package/src/index.ts +2 -0
  293. package/src/modules/App.ts +14 -0
  294. package/src/modules/CompressOptions.ts +101 -0
  295. package/src/modules/EnvironmentVariables.ts +16 -0
  296. package/src/modules/ListenOptions.ts +4 -0
  297. package/src/modules/SSLApp.ts +13 -0
  298. package/src/modules/getParts.ts +29 -0
  299. package/src/modules/index.ts +8 -0
  300. package/src/modules/us_listen_socket_close.ts +15 -0
  301. package/src/modules/us_socket_local_port.ts +16 -0
  302. package/src/types/AppDescriptor.ts +7 -0
  303. package/src/types/AppOptions.ts +22 -0
  304. package/src/types/HttpRequest.ts +74 -0
  305. package/src/types/HttpResponse.ts +309 -0
  306. package/src/types/HttpRouterOptions.ts +24 -0
  307. package/src/types/MultipartField.ts +6 -0
  308. package/src/types/RecognizedString.ts +11 -0
  309. package/src/types/TemplatedApp.ts +285 -0
  310. package/src/types/WebSocket.ts +147 -0
  311. package/src/types/WebSocketBehaviour.ts +137 -0
  312. package/src/types/index.ts +12 -0
  313. package/src/types/us_listen_socket.ts +6 -0
  314. package/src/types/us_socket.ts +6 -0
  315. package/src/types/us_socket_context_t.ts +6 -0
@@ -0,0 +1,218 @@
1
+ /*
2
+ * Authored by Alex Hultman, 2018-2020.
3
+ * Intellectual property of third-party.
4
+
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ /* This Server Name Indication hostname tree is written in C++ but could be ported to C.
19
+ * Overall it looks like crap, but has no memory allocations in fast path and is O(log n). */
20
+
21
+ #ifndef SNI_TREE_H
22
+ #define SNI_TREE_H
23
+
24
+ #ifndef LIBUS_NO_SSL
25
+
26
+ #include <map>
27
+ #include <memory>
28
+ #include <string_view>
29
+ #include <cstring>
30
+ #include <cstdlib>
31
+ #include <algorithm>
32
+
33
+ /* We only handle a maximum of 10 labels per hostname */
34
+ #define MAX_LABELS 10
35
+
36
+ /* This cannot be shared */
37
+ thread_local void (*sni_free_cb)(void *);
38
+
39
+ struct sni_node {
40
+ /* Empty nodes must always hold null */
41
+ void *user = nullptr;
42
+ std::map<std::string_view, std::unique_ptr<sni_node>> children;
43
+
44
+ ~sni_node() {
45
+ for (auto &p : children) {
46
+ /* The data of our string_views are managed by malloc */
47
+ free((void *) p.first.data());
48
+
49
+ /* Call destructor passed to sni_free only if we hold data.
50
+ * This is important since sni_remove does not have sni_free_cb set */
51
+ if (p.second.get()->user) {
52
+ sni_free_cb(p.second.get()->user);
53
+ }
54
+ }
55
+ }
56
+ };
57
+
58
+ // this can only delete ONE single node, but may cull "empty nodes with null as data"
59
+ void *removeUser(struct sni_node *root, unsigned int label, std::string_view *labels, unsigned int numLabels) {
60
+
61
+ /* If we are in the bottom (past bottom by one), there is nothing to remove */
62
+ if (label == numLabels) {
63
+ void *user = root->user;
64
+ /* Mark us for culling on the way up */
65
+ root->user = nullptr;
66
+ return user;
67
+ }
68
+
69
+ /* Is this label a child of root? */
70
+ auto it = root->children.find(labels[label]);
71
+ if (it == root->children.end()) {
72
+ /* We cannot continue */
73
+ return nullptr;
74
+ }
75
+
76
+ void *removedUser = removeUser(it->second.get(), label + 1, labels, numLabels);
77
+
78
+ /* On the way back up, we may cull empty nodes with no children.
79
+ * This ends up being where we remove all nodes */
80
+ if (it->second.get()->children.empty() && it->second.get()->user == nullptr) {
81
+
82
+ /* The data of our string_views are managed by malloc */
83
+ free((void *) it->first.data());
84
+
85
+ /* This can only happen with user set to null, otherwise we use sni_free_cb which is unset by sni_remove */
86
+ root->children.erase(it);
87
+ }
88
+
89
+ return removedUser;
90
+ }
91
+
92
+ void *getUser(struct sni_node *root, unsigned int label, std::string_view *labels, unsigned int numLabels) {
93
+
94
+ /* Do we have labels to match? Otherwise, return where we stand */
95
+ if (label == numLabels) {
96
+ return root->user;
97
+ }
98
+
99
+ /* Try and match by our label */
100
+ auto it = root->children.find(labels[label]);
101
+ if (it != root->children.end()) {
102
+ void *user = getUser(it->second.get(), label + 1, labels, numLabels);
103
+ if (user) {
104
+ return user;
105
+ }
106
+ }
107
+
108
+ /* Try and match by wildcard */
109
+ it = root->children.find("*");
110
+ if (it == root->children.end()) {
111
+ /* Matching has failed for both label and wildcard */
112
+ return nullptr;
113
+ }
114
+
115
+ /* We matched by wildcard */
116
+ return getUser(it->second.get(), label + 1, labels, numLabels);
117
+ }
118
+
119
+ extern "C" {
120
+
121
+ void *sni_new() {
122
+ return new sni_node;
123
+ }
124
+
125
+ void sni_free(void *sni, void (*cb)(void *)) {
126
+ /* We want to run this callback for every remaining name */
127
+ sni_free_cb = cb;
128
+
129
+ delete (sni_node *) sni;
130
+ }
131
+
132
+ /* Returns non-null if this name already exists */
133
+ int sni_add(void *sni, const char *hostname, void *user) {
134
+ struct sni_node *root = (struct sni_node *) sni;
135
+
136
+ /* Traverse all labels in hostname */
137
+ for (std::string_view view(hostname, strlen(hostname)), label;
138
+ view.length(); view.remove_prefix(std::min(view.length(), label.length() + 1))) {
139
+ /* Label is the token separated by dot */
140
+ label = view.substr(0, view.find('.', 0));
141
+
142
+ auto it = root->children.find(label);
143
+ if (it == root->children.end()) {
144
+ /* Duplicate this label for our kept string_view of it */
145
+ void *labelString = malloc(label.length());
146
+ memcpy(labelString, label.data(), label.length());
147
+
148
+ it = root->children.emplace(std::string_view((char *) labelString, label.length()),
149
+ std::make_unique<sni_node>()).first;
150
+ }
151
+
152
+ root = it->second.get();
153
+ }
154
+
155
+ /* We must never add multiple contexts for the same name, as that would overwrite and leak */
156
+ if (root->user) {
157
+ return 1;
158
+ }
159
+
160
+ root->user = user;
161
+
162
+ return 0;
163
+ }
164
+
165
+ /* Removes the exact match. Wildcards are treated as the verbatim asterisk char, not as an actual wildcard */
166
+ void *sni_remove(void *sni, const char *hostname) {
167
+ struct sni_node *root = (struct sni_node *) sni;
168
+
169
+ /* I guess 10 labels is an okay limit */
170
+ std::string_view labels[10];
171
+ unsigned int numLabels = 0;
172
+
173
+ /* We traverse all labels first of all */
174
+ for (std::string_view view(hostname, strlen(hostname)), label;
175
+ view.length(); view.remove_prefix(std::min(view.length(), label.length() + 1))) {
176
+ /* Label is the token separated by dot */
177
+ label = view.substr(0, view.find('.', 0));
178
+
179
+ /* Anything longer than 10 labels is forbidden */
180
+ if (numLabels == 10) {
181
+ return nullptr;
182
+ }
183
+
184
+ labels[numLabels++] = label;
185
+ }
186
+
187
+ return removeUser(root, 0, labels, numLabels);
188
+ }
189
+
190
+ void *sni_find(void *sni, const char *hostname) {
191
+ struct sni_node *root = (struct sni_node *) sni;
192
+
193
+ /* I guess 10 labels is an okay limit */
194
+ std::string_view labels[10];
195
+ unsigned int numLabels = 0;
196
+
197
+ /* We traverse all labels first of all */
198
+ for (std::string_view view(hostname, strlen(hostname)), label;
199
+ view.length(); view.remove_prefix(std::min(view.length(), label.length() + 1))) {
200
+ /* Label is the token separated by dot */
201
+ label = view.substr(0, view.find('.', 0));
202
+
203
+ /* Anything longer than 10 labels is forbidden */
204
+ if (numLabels == 10) {
205
+ return nullptr;
206
+ }
207
+
208
+ labels[numLabels++] = label;
209
+ }
210
+
211
+ return getUser(root, 0, labels, numLabels);
212
+ }
213
+
214
+ }
215
+
216
+ #endif
217
+
218
+ #endif
@@ -0,0 +1,475 @@
1
+ /*
2
+ * Authored by Alex Hultman, 2018-2021.
3
+ * Intellectual property of third-party.
4
+
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ extern "C" {
19
+ #include "libusockets.h"
20
+ #include "internal/internal.h"
21
+ #include <stdlib.h>
22
+ }
23
+
24
+ #ifdef LIBUS_USE_ASIO
25
+
26
+ #include <boost/asio.hpp>
27
+ #include <iostream>
28
+ #include <mutex>
29
+ #include <memory>
30
+ #include <boost/version.hpp>
31
+
32
+ // New interfaces require boost 1.66.0
33
+ #if BOOST_VERSION < 106600
34
+ #define LIBUS_USE_OLD_ASIO
35
+ #define LIBUS_ASIO_DESCRIPTOR boost::asio::posix::stream_descriptor
36
+ #define LIBUS_ASIO_LOOP boost::asio::io_service
37
+ #else
38
+ #define LIBUS_ASIO_DESCRIPTOR boost::asio::posix::descriptor
39
+ #define LIBUS_ASIO_LOOP boost::asio::io_context
40
+ #endif
41
+
42
+ // setting polls to 1 disables fallthrough
43
+ int polls = 0; // temporary solution keeping track of outstanding work
44
+
45
+ // define a timer internally as something that inherits from callback_t
46
+ // us_timer_t is convertible to this one
47
+ struct boost_timer : us_internal_callback_t {
48
+ boost::asio::deadline_timer timer;
49
+ std::shared_ptr<boost_timer> isValid;
50
+
51
+ unsigned char nr = 0;
52
+
53
+ boost_timer(LIBUS_ASIO_LOOP *io) : timer(*io) {
54
+ isValid.reset(this, [](boost_timer *t) {});
55
+ }
56
+ };
57
+
58
+ struct boost_block_poll_t : LIBUS_ASIO_DESCRIPTOR {
59
+
60
+ boost_block_poll_t(LIBUS_ASIO_LOOP *io, us_poll_t *p) : LIBUS_ASIO_DESCRIPTOR(*io), p(p) {
61
+ isValid.reset(this, [](boost_block_poll_t *t) {});
62
+ }
63
+
64
+ std::shared_ptr<boost_block_poll_t> isValid;
65
+
66
+ unsigned char nr = 0;
67
+ struct us_poll_t *p;
68
+ };
69
+
70
+ extern "C" {
71
+
72
+ // poll
73
+ void us_poll_init(struct us_poll_t *p, LIBUS_SOCKET_DESCRIPTOR fd, int poll_type) {
74
+ struct boost_block_poll_t *boost_block = (struct boost_block_poll_t *) p->boost_block;
75
+ boost_block->assign(fd);
76
+ p->poll_type = poll_type;
77
+ p->events = 0;
78
+
79
+ p->fd = fd; //apparently we access fd after close
80
+ }
81
+
82
+ void us_poll_free(struct us_poll_t *p, struct us_loop_t *loop) {
83
+ struct boost_block_poll_t *boost_block = (struct boost_block_poll_t *) p->boost_block;
84
+
85
+ delete boost_block;
86
+ free(p);
87
+ }
88
+
89
+ void poll_for_error(struct boost_block_poll_t *boost_block) {
90
+ /* There is no such thing as polling for error in old asio */
91
+ #ifndef LIBUS_USE_OLD_ASIO
92
+ polls++;
93
+ boost_block->async_wait(boost::asio::posix::descriptor::wait_type::wait_error, [nr = boost_block->nr, weakBoostBlock = std::weak_ptr<boost_block_poll_t>(boost_block->isValid)](boost::system::error_code ec) {
94
+ polls--;
95
+
96
+ if (ec != boost::asio::error::operation_aborted) {
97
+
98
+ // post mortem check
99
+ struct boost_block_poll_t *boost_block;
100
+ if (auto observe = weakBoostBlock.lock()) {
101
+ boost_block = observe.get();
102
+ } else {
103
+ return;
104
+ }
105
+
106
+ // get boost_block from weakptr
107
+ if (nr != boost_block->nr) {
108
+ return;
109
+ }
110
+
111
+ poll_for_error(boost_block); // ska man verkligen polla for error igen
112
+ us_internal_dispatch_ready_poll(boost_block->p, 1, LIBUS_SOCKET_READABLE | LIBUS_SOCKET_WRITABLE);
113
+ }
114
+ });
115
+ #endif
116
+ }
117
+
118
+ void poll_for_read(struct boost_block_poll_t *boost_block);
119
+
120
+ inline void handle_read(const std::weak_ptr<boost_block_poll_t> &weakBoostBlock, unsigned char nr, boost::system::error_code ec) {
121
+ if (ec != boost::asio::error::operation_aborted) {
122
+
123
+ // post mortem check
124
+ struct boost_block_poll_t *boost_block;
125
+ if (auto observe = weakBoostBlock.lock()) {
126
+ boost_block = observe.get();
127
+ } else {
128
+ return;
129
+ }
130
+
131
+ // get boost_block from weakptr
132
+ if (nr != boost_block->nr) {
133
+ return;
134
+ }
135
+
136
+ poll_for_read(boost_block);
137
+ us_internal_dispatch_ready_poll(boost_block->p, ec ? -1 : 0, LIBUS_SOCKET_READABLE);
138
+ }
139
+ }
140
+
141
+ void poll_for_read(struct boost_block_poll_t *boost_block) {
142
+ polls++;
143
+ #ifndef LIBUS_USE_OLD_ASIO
144
+ boost_block->async_wait(boost::asio::posix::descriptor::wait_type::wait_read, [nr = boost_block->nr, weakBoostBlock = std::weak_ptr<boost_block_poll_t>(boost_block->isValid)](boost::system::error_code ec) {
145
+ polls--;
146
+ handle_read(weakBoostBlock, nr, ec);
147
+ });
148
+ #else
149
+ boost_block->async_read_some(boost::asio::null_buffers(), [nr = boost_block->nr, weakBoostBlock = std::weak_ptr<boost_block_poll_t>(boost_block->isValid)](boost::system::error_code ec, std::size_t) {
150
+ polls--;
151
+ handle_read(weakBoostBlock, nr, ec);
152
+ });
153
+ #endif
154
+ }
155
+
156
+ void poll_for_write(struct boost_block_poll_t *boost_block);
157
+
158
+ inline void handle_write(const std::weak_ptr<boost_block_poll_t> &weakBoostBlock, unsigned char nr, boost::system::error_code ec) {
159
+ if (ec != boost::asio::error::operation_aborted) {
160
+
161
+ // post mortem check
162
+ struct boost_block_poll_t *boost_block;
163
+ if (auto observe = weakBoostBlock.lock()) {
164
+ boost_block = observe.get();
165
+ } else {
166
+ return;
167
+ }
168
+
169
+ // get boost_block from weakptr
170
+ if (nr != boost_block->nr) {
171
+ return;
172
+ }
173
+ poll_for_write(boost_block);
174
+ us_internal_dispatch_ready_poll(boost_block->p, ec ? -1 : 0, LIBUS_SOCKET_WRITABLE);
175
+ }
176
+ }
177
+
178
+ void poll_for_write(struct boost_block_poll_t *boost_block) {
179
+ polls++;
180
+ #ifndef LIBUS_USE_OLD_ASIO
181
+ boost_block->async_wait(boost::asio::posix::descriptor::wait_type::wait_write, [nr = boost_block->nr, weakBoostBlock = std::weak_ptr<boost_block_poll_t>(boost_block->isValid)](boost::system::error_code ec) {
182
+ polls--;
183
+ handle_write(weakBoostBlock, nr, ec);
184
+ });
185
+ #else
186
+ boost_block->async_write_some(boost::asio::null_buffers(), [nr = boost_block->nr, weakBoostBlock = std::weak_ptr<boost_block_poll_t>(boost_block->isValid)](boost::system::error_code ec, std::size_t) {
187
+ polls--;
188
+ handle_write(weakBoostBlock, nr, ec);
189
+ });
190
+ #endif
191
+ }
192
+
193
+ void us_poll_start(struct us_poll_t *p, struct us_loop_t *loop, int events) {
194
+ struct boost_block_poll_t *boost_block = (struct boost_block_poll_t *) p->boost_block;
195
+
196
+ p->events = events;
197
+ poll_for_error(boost_block);
198
+
199
+ if (events & LIBUS_SOCKET_READABLE) {
200
+ poll_for_read(boost_block);
201
+ }
202
+
203
+ if (events & LIBUS_SOCKET_WRITABLE) {
204
+ poll_for_write(boost_block);
205
+ }
206
+ }
207
+
208
+ void us_poll_change(struct us_poll_t *p, struct us_loop_t *loop, int events) {
209
+ struct boost_block_poll_t *boost_block = (struct boost_block_poll_t *) p->boost_block;
210
+
211
+ boost_block->nr++;
212
+ boost_block->cancel();
213
+
214
+ us_poll_start(p, loop, events);
215
+ }
216
+
217
+ void us_poll_stop(struct us_poll_t *p, struct us_loop_t *loop) {
218
+ struct boost_block_poll_t *boost_block = (struct boost_block_poll_t *) p->boost_block;
219
+
220
+ boost_block->nr++;
221
+ boost_block->release();
222
+ }
223
+
224
+ int us_poll_events(struct us_poll_t *p) {
225
+ return p->events;
226
+ }
227
+
228
+ unsigned int us_internal_accept_poll_event(struct us_poll_t *p) {
229
+ return 0;
230
+ }
231
+
232
+ int us_internal_poll_type(struct us_poll_t *p) {
233
+ return p->poll_type;
234
+ }
235
+
236
+ void us_internal_poll_set_type(struct us_poll_t *p, int poll_type) {
237
+ p->poll_type = poll_type;
238
+ }
239
+
240
+ LIBUS_SOCKET_DESCRIPTOR us_poll_fd(struct us_poll_t *p) {
241
+ struct boost_block_poll_t *boost_block = (struct boost_block_poll_t *) p->boost_block;
242
+
243
+
244
+ return p->fd;
245
+
246
+ //return boost_block->native_handle();
247
+ }
248
+
249
+ // if we get an io_context ptr as hint, we use it
250
+ // otherwise we create a new one for only us
251
+ struct us_loop_t *us_create_loop(void *hint, void (*wakeup_cb)(struct us_loop_t *loop), void (*pre_cb)(struct us_loop_t *loop), void (*post_cb)(struct us_loop_t *loop), unsigned int ext_size) {
252
+ struct us_loop_t *loop = (struct us_loop_t *) malloc(sizeof(struct us_loop_t) + ext_size);
253
+
254
+ loop->io = hint ? hint : new LIBUS_ASIO_LOOP();
255
+ loop->is_default = hint != 0;
256
+
257
+ // here we create two unreffed handles - timer and async
258
+ us_internal_loop_data_init(loop, wakeup_cb, pre_cb, post_cb);
259
+
260
+ // if we do not own this loop, we need to integrate and set up timer
261
+ if (hint) {
262
+ us_loop_integrate(loop);
263
+ }
264
+
265
+ return loop;
266
+ }
267
+
268
+ // based on if this was default loop or not
269
+ void us_loop_free(struct us_loop_t *loop) {
270
+ us_internal_loop_data_free(loop);
271
+
272
+ if (!loop->is_default) {
273
+ delete (LIBUS_ASIO_LOOP *) loop->io;
274
+ }
275
+
276
+ free(loop);
277
+ }
278
+
279
+ // we need fallthrough to correspond to our polls
280
+ // therefore we exit when our polls are 0
281
+ // if third party asio server wants to keep the loop running
282
+ // they have to use a guard such as a us_timer_t
283
+ void us_loop_run(struct us_loop_t *loop) {
284
+ us_loop_integrate(loop);
285
+
286
+ // this way of running adds one extra epoll_wait per event loop iteration
287
+ // but does not add per-poll overhead. besides, asio is sprinkled with inefficiencies
288
+ // everywhere so it's negligible for what it solves (we must have pre, post callbacks)
289
+ while (polls) {
290
+ us_internal_loop_pre(loop);
291
+ size_t num = ((LIBUS_ASIO_LOOP *) loop->io)->run_one();
292
+ if (!num) {
293
+ break;
294
+ }
295
+
296
+ for (int i = 0; true; i++) {
297
+ num = ((LIBUS_ASIO_LOOP *) loop->io)->poll_one();
298
+ if (!num || i == 999) {
299
+ break;
300
+ }
301
+ }
302
+ us_internal_loop_post(loop);
303
+ }
304
+ }
305
+
306
+ struct us_poll_t *us_create_poll(struct us_loop_t *loop, int fallthrough, unsigned int ext_size) {
307
+ struct us_poll_t *p = (struct us_poll_t *) malloc(sizeof(struct us_poll_t) + ext_size);
308
+ p->boost_block = new boost_block_poll_t( (LIBUS_ASIO_LOOP *)loop->io, p);
309
+
310
+ return p;
311
+ }
312
+
313
+ /* If we update our block position we have to updarte the uv_poll data to point to us */
314
+ struct us_poll_t *us_poll_resize(struct us_poll_t *p, struct us_loop_t *loop, unsigned int ext_size) {
315
+ p = (struct us_poll_t *) realloc(p, sizeof(struct us_poll_t) + ext_size);
316
+
317
+ // captures must never capture p directly, only boost_block and derive p from there
318
+ ((struct boost_block_poll_t *) p->boost_block)->p = p;
319
+
320
+ return p;
321
+ }
322
+
323
+ // timer
324
+ struct us_timer_t *us_create_timer(struct us_loop_t *loop, int fallthrough, unsigned int ext_size) {
325
+ struct boost_timer *cb = (struct boost_timer *) malloc(sizeof(struct boost_timer) + ext_size);
326
+
327
+ // inplace construct the timer on this callback_t
328
+ new (cb) boost_timer((LIBUS_ASIO_LOOP *)loop->io);
329
+
330
+ cb->loop = loop;
331
+ cb->cb_expects_the_loop = 0;
332
+ cb->p.poll_type = POLL_TYPE_CALLBACK; // this is missing from libuv flow
333
+
334
+ if (fallthrough) {
335
+ //uv_unref((uv_handle_t *) uv_timer);
336
+ }
337
+
338
+ return (struct us_timer_t *) cb;
339
+ }
340
+
341
+ void *us_timer_ext(struct us_timer_t *timer) {
342
+ return ((struct boost_timer *) timer) + 1;
343
+ }
344
+
345
+ void us_timer_close(struct us_timer_t *t) {
346
+ ((boost_timer *) t)->timer.cancel();
347
+ ((boost_timer *) t)->~boost_timer();
348
+ free(t);
349
+ }
350
+
351
+ void poll_for_timeout(struct boost_timer *b_timer, int repeat_ms) {
352
+ b_timer->timer.async_wait([nr = b_timer->nr, repeat_ms, weakBoostBlock = std::weak_ptr<boost_timer>(b_timer->isValid)](const boost::system::error_code &ec) {
353
+ if (ec != boost::asio::error::operation_aborted) {
354
+
355
+ struct boost_timer *b_timer;
356
+ if (auto observe = weakBoostBlock.lock()) {
357
+ b_timer = observe.get();
358
+ } else {
359
+ return;
360
+ }
361
+
362
+ if (nr != b_timer->nr) {
363
+ return;
364
+ }
365
+
366
+ if (repeat_ms) {
367
+
368
+ if (!polls) {
369
+ // we do fallthrough if no other polling
370
+ // this is problematic if WE fallthrough
371
+ // but other parts do not
372
+ // that causes timeouts to stop working
373
+ // we should really ask the executor if there is work
374
+ // if there is, then continue ticking until there isn't
375
+ return;
376
+ }
377
+
378
+ b_timer->timer.expires_at(b_timer->timer.expires_at() + boost::posix_time::milliseconds(repeat_ms));
379
+ poll_for_timeout(b_timer, repeat_ms);
380
+ }
381
+ us_internal_dispatch_ready_poll((struct us_poll_t *)b_timer, 0, LIBUS_SOCKET_READABLE);
382
+ }
383
+ });
384
+ }
385
+
386
+ void us_timer_set(struct us_timer_t *t, void (*cb)(struct us_timer_t *t), int ms, int repeat_ms) {
387
+ struct boost_timer *b_timer = (struct boost_timer *) t;
388
+
389
+ if (!ms) {
390
+ b_timer->nr++;
391
+ b_timer->timer.cancel();
392
+ } else {
393
+ b_timer->cb = (void(*)(struct us_internal_callback_t *)) cb;
394
+
395
+ b_timer->timer.expires_from_now(boost::posix_time::milliseconds(ms));
396
+ poll_for_timeout(b_timer, repeat_ms);
397
+ }
398
+ }
399
+
400
+ struct us_loop_t *us_timer_loop(struct us_timer_t *t) {
401
+ struct us_internal_callback_t *internal_cb = (struct us_internal_callback_t *) t;
402
+
403
+ return internal_cb->loop;
404
+ }
405
+
406
+ // async (internal only) probably map to io_context::post
407
+ struct boost_async : us_internal_callback_t {
408
+ std::mutex m;
409
+ std::shared_ptr<boost_async> isValid;
410
+
411
+ boost_async() {
412
+ isValid.reset(this, [](boost_async *a) {});
413
+ }
414
+ };
415
+
416
+ struct us_internal_async *us_internal_create_async(struct us_loop_t *loop, int fallthrough, unsigned int ext_size) {
417
+ struct boost_async *cb = (struct boost_async *) malloc(sizeof(struct boost_async) + ext_size);
418
+
419
+ // inplace construct
420
+ new (cb) boost_async();
421
+
422
+ // these properties are accessed from another thread when wakeup
423
+ cb->m.lock();
424
+ cb->loop = loop; // the only lock needed
425
+ cb->cb_expects_the_loop = 0;
426
+ cb->p.poll_type = POLL_TYPE_CALLBACK; // this is missing from libuv flow
427
+ cb->m.unlock();
428
+
429
+ if (fallthrough) {
430
+ //uv_unref((uv_handle_t *) uv_timer);
431
+ }
432
+
433
+ return (struct us_internal_async *) cb;
434
+ }
435
+
436
+ void us_internal_async_close(struct us_internal_async *a) {
437
+ ((boost_async *) a)->~boost_async();
438
+ free(a);
439
+ }
440
+
441
+ void us_internal_async_set(struct us_internal_async *a, void (*cb)(struct us_internal_async *)) {
442
+ struct boost_async *internal_cb = (struct boost_async *) a;
443
+
444
+ internal_cb->cb = (void(*)(struct us_internal_callback_t *)) cb;
445
+ }
446
+
447
+ void us_internal_async_wakeup(struct us_internal_async *a) {
448
+ struct boost_async *cb = (struct boost_async *) a;
449
+
450
+ // this doesn't really guarantee loop.io being visible here
451
+ // really we should use the loops mutex, and have the loops constructor
452
+ // use its own mutex, then we are guaranteed to have visibility here
453
+ cb->m.lock();
454
+ LIBUS_ASIO_LOOP *io = (LIBUS_ASIO_LOOP *)cb->loop->io;
455
+ cb->m.unlock();
456
+
457
+ // should increase and decrease polls (again, loop mutex)
458
+ io->post([weakBoostBlock = std::weak_ptr<boost_async>(cb->isValid)]() {
459
+
460
+ // was the async deleted before we came here?
461
+ struct boost_async *cb;
462
+ if (auto observe = weakBoostBlock.lock()) {
463
+ cb = observe.get();
464
+ } else {
465
+ return;
466
+ }
467
+
468
+ us_internal_dispatch_ready_poll((struct us_poll_t *) cb, 0, 0);
469
+
470
+ });
471
+ }
472
+
473
+ }
474
+
475
+ #endif