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.
- package/LICENSE +201 -0
- package/README.md +84 -0
- package/UwsReactNative.podspec +71 -0
- package/android/build.gradle.kts +138 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/id/sufeni/oss/uwsreactnative/UwsReactNativePackage.kt +18 -0
- package/apple/OnLoad.mm +17 -0
- package/cpp/CMakeLists.txt +154 -0
- package/cpp/UwsReactNativeModule.cpp +191 -0
- package/cpp/UwsReactNativeModule.h +30 -0
- package/cpp/app/AppHost.h +37 -0
- package/cpp/app/AppRunner.h +88 -0
- package/cpp/app/CMakeLists.txt +39 -0
- package/cpp/app/HttpRequestObject.h +173 -0
- package/cpp/app/HttpResponseObject.h +623 -0
- package/cpp/app/RecognizedString.h +71 -0
- package/cpp/app/TemplatedAppObject.h +472 -0
- package/cpp/bridging/ArrayBuffer.h +150 -0
- package/cpp/bridging/CMakeLists.txt +34 -0
- package/cpp/jsi/Buffer.h +91 -0
- package/cpp/jsi/CMakeLists.txt +34 -0
- package/cpp/uSockets/bsd.c +767 -0
- package/cpp/uSockets/context.c +524 -0
- package/cpp/uSockets/crypto/openssl.c +858 -0
- package/cpp/uSockets/crypto/sni_tree.cpp +218 -0
- package/cpp/uSockets/eventing/asio.cpp +475 -0
- package/cpp/uSockets/eventing/epoll_kqueue.c +458 -0
- package/cpp/uSockets/eventing/gcd.c +278 -0
- package/cpp/uSockets/eventing/libuv.c +305 -0
- package/cpp/uSockets/internal/eventing/asio.h +45 -0
- package/cpp/uSockets/internal/eventing/epoll_kqueue.h +67 -0
- package/cpp/uSockets/internal/eventing/gcd.h +40 -0
- package/cpp/uSockets/internal/eventing/libuv.h +45 -0
- package/cpp/uSockets/internal/internal.h +224 -0
- package/cpp/uSockets/internal/loop_data.h +38 -0
- package/cpp/uSockets/internal/networking/bsd.h +109 -0
- package/cpp/uSockets/io_uring/internal.h +98 -0
- package/cpp/uSockets/io_uring/io_context.c +302 -0
- package/cpp/uSockets/io_uring/io_loop.c +422 -0
- package/cpp/uSockets/io_uring/io_socket.c +139 -0
- package/cpp/uSockets/libusockets.h +350 -0
- package/cpp/uSockets/loop.c +396 -0
- package/cpp/uSockets/quic.c +1071 -0
- package/cpp/uSockets/quic.h +68 -0
- package/cpp/uSockets/socket.c +231 -0
- package/cpp/uSockets/udp.c +151 -0
- package/cpp/uWebSockets/uWebSockets/App.h +676 -0
- package/cpp/uWebSockets/uWebSockets/AsyncSocket.h +376 -0
- package/cpp/uWebSockets/uWebSockets/AsyncSocketData.h +94 -0
- package/cpp/uWebSockets/uWebSockets/BloomFilter.h +83 -0
- package/cpp/uWebSockets/uWebSockets/CachingApp.h +115 -0
- package/cpp/uWebSockets/uWebSockets/ChunkedEncoding.h +237 -0
- package/cpp/uWebSockets/uWebSockets/ClientApp.h +36 -0
- package/cpp/uWebSockets/uWebSockets/Http3App.h +140 -0
- package/cpp/uWebSockets/uWebSockets/Http3Context.h +157 -0
- package/cpp/uWebSockets/uWebSockets/Http3ContextData.h +21 -0
- package/cpp/uWebSockets/uWebSockets/Http3Request.h +22 -0
- package/cpp/uWebSockets/uWebSockets/Http3Response.h +120 -0
- package/cpp/uWebSockets/uWebSockets/Http3ResponseData.h +25 -0
- package/cpp/uWebSockets/uWebSockets/HttpContext.h +521 -0
- package/cpp/uWebSockets/uWebSockets/HttpContextData.h +60 -0
- package/cpp/uWebSockets/uWebSockets/HttpErrors.h +53 -0
- package/cpp/uWebSockets/uWebSockets/HttpParser.h +747 -0
- package/cpp/uWebSockets/uWebSockets/HttpResponse.h +605 -0
- package/cpp/uWebSockets/uWebSockets/HttpResponseData.h +123 -0
- package/cpp/uWebSockets/uWebSockets/HttpRouter.h +384 -0
- package/cpp/uWebSockets/uWebSockets/LocalCluster.h +64 -0
- package/cpp/uWebSockets/uWebSockets/Loop.h +238 -0
- package/cpp/uWebSockets/uWebSockets/LoopData.h +113 -0
- package/cpp/uWebSockets/uWebSockets/MessageParser.h +64 -0
- package/cpp/uWebSockets/uWebSockets/MoveOnlyFunction.h +389 -0
- package/cpp/uWebSockets/uWebSockets/Multipart.h +231 -0
- package/cpp/uWebSockets/uWebSockets/PerMessageDeflate.h +303 -0
- package/cpp/uWebSockets/uWebSockets/ProxyParser.h +187 -0
- package/cpp/uWebSockets/uWebSockets/QueryParser.h +120 -0
- package/cpp/uWebSockets/uWebSockets/TopicTree.h +364 -0
- package/cpp/uWebSockets/uWebSockets/Utilities.h +66 -0
- package/cpp/uWebSockets/uWebSockets/WebSocket.h +407 -0
- package/cpp/uWebSockets/uWebSockets/WebSocketContext.h +436 -0
- package/cpp/uWebSockets/uWebSockets/WebSocketContextData.h +109 -0
- package/cpp/uWebSockets/uWebSockets/WebSocketData.h +86 -0
- package/cpp/uWebSockets/uWebSockets/WebSocketExtensions.h +256 -0
- package/cpp/uWebSockets/uWebSockets/WebSocketHandshake.h +145 -0
- package/cpp/uWebSockets/uWebSockets/WebSocketProtocol.h +525 -0
- package/lib/commonjs/_internal/native-modules/NativeReactNativeUws.js +12 -0
- package/lib/commonjs/_internal/native-modules/NativeReactNativeUws.js.map +1 -0
- package/lib/commonjs/index.js +28 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/modules/App.js +17 -0
- package/lib/commonjs/modules/App.js.map +1 -0
- package/lib/commonjs/modules/CompressOptions.js +103 -0
- package/lib/commonjs/modules/CompressOptions.js.map +1 -0
- package/lib/commonjs/modules/EnvironmentVariables.js +17 -0
- package/lib/commonjs/modules/EnvironmentVariables.js.map +1 -0
- package/lib/commonjs/modules/ListenOptions.js +12 -0
- package/lib/commonjs/modules/ListenOptions.js.map +1 -0
- package/lib/commonjs/modules/SSLApp.js +15 -0
- package/lib/commonjs/modules/SSLApp.js.map +1 -0
- package/lib/commonjs/modules/getParts.js +28 -0
- package/lib/commonjs/modules/getParts.js.map +1 -0
- package/lib/commonjs/modules/index.js +61 -0
- package/lib/commonjs/modules/index.js.map +1 -0
- package/lib/commonjs/modules/us_listen_socket_close.js +21 -0
- package/lib/commonjs/modules/us_listen_socket_close.js.map +1 -0
- package/lib/commonjs/modules/us_socket_local_port.js +22 -0
- package/lib/commonjs/modules/us_socket_local_port.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/types/AppDescriptor.js +2 -0
- package/lib/commonjs/types/AppDescriptor.js.map +1 -0
- package/lib/commonjs/types/AppOptions.js +2 -0
- package/lib/commonjs/types/AppOptions.js.map +1 -0
- package/lib/commonjs/types/HttpRequest.js +6 -0
- package/lib/commonjs/types/HttpRequest.js.map +1 -0
- package/lib/commonjs/types/HttpResponse.js +6 -0
- package/lib/commonjs/types/HttpResponse.js.map +1 -0
- package/lib/commonjs/types/HttpRouterOptions.js +2 -0
- package/lib/commonjs/types/HttpRouterOptions.js.map +1 -0
- package/lib/commonjs/types/MultipartField.js +2 -0
- package/lib/commonjs/types/MultipartField.js.map +1 -0
- package/lib/commonjs/types/RecognizedString.js +2 -0
- package/lib/commonjs/types/RecognizedString.js.map +1 -0
- package/lib/commonjs/types/TemplatedApp.js +6 -0
- package/lib/commonjs/types/TemplatedApp.js.map +1 -0
- package/lib/commonjs/types/WebSocket.js +6 -0
- package/lib/commonjs/types/WebSocket.js.map +1 -0
- package/lib/commonjs/types/WebSocketBehaviour.js +139 -0
- package/lib/commonjs/types/WebSocketBehaviour.js.map +1 -0
- package/lib/commonjs/types/index.js +2 -0
- package/lib/commonjs/types/index.js.map +1 -0
- package/lib/commonjs/types/us_listen_socket.js +2 -0
- package/lib/commonjs/types/us_listen_socket.js.map +1 -0
- package/lib/commonjs/types/us_socket.js +2 -0
- package/lib/commonjs/types/us_socket.js.map +1 -0
- package/lib/commonjs/types/us_socket_context_t.js +2 -0
- package/lib/commonjs/types/us_socket_context_t.js.map +1 -0
- package/lib/module/_internal/native-modules/NativeReactNativeUws.js +10 -0
- package/lib/module/_internal/native-modules/NativeReactNativeUws.js.map +1 -0
- package/lib/module/index.js +5 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/modules/App.js +12 -0
- package/lib/module/modules/App.js.map +1 -0
- package/lib/module/modules/CompressOptions.js +103 -0
- package/lib/module/modules/CompressOptions.js.map +1 -0
- package/lib/module/modules/EnvironmentVariables.js +13 -0
- package/lib/module/modules/EnvironmentVariables.js.map +1 -0
- package/lib/module/modules/ListenOptions.js +8 -0
- package/lib/module/modules/ListenOptions.js.map +1 -0
- package/lib/module/modules/SSLApp.js +15 -0
- package/lib/module/modules/SSLApp.js.map +1 -0
- package/lib/module/modules/getParts.js +23 -0
- package/lib/module/modules/getParts.js.map +1 -0
- package/lib/module/modules/index.js +11 -0
- package/lib/module/modules/index.js.map +1 -0
- package/lib/module/modules/us_listen_socket_close.js +17 -0
- package/lib/module/modules/us_listen_socket_close.js.map +1 -0
- package/lib/module/modules/us_socket_local_port.js +18 -0
- package/lib/module/modules/us_socket_local_port.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/types/AppDescriptor.js +2 -0
- package/lib/module/types/AppDescriptor.js.map +1 -0
- package/lib/module/types/AppOptions.js +2 -0
- package/lib/module/types/AppOptions.js.map +1 -0
- package/lib/module/types/HttpRequest.js +4 -0
- package/lib/module/types/HttpRequest.js.map +1 -0
- package/lib/module/types/HttpResponse.js +4 -0
- package/lib/module/types/HttpResponse.js.map +1 -0
- package/lib/module/types/HttpRouterOptions.js +2 -0
- package/lib/module/types/HttpRouterOptions.js.map +1 -0
- package/lib/module/types/MultipartField.js +2 -0
- package/lib/module/types/MultipartField.js.map +1 -0
- package/lib/module/types/RecognizedString.js +2 -0
- package/lib/module/types/RecognizedString.js.map +1 -0
- package/lib/module/types/TemplatedApp.js +4 -0
- package/lib/module/types/TemplatedApp.js.map +1 -0
- package/lib/module/types/WebSocket.js +4 -0
- package/lib/module/types/WebSocket.js.map +1 -0
- package/lib/module/types/WebSocketBehaviour.js +139 -0
- package/lib/module/types/WebSocketBehaviour.js.map +1 -0
- package/lib/module/types/index.js +2 -0
- package/lib/module/types/index.js.map +1 -0
- package/lib/module/types/us_listen_socket.js +2 -0
- package/lib/module/types/us_listen_socket.js.map +1 -0
- package/lib/module/types/us_socket.js +2 -0
- package/lib/module/types/us_socket.js.map +1 -0
- package/lib/module/types/us_socket_context_t.js +2 -0
- package/lib/module/types/us_socket_context_t.js.map +1 -0
- package/lib/typescript/commonjs/_internal/native-modules/NativeReactNativeUws.d.ts +22 -0
- package/lib/typescript/commonjs/_internal/native-modules/NativeReactNativeUws.d.ts.map +1 -0
- package/lib/typescript/commonjs/index.d.ts +3 -0
- package/lib/typescript/commonjs/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/App.d.ts +7 -0
- package/lib/typescript/commonjs/modules/App.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/CompressOptions.d.ts +1 -0
- package/lib/typescript/commonjs/modules/CompressOptions.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/EnvironmentVariables.d.ts +16 -0
- package/lib/typescript/commonjs/modules/EnvironmentVariables.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/ListenOptions.d.ts +5 -0
- package/lib/typescript/commonjs/modules/ListenOptions.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/SSLApp.d.ts +1 -0
- package/lib/typescript/commonjs/modules/SSLApp.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/getParts.d.ts +6 -0
- package/lib/typescript/commonjs/modules/getParts.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/index.d.ts +6 -0
- package/lib/typescript/commonjs/modules/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/us_listen_socket_close.d.ts +5 -0
- package/lib/typescript/commonjs/modules/us_listen_socket_close.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/us_socket_local_port.d.ts +5 -0
- package/lib/typescript/commonjs/modules/us_socket_local_port.d.ts.map +1 -0
- package/lib/typescript/commonjs/package.json +1 -0
- package/lib/typescript/commonjs/types/AppDescriptor.d.ts +8 -0
- package/lib/typescript/commonjs/types/AppDescriptor.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/AppOptions.d.ts +17 -0
- package/lib/typescript/commonjs/types/AppOptions.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/HttpRequest.d.ts +54 -0
- package/lib/typescript/commonjs/types/HttpRequest.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/HttpResponse.d.ts +169 -0
- package/lib/typescript/commonjs/types/HttpResponse.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/HttpRouterOptions.d.ts +24 -0
- package/lib/typescript/commonjs/types/HttpRouterOptions.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/MultipartField.d.ts +7 -0
- package/lib/typescript/commonjs/types/MultipartField.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/RecognizedString.d.ts +10 -0
- package/lib/typescript/commonjs/types/RecognizedString.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/TemplatedApp.d.ts +108 -0
- package/lib/typescript/commonjs/types/TemplatedApp.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/WebSocket.d.ts +96 -0
- package/lib/typescript/commonjs/types/WebSocket.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/WebSocketBehaviour.d.ts +1 -0
- package/lib/typescript/commonjs/types/WebSocketBehaviour.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/index.d.ts +6 -0
- package/lib/typescript/commonjs/types/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/us_listen_socket.d.ts +7 -0
- package/lib/typescript/commonjs/types/us_listen_socket.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/us_socket.d.ts +7 -0
- package/lib/typescript/commonjs/types/us_socket.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/us_socket_context_t.d.ts +7 -0
- package/lib/typescript/commonjs/types/us_socket_context_t.d.ts.map +1 -0
- package/lib/typescript/module/_internal/native-modules/NativeReactNativeUws.d.ts +22 -0
- package/lib/typescript/module/_internal/native-modules/NativeReactNativeUws.d.ts.map +1 -0
- package/lib/typescript/module/index.d.ts +3 -0
- package/lib/typescript/module/index.d.ts.map +1 -0
- package/lib/typescript/module/modules/App.d.ts +7 -0
- package/lib/typescript/module/modules/App.d.ts.map +1 -0
- package/lib/typescript/module/modules/CompressOptions.d.ts +1 -0
- package/lib/typescript/module/modules/CompressOptions.d.ts.map +1 -0
- package/lib/typescript/module/modules/EnvironmentVariables.d.ts +16 -0
- package/lib/typescript/module/modules/EnvironmentVariables.d.ts.map +1 -0
- package/lib/typescript/module/modules/ListenOptions.d.ts +5 -0
- package/lib/typescript/module/modules/ListenOptions.d.ts.map +1 -0
- package/lib/typescript/module/modules/SSLApp.d.ts +1 -0
- package/lib/typescript/module/modules/SSLApp.d.ts.map +1 -0
- package/lib/typescript/module/modules/getParts.d.ts +6 -0
- package/lib/typescript/module/modules/getParts.d.ts.map +1 -0
- package/lib/typescript/module/modules/index.d.ts +6 -0
- package/lib/typescript/module/modules/index.d.ts.map +1 -0
- package/lib/typescript/module/modules/us_listen_socket_close.d.ts +5 -0
- package/lib/typescript/module/modules/us_listen_socket_close.d.ts.map +1 -0
- package/lib/typescript/module/modules/us_socket_local_port.d.ts +5 -0
- package/lib/typescript/module/modules/us_socket_local_port.d.ts.map +1 -0
- package/lib/typescript/module/package.json +1 -0
- package/lib/typescript/module/types/AppDescriptor.d.ts +8 -0
- package/lib/typescript/module/types/AppDescriptor.d.ts.map +1 -0
- package/lib/typescript/module/types/AppOptions.d.ts +17 -0
- package/lib/typescript/module/types/AppOptions.d.ts.map +1 -0
- package/lib/typescript/module/types/HttpRequest.d.ts +54 -0
- package/lib/typescript/module/types/HttpRequest.d.ts.map +1 -0
- package/lib/typescript/module/types/HttpResponse.d.ts +169 -0
- package/lib/typescript/module/types/HttpResponse.d.ts.map +1 -0
- package/lib/typescript/module/types/HttpRouterOptions.d.ts +24 -0
- package/lib/typescript/module/types/HttpRouterOptions.d.ts.map +1 -0
- package/lib/typescript/module/types/MultipartField.d.ts +7 -0
- package/lib/typescript/module/types/MultipartField.d.ts.map +1 -0
- package/lib/typescript/module/types/RecognizedString.d.ts +10 -0
- package/lib/typescript/module/types/RecognizedString.d.ts.map +1 -0
- package/lib/typescript/module/types/TemplatedApp.d.ts +108 -0
- package/lib/typescript/module/types/TemplatedApp.d.ts.map +1 -0
- package/lib/typescript/module/types/WebSocket.d.ts +96 -0
- package/lib/typescript/module/types/WebSocket.d.ts.map +1 -0
- package/lib/typescript/module/types/WebSocketBehaviour.d.ts +1 -0
- package/lib/typescript/module/types/WebSocketBehaviour.d.ts.map +1 -0
- package/lib/typescript/module/types/index.d.ts +6 -0
- package/lib/typescript/module/types/index.d.ts.map +1 -0
- package/lib/typescript/module/types/us_listen_socket.d.ts +7 -0
- package/lib/typescript/module/types/us_listen_socket.d.ts.map +1 -0
- package/lib/typescript/module/types/us_socket.d.ts +7 -0
- package/lib/typescript/module/types/us_socket.d.ts.map +1 -0
- package/lib/typescript/module/types/us_socket_context_t.d.ts +7 -0
- package/lib/typescript/module/types/us_socket_context_t.d.ts.map +1 -0
- package/lib/typescript/tsconfig.tsbuildinfo +1 -0
- package/package.json +62 -0
- package/src/_internal/native-modules/NativeUwsReactNative.ts +40 -0
- package/src/index.ts +2 -0
- package/src/modules/App.ts +14 -0
- package/src/modules/CompressOptions.ts +101 -0
- package/src/modules/EnvironmentVariables.ts +16 -0
- package/src/modules/ListenOptions.ts +4 -0
- package/src/modules/SSLApp.ts +13 -0
- package/src/modules/getParts.ts +29 -0
- package/src/modules/index.ts +8 -0
- package/src/modules/us_listen_socket_close.ts +15 -0
- package/src/modules/us_socket_local_port.ts +16 -0
- package/src/types/AppDescriptor.ts +7 -0
- package/src/types/AppOptions.ts +22 -0
- package/src/types/HttpRequest.ts +74 -0
- package/src/types/HttpResponse.ts +309 -0
- package/src/types/HttpRouterOptions.ts +24 -0
- package/src/types/MultipartField.ts +6 -0
- package/src/types/RecognizedString.ts +11 -0
- package/src/types/TemplatedApp.ts +285 -0
- package/src/types/WebSocket.ts +147 -0
- package/src/types/WebSocketBehaviour.ts +137 -0
- package/src/types/index.ts +12 -0
- package/src/types/us_listen_socket.ts +6 -0
- package/src/types/us_socket.ts +6 -0
- package/src/types/us_socket_context_t.ts +6 -0
|
@@ -0,0 +1,278 @@
|
|
|
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
|
+
#include "libusockets.h"
|
|
19
|
+
#include "internal/internal.h"
|
|
20
|
+
#include <stdlib.h>
|
|
21
|
+
|
|
22
|
+
#ifdef LIBUS_USE_GCD
|
|
23
|
+
|
|
24
|
+
/* Loops */
|
|
25
|
+
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) {
|
|
26
|
+
struct us_loop_t *loop = (struct us_loop_t *) malloc(sizeof(struct us_loop_t) + ext_size);
|
|
27
|
+
|
|
28
|
+
// init the queue from hint
|
|
29
|
+
|
|
30
|
+
us_internal_loop_data_init(loop, wakeup_cb, pre_cb, post_cb);
|
|
31
|
+
|
|
32
|
+
// why not call integrate here? instead of in run
|
|
33
|
+
|
|
34
|
+
return loop;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
void us_loop_free(struct us_loop_t *loop) {
|
|
38
|
+
us_internal_loop_data_free(loop);
|
|
39
|
+
|
|
40
|
+
// free queue if different from main
|
|
41
|
+
|
|
42
|
+
free(loop);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/* We don't actually need to include CoreFoundation as we only need one single function,
|
|
46
|
+
* It will be up to the user to link to CoreFoundation, however that should be automatic in most use cases */
|
|
47
|
+
extern void CFRunLoopRun();
|
|
48
|
+
|
|
49
|
+
void us_loop_run(struct us_loop_t *loop) {
|
|
50
|
+
us_loop_integrate(loop);
|
|
51
|
+
|
|
52
|
+
/* We are absolutely not compatible with dispatch_main,
|
|
53
|
+
* However every real application should run with CoreFoundation,
|
|
54
|
+
* Foundation or Cocoa as the main loop, driving dispatch anyways */
|
|
55
|
+
CFRunLoopRun();
|
|
56
|
+
|
|
57
|
+
/* I guess "fallthrough" polls should be added to another run mode than the default one to fall through */
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
void gcd_read_handler(void *p) {
|
|
61
|
+
us_internal_dispatch_ready_poll((struct us_poll_t *) p, 0, LIBUS_SOCKET_READABLE);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
void gcd_write_handler(void *p) {
|
|
65
|
+
us_internal_dispatch_ready_poll((struct us_poll_t *) p, 0, LIBUS_SOCKET_WRITABLE);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/* Polls */
|
|
69
|
+
void us_poll_init(struct us_poll_t *p, LIBUS_SOCKET_DESCRIPTOR fd, int poll_type) {
|
|
70
|
+
p->poll_type = poll_type;
|
|
71
|
+
p->fd = fd;
|
|
72
|
+
|
|
73
|
+
/* I guess these are already activated? */
|
|
74
|
+
p->gcd_read = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, p->fd, 0, dispatch_get_main_queue());
|
|
75
|
+
dispatch_set_context(p->gcd_read, p);
|
|
76
|
+
dispatch_source_set_event_handler_f(p->gcd_read, gcd_read_handler);
|
|
77
|
+
dispatch_source_set_cancel_handler_f(p->gcd_read, gcd_read_handler);
|
|
78
|
+
|
|
79
|
+
p->gcd_write = dispatch_source_create(DISPATCH_SOURCE_TYPE_WRITE, p->fd, 0, dispatch_get_main_queue());
|
|
80
|
+
dispatch_set_context(p->gcd_write, p);
|
|
81
|
+
dispatch_source_set_event_handler_f(p->gcd_write, gcd_write_handler);
|
|
82
|
+
dispatch_source_set_cancel_handler_f(p->gcd_write, gcd_write_handler);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
void us_poll_free(struct us_poll_t *p, struct us_loop_t *loop) {
|
|
86
|
+
/* It is program error to release suspended filters */
|
|
87
|
+
us_poll_change(p, loop, LIBUS_SOCKET_READABLE | LIBUS_SOCKET_WRITABLE);
|
|
88
|
+
dispatch_release(p->gcd_read);
|
|
89
|
+
dispatch_release(p->gcd_write);
|
|
90
|
+
free(p);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
void us_poll_start(struct us_poll_t *p, struct us_loop_t *loop, int events) {
|
|
94
|
+
p->events = events;
|
|
95
|
+
|
|
96
|
+
if (events & LIBUS_SOCKET_READABLE) {
|
|
97
|
+
dispatch_resume(p->gcd_read);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (events & LIBUS_SOCKET_WRITABLE) {
|
|
101
|
+
dispatch_resume(p->gcd_write);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
void us_poll_change(struct us_poll_t *p, struct us_loop_t *loop, int events) {
|
|
106
|
+
int old_events = p->events;
|
|
107
|
+
|
|
108
|
+
if ((old_events & LIBUS_SOCKET_READABLE) != (events & LIBUS_SOCKET_READABLE)) {
|
|
109
|
+
if (old_events & LIBUS_SOCKET_READABLE) {
|
|
110
|
+
dispatch_suspend(p->gcd_read);
|
|
111
|
+
} else {
|
|
112
|
+
dispatch_resume(p->gcd_read);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if ((old_events & LIBUS_SOCKET_WRITABLE) != (events & LIBUS_SOCKET_WRITABLE)) {
|
|
117
|
+
if (old_events & LIBUS_SOCKET_WRITABLE) {
|
|
118
|
+
dispatch_suspend(p->gcd_write);
|
|
119
|
+
} else {
|
|
120
|
+
dispatch_resume(p->gcd_write);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
p->events = events;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
void us_poll_stop(struct us_poll_t *p, struct us_loop_t *loop) {
|
|
128
|
+
if (p->events & LIBUS_SOCKET_READABLE) {
|
|
129
|
+
dispatch_suspend(p->gcd_read);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (p->events & LIBUS_SOCKET_WRITABLE) {
|
|
133
|
+
dispatch_suspend(p->gcd_write);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
p->events = 0;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
int us_poll_events(struct us_poll_t *p) {
|
|
140
|
+
return p->events;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
void *us_poll_ext(struct us_poll_t *p) {
|
|
144
|
+
return p + 1;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
unsigned int us_internal_accept_poll_event(struct us_poll_t *p) {
|
|
148
|
+
//printf("us_internal_accept_poll_event\n");
|
|
149
|
+
return 0;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
int us_internal_poll_type(struct us_poll_t *p) {
|
|
153
|
+
return p->poll_type & 3;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
void us_internal_poll_set_type(struct us_poll_t *p, int poll_type) {
|
|
157
|
+
p->poll_type = poll_type;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
LIBUS_SOCKET_DESCRIPTOR us_poll_fd(struct us_poll_t *p) {
|
|
161
|
+
return p->fd;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
struct us_poll_t *us_create_poll(struct us_loop_t *loop, int fallthrough, unsigned int ext_size) {
|
|
165
|
+
struct us_poll_t *poll = (struct us_poll_t *) malloc(sizeof(struct us_poll_t) + ext_size);
|
|
166
|
+
|
|
167
|
+
return poll;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
struct us_poll_t *us_poll_resize(struct us_poll_t *p, struct us_loop_t *loop, unsigned int ext_size) {
|
|
171
|
+
int events = us_poll_events(p);
|
|
172
|
+
|
|
173
|
+
struct us_poll_t *new_p = realloc(p, sizeof(struct us_poll_t) + ext_size + 1024);
|
|
174
|
+
if (p != new_p) {
|
|
175
|
+
/* It is a program error to release suspended filters */
|
|
176
|
+
us_poll_change(new_p, loop, LIBUS_SOCKET_READABLE | LIBUS_SOCKET_WRITABLE);
|
|
177
|
+
dispatch_release(new_p->gcd_read);
|
|
178
|
+
dispatch_release(new_p->gcd_write);
|
|
179
|
+
|
|
180
|
+
/* Create and start new filters */
|
|
181
|
+
us_poll_init(new_p, us_poll_fd(new_p), us_internal_poll_type(new_p));
|
|
182
|
+
us_poll_start(new_p, loop, events);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return new_p;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/* Timers */
|
|
189
|
+
void gcd_timer_handler(void *t) {
|
|
190
|
+
struct us_internal_callback_t *internal_cb = (struct us_internal_callback_t *) t;
|
|
191
|
+
|
|
192
|
+
internal_cb->cb(t);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
struct us_timer_t *us_create_timer(struct us_loop_t *loop, int fallthrough, unsigned int ext_size) {
|
|
196
|
+
struct us_internal_callback_t *cb = malloc(sizeof(struct us_internal_callback_t) + sizeof(dispatch_source_t) + ext_size);
|
|
197
|
+
|
|
198
|
+
cb->loop = loop;
|
|
199
|
+
cb->cb_expects_the_loop = 0;
|
|
200
|
+
cb->leave_poll_ready = 0;
|
|
201
|
+
|
|
202
|
+
dispatch_source_t *gcd_timer = (dispatch_source_t *) (cb + 1);
|
|
203
|
+
|
|
204
|
+
*gcd_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
|
|
205
|
+
dispatch_source_set_event_handler_f(*gcd_timer, gcd_timer_handler);
|
|
206
|
+
dispatch_set_context(*gcd_timer, cb);
|
|
207
|
+
|
|
208
|
+
if (fallthrough) {
|
|
209
|
+
//uv_unref((uv_handle_t *) uv_timer);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return (struct us_timer_t *) cb;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
void *us_timer_ext(struct us_timer_t *timer) {
|
|
216
|
+
struct us_internal_callback_t *cb = (struct us_internal_callback_t *) timer;
|
|
217
|
+
|
|
218
|
+
return (cb + 1);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
void us_timer_close(struct us_timer_t *t) {
|
|
222
|
+
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
void us_timer_set(struct us_timer_t *t, void (*cb)(struct us_timer_t *t), int ms, int repeat_ms) {
|
|
226
|
+
struct us_internal_callback_t *internal_cb = (struct us_internal_callback_t *) t;
|
|
227
|
+
|
|
228
|
+
internal_cb->cb = (void(*)(struct us_internal_callback_t *)) cb;
|
|
229
|
+
|
|
230
|
+
dispatch_source_t *gcd_timer = (dispatch_source_t *) (internal_cb + 1);
|
|
231
|
+
uint64_t nanos = (uint64_t)ms * 1000000;
|
|
232
|
+
dispatch_source_set_timer(*gcd_timer, 0, nanos, 0);
|
|
233
|
+
dispatch_activate(*gcd_timer);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
struct us_loop_t *us_timer_loop(struct us_timer_t *t) {
|
|
237
|
+
struct us_internal_callback_t *internal_cb = (struct us_internal_callback_t *) t;
|
|
238
|
+
|
|
239
|
+
return internal_cb->loop;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/* Asyncs */
|
|
243
|
+
void async_handler(void *c) {
|
|
244
|
+
struct us_internal_callback_t *internal_cb = (struct us_internal_callback_t *) c;
|
|
245
|
+
|
|
246
|
+
internal_cb->cb((struct us_internal_callback_t *) internal_cb->loop);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
struct us_internal_async *us_internal_create_async(struct us_loop_t *loop, int fallthrough, unsigned int ext_size) {
|
|
250
|
+
struct us_internal_callback_t *cb = malloc(sizeof(struct us_internal_callback_t) + ext_size);
|
|
251
|
+
|
|
252
|
+
cb->loop = loop;
|
|
253
|
+
cb->cb_expects_the_loop = 1;
|
|
254
|
+
cb->leave_poll_ready = 0;
|
|
255
|
+
|
|
256
|
+
if (fallthrough) {
|
|
257
|
+
//uv_unref((uv_handle_t *) uv_timer);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return (struct us_internal_async *) cb;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
void us_internal_async_close(struct us_internal_async *a) {
|
|
264
|
+
// cancel? free?
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
void us_internal_async_set(struct us_internal_async *a, void (*cb)(struct us_internal_async *)) {
|
|
268
|
+
struct us_internal_callback_t *internal_cb = (struct us_internal_callback_t *) a;
|
|
269
|
+
|
|
270
|
+
internal_cb->cb = (void (*)(struct us_internal_callback_t *)) cb;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
void us_internal_async_wakeup(struct us_internal_async *a) {
|
|
274
|
+
// will probably need to track in-flight work item and cancel in close
|
|
275
|
+
dispatch_async_f(dispatch_get_main_queue(), a, async_handler);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
#endif
|
|
@@ -0,0 +1,305 @@
|
|
|
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
|
+
#include "libusockets.h"
|
|
19
|
+
#include "internal/internal.h"
|
|
20
|
+
#include <stdlib.h>
|
|
21
|
+
|
|
22
|
+
#ifdef LIBUS_USE_LIBUV
|
|
23
|
+
|
|
24
|
+
/* uv_poll_t->data always (except for most times after calling us_poll_stop) points to the us_poll_t */
|
|
25
|
+
static void poll_cb(uv_poll_t *p, int status, int events) {
|
|
26
|
+
us_internal_dispatch_ready_poll((struct us_poll_t *) p->data, status < 0, events);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static void prepare_cb(uv_prepare_t *p) {
|
|
30
|
+
struct us_loop_t *loop = p->data;
|
|
31
|
+
us_internal_loop_pre(loop);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/* Note: libuv timers execute AFTER the post callback */
|
|
35
|
+
static void check_cb(uv_check_t *p) {
|
|
36
|
+
struct us_loop_t *loop = p->data;
|
|
37
|
+
us_internal_loop_post(loop);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/* Not used for polls, since polls need two frees */
|
|
41
|
+
static void close_cb_free(uv_handle_t *h) {
|
|
42
|
+
free(h->data);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/* This one is different for polls, since we need two frees here */
|
|
46
|
+
static void close_cb_free_poll(uv_handle_t *h) {
|
|
47
|
+
/* It is only in case we called us_poll_stop then quickly us_poll_free that we enter this.
|
|
48
|
+
* Most of the time, actual freeing is done by us_poll_free. */
|
|
49
|
+
if (h->data) {
|
|
50
|
+
free(h->data);
|
|
51
|
+
free(h);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static void timer_cb(uv_timer_t *t) {
|
|
56
|
+
struct us_internal_callback_t *cb = t->data;
|
|
57
|
+
cb->cb(cb);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static void async_cb(uv_async_t *a) {
|
|
61
|
+
struct us_internal_callback_t *cb = a->data;
|
|
62
|
+
// internal asyncs give their loop, not themselves
|
|
63
|
+
cb->cb((struct us_internal_callback_t *) cb->loop);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// poll
|
|
67
|
+
void us_poll_init(struct us_poll_t *p, LIBUS_SOCKET_DESCRIPTOR fd, int poll_type) {
|
|
68
|
+
p->poll_type = poll_type;
|
|
69
|
+
p->fd = fd;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
void us_poll_free(struct us_poll_t *p, struct us_loop_t *loop) {
|
|
73
|
+
/* The idea here is like so; in us_poll_stop we call uv_close after setting data of uv-poll to 0.
|
|
74
|
+
* This means that in close_cb_free we call free on 0 with does nothing, since us_poll_stop should
|
|
75
|
+
* not really free the poll. HOWEVER, if we then call us_poll_free while still closing the uv-poll,
|
|
76
|
+
* we simply change back the data to point to our structure so that we actually do free it like we should. */
|
|
77
|
+
if (uv_is_closing((uv_handle_t *) p->uv_p)) {
|
|
78
|
+
p->uv_p->data = p;
|
|
79
|
+
} else {
|
|
80
|
+
free(p->uv_p);
|
|
81
|
+
free(p);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
void us_poll_start(struct us_poll_t *p, struct us_loop_t *loop, int events) {
|
|
86
|
+
p->poll_type = us_internal_poll_type(p) | ((events & LIBUS_SOCKET_READABLE) ? POLL_TYPE_POLLING_IN : 0) | ((events & LIBUS_SOCKET_WRITABLE) ? POLL_TYPE_POLLING_OUT : 0);
|
|
87
|
+
|
|
88
|
+
uv_poll_init_socket(loop->uv_loop, p->uv_p, p->fd);
|
|
89
|
+
uv_poll_start(p->uv_p, events, poll_cb);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
void us_poll_change(struct us_poll_t *p, struct us_loop_t *loop, int events) {
|
|
93
|
+
if (us_poll_events(p) != events) {
|
|
94
|
+
p->poll_type = us_internal_poll_type(p) | ((events & LIBUS_SOCKET_READABLE) ? POLL_TYPE_POLLING_IN : 0) | ((events & LIBUS_SOCKET_WRITABLE) ? POLL_TYPE_POLLING_OUT : 0);
|
|
95
|
+
|
|
96
|
+
uv_poll_start(p->uv_p, events, poll_cb);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
void us_poll_stop(struct us_poll_t *p, struct us_loop_t *loop) {
|
|
101
|
+
uv_poll_stop(p->uv_p);
|
|
102
|
+
|
|
103
|
+
/* We normally only want to close the poll here, not free it. But if we stop it, then quickly "free" it with
|
|
104
|
+
* us_poll_free, we postpone the actual freeing to close_cb_free_poll whenever it triggers.
|
|
105
|
+
* That's why we set data to null here, so that us_poll_free can reset it if needed */
|
|
106
|
+
p->uv_p->data = 0;
|
|
107
|
+
uv_close((uv_handle_t *) p->uv_p, close_cb_free_poll);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
int us_poll_events(struct us_poll_t *p) {
|
|
111
|
+
return ((p->poll_type & POLL_TYPE_POLLING_IN) ? LIBUS_SOCKET_READABLE : 0) | ((p->poll_type & POLL_TYPE_POLLING_OUT) ? LIBUS_SOCKET_WRITABLE : 0);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
unsigned int us_internal_accept_poll_event(struct us_poll_t *p) {
|
|
115
|
+
return 0;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
int us_internal_poll_type(struct us_poll_t *p) {
|
|
119
|
+
return p->poll_type & 3;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
void us_internal_poll_set_type(struct us_poll_t *p, int poll_type) {
|
|
123
|
+
p->poll_type = poll_type | (p->poll_type & 12);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
LIBUS_SOCKET_DESCRIPTOR us_poll_fd(struct us_poll_t *p) {
|
|
127
|
+
return p->fd;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
void us_loop_pump(struct us_loop_t *loop) {
|
|
131
|
+
uv_run(loop->uv_loop, UV_RUN_NOWAIT);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
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) {
|
|
135
|
+
struct us_loop_t *loop = (struct us_loop_t *) malloc(sizeof(struct us_loop_t) + ext_size);
|
|
136
|
+
|
|
137
|
+
loop->uv_loop = hint ? hint : uv_loop_new();
|
|
138
|
+
loop->is_default = hint != 0;
|
|
139
|
+
|
|
140
|
+
loop->uv_pre = malloc(sizeof(uv_prepare_t));
|
|
141
|
+
uv_prepare_init(loop->uv_loop, loop->uv_pre);
|
|
142
|
+
uv_prepare_start(loop->uv_pre, prepare_cb);
|
|
143
|
+
uv_unref((uv_handle_t *) loop->uv_pre);
|
|
144
|
+
loop->uv_pre->data = loop;
|
|
145
|
+
|
|
146
|
+
loop->uv_check = malloc(sizeof(uv_check_t));
|
|
147
|
+
uv_check_init(loop->uv_loop, loop->uv_check);
|
|
148
|
+
uv_unref((uv_handle_t *) loop->uv_check);
|
|
149
|
+
uv_check_start(loop->uv_check, check_cb);
|
|
150
|
+
loop->uv_check->data = loop;
|
|
151
|
+
|
|
152
|
+
// here we create two unreffed handles - timer and async
|
|
153
|
+
us_internal_loop_data_init(loop, wakeup_cb, pre_cb, post_cb);
|
|
154
|
+
|
|
155
|
+
// if we do not own this loop, we need to integrate and set up timer
|
|
156
|
+
if (hint) {
|
|
157
|
+
us_loop_integrate(loop);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return loop;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// based on if this was default loop or not
|
|
164
|
+
void us_loop_free(struct us_loop_t *loop) {
|
|
165
|
+
// ref and close down prepare and check
|
|
166
|
+
uv_ref((uv_handle_t *) loop->uv_pre);
|
|
167
|
+
uv_prepare_stop(loop->uv_pre);
|
|
168
|
+
loop->uv_pre->data = loop->uv_pre;
|
|
169
|
+
uv_close((uv_handle_t *) loop->uv_pre, close_cb_free);
|
|
170
|
+
|
|
171
|
+
uv_ref((uv_handle_t *) loop->uv_check);
|
|
172
|
+
uv_check_stop(loop->uv_check);
|
|
173
|
+
loop->uv_check->data = loop->uv_check;
|
|
174
|
+
uv_close((uv_handle_t *) loop->uv_check, close_cb_free);
|
|
175
|
+
|
|
176
|
+
us_internal_loop_data_free(loop);
|
|
177
|
+
|
|
178
|
+
// we need to run the loop one last round to call all close callbacks
|
|
179
|
+
// we cannot do this if we do not own the loop, default
|
|
180
|
+
if (!loop->is_default) {
|
|
181
|
+
uv_run(loop->uv_loop, UV_RUN_NOWAIT);
|
|
182
|
+
uv_loop_delete(loop->uv_loop);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// now we can free our part
|
|
186
|
+
free(loop);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
void us_loop_run(struct us_loop_t *loop) {
|
|
190
|
+
us_loop_integrate(loop);
|
|
191
|
+
|
|
192
|
+
uv_run(loop->uv_loop, UV_RUN_DEFAULT);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
struct us_poll_t *us_create_poll(struct us_loop_t *loop, int fallthrough, unsigned int ext_size) {
|
|
196
|
+
struct us_poll_t *p = (struct us_poll_t *) malloc(sizeof(struct us_poll_t) + ext_size);
|
|
197
|
+
p->uv_p = malloc(sizeof(uv_poll_t));
|
|
198
|
+
p->uv_p->data = p;
|
|
199
|
+
return p;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/* If we update our block position we have to updarte the uv_poll data to point to us */
|
|
203
|
+
struct us_poll_t *us_poll_resize(struct us_poll_t *p, struct us_loop_t *loop, unsigned int ext_size) {
|
|
204
|
+
|
|
205
|
+
struct us_poll_t *new_p = realloc(p, sizeof(struct us_poll_t) + ext_size);
|
|
206
|
+
new_p->uv_p->data = new_p;
|
|
207
|
+
|
|
208
|
+
return new_p;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// timer
|
|
212
|
+
struct us_timer_t *us_create_timer(struct us_loop_t *loop, int fallthrough, unsigned int ext_size) {
|
|
213
|
+
struct us_internal_callback_t *cb = malloc(sizeof(struct us_internal_callback_t) + sizeof(uv_timer_t) + ext_size);
|
|
214
|
+
|
|
215
|
+
cb->loop = loop;
|
|
216
|
+
cb->cb_expects_the_loop = 0; // never read?
|
|
217
|
+
cb->leave_poll_ready = 0; // never read?
|
|
218
|
+
|
|
219
|
+
uv_timer_t *uv_timer = (uv_timer_t *) (cb + 1);
|
|
220
|
+
uv_timer_init(loop->uv_loop, uv_timer);
|
|
221
|
+
uv_timer->data = cb;
|
|
222
|
+
|
|
223
|
+
if (fallthrough) {
|
|
224
|
+
uv_unref((uv_handle_t *) uv_timer);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return (struct us_timer_t *) cb;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
void *us_timer_ext(struct us_timer_t *timer) {
|
|
231
|
+
return ((char *) timer) + sizeof(struct us_internal_callback_t) + sizeof(uv_timer_t);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
void us_timer_close(struct us_timer_t *t) {
|
|
235
|
+
struct us_internal_callback_t *cb = (struct us_internal_callback_t *) t;
|
|
236
|
+
|
|
237
|
+
uv_timer_t *uv_timer = (uv_timer_t *) (cb + 1);
|
|
238
|
+
|
|
239
|
+
// always ref the timer before closing it
|
|
240
|
+
uv_ref((uv_handle_t *) uv_timer);
|
|
241
|
+
|
|
242
|
+
uv_timer_stop(uv_timer);
|
|
243
|
+
|
|
244
|
+
uv_timer->data = cb;
|
|
245
|
+
uv_close((uv_handle_t *) uv_timer, close_cb_free);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
void us_timer_set(struct us_timer_t *t, void (*cb)(struct us_timer_t *t), int ms, int repeat_ms) {
|
|
249
|
+
struct us_internal_callback_t *internal_cb = (struct us_internal_callback_t *) t;
|
|
250
|
+
|
|
251
|
+
internal_cb->cb = (void(*)(struct us_internal_callback_t *)) cb;
|
|
252
|
+
|
|
253
|
+
uv_timer_t *uv_timer = (uv_timer_t *) (internal_cb + 1);
|
|
254
|
+
if (!ms) {
|
|
255
|
+
uv_timer_stop(uv_timer);
|
|
256
|
+
} else {
|
|
257
|
+
uv_timer_start(uv_timer, timer_cb, ms, repeat_ms);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
struct us_loop_t *us_timer_loop(struct us_timer_t *t) {
|
|
262
|
+
struct us_internal_callback_t *internal_cb = (struct us_internal_callback_t *) t;
|
|
263
|
+
|
|
264
|
+
return internal_cb->loop;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// async (internal only)
|
|
268
|
+
struct us_internal_async *us_internal_create_async(struct us_loop_t *loop, int fallthrough, unsigned int ext_size) {
|
|
269
|
+
struct us_internal_callback_t *cb = malloc(sizeof(struct us_internal_callback_t) + sizeof(uv_async_t) + ext_size);
|
|
270
|
+
|
|
271
|
+
cb->loop = loop;
|
|
272
|
+
return (struct us_internal_async *) cb;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
void us_internal_async_close(struct us_internal_async *a) {
|
|
276
|
+
struct us_internal_callback_t *cb = (struct us_internal_callback_t *) a;
|
|
277
|
+
|
|
278
|
+
uv_async_t *uv_async = (uv_async_t *) (cb + 1);
|
|
279
|
+
|
|
280
|
+
// always ref the async before closing it
|
|
281
|
+
uv_ref((uv_handle_t *) uv_async);
|
|
282
|
+
|
|
283
|
+
uv_async->data = cb;
|
|
284
|
+
uv_close((uv_handle_t *) uv_async, close_cb_free);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
void us_internal_async_set(struct us_internal_async *a, void (*cb)(struct us_internal_async *)) {
|
|
288
|
+
struct us_internal_callback_t *internal_cb = (struct us_internal_callback_t *) a;
|
|
289
|
+
|
|
290
|
+
internal_cb->cb = (void (*)(struct us_internal_callback_t *)) cb;
|
|
291
|
+
|
|
292
|
+
uv_async_t *uv_async = (uv_async_t *) (internal_cb + 1);
|
|
293
|
+
uv_async_init(internal_cb->loop->uv_loop, uv_async, async_cb);
|
|
294
|
+
uv_unref((uv_handle_t *) uv_async);
|
|
295
|
+
uv_async->data = internal_cb;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
void us_internal_async_wakeup(struct us_internal_async *a) {
|
|
299
|
+
struct us_internal_callback_t *internal_cb = (struct us_internal_callback_t *) a;
|
|
300
|
+
|
|
301
|
+
uv_async_t *uv_async = (uv_async_t *) (internal_cb + 1);
|
|
302
|
+
uv_async_send(uv_async);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
#endif
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
#ifndef ASIO_H
|
|
19
|
+
#define ASIO_H
|
|
20
|
+
|
|
21
|
+
#include "internal/loop_data.h"
|
|
22
|
+
|
|
23
|
+
#define LIBUS_SOCKET_READABLE 1
|
|
24
|
+
#define LIBUS_SOCKET_WRITABLE 2
|
|
25
|
+
|
|
26
|
+
struct us_loop_t {
|
|
27
|
+
alignas(LIBUS_EXT_ALIGNMENT) struct us_internal_loop_data_t data;
|
|
28
|
+
|
|
29
|
+
// a loop is an io_context
|
|
30
|
+
void *io;
|
|
31
|
+
|
|
32
|
+
// whether or not we got an io_context as hint or not
|
|
33
|
+
int is_default;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// it is no longer valid to cast a pointer to us_poll_t to a pointer of uv_poll_t
|
|
37
|
+
struct us_poll_t {
|
|
38
|
+
void *boost_block;
|
|
39
|
+
|
|
40
|
+
LIBUS_SOCKET_DESCRIPTOR fd;
|
|
41
|
+
unsigned char poll_type;
|
|
42
|
+
int events;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
#endif // ASIO_H
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Authored by Alex Hultman, 2018-2019.
|
|
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
|
+
#ifndef EPOLL_KQUEUE_H
|
|
19
|
+
#define EPOLL_KQUEUE_H
|
|
20
|
+
|
|
21
|
+
#include "internal/loop_data.h"
|
|
22
|
+
|
|
23
|
+
#ifdef LIBUS_USE_EPOLL
|
|
24
|
+
#include <sys/epoll.h>
|
|
25
|
+
#include <sys/timerfd.h>
|
|
26
|
+
#include <sys/eventfd.h>
|
|
27
|
+
#define LIBUS_SOCKET_READABLE EPOLLIN
|
|
28
|
+
#define LIBUS_SOCKET_WRITABLE EPOLLOUT
|
|
29
|
+
#else
|
|
30
|
+
#include <sys/event.h>
|
|
31
|
+
/* Kqueue's EVFILT_ is NOT a bitfield, you cannot OR together them.
|
|
32
|
+
* We therefore have our own bitfield we then translate in every call */
|
|
33
|
+
#define LIBUS_SOCKET_READABLE 1
|
|
34
|
+
#define LIBUS_SOCKET_WRITABLE 2
|
|
35
|
+
#endif
|
|
36
|
+
|
|
37
|
+
struct us_loop_t {
|
|
38
|
+
alignas(LIBUS_EXT_ALIGNMENT) struct us_internal_loop_data_t data;
|
|
39
|
+
|
|
40
|
+
/* Number of non-fallthrough polls in the loop */
|
|
41
|
+
int num_polls;
|
|
42
|
+
|
|
43
|
+
/* Number of ready polls this iteration */
|
|
44
|
+
int num_ready_polls;
|
|
45
|
+
|
|
46
|
+
/* Current index in list of ready polls */
|
|
47
|
+
int current_ready_poll;
|
|
48
|
+
|
|
49
|
+
/* Loop's own file descriptor */
|
|
50
|
+
int fd;
|
|
51
|
+
|
|
52
|
+
/* The list of ready polls */
|
|
53
|
+
#ifdef LIBUS_USE_EPOLL
|
|
54
|
+
struct epoll_event ready_polls[1024];
|
|
55
|
+
#else
|
|
56
|
+
struct kevent ready_polls[1024];
|
|
57
|
+
#endif
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
struct us_poll_t {
|
|
61
|
+
alignas(LIBUS_EXT_ALIGNMENT) struct {
|
|
62
|
+
signed int fd : 28; // we could have this unsigned if we wanted to, -1 should never be used
|
|
63
|
+
unsigned int poll_type : 4;
|
|
64
|
+
} state;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
#endif // EPOLL_KQUEUE_H
|