maph 1.0.0 High-Performance Memory-Mapped Key-Value Database with O(1) Lookups

Loading...
Searching...
No Matches
internal.h
Go to the documentation of this file.
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 "internal/networking/bsd.h"
29
30/* We have many different eventing implementations */
31#if defined(LIBUS_USE_EPOLL) || defined(LIBUS_USE_KQUEUE)
32#include "internal/eventing/epoll_kqueue.h"
33#endif
34#ifdef LIBUS_USE_LIBUV
35#include "internal/eventing/libuv.h"
36#endif
37#ifdef LIBUS_USE_GCD
38#include "internal/eventing/gcd.h"
39#endif
40#ifdef LIBUS_USE_ASIO
41#include "internal/eventing/asio.h"
42#endif
43
44/* Poll type and what it polls for */
45 enum {
46 /* Two first bits */
47 POLL_TYPE_SOCKET = 0,
48 POLL_TYPE_SOCKET_SHUT_DOWN = 1,
49 POLL_TYPE_SEMI_SOCKET = 2,
50 POLL_TYPE_CALLBACK = 3,
51
52 /* Two last bits */
53 POLL_TYPE_POLLING_OUT = 4,
54 POLL_TYPE_POLLING_IN = 8
55};
56
57/* Loop related */
58 void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int events);
59 void us_internal_timer_sweep(struct us_loop_t *loop);
60 void us_internal_free_closed_sockets(struct us_loop_t *loop);
61 void us_internal_loop_link(struct us_loop_t *loop, struct us_socket_context_t *context);
62 void us_internal_loop_unlink(struct us_loop_t *loop, struct us_socket_context_t *context);
63 void us_internal_loop_data_init(struct us_loop_t *loop, void (*wakeup_cb)(struct us_loop_t *loop),
64 void (*pre_cb)(struct us_loop_t *loop), void (*post_cb)(struct us_loop_t *loop));
65 void us_internal_loop_data_free(struct us_loop_t *loop);
66 void us_internal_loop_pre(struct us_loop_t *loop);
67 void us_internal_loop_post(struct us_loop_t *loop);
68
69/* Asyncs (old) */
70 struct us_internal_async *us_internal_create_async(struct us_loop_t *loop, int fallthrough, unsigned int ext_size);
71 void us_internal_async_close(struct us_internal_async *a);
72 void us_internal_async_set(struct us_internal_async *a, void (*cb)(struct us_internal_async *));
73 void us_internal_async_wakeup(struct us_internal_async *a);
74
75/* Eventing related */
76 unsigned int us_internal_accept_poll_event(struct us_poll_t *p);
77 int us_internal_poll_type(struct us_poll_t *p);
78 void us_internal_poll_set_type(struct us_poll_t *p, int poll_type);
79
80/* SSL loop data */
81 void us_internal_init_loop_ssl_data(struct us_loop_t *loop);
82 void us_internal_free_loop_ssl_data(struct us_loop_t *loop);
83
84/* Socket context related */
85 void us_internal_socket_context_link_socket(struct us_socket_context_t *context, struct us_socket_t *s);
86 void us_internal_socket_context_unlink_socket(struct us_socket_context_t *context, struct us_socket_t *s);
87
88/* Sockets are polls */
89 struct us_socket_t {
90 alignas(LIBUS_EXT_ALIGNMENT) struct us_poll_t p; // 4 bytes
91 unsigned char timeout; // 1 byte
92 unsigned char long_timeout; // 1 byte
93 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 */
94 struct us_socket_context_t *context;
95 struct us_socket_t *prev, *next;
96};
97
98/* Internal callback types are polls just like sockets */
99 struct us_internal_callback_t {
100 alignas(LIBUS_EXT_ALIGNMENT) struct us_poll_t p;
101 struct us_loop_t *loop;
102 int cb_expects_the_loop;
103 int leave_poll_ready;
104 void (*cb)(struct us_internal_callback_t *cb);
105};
106
107/* Listen sockets are sockets */
108 struct us_listen_socket_t {
109 alignas(LIBUS_EXT_ALIGNMENT) struct us_socket_t s;
110 unsigned int socket_ext_size;
111};
112
113/* Listen sockets are keps in their own list */
114 void us_internal_socket_context_link_listen_socket(struct us_socket_context_t *context, struct us_listen_socket_t *s);
115 void us_internal_socket_context_unlink_listen_socket(struct us_socket_context_t *context, struct us_listen_socket_t *s);
116
117 struct us_socket_context_t {
118 alignas(LIBUS_EXT_ALIGNMENT) struct us_loop_t *loop;
119 uint32_t global_tick;
120 unsigned char timestamp;
121 unsigned char long_timestamp;
122 struct us_socket_t *head_sockets;
123 struct us_listen_socket_t *head_listen_sockets;
124 struct us_socket_t *iterator;
125 struct us_socket_context_t *prev, *next;
126
127 struct us_socket_t *(*on_open)(struct us_socket_t *, int is_client, char *ip, int ip_length);
128 struct us_socket_t *(*on_data)(struct us_socket_t *, char *data, int length);
129 struct us_socket_t *(*on_writable)(struct us_socket_t *);
130 struct us_socket_t *(*on_close)(struct us_socket_t *, int code, void *reason);
131 //void (*on_timeout)(struct us_socket_context *);
132 struct us_socket_t *(*on_socket_timeout)(struct us_socket_t *);
133 struct us_socket_t *(*on_socket_long_timeout)(struct us_socket_t *);
134 struct us_socket_t *(*on_end)(struct us_socket_t *);
135 struct us_socket_t *(*on_connect_error)(struct us_socket_t *, int code);
136 int (*is_low_prio)(struct us_socket_t *);
137};
138
139/* Internal SSL interface */
140#ifndef LIBUS_NO_SSL
141
142 struct us_internal_ssl_socket_context_t;
143 struct us_internal_ssl_socket_t;
144
145/* SNI functions */
146 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);
147 void us_internal_ssl_socket_context_remove_server_name(struct us_internal_ssl_socket_context_t *context, const char *hostname_pattern);
148 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 *));
149 void *us_internal_ssl_socket_get_sni_userdata(struct us_internal_ssl_socket_t *s);
150 void *us_internal_ssl_socket_context_find_server_name_userdata(struct us_internal_ssl_socket_context_t *context, const char *hostname_pattern);
151
152 void *us_internal_ssl_socket_get_native_handle(struct us_internal_ssl_socket_t *s);
153 void *us_internal_ssl_socket_context_get_native_handle(struct us_internal_ssl_socket_context_t *context);
154
155 struct us_internal_ssl_socket_context_t *us_internal_create_ssl_socket_context(struct us_loop_t *loop,
156 int context_ext_size, struct us_socket_context_options_t options);
157
158 void us_internal_ssl_socket_context_free(struct us_internal_ssl_socket_context_t *context);
159 void us_internal_ssl_socket_context_on_open(struct us_internal_ssl_socket_context_t *context,
160 struct us_internal_ssl_socket_t *(*on_open)(struct us_internal_ssl_socket_t *s, int is_client, char *ip, int ip_length));
161
162 void us_internal_ssl_socket_context_on_close(struct us_internal_ssl_socket_context_t *context,
163 struct us_internal_ssl_socket_t *(*on_close)(struct us_internal_ssl_socket_t *s, int code, void *reason));
164
165 void us_internal_ssl_socket_context_on_data(struct us_internal_ssl_socket_context_t *context,
166 struct us_internal_ssl_socket_t *(*on_data)(struct us_internal_ssl_socket_t *s, char *data, int length));
167
168 void us_internal_ssl_socket_context_on_writable(struct us_internal_ssl_socket_context_t *context,
169 struct us_internal_ssl_socket_t *(*on_writable)(struct us_internal_ssl_socket_t *s));
170
171 void us_internal_ssl_socket_context_on_timeout(struct us_internal_ssl_socket_context_t *context,
172 struct us_internal_ssl_socket_t *(*on_timeout)(struct us_internal_ssl_socket_t *s));
173
174 void us_internal_ssl_socket_context_on_long_timeout(struct us_internal_ssl_socket_context_t *context,
175 struct us_internal_ssl_socket_t *(*on_timeout)(struct us_internal_ssl_socket_t *s));
176
177 void us_internal_ssl_socket_context_on_end(struct us_internal_ssl_socket_context_t *context,
178 struct us_internal_ssl_socket_t *(*on_end)(struct us_internal_ssl_socket_t *s));
179
180 void us_internal_ssl_socket_context_on_connect_error(struct us_internal_ssl_socket_context_t *context,
181 struct us_internal_ssl_socket_t *(*on_connect_error)(struct us_internal_ssl_socket_t *s, int code));
182
183 struct us_listen_socket_t *us_internal_ssl_socket_context_listen(struct us_internal_ssl_socket_context_t *context,
184 const char *host, int port, int options, int socket_ext_size);
185
186 struct us_listen_socket_t *us_internal_ssl_socket_context_listen_unix(struct us_internal_ssl_socket_context_t *context,
187 const char *path, int options, int socket_ext_size);
188
189 struct us_internal_ssl_socket_t *us_internal_ssl_socket_context_connect(struct us_internal_ssl_socket_context_t *context,
190 const char *host, int port, const char *source_host, int options, int socket_ext_size);
191
192
193 struct us_internal_ssl_socket_t *us_internal_ssl_socket_context_connect_unix(struct us_internal_ssl_socket_context_t *context,
194 const char *server_path, int options, int socket_ext_size);
195
196 int us_internal_ssl_socket_write(struct us_internal_ssl_socket_t *s, const char *data, int length, int msg_more);
197 void us_internal_ssl_socket_timeout(struct us_internal_ssl_socket_t *s, unsigned int seconds);
198 void *us_internal_ssl_socket_context_ext(struct us_internal_ssl_socket_context_t *s);
199 struct us_internal_ssl_socket_context_t *us_internal_ssl_socket_get_context(struct us_internal_ssl_socket_t *s);
200 void *us_internal_ssl_socket_ext(struct us_internal_ssl_socket_t *s);
201 int us_internal_ssl_socket_is_shut_down(struct us_internal_ssl_socket_t *s);
202 void us_internal_ssl_socket_shutdown(struct us_internal_ssl_socket_t *s);
203
204 struct us_internal_ssl_socket_t *us_internal_ssl_socket_context_adopt_socket(struct us_internal_ssl_socket_context_t *context,
205 struct us_internal_ssl_socket_t *s, int ext_size);
206
207 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);
208 struct us_loop_t *us_internal_ssl_socket_context_loop(struct us_internal_ssl_socket_context_t *context);
209
210#endif
211
212#endif // INTERNAL_H
asio.h
bsd.h
epoll_kqueue.h
gcd.h
us_internal_ssl_socket_context_free
void us_internal_ssl_socket_context_free(struct us_internal_ssl_socket_context_t *context)
us_internal_poll_set_type
void us_internal_poll_set_type(struct us_poll_t *p, int poll_type)
us_internal_loop_unlink
void us_internal_loop_unlink(struct us_loop_t *loop, struct us_socket_context_t *context)
Definition loop.c:68
us_internal_init_loop_ssl_data
void us_internal_init_loop_ssl_data(struct us_loop_t *loop)
us_internal_async_wakeup
void us_internal_async_wakeup(struct us_internal_async *a)
us_listen_socket_t::s
struct us_socket_t s
Definition internal.h:109
us_internal_socket_context_link_listen_socket
void us_internal_socket_context_link_listen_socket(struct us_socket_context_t *context, struct us_listen_socket_t *s)
Definition context.c:110
us_internal_ssl_socket_context_on_connect_error
void us_internal_ssl_socket_context_on_connect_error(struct us_internal_ssl_socket_context_t *context, struct us_internal_ssl_socket_t *(*on_connect_error)(struct us_internal_ssl_socket_t *s, int code))
us_socket_t::p
struct us_poll_t p
Definition internal.h:90
us_internal_timer_sweep
void us_internal_timer_sweep(struct us_loop_t *loop)
Definition loop.c:83
us_internal_ssl_socket_context_adopt_socket
struct us_internal_ssl_socket_t * us_internal_ssl_socket_context_adopt_socket(struct us_internal_ssl_socket_context_t *context, struct us_internal_ssl_socket_t *s, int ext_size)
us_internal_ssl_socket_ext
void * us_internal_ssl_socket_ext(struct us_internal_ssl_socket_t *s)
us_internal_ssl_socket_context_on_close
void us_internal_ssl_socket_context_on_close(struct us_internal_ssl_socket_context_t *context, struct us_internal_ssl_socket_t *(*on_close)(struct us_internal_ssl_socket_t *s, int code, void *reason))
us_internal_async_set
void us_internal_async_set(struct us_internal_async *a, void(*cb)(struct us_internal_async *))
us_socket_t::long_timeout
unsigned char long_timeout
Definition internal.h:92
us_internal_loop_post
void us_internal_loop_post(struct us_loop_t *loop)
Definition loop.c:190
us_socket_t::next
struct us_socket_t * next
Definition internal.h:95
us_internal_ssl_socket_get_native_handle
void * us_internal_ssl_socket_get_native_handle(struct us_internal_ssl_socket_t *s)
us_internal_ssl_socket_context_connect_unix
struct us_internal_ssl_socket_t * us_internal_ssl_socket_context_connect_unix(struct us_internal_ssl_socket_context_t *context, const char *server_path, int options, int socket_ext_size)
us_internal_ssl_socket_context_remove_server_name
void us_internal_ssl_socket_context_remove_server_name(struct us_internal_ssl_socket_context_t *context, const char *hostname_pattern)
us_internal_ssl_socket_context_on_timeout
void us_internal_ssl_socket_context_on_timeout(struct us_internal_ssl_socket_context_t *context, struct us_internal_ssl_socket_t *(*on_timeout)(struct us_internal_ssl_socket_t *s))
us_internal_ssl_socket_timeout
void us_internal_ssl_socket_timeout(struct us_internal_ssl_socket_t *s, unsigned int seconds)
us_internal_create_child_ssl_socket_context
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)
us_internal_loop_data_free
void us_internal_loop_data_free(struct us_loop_t *loop)
Definition loop.c:42
us_internal_ssl_socket_context_listen
struct us_listen_socket_t * us_internal_ssl_socket_context_listen(struct us_internal_ssl_socket_context_t *context, const char *host, int port, int options, int socket_ext_size)
us_internal_create_ssl_socket_context
struct us_internal_ssl_socket_context_t * us_internal_create_ssl_socket_context(struct us_loop_t *loop, int context_ext_size, struct us_socket_context_options_t options)
us_internal_ssl_socket_context_on_data
void us_internal_ssl_socket_context_on_data(struct us_internal_ssl_socket_context_t *context, struct us_internal_ssl_socket_t *(*on_data)(struct us_internal_ssl_socket_t *s, char *data, int length))
us_internal_ssl_socket_context_add_server_name
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)
us_internal_async_close
void us_internal_async_close(struct us_internal_async *a)
us_internal_ssl_socket_context_listen_unix
struct us_listen_socket_t * us_internal_ssl_socket_context_listen_unix(struct us_internal_ssl_socket_context_t *context, const char *path, int options, int socket_ext_size)
us_internal_ssl_socket_get_sni_userdata
void * us_internal_ssl_socket_get_sni_userdata(struct us_internal_ssl_socket_t *s)
us_internal_ssl_socket_context_on_long_timeout
void us_internal_ssl_socket_context_on_long_timeout(struct us_internal_ssl_socket_context_t *context, struct us_internal_ssl_socket_t *(*on_timeout)(struct us_internal_ssl_socket_t *s))
us_socket_t::timeout
unsigned char timeout
Definition internal.h:91
us_internal_create_async
struct us_internal_async * us_internal_create_async(struct us_loop_t *loop, int fallthrough, unsigned int ext_size)
us_socket_t::prev
struct us_socket_t * prev
Definition internal.h:95
us_internal_ssl_socket_get_context
struct us_internal_ssl_socket_context_t * us_internal_ssl_socket_get_context(struct us_internal_ssl_socket_t *s)
us_internal_poll_type
int us_internal_poll_type(struct us_poll_t *p)
us_internal_loop_link
void us_internal_loop_link(struct us_loop_t *loop, struct us_socket_context_t *context)
Definition loop.c:57
us_internal_loop_pre
void us_internal_loop_pre(struct us_loop_t *loop)
Definition loop.c:184
us_internal_socket_context_unlink_socket
void us_internal_socket_context_unlink_socket(struct us_socket_context_t *context, struct us_socket_t *s)
Definition context.c:89
us_socket_t::low_prio_state
unsigned short low_prio_state
Definition internal.h:93
us_socket_t::context
struct us_socket_context_t * context
Definition internal.h:94
us_internal_ssl_socket_context_ext
void * us_internal_ssl_socket_context_ext(struct us_internal_ssl_socket_context_t *s)
us_internal_free_loop_ssl_data
void us_internal_free_loop_ssl_data(struct us_loop_t *loop)
us_internal_ssl_socket_context_loop
struct us_loop_t * us_internal_ssl_socket_context_loop(struct us_internal_ssl_socket_context_t *context)
us_internal_ssl_socket_context_find_server_name_userdata
void * us_internal_ssl_socket_context_find_server_name_userdata(struct us_internal_ssl_socket_context_t *context, const char *hostname_pattern)
us_internal_ssl_socket_is_shut_down
int us_internal_ssl_socket_is_shut_down(struct us_internal_ssl_socket_t *s)
us_internal_ssl_socket_shutdown
void us_internal_ssl_socket_shutdown(struct us_internal_ssl_socket_t *s)
us_internal_dispatch_ready_poll
void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int events)
Definition loop.c:195
us_listen_socket_t::socket_ext_size
unsigned int socket_ext_size
Definition internal.h:110
us_internal_ssl_socket_context_on_writable
void us_internal_ssl_socket_context_on_writable(struct us_internal_ssl_socket_context_t *context, struct us_internal_ssl_socket_t *(*on_writable)(struct us_internal_ssl_socket_t *s))
POLL_TYPE_CALLBACK
@ POLL_TYPE_CALLBACK
Definition internal.h:50
POLL_TYPE_POLLING_IN
@ POLL_TYPE_POLLING_IN
Definition internal.h:54
POLL_TYPE_SEMI_SOCKET
@ POLL_TYPE_SEMI_SOCKET
Definition internal.h:49
POLL_TYPE_POLLING_OUT
@ POLL_TYPE_POLLING_OUT
Definition internal.h:53
POLL_TYPE_SOCKET
@ POLL_TYPE_SOCKET
Definition internal.h:47
POLL_TYPE_SOCKET_SHUT_DOWN
@ POLL_TYPE_SOCKET_SHUT_DOWN
Definition internal.h:48
us_internal_ssl_socket_context_on_server_name
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 *))
us_internal_ssl_socket_context_get_native_handle
void * us_internal_ssl_socket_context_get_native_handle(struct us_internal_ssl_socket_context_t *context)
us_internal_ssl_socket_context_on_end
void us_internal_ssl_socket_context_on_end(struct us_internal_ssl_socket_context_t *context, struct us_internal_ssl_socket_t *(*on_end)(struct us_internal_ssl_socket_t *s))
us_internal_free_closed_sockets
void us_internal_free_closed_sockets(struct us_loop_t *loop)
Definition loop.c:163
us_internal_socket_context_link_socket
void us_internal_socket_context_link_socket(struct us_socket_context_t *context, struct us_socket_t *s)
Definition context.c:121
us_internal_accept_poll_event
unsigned int us_internal_accept_poll_event(struct us_poll_t *p)
us_internal_ssl_socket_context_on_open
void us_internal_ssl_socket_context_on_open(struct us_internal_ssl_socket_context_t *context, struct us_internal_ssl_socket_t *(*on_open)(struct us_internal_ssl_socket_t *s, int is_client, char *ip, int ip_length))
us_internal_loop_data_init
void us_internal_loop_data_init(struct us_loop_t *loop, void(*wakeup_cb)(struct us_loop_t *loop), void(*pre_cb)(struct us_loop_t *loop), void(*post_cb)(struct us_loop_t *loop))
Definition loop.c:23
us_internal_ssl_socket_write
int us_internal_ssl_socket_write(struct us_internal_ssl_socket_t *s, const char *data, int length, int msg_more)
us_internal_socket_context_unlink_listen_socket
void us_internal_socket_context_unlink_listen_socket(struct us_socket_context_t *context, struct us_listen_socket_t *s)
Definition context.c:69
us_internal_ssl_socket_context_connect
struct us_internal_ssl_socket_t * us_internal_ssl_socket_context_connect(struct us_internal_ssl_socket_context_t *context, const char *host, int port, const char *source_host, int options, int socket_ext_size)
us_listen_socket_t
Definition internal.h:108
us_socket_t
Definition internal.h:89
LIBUS_EXT_ALIGNMENT
#define LIBUS_EXT_ALIGNMENT
Definition libusockets.h:28
us_socket_context_options_t
Definition libusockets.h:124
libuv.h
us_poll_t
Definition asio.h:37
us_loop_t
Definition asio.h:26
us_internal_callback_t
Definition internal.h:99
us_internal_callback_t::loop
struct us_loop_t * loop
Definition internal.h:101
us_internal_callback_t::cb_expects_the_loop
int cb_expects_the_loop
Definition internal.h:102
us_internal_callback_t::cb
void(* cb)(struct us_internal_callback_t *cb)
Definition internal.h:104
us_internal_callback_t::leave_poll_ready
int leave_poll_ready
Definition internal.h:103
us_internal_callback_t::p
struct us_poll_t p
Definition internal.h:100
us_socket_context_t
Definition internal.h:117
us_socket_context_t::head_sockets
struct us_socket_t * head_sockets
Definition internal.h:122
us_socket_context_t::long_timestamp
unsigned char long_timestamp
Definition internal.h:121
us_socket_context_t::timestamp
unsigned char timestamp
Definition internal.h:120
us_socket_context_t::is_low_prio
int(* is_low_prio)(struct us_socket_t *)
Definition internal.h:136
us_socket_context_t::loop
struct us_loop_t * loop
Definition internal.h:118
us_socket_context_t::prev
struct us_socket_context_t * prev
Definition internal.h:125
us_socket_context_t::head_listen_sockets
struct us_listen_socket_t * head_listen_sockets
Definition internal.h:123
us_socket_context_t::iterator
struct us_socket_t * iterator
Definition internal.h:124
us_socket_context_t::next
struct us_socket_context_t * next
Definition internal.h:125
us_socket_context_t::global_tick
uint32_t global_tick
Definition internal.h:119