rbsrt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/ext/rbsrt/rbsrt.h ADDED
@@ -0,0 +1,265 @@
1
+ /*
2
+ * Ruby SRT - Ruby binding for Secure, Reliable, Transport
3
+ * Copyright (c) 2019 Klaas Speller, Recce.
4
+ *
5
+ * This Source Code Form is subject to the terms of the Mozilla Public
6
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
7
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
+ *
9
+ */
10
+
11
+ /**
12
+ * A Ruby wrapper for SRT (Secure Reliable Transport)
13
+ *
14
+ * @author: Klaas Speller <klaas@recce.nl>
15
+ */
16
+
17
+ #ifndef RBSRT_HEADER
18
+ #define RBSRT_HEADER
19
+
20
+ #include <stdatomic.h>
21
+
22
+ #include <ruby/ruby.h>
23
+ #include <srt/srt.h>
24
+
25
+
26
+ // MARK: - Utils
27
+
28
+ // MARK: - Status
29
+
30
+ #define RBSRT_SUCCESS 0
31
+ #define RBSRT_FAILURE 1
32
+
33
+
34
+ // MARK: Debug
35
+
36
+ // #define RBSRT_HAVE_DEBUG
37
+
38
+ #ifdef RBSRT_HAVE_DEBUG
39
+ #define RBSRT_DEBUG 1
40
+ #endif
41
+
42
+
43
+ #if RBSRT_DEBUG
44
+ #define RBSRT_DEBUG_MSG_MAX 1024
45
+ #define RBSRT_DEBUG_PRINT(...) \
46
+ { \
47
+ char debugmsg__[RBSRT_DEBUG_MSG_MAX]; \
48
+ sprintf(debugmsg__, ## __VA_ARGS__); \
49
+ fprintf(stderr, "rbsrt - %s\n", debugmsg__); \
50
+ } \
51
+
52
+ #define DEBUG_ERROR_PRINT(...) \
53
+ { \
54
+ char debugmsg__[RBSRT_DEBUG_MSG_MAX]; \
55
+ sprintf(debugmsg__, ## __VA_ARGS__); \
56
+ fprintf(stderr, "rbsrt error - %s\n", debugmsg__); \
57
+ }
58
+ #else
59
+ #define RBSRT_DEBUG_PRINT(...)
60
+ #define DEBUG_ERROR_PRINT(...)
61
+ #endif
62
+
63
+
64
+ // MARK: Constants
65
+
66
+ #define RBSRT_PAYLOAD_SIZE 1316
67
+
68
+
69
+ // MARK: - Structs
70
+
71
+ typedef struct RBSRTSocketBase
72
+ {
73
+ SRTSOCKET socket;
74
+ } rbsrt_socket_base_t;
75
+
76
+ typedef struct RBSRTSocket
77
+ {
78
+ SRTSOCKET socket;
79
+ } rbsrt_socket_t;
80
+
81
+ typedef struct RBSRTConnection
82
+ {
83
+ SRTSOCKET socket;
84
+ VALUE at_data_block;
85
+ VALUE at_close_block;
86
+ } rbsrt_connection_t;
87
+
88
+ typedef struct RBSRTServer
89
+ {
90
+ SRTSOCKET socket;
91
+ int flags; // TODO: Deprecate flags
92
+ atomic_size_t num_connections;
93
+ SRT_EPOLL_T epollid;
94
+ VALUE acceptor_block;
95
+ } rbsrt_server_t;
96
+
97
+ typedef struct RBSRTClient
98
+ {
99
+ SRTSOCKET socket;
100
+ int flags; // TODO: Deprecate flags
101
+ } rbsrt_client_t;
102
+
103
+ typedef struct RBSRTPoll
104
+ {
105
+ SRT_EPOLL_T epollid;
106
+ VALUE sockets_by_id;
107
+ } rbsrt_poll_t;
108
+
109
+ typedef struct RBSRTStats
110
+ {
111
+ SRT_TRACEBSTATS perf;
112
+ } rbsrt_stats_t;
113
+
114
+
115
+ // MARK: - Ruby Struct Headers
116
+
117
+ // MARK: SRT::Socket Class
118
+
119
+ size_t rbsrt_socket_dsize(const void *socket);
120
+ void rbsrt_socket_dmark(void *data);
121
+ void rbsrt_socket_deallocate(rbsrt_socket_t *socket);
122
+
123
+ // MARK: SRT::Connection Class
124
+
125
+ size_t rbsrt_connection_dsize(const void *connection);
126
+ void rbsrt_connection_dmark(void *data);
127
+ void rbsrt_connection_deallocate(rbsrt_connection_t *connection);
128
+
129
+ // MARK: SRT::Server Class
130
+
131
+ size_t rbsrt_server_dsize(const void *server);
132
+ void rbsrt_server_dmark(void *data);
133
+ void rbsrt_server_deallocate(rbsrt_server_t *server);
134
+
135
+ // MARK: SRT::Client Class
136
+
137
+ size_t rbsrt_client_dsize(const void *client);
138
+ void rbsrt_client_dmark(void *data);
139
+ void rbsrt_client_deallocate(rbsrt_client_t *client);
140
+
141
+ // MARK: SRT::Poll Class
142
+
143
+ size_t rbsrt_poll_dsize(const void *poll);
144
+ void rbsrt_poll_dmark(void *data);
145
+ void rbsrt_poll_deallocate(rbsrt_poll_t *poll);
146
+
147
+
148
+ // MARK: SRT::Stats Class
149
+
150
+ size_t rbsrt_stats_dsize(const void *stats);
151
+ void rbsrt_stats_dmark(void *data);
152
+ void rbsrt_stats_deallocate(rbsrt_stats_t *stats);
153
+
154
+
155
+ // MARK: - Ruby Structs
156
+
157
+ static const rb_data_type_t rbsrt_socket_rbtype = {
158
+ .wrap_struct_name = "rbsrt_socket",
159
+ .function = {
160
+ .dfree = (void *)rbsrt_socket_deallocate,
161
+ .dsize = rbsrt_socket_dsize,
162
+ .dmark = rbsrt_socket_dmark
163
+ },
164
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
165
+ };
166
+
167
+ static const rb_data_type_t rbsrt_connection_rbtype = {
168
+ .wrap_struct_name = "rbsrt_connection",
169
+ .function = {
170
+ .dfree = (void *)rbsrt_connection_deallocate,
171
+ .dsize = rbsrt_connection_dsize,
172
+ .dmark = rbsrt_connection_dmark,
173
+ },
174
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
175
+ };
176
+
177
+ static const rb_data_type_t rbsrt_server_rbtype = {
178
+ .wrap_struct_name = "rbsrt_server",
179
+ .function = {
180
+ .dfree = (void *)rbsrt_server_deallocate,
181
+ .dsize = rbsrt_server_dsize,
182
+ .dmark = rbsrt_server_dmark
183
+ },
184
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
185
+ };
186
+
187
+ static const rb_data_type_t rbsrt_client_rbtype = {
188
+ .wrap_struct_name = "rbsrt_client",
189
+ .function = {
190
+ .dfree = (void *)rbsrt_client_deallocate,
191
+ .dsize = rbsrt_client_dsize,
192
+ .dmark = rbsrt_client_dmark
193
+ },
194
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
195
+ };
196
+
197
+ static const rb_data_type_t rbsrt_poll_rbtype = {
198
+ .wrap_struct_name = "rbsrt_poll",
199
+ .function = {
200
+ .dfree = (void *)rbsrt_poll_deallocate,
201
+ .dsize = rbsrt_poll_dsize,
202
+ .dmark = rbsrt_poll_dmark
203
+ },
204
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
205
+ };
206
+
207
+ static const rb_data_type_t rbsrt_stats_rbtype = {
208
+ .wrap_struct_name = "rbsrt_stats",
209
+ .function = {
210
+ .dfree = (void *)rbsrt_stats_deallocate,
211
+ .dsize = rbsrt_stats_dsize,
212
+ .dmark = rbsrt_stats_dmark
213
+ },
214
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
215
+ };
216
+
217
+
218
+ // MARK: Unwraps
219
+
220
+ #define RBSRT_IS_SOCKET(socket) (srt_getsockstate(socket->socket) != SRTS_NONEXIST)
221
+
222
+ #define RBSRT_CHECK_SOCKET(input, socket) \
223
+ if (!RBSRT_IS_SOCKET(socket)) \
224
+ { \
225
+ rb_raise(rb_eTypeError, \
226
+ "wrong type %"PRIsVALUE" is not a SRT Socket Type", \
227
+ rb_obj_class(input)); \
228
+ } \
229
+
230
+ // NOTE: Bypassing some safty guards allowing us to use same function for server,
231
+ // connection, socket, etc.
232
+ #define RBSRT_SOCKET_BASE_UNWRAP(input, output) \
233
+ rbsrt_socket_base_t *output = (rbsrt_socket_base_t *)DATA_PTR(input); \
234
+ RBSRT_CHECK_SOCKET(input, output); \
235
+
236
+ #define RBSRT_SOCKET_UNWRAP(input, output) \
237
+ rbsrt_socket_t *output; \
238
+ TypedData_Get_Struct(input, rbsrt_socket_t, &rbsrt_socket_rbtype, output); \
239
+
240
+ #define RBSRT_CONNECTION_UNWRAP(input, output) \
241
+ rbsrt_connection_t *output; \
242
+ TypedData_Get_Struct(input, rbsrt_connection_t, &rbsrt_connection_rbtype, output); \
243
+
244
+ #define RBSRT_SERVER_UNWRAP(input, output) \
245
+ rbsrt_server_t *output; \
246
+ TypedData_Get_Struct(input, rbsrt_server_t, &rbsrt_server_rbtype, output); \
247
+
248
+ #define RBSRT_CLIENT_UNWRAP(input, output) \
249
+ rbsrt_client_t *output; \
250
+ TypedData_Get_Struct(input, rbsrt_client_t, &rbsrt_client_rbtype, output); \
251
+
252
+ #define RBSRT_POLL_UNWRAP(input, output) \
253
+ rbsrt_poll_t *output; \
254
+ TypedData_Get_Struct(input, rbsrt_poll_t, &rbsrt_poll_rbtype, output); \
255
+
256
+ #define RBSRT_STATS_UNWRAP(input, output) \
257
+ rbsrt_stats_t *output; \
258
+ TypedData_Get_Struct(input, rbsrt_stats_t, &rbsrt_stats_rbtype, output); \
259
+
260
+
261
+ // MARK: - Errors
262
+
263
+ _Noreturn void rbsrt_raise_last_srt_error(void);
264
+
265
+ #endif