opal-up 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +209 -0
  3. data/README.md +81 -28
  4. data/bin/up_ruby +4 -0
  5. data/bin/up_ruby_cluster +4 -0
  6. data/ext/up_ext/App.h +606 -0
  7. data/ext/up_ext/AsyncSocket.h +355 -0
  8. data/ext/up_ext/AsyncSocketData.h +87 -0
  9. data/ext/up_ext/BloomFilter.h +83 -0
  10. data/ext/up_ext/ChunkedEncoding.h +236 -0
  11. data/ext/up_ext/ClientApp.h +36 -0
  12. data/ext/up_ext/HttpContext.h +502 -0
  13. data/ext/up_ext/HttpContextData.h +56 -0
  14. data/ext/up_ext/HttpErrors.h +53 -0
  15. data/ext/up_ext/HttpParser.h +680 -0
  16. data/ext/up_ext/HttpResponse.h +578 -0
  17. data/ext/up_ext/HttpResponseData.h +95 -0
  18. data/ext/up_ext/HttpRouter.h +380 -0
  19. data/ext/up_ext/Loop.h +204 -0
  20. data/ext/up_ext/LoopData.h +112 -0
  21. data/ext/up_ext/MoveOnlyFunction.h +377 -0
  22. data/ext/up_ext/PerMessageDeflate.h +315 -0
  23. data/ext/up_ext/ProxyParser.h +163 -0
  24. data/ext/up_ext/QueryParser.h +120 -0
  25. data/ext/up_ext/TopicTree.h +363 -0
  26. data/ext/up_ext/Utilities.h +66 -0
  27. data/ext/up_ext/WebSocket.h +381 -0
  28. data/ext/up_ext/WebSocketContext.h +434 -0
  29. data/ext/up_ext/WebSocketContextData.h +109 -0
  30. data/ext/up_ext/WebSocketData.h +86 -0
  31. data/ext/up_ext/WebSocketExtensions.h +256 -0
  32. data/ext/up_ext/WebSocketHandshake.h +145 -0
  33. data/ext/up_ext/WebSocketProtocol.h +506 -0
  34. data/ext/up_ext/bsd.c +767 -0
  35. data/ext/up_ext/bsd.h +109 -0
  36. data/ext/up_ext/context.c +524 -0
  37. data/ext/up_ext/epoll_kqueue.c +458 -0
  38. data/ext/up_ext/epoll_kqueue.h +67 -0
  39. data/ext/up_ext/extconf.rb +5 -0
  40. data/ext/up_ext/internal.h +224 -0
  41. data/ext/up_ext/libusockets.h +350 -0
  42. data/ext/up_ext/libuwebsockets.cpp +1374 -0
  43. data/ext/up_ext/libuwebsockets.h +260 -0
  44. data/ext/up_ext/loop.c +386 -0
  45. data/ext/up_ext/loop_data.h +38 -0
  46. data/ext/up_ext/socket.c +231 -0
  47. data/ext/up_ext/up_ext.c +278 -0
  48. data/lib/up/node/rack_env.rb +2 -2
  49. data/lib/up/ruby/cluster_cli.rb +10 -0
  50. data/lib/up/ruby/rack_cluster.rb +26 -0
  51. data/lib/up/ruby/rack_env.rb +97 -0
  52. data/lib/up/ruby/rack_server.rb +26 -0
  53. data/lib/up/ruby/server_cli.rb +10 -0
  54. data/lib/up/u_web_socket/rack_env.rb +1 -1
  55. data/lib/up/version.rb +1 -1
  56. metadata +71 -18
  57. data/.gitignore +0 -5
  58. data/Gemfile +0 -2
  59. data/example_rack_app/Gemfile +0 -3
  60. data/example_rack_app/config.ru +0 -6
  61. data/example_rack_app/rack_app.rb +0 -5
  62. data/example_roda_app/Gemfile +0 -6
  63. data/example_roda_app/config.ru +0 -6
  64. data/example_roda_app/roda_app.rb +0 -37
  65. data/example_sinatra_app/Gemfile +0 -6
  66. data/example_sinatra_app/config.ru +0 -6
  67. data/example_sinatra_app/sinatra_app.rb +0 -7
  68. data/opal-up.gemspec +0 -27
  69. data/up_logo.svg +0 -256
@@ -0,0 +1,224 @@
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 INTERNAL_H
19
+ #define INTERNAL_H
20
+
21
+ #if defined(_MSC_VER)
22
+ #define alignas(x) __declspec(align(x))
23
+ #else
24
+ #include <stdalign.h>
25
+ #endif
26
+
27
+ /* We only have one networking implementation so far */
28
+ #include "bsd.h"
29
+
30
+ // WARNING: This MUST be included after bsd.h, otherwise sys/socket includes
31
+ // fail surprisingly.
32
+ #include <stdint.h>
33
+
34
+ #ifndef LIBUS_USE_IO_URING
35
+
36
+ /* We have many different eventing implementations */
37
+ #if defined(LIBUS_USE_EPOLL) || defined(LIBUS_USE_KQUEUE)
38
+ #include "epoll_kqueue.h"
39
+ #endif
40
+ #ifdef LIBUS_USE_LIBUV
41
+ #include "internal/eventing/libuv.h"
42
+ #endif
43
+ #ifdef LIBUS_USE_GCD
44
+ #include "internal/eventing/gcd.h"
45
+ #endif
46
+ #ifdef LIBUS_USE_ASIO
47
+ #include "internal/eventing/asio.h"
48
+ #endif
49
+
50
+ /* Poll type and what it polls for */
51
+ enum {
52
+ /* Two first bits */
53
+ POLL_TYPE_SOCKET = 0,
54
+ POLL_TYPE_SOCKET_SHUT_DOWN = 1,
55
+ POLL_TYPE_SEMI_SOCKET = 2,
56
+ POLL_TYPE_CALLBACK = 3,
57
+
58
+ /* Two last bits */
59
+ POLL_TYPE_POLLING_OUT = 4,
60
+ POLL_TYPE_POLLING_IN = 8
61
+ };
62
+
63
+ /* Loop related */
64
+ void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int events);
65
+ void us_internal_timer_sweep(struct us_loop_t *loop);
66
+ void us_internal_free_closed_sockets(struct us_loop_t *loop);
67
+ void us_internal_loop_link(struct us_loop_t *loop, struct us_socket_context_t *context);
68
+ void us_internal_loop_unlink(struct us_loop_t *loop, struct us_socket_context_t *context);
69
+ void us_internal_loop_data_init(struct us_loop_t *loop, void (*wakeup_cb)(struct us_loop_t *loop),
70
+ void (*pre_cb)(struct us_loop_t *loop), void (*post_cb)(struct us_loop_t *loop));
71
+ void us_internal_loop_data_free(struct us_loop_t *loop);
72
+ void us_internal_loop_pre(struct us_loop_t *loop);
73
+ void us_internal_loop_post(struct us_loop_t *loop);
74
+
75
+ /* Asyncs (old) */
76
+ struct us_internal_async *us_internal_create_async(struct us_loop_t *loop, int fallthrough, unsigned int ext_size);
77
+ void us_internal_async_close(struct us_internal_async *a);
78
+ void us_internal_async_set(struct us_internal_async *a, void (*cb)(struct us_internal_async *));
79
+ void us_internal_async_wakeup(struct us_internal_async *a);
80
+
81
+ /* Eventing related */
82
+ unsigned int us_internal_accept_poll_event(struct us_poll_t *p);
83
+ int us_internal_poll_type(struct us_poll_t *p);
84
+ void us_internal_poll_set_type(struct us_poll_t *p, int poll_type);
85
+
86
+ /* SSL loop data */
87
+ void us_internal_init_loop_ssl_data(struct us_loop_t *loop);
88
+ void us_internal_free_loop_ssl_data(struct us_loop_t *loop);
89
+
90
+ /* Socket context related */
91
+ void us_internal_socket_context_link_socket(struct us_socket_context_t *context, struct us_socket_t *s);
92
+ void us_internal_socket_context_unlink_socket(struct us_socket_context_t *context, struct us_socket_t *s);
93
+
94
+ /* Sockets are polls */
95
+ struct us_socket_t {
96
+ alignas(LIBUS_EXT_ALIGNMENT) struct us_poll_t p; // 4 bytes
97
+ unsigned char timeout; // 1 byte
98
+ unsigned char long_timeout; // 1 byte
99
+ unsigned short low_prio_state; /* 0 = not in low-prio queue, 1 = is in low-prio queue, 2 = was in low-prio queue in this iteration */
100
+ struct us_socket_context_t *context;
101
+ struct us_socket_t *prev, *next;
102
+ };
103
+
104
+ /* Internal callback types are polls just like sockets */
105
+ struct us_internal_callback_t {
106
+ alignas(LIBUS_EXT_ALIGNMENT) struct us_poll_t p;
107
+ struct us_loop_t *loop;
108
+ int cb_expects_the_loop;
109
+ int leave_poll_ready;
110
+ void (*cb)(struct us_internal_callback_t *cb);
111
+ };
112
+
113
+ /* Listen sockets are sockets */
114
+ struct us_listen_socket_t {
115
+ alignas(LIBUS_EXT_ALIGNMENT) struct us_socket_t s;
116
+ unsigned int socket_ext_size;
117
+ };
118
+
119
+ /* Listen sockets are keps in their own list */
120
+ void us_internal_socket_context_link_listen_socket(struct us_socket_context_t *context, struct us_listen_socket_t *s);
121
+ void us_internal_socket_context_unlink_listen_socket(struct us_socket_context_t *context, struct us_listen_socket_t *s);
122
+
123
+ struct us_socket_context_t {
124
+ alignas(LIBUS_EXT_ALIGNMENT) struct us_loop_t *loop;
125
+ uint32_t global_tick;
126
+ unsigned char timestamp;
127
+ unsigned char long_timestamp;
128
+ struct us_socket_t *head_sockets;
129
+ struct us_listen_socket_t *head_listen_sockets;
130
+ struct us_socket_t *iterator;
131
+ struct us_socket_context_t *prev, *next;
132
+
133
+ LIBUS_SOCKET_DESCRIPTOR (*on_pre_open)(LIBUS_SOCKET_DESCRIPTOR fd);
134
+ struct us_socket_t *(*on_open)(struct us_socket_t *, int is_client, char *ip, int ip_length);
135
+ struct us_socket_t *(*on_data)(struct us_socket_t *, char *data, int length);
136
+ struct us_socket_t *(*on_writable)(struct us_socket_t *);
137
+ struct us_socket_t *(*on_close)(struct us_socket_t *, int code, void *reason);
138
+ //void (*on_timeout)(struct us_socket_context *);
139
+ struct us_socket_t *(*on_socket_timeout)(struct us_socket_t *);
140
+ struct us_socket_t *(*on_socket_long_timeout)(struct us_socket_t *);
141
+ struct us_socket_t *(*on_end)(struct us_socket_t *);
142
+ struct us_socket_t *(*on_connect_error)(struct us_socket_t *, int code);
143
+ int (*is_low_prio)(struct us_socket_t *);
144
+ };
145
+
146
+ #endif
147
+
148
+ /* Internal SSL interface */
149
+ #ifndef LIBUS_NO_SSL
150
+
151
+ struct us_internal_ssl_socket_context_t;
152
+ struct us_internal_ssl_socket_t;
153
+
154
+ /* SNI functions */
155
+ void us_internal_ssl_socket_context_add_server_name(struct us_internal_ssl_socket_context_t *context, const char *hostname_pattern, struct us_socket_context_options_t options, void *user);
156
+ void us_internal_ssl_socket_context_remove_server_name(struct us_internal_ssl_socket_context_t *context, const char *hostname_pattern);
157
+ void us_internal_ssl_socket_context_on_server_name(struct us_internal_ssl_socket_context_t *context, void (*cb)(struct us_internal_ssl_socket_context_t *, const char *));
158
+ void *us_internal_ssl_socket_get_sni_userdata(struct us_internal_ssl_socket_t *s);
159
+ void *us_internal_ssl_socket_context_find_server_name_userdata(struct us_internal_ssl_socket_context_t *context, const char *hostname_pattern);
160
+
161
+ void *us_internal_ssl_socket_get_native_handle(struct us_internal_ssl_socket_t *s);
162
+ void *us_internal_ssl_socket_context_get_native_handle(struct us_internal_ssl_socket_context_t *context);
163
+
164
+ struct us_internal_ssl_socket_context_t *us_internal_create_ssl_socket_context(struct us_loop_t *loop,
165
+ int context_ext_size, struct us_socket_context_options_t options);
166
+
167
+ void us_internal_ssl_socket_context_free(struct us_internal_ssl_socket_context_t *context);
168
+ void us_internal_ssl_socket_context_on_open(struct us_internal_ssl_socket_context_t *context,
169
+ struct us_internal_ssl_socket_t *(*on_open)(struct us_internal_ssl_socket_t *s, int is_client, char *ip, int ip_length));
170
+
171
+ void us_internal_ssl_socket_context_on_close(struct us_internal_ssl_socket_context_t *context,
172
+ struct us_internal_ssl_socket_t *(*on_close)(struct us_internal_ssl_socket_t *s, int code, void *reason));
173
+
174
+ void us_internal_ssl_socket_context_on_data(struct us_internal_ssl_socket_context_t *context,
175
+ struct us_internal_ssl_socket_t *(*on_data)(struct us_internal_ssl_socket_t *s, char *data, int length));
176
+
177
+ void us_internal_ssl_socket_context_on_writable(struct us_internal_ssl_socket_context_t *context,
178
+ struct us_internal_ssl_socket_t *(*on_writable)(struct us_internal_ssl_socket_t *s));
179
+
180
+ void us_internal_ssl_socket_context_on_timeout(struct us_internal_ssl_socket_context_t *context,
181
+ struct us_internal_ssl_socket_t *(*on_timeout)(struct us_internal_ssl_socket_t *s));
182
+
183
+ void us_internal_ssl_socket_context_on_long_timeout(struct us_internal_ssl_socket_context_t *context,
184
+ struct us_internal_ssl_socket_t *(*on_timeout)(struct us_internal_ssl_socket_t *s));
185
+
186
+ void us_internal_ssl_socket_context_on_end(struct us_internal_ssl_socket_context_t *context,
187
+ struct us_internal_ssl_socket_t *(*on_end)(struct us_internal_ssl_socket_t *s));
188
+
189
+ void us_internal_ssl_socket_context_on_connect_error(struct us_internal_ssl_socket_context_t *context,
190
+ struct us_internal_ssl_socket_t *(*on_connect_error)(struct us_internal_ssl_socket_t *s, int code));
191
+
192
+ struct us_listen_socket_t *us_internal_ssl_socket_context_listen(struct us_internal_ssl_socket_context_t *context,
193
+ const char *host, int port, int options, int socket_ext_size);
194
+
195
+ struct us_listen_socket_t *us_internal_ssl_socket_context_listen_unix(struct us_internal_ssl_socket_context_t *context,
196
+ const char *path, int options, int socket_ext_size);
197
+
198
+ struct us_internal_ssl_socket_t *us_internal_ssl_adopt_accepted_socket(struct us_internal_ssl_socket_context_t *context, LIBUS_SOCKET_DESCRIPTOR accepted_fd,
199
+ unsigned int socket_ext_size, char *addr_ip, int addr_ip_length);
200
+
201
+ struct us_internal_ssl_socket_t *us_internal_ssl_socket_context_connect(struct us_internal_ssl_socket_context_t *context,
202
+ const char *host, int port, const char *source_host, int options, int socket_ext_size);
203
+
204
+
205
+ struct us_internal_ssl_socket_t *us_internal_ssl_socket_context_connect_unix(struct us_internal_ssl_socket_context_t *context,
206
+ const char *server_path, int options, int socket_ext_size);
207
+
208
+ int us_internal_ssl_socket_write(struct us_internal_ssl_socket_t *s, const char *data, int length, int msg_more);
209
+ void us_internal_ssl_socket_timeout(struct us_internal_ssl_socket_t *s, unsigned int seconds);
210
+ void *us_internal_ssl_socket_context_ext(struct us_internal_ssl_socket_context_t *s);
211
+ struct us_internal_ssl_socket_context_t *us_internal_ssl_socket_get_context(struct us_internal_ssl_socket_t *s);
212
+ void *us_internal_ssl_socket_ext(struct us_internal_ssl_socket_t *s);
213
+ int us_internal_ssl_socket_is_shut_down(struct us_internal_ssl_socket_t *s);
214
+ void us_internal_ssl_socket_shutdown(struct us_internal_ssl_socket_t *s);
215
+
216
+ struct us_internal_ssl_socket_t *us_internal_ssl_socket_context_adopt_socket(struct us_internal_ssl_socket_context_t *context,
217
+ struct us_internal_ssl_socket_t *s, int ext_size);
218
+
219
+ struct us_internal_ssl_socket_context_t *us_internal_create_child_ssl_socket_context(struct us_internal_ssl_socket_context_t *context, int context_ext_size);
220
+ struct us_loop_t *us_internal_ssl_socket_context_loop(struct us_internal_ssl_socket_context_t *context);
221
+
222
+ #endif
223
+
224
+ #endif // INTERNAL_H
@@ -0,0 +1,350 @@
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 LIBUSOCKETS_H
19
+ #define LIBUSOCKETS_H
20
+
21
+ /* 512kb shared receive buffer */
22
+ #define LIBUS_RECV_BUFFER_LENGTH 524288
23
+ /* A timeout granularity of 4 seconds means give or take 4 seconds from set timeout */
24
+ #define LIBUS_TIMEOUT_GRANULARITY 4
25
+ /* 32 byte padding of receive buffer ends */
26
+ #define LIBUS_RECV_BUFFER_PADDING 32
27
+ /* Guaranteed alignment of extension memory */
28
+ #define LIBUS_EXT_ALIGNMENT 16
29
+
30
+ /* Define what a socket descriptor is based on platform */
31
+ #ifdef _WIN32
32
+ #ifndef NOMINMAX
33
+ #define NOMINMAX
34
+ #endif
35
+ #include <winsock2.h>
36
+ #define LIBUS_SOCKET_DESCRIPTOR SOCKET
37
+ #else
38
+ #define LIBUS_SOCKET_DESCRIPTOR int
39
+ #endif
40
+
41
+ #ifdef __cplusplus
42
+ extern "C" {
43
+ #endif
44
+
45
+ enum {
46
+ /* No meaning, default listen option */
47
+ LIBUS_LISTEN_DEFAULT,
48
+ /* We exclusively own this port, do not share it */
49
+ LIBUS_LISTEN_EXCLUSIVE_PORT
50
+ };
51
+
52
+ /* Library types publicly available */
53
+ struct us_socket_t;
54
+ struct us_timer_t;
55
+ struct us_socket_context_t;
56
+ struct us_loop_t;
57
+ struct us_poll_t;
58
+ struct us_udp_socket_t;
59
+ struct us_udp_packet_buffer_t;
60
+
61
+ /* Extra for io_uring */
62
+ char *us_socket_send_buffer(int ssl, struct us_socket_t *s);
63
+
64
+ /* Public interface for UDP sockets */
65
+
66
+ /* Peeks data and length of UDP payload */
67
+ char *us_udp_packet_buffer_payload(struct us_udp_packet_buffer_t *buf, int index);
68
+ int us_udp_packet_buffer_payload_length(struct us_udp_packet_buffer_t *buf, int index);
69
+
70
+ /* Copies out local (received destination) ip (4 or 16 bytes) of received packet */
71
+ int us_udp_packet_buffer_local_ip(struct us_udp_packet_buffer_t *buf, int index, char *ip);
72
+
73
+ /* Get the bound port in host byte order */
74
+ int us_udp_socket_bound_port(struct us_udp_socket_t *s);
75
+
76
+ /* Peeks peer addr (sockaddr) of received packet */
77
+ char *us_udp_packet_buffer_peer(struct us_udp_packet_buffer_t *buf, int index);
78
+
79
+ /* Peeks ECN of received packet */
80
+ int us_udp_packet_buffer_ecn(struct us_udp_packet_buffer_t *buf, int index);
81
+
82
+ /* Receives a set of packets into specified packet buffer */
83
+ int us_udp_socket_receive(struct us_udp_socket_t *s, struct us_udp_packet_buffer_t *buf);
84
+
85
+ void us_udp_buffer_set_packet_payload(struct us_udp_packet_buffer_t *send_buf, int index, int offset, void *payload, int length, void *peer_addr);
86
+
87
+ int us_udp_socket_send(struct us_udp_socket_t *s, struct us_udp_packet_buffer_t *buf, int num);
88
+
89
+ /* Allocates a packet buffer that is reuable per thread. Mutated by us_udp_socket_receive. */
90
+ struct us_udp_packet_buffer_t *us_create_udp_packet_buffer();
91
+
92
+ /* Creates a (heavy-weight) UDP socket with a user space ring buffer. Again, this one is heavy weight and
93
+ * shoud be reused. One entire QUIC server can be implemented using only one single UDP socket so weight
94
+ * is not a concern as is the case for TCP sockets which are 1-to-1 with TCP connections. */
95
+ //struct us_udp_socket_t *us_create_udp_socket(struct us_loop_t *loop, void (*read_cb)(struct us_udp_socket_t *), unsigned short port);
96
+
97
+ //struct us_udp_socket_t *us_create_udp_socket(struct us_loop_t *loop, void (*data_cb)(struct us_udp_socket_t *, struct us_udp_packet_buffer_t *, int), void (*drain_cb)(struct us_udp_socket_t *), char *host, unsigned short port);
98
+
99
+ struct us_udp_socket_t *us_create_udp_socket(struct us_loop_t *loop, struct us_udp_packet_buffer_t *buf, void (*data_cb)(struct us_udp_socket_t *, struct us_udp_packet_buffer_t *, int), void (*drain_cb)(struct us_udp_socket_t *), const char *host, unsigned short port, void *user);
100
+
101
+ /* This one is ugly, should be ext! not user */
102
+ void *us_udp_socket_user(struct us_udp_socket_t *s);
103
+
104
+ /* Binds the UDP socket to an interface and port */
105
+ int us_udp_socket_bind(struct us_udp_socket_t *s, const char *hostname, unsigned int port);
106
+
107
+ /* Public interfaces for timers */
108
+
109
+ /* Create a new high precision, low performance timer. May fail and return null */
110
+ struct us_timer_t *us_create_timer(struct us_loop_t *loop, int fallthrough, unsigned int ext_size);
111
+
112
+ /* Returns user data extension for this timer */
113
+ void *us_timer_ext(struct us_timer_t *timer);
114
+
115
+ /* */
116
+ void us_timer_close(struct us_timer_t *timer);
117
+
118
+ /* Arm a timer with a delay from now and eventually a repeat delay.
119
+ * Specify 0 as repeat delay to disable repeating. Specify both 0 to disarm. */
120
+ void us_timer_set(struct us_timer_t *timer, void (*cb)(struct us_timer_t *t), int ms, int repeat_ms);
121
+
122
+ /* Returns the loop for this timer */
123
+ struct us_loop_t *us_timer_loop(struct us_timer_t *t);
124
+
125
+ /* Public interfaces for contexts */
126
+
127
+ struct us_socket_context_options_t {
128
+ const char *key_file_name;
129
+ const char *cert_file_name;
130
+ const char *passphrase;
131
+ const char *dh_params_file_name;
132
+ const char *ca_file_name;
133
+ const char *ssl_ciphers;
134
+ int ssl_prefer_low_memory_usage; /* Todo: rename to prefer_low_memory_usage and apply for TCP as well */
135
+ };
136
+
137
+ /* Return 15-bit timestamp for this context */
138
+ unsigned short us_socket_context_timestamp(int ssl, struct us_socket_context_t *context);
139
+
140
+ /* Adds SNI domain and cert in asn1 format */
141
+ void us_socket_context_add_server_name(int ssl, struct us_socket_context_t *context, const char *hostname_pattern, struct us_socket_context_options_t options, void *user);
142
+ void us_socket_context_remove_server_name(int ssl, struct us_socket_context_t *context, const char *hostname_pattern);
143
+ void us_socket_context_on_server_name(int ssl, struct us_socket_context_t *context, void (*cb)(struct us_socket_context_t *, const char *hostname));
144
+ void *us_socket_server_name_userdata(int ssl, struct us_socket_t *s);
145
+ void *us_socket_context_find_server_name_userdata(int ssl, struct us_socket_context_t *context, const char *hostname_pattern);
146
+
147
+ /* Returns the underlying SSL native handle, such as SSL_CTX or nullptr */
148
+ void *us_socket_context_get_native_handle(int ssl, struct us_socket_context_t *context);
149
+
150
+ /* A socket context holds shared callbacks and user data extension for associated sockets */
151
+ struct us_socket_context_t *us_create_socket_context(int ssl, struct us_loop_t *loop,
152
+ int ext_size, struct us_socket_context_options_t options);
153
+
154
+ /* Delete resources allocated at creation time. */
155
+ void us_socket_context_free(int ssl, struct us_socket_context_t *context);
156
+
157
+ /* Setters of various async callbacks */
158
+ void us_socket_context_on_pre_open(int ssl, struct us_socket_context_t *context,
159
+ LIBUS_SOCKET_DESCRIPTOR (*on_pre_open)(LIBUS_SOCKET_DESCRIPTOR fd));
160
+ void us_socket_context_on_open(int ssl, struct us_socket_context_t *context,
161
+ struct us_socket_t *(*on_open)(struct us_socket_t *s, int is_client, char *ip, int ip_length));
162
+ void us_socket_context_on_close(int ssl, struct us_socket_context_t *context,
163
+ struct us_socket_t *(*on_close)(struct us_socket_t *s, int code, void *reason));
164
+ void us_socket_context_on_data(int ssl, struct us_socket_context_t *context,
165
+ struct us_socket_t *(*on_data)(struct us_socket_t *s, char *data, int length));
166
+ void us_socket_context_on_writable(int ssl, struct us_socket_context_t *context,
167
+ struct us_socket_t *(*on_writable)(struct us_socket_t *s));
168
+ void us_socket_context_on_timeout(int ssl, struct us_socket_context_t *context,
169
+ struct us_socket_t *(*on_timeout)(struct us_socket_t *s));
170
+ void us_socket_context_on_long_timeout(int ssl, struct us_socket_context_t *context,
171
+ struct us_socket_t *(*on_timeout)(struct us_socket_t *s));
172
+ /* This one is only used for when a connecting socket fails in a late stage. */
173
+ void us_socket_context_on_connect_error(int ssl, struct us_socket_context_t *context,
174
+ struct us_socket_t *(*on_connect_error)(struct us_socket_t *s, int code));
175
+
176
+ /* Emitted when a socket has been half-closed */
177
+ void us_socket_context_on_end(int ssl, struct us_socket_context_t *context, struct us_socket_t *(*on_end)(struct us_socket_t *s));
178
+
179
+ /* Returns user data extension for this socket context */
180
+ void *us_socket_context_ext(int ssl, struct us_socket_context_t *context);
181
+
182
+ /* Closes all open sockets, including listen sockets. Does not invalidate the socket context. */
183
+ void us_socket_context_close(int ssl, struct us_socket_context_t *context);
184
+
185
+ /* Listen for connections. Acts as the main driving cog in a server. Will call set async callbacks. */
186
+ struct us_listen_socket_t *us_socket_context_listen(int ssl, struct us_socket_context_t *context,
187
+ const char *host, int port, int options, int socket_ext_size);
188
+
189
+ struct us_listen_socket_t *us_socket_context_listen_unix(int ssl, struct us_socket_context_t *context,
190
+ const char *path, int options, int socket_ext_size);
191
+
192
+ /* listen_socket.c/.h */
193
+ void us_listen_socket_close(int ssl, struct us_listen_socket_t *ls);
194
+
195
+ /* Adopt a socket which was accepted either internally, or from another accept() outside libusockets */
196
+ struct us_socket_t *us_adopt_accepted_socket(int ssl, struct us_socket_context_t *context, LIBUS_SOCKET_DESCRIPTOR client_fd,
197
+ unsigned int socket_ext_size, char *addr_ip, int addr_ip_length);
198
+
199
+ /* Land in on_open or on_connection_error or return null or return socket */
200
+ struct us_socket_t *us_socket_context_connect(int ssl, struct us_socket_context_t *context,
201
+ const char *host, int port, const char *source_host, int options, int socket_ext_size);
202
+
203
+ struct us_socket_t *us_socket_context_connect_unix(int ssl, struct us_socket_context_t *context,
204
+ const char *server_path, int options, int socket_ext_size);
205
+
206
+ /* Is this socket established? Can be used to check if a connecting socket has fired the on_open event yet.
207
+ * Can also be used to determine if a socket is a listen_socket or not, but you probably know that already. */
208
+ int us_socket_is_established(int ssl, struct us_socket_t *s);
209
+
210
+ /* Cancel a connecting socket. Can be used together with us_socket_timeout to limit connection times.
211
+ * Entirely destroys the socket - this function works like us_socket_close but does not trigger on_close event since
212
+ * you never got the on_open event first. */
213
+ struct us_socket_t *us_socket_close_connecting(int ssl, struct us_socket_t *s);
214
+
215
+ /* Returns the loop for this socket context. */
216
+ struct us_loop_t *us_socket_context_loop(int ssl, struct us_socket_context_t *context);
217
+
218
+ /* Invalidates passed socket, returning a new resized socket which belongs to a different socket context.
219
+ * Used mainly for "socket upgrades" such as when transitioning from HTTP to WebSocket. */
220
+ struct us_socket_t *us_socket_context_adopt_socket(int ssl, struct us_socket_context_t *context, struct us_socket_t *s, int ext_size);
221
+
222
+ /* Create a child socket context which acts much like its own socket context with its own callbacks yet still relies on the
223
+ * parent socket context for some shared resources. Child socket contexts should be used together with socket adoptions and nothing else. */
224
+ struct us_socket_context_t *us_create_child_socket_context(int ssl, struct us_socket_context_t *context, int context_ext_size);
225
+
226
+ /* Public interfaces for loops */
227
+
228
+ /* Returns a new event loop with user data extension */
229
+ struct us_loop_t *us_create_loop(void *hint, void (*wakeup_cb)(struct us_loop_t *loop),
230
+ void (*pre_cb)(struct us_loop_t *loop), void (*post_cb)(struct us_loop_t *loop), unsigned int ext_size);
231
+
232
+ /* Frees the loop immediately */
233
+ void us_loop_free(struct us_loop_t *loop);
234
+
235
+ /* Returns the loop user data extension */
236
+ void *us_loop_ext(struct us_loop_t *loop);
237
+
238
+ /* Blocks the calling thread and drives the event loop until no more non-fallthrough polls are scheduled */
239
+ void us_loop_run(struct us_loop_t *loop);
240
+
241
+ /* Signals the loop from any thread to wake up and execute its wakeup handler from the loop's own running thread.
242
+ * This is the only fully thread-safe function and serves as the basis for thread safety */
243
+ void us_wakeup_loop(struct us_loop_t *loop);
244
+
245
+ /* Hook up timers in existing loop */
246
+ void us_loop_integrate(struct us_loop_t *loop);
247
+
248
+ /* Returns the loop iteration number */
249
+ long long us_loop_iteration_number(struct us_loop_t *loop);
250
+
251
+ /* Public interfaces for polls */
252
+
253
+ /* A fallthrough poll does not keep the loop running, it falls through */
254
+ struct us_poll_t *us_create_poll(struct us_loop_t *loop, int fallthrough, unsigned int ext_size);
255
+
256
+ /* After stopping a poll you must manually free the memory */
257
+ void us_poll_free(struct us_poll_t *p, struct us_loop_t *loop);
258
+
259
+ /* Associate this poll with a socket descriptor and poll type */
260
+ void us_poll_init(struct us_poll_t *p, LIBUS_SOCKET_DESCRIPTOR fd, int poll_type);
261
+
262
+ /* Start, change and stop polling for events */
263
+ void us_poll_start(struct us_poll_t *p, struct us_loop_t *loop, int events);
264
+ void us_poll_change(struct us_poll_t *p, struct us_loop_t *loop, int events);
265
+ void us_poll_stop(struct us_poll_t *p, struct us_loop_t *loop);
266
+
267
+ /* Return what events we are polling for */
268
+ int us_poll_events(struct us_poll_t *p);
269
+
270
+ /* Returns the user data extension of this poll */
271
+ void *us_poll_ext(struct us_poll_t *p);
272
+
273
+ /* Get associated socket descriptor from a poll */
274
+ LIBUS_SOCKET_DESCRIPTOR us_poll_fd(struct us_poll_t *p);
275
+
276
+ /* Resize an active poll */
277
+ struct us_poll_t *us_poll_resize(struct us_poll_t *p, struct us_loop_t *loop, unsigned int ext_size);
278
+
279
+ /* Public interfaces for sockets */
280
+
281
+ /* Returns the underlying native handle for a socket, such as SSL or file descriptor.
282
+ * In the case of file descriptor, the value of pointer is fd. */
283
+ void *us_socket_get_native_handle(int ssl, struct us_socket_t *s);
284
+
285
+ /* Write up to length bytes of data. Returns actual bytes written.
286
+ * Will call the on_writable callback of active socket context on failure to write everything off in one go.
287
+ * Set hint msg_more if you have more immediate data to write. */
288
+ int us_socket_write(int ssl, struct us_socket_t *s, const char *data, int length, int msg_more);
289
+
290
+ /* Special path for non-SSL sockets. Used to send header and payload in one go. Works like us_socket_write. */
291
+ int us_socket_write2(int ssl, struct us_socket_t *s, const char *header, int header_length, const char *payload, int payload_length);
292
+
293
+ /* Set a low precision, high performance timer on a socket. A socket can only have one single active timer
294
+ * at any given point in time. Will remove any such pre set timer */
295
+ void us_socket_timeout(int ssl, struct us_socket_t *s, unsigned int seconds);
296
+
297
+ /* Set a low precision, high performance timer on a socket. Suitable for per-minute precision. */
298
+ void us_socket_long_timeout(int ssl, struct us_socket_t *s, unsigned int minutes);
299
+
300
+ /* Return the user data extension of this socket */
301
+ void *us_socket_ext(int ssl, struct us_socket_t *s);
302
+
303
+ /* Return the socket context of this socket */
304
+ struct us_socket_context_t *us_socket_context(int ssl, struct us_socket_t *s);
305
+
306
+ /* Withdraw any msg_more status and flush any pending data */
307
+ void us_socket_flush(int ssl, struct us_socket_t *s);
308
+
309
+ /* Shuts down the connection by sending FIN and/or close_notify */
310
+ void us_socket_shutdown(int ssl, struct us_socket_t *s);
311
+
312
+ /* Shuts down the connection in terms of read, meaning next event loop
313
+ * iteration will catch the socket being closed. Can be used to defer closing
314
+ * to next event loop iteration. */
315
+ void us_socket_shutdown_read(int ssl, struct us_socket_t *s);
316
+
317
+ /* Returns whether the socket has been shut down or not */
318
+ int us_socket_is_shut_down(int ssl, struct us_socket_t *s);
319
+
320
+ /* Returns whether this socket has been closed. Only valid if memory has not yet been released. */
321
+ int us_socket_is_closed(int ssl, struct us_socket_t *s);
322
+
323
+ /* Immediately closes the socket */
324
+ struct us_socket_t *us_socket_close(int ssl, struct us_socket_t *s, int code, void *reason);
325
+
326
+ /* Returns local port or -1 on failure. */
327
+ int us_socket_local_port(int ssl, struct us_socket_t *s);
328
+
329
+ /* Returns remote ephemeral port or -1 on failure. */
330
+ int us_socket_remote_port(int ssl, struct us_socket_t *s);
331
+
332
+ /* Copy remote (IP) address of socket, or fail with zero length. */
333
+ void us_socket_remote_address(int ssl, struct us_socket_t *s, char *buf, int *length);
334
+
335
+ #ifdef __cplusplus
336
+ }
337
+ #endif
338
+
339
+ /* Decide what eventing system to use by default */
340
+ #if !defined(LIBUS_USE_IO_URING) && !defined(LIBUS_USE_EPOLL) && !defined(LIBUS_USE_LIBUV) && !defined(LIBUS_USE_GCD) && !defined(LIBUS_USE_KQUEUE) && !defined(LIBUS_USE_ASIO)
341
+ #if defined(_WIN32)
342
+ #define LIBUS_USE_LIBUV
343
+ #elif defined(__APPLE__) || defined(__FreeBSD__)
344
+ #define LIBUS_USE_KQUEUE
345
+ #else
346
+ #define LIBUS_USE_EPOLL
347
+ #endif
348
+ #endif
349
+
350
+ #endif // LIBUSOCKETS_H