WebSocket.xs

filter: Statementstmt 77.7% 418 / 538 Branchbran 48.7% 187 / 384 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 65.6% 605 / 922 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« vendor/bq_websocket/bq_websocket.c ↑ Summary blib/lib/Linux/Event/WebSocket.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "stream_consumer_abi.h"
5  
6 #include <stdint.h>
7 #include <string.h>
8  
9 #define BQWS_SINGLE_THREAD 1
10 #define BQWS_DEBUG 0
11 #define bqws_mutex int
12 #define bqws_mutex_init(m) ((void)(m))
13 #define bqws_mutex_free(m) ((void)(m))
14 #define bqws_mutex_lock(m) ((void)(m))
15 #define bqws_mutex_unlock(m) ((void)(m))
16 #define bqws_assert_locked(m) ((void)(m))
17 #include "vendor/bq_websocket/bq_websocket.c"
18  
19 typedef struct {
20 bqws_socket *ws;
21 } lews_bq;
22  
23 static lews_bq *
24 55 lews_bq_create(const char *endpoint_type, UV max_message_size)
25 {
26 lews_bq *state;
27 bqws_opts opts;
28  
29 55 Newxz(state, 1, lews_bq);
30 55 Zero(&opts, 1, bqws_opts);
31  
32 55 opts.skip_handshake = true;
33 55 opts.recv_control_messages = true;
34 55 opts.ping_interval = SIZE_MAX;
35 55 opts.connect_timeout = SIZE_MAX;
36 55 opts.close_timeout = SIZE_MAX;
37 55 opts.ping_response_timeout = SIZE_MAX;
38 55 opts.limits.max_memory_used = SIZE_MAX;
39 55 opts.limits.max_recv_msg_size =
40 55 (size_t)(max_message_size < 125 ? 125 : max_message_size);
41 55 opts.limits.max_recv_queue_messages = SIZE_MAX;
42 55 opts.limits.max_recv_queue_size = SIZE_MAX;
43 55 opts.limits.max_partial_message_parts = SIZE_MAX;
44  
45 55 ▶ if (strEQ(endpoint_type, "client")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(endpoint_type, "client")) { 13 42 55
46 13 state->ws = bqws_new_client(&opts, NULL);
47 42 ▶ } else if (strEQ(endpoint_type, "server")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(endpoint_type, "server")) { 42 0 42
48 42 state->ws = bqws_new_server(&opts, NULL);
49 } else {
50 0 Safefree(state);
51 0 return NULL;
52 }
53  
54 55 ▶ if (state->ws == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->ws == NULL) { 0 55 55
55 0 Safefree(state);
56 0 return NULL;
57 }
58  
59 55 return state;
60 }
61  
62 static void
63 55 lews_bq_free(lews_bq *state)
64 {
65 55 ▶ if (state == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (state == NULL) 0 55 55
66 0 return;
67 55 ▶ if (state->ws != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->ws != NULL) { 55 0 55
68 55 bqws_free_socket(state->ws);
69 55 state->ws = NULL;
70 }
71 55 Safefree(state);
72 }
73  
74 static SV *
75 55 lews_bq_new_object(
76 const char *class,
77 const char *endpoint_type,
78 UV max_message_size
79 )
80 {
81 55 lews_bq *state = lews_bq_create(endpoint_type, max_message_size);
82 SV *object;
83  
84 55 ▶ if (state == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (state == NULL) 0 55 55
85 0 return NULL;
86  
87 55 object = newSV(0);
88 55 sv_setref_pv(object, class, (void *)state);
89 55 return object;
90 }
91  
92 static lews_bq *
93 124 lews_bq_from_sv(SV *self)
94 {
95 lews_bq *state;
96  
97 124 ▶ if (!SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self)) { 0 124 124
98 0 croak("invalid Linux::Event::WebSocket::_BQ object");
99 }
100  
101 124 state = INT2PTR(lews_bq *, SvIV((SV *)SvRV(self)));
102 124 ▶ if (state == NULL || state->ws == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (state == NULL || state->ws == NULL) { 124 0 124
if (state == NULL || state->ws == NULL) { 0 124 124
103 0 croak("invalid Linux::Event::WebSocket::_BQ state");
104 }
105 124 return state;
106 }
107  
108 static SV *
109 62 lews_bq_flush(lews_bq *state)
110 {
111 62 SV *out = newSVpvn("", 0);
112 uint8_t buffer[65536];
113  
114 0 for (;;) {
115 62 size_t n = bqws_write_to(state->ws, buffer, sizeof(buffer));
116 62 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 43 19 62
117 43 sv_catpvn(out, (const char *)buffer, n);
118 }
119 62 ▶ if (n < sizeof(buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < sizeof(buffer)) { 62 0 62
120 62 break;
121 }
122 }
123  
124 62 return out;
125 }
126  
127  
128 static SV *
129 40 lews_bq_payload_sv(IV opcode, const char *data, size_t size)
130 {
131 40 ▶ const U8 *bytes = (const U8 *)(data == NULL ? "" : data);
Branch: 1 branch
BranchTrueFalseTotal
const U8 *bytes = (const U8 *)(data == NULL ? "" : data); 40 0 40
132 40 SV *payload = newSVpvn((const char *)bytes, size);
133  
134 40 ▶ if (opcode == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (opcode == 1) { 26 14 40
135 26 const U8 *first_variant = NULL;
136  
137 26 ▶ if (!is_utf8_invariant_string_loc(bytes, (STRLEN)size, &first_variant)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_utf8_invariant_string_loc(bytes, (STRLEN)size, &first_variant)) { 15 11 26
138 15 ▶ if (!is_utf8_string_flags(
Branch: 1 branch
BranchTrueFalseTotal
if (!is_utf8_string_flags( 9 6 15
139 bytes,
140 (STRLEN)size,
141 UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE
142 )) {
143 9 SvREFCNT_dec(payload);
144 9 return NULL;
145 }
146 6 SvUTF8_on(payload);
147 }
148 }
149  
150 31 return payload;
151 }
152  
153 static SV *
154 9 lews_bq_call_invalid_utf8(SV *target, SV *connection)
155 {
156 9 SV *error = NULL;
157 9 dSP;
158  
159 9 ENTER;
160 9 SAVETMPS;
161  
162 9 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 9 9
163 9 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 9 9
164 9 PUSHs(target);
165 9 PUSHs(connection);
166 9 PUTBACK;
167  
168 9 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 9 0 9
169 9 call_method("_bq_invalid_utf8", G_DISCARD | G_EVAL);
170 9 SPAGAIN;
171  
172 9 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 9 0 9
if (SvTRUE(ERRSV)) { 0 9 9
173 0 ▶ error = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
error = newSVsv(ERRSV); 0 0 0
174 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
175 }
176  
177 9 PUTBACK;
178 9 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 9 0 9
179 9 LEAVE;
180  
181 9 return error;
182 }
183  
184 static SV *
185 40 lews_bq_call_event(
186 SV *target,
187 SV *connection,
188 IV opcode,
189 const char *data,
190 size_t size,
191 int *invalid_utf8
192 )
193 {
194 40 SV *error = NULL;
195 40 dSP;
196  
197 40 ENTER;
198 40 SAVETMPS;
199  
200 40 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 40 40
201 40 ▶ EXTEND(SP, 4);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 4); 0 40 40
202 40 PUSHs(target);
203 40 PUSHs(connection);
204 {
205 40 SV *payload = lews_bq_payload_sv(opcode, data, size);
206 40 ▶ if (payload == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (payload == NULL) { 9 31 40
207 9 ▶ if (invalid_utf8 != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (invalid_utf8 != NULL) 0 9 9
208 0 *invalid_utf8 = 1;
209 9 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 9 9
210 9 LEAVE;
211 9 return lews_bq_call_invalid_utf8(target, connection);
212 }
213 31 PUSHs(sv_2mortal(newSViv(opcode)));
214 31 PUSHs(sv_2mortal(payload));
215 }
216 31 PUTBACK;
217  
218 31 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 31 0 31
219 31 call_method("_bq_event", G_DISCARD | G_EVAL);
220 31 SPAGAIN;
221  
222 31 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 31 0 31
if (SvTRUE(ERRSV)) { 0 31 31
223 0 ▶ error = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
error = newSVsv(ERRSV); 0 0 0
224 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
225 }
226  
227 31 PUTBACK;
228 31 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 31 0 31
229 31 LEAVE;
230  
231 31 return error;
232 }
233  
234  
235 typedef struct {
236 const les_consumer_host_api_v1_t *host;
237 void *host_context;
238 SV *stream;
239 SV *native;
240 SV *engine;
241 lews_bq *bq;
242 } lews_raw_consumer;
243  
244 static int
245 12 lews_raw_stream_config(
246 pTHX_
247 SV *stream,
248 char endpoint_type[7],
249 UV *max_message_size
250 )
251 {
252 int count;
253 SV *config;
254 AV *values;
255 SV **endpoint;
256 SV **limit;
257 const char *value;
258 STRLEN value_len;
259 12 dSP;
260  
261 12 ENTER;
262 12 SAVETMPS;
263 12 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 12 12
264 12 ▶ XPUSHs(stream);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(stream); 0 12 12
265 12 PUTBACK;
266 12 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 12 0 12
267 12 count = call_method("_websocket_raw_config", G_SCALAR | G_EVAL);
268 12 SPAGAIN;
269  
270 12 ▶ if (SvTRUE(ERRSV) || count != 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) || count != 1) { 12 0 12
if (SvTRUE(ERRSV) || count != 1) { 12 0 12
if (SvTRUE(ERRSV) || count != 1) { 0 12 12
271 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
272 0 PUTBACK;
273 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
274 0 LEAVE;
275 0 return 0;
276 }
277  
278 12 config = POPs;
279 12 ▶ if (!SvROK(config) || SvTYPE(SvRV(config)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(config) || SvTYPE(SvRV(config)) != SVt_PVAV) { 12 0 12
if (!SvROK(config) || SvTYPE(SvRV(config)) != SVt_PVAV) { 0 12 12
280 0 PUTBACK;
281 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
282 0 LEAVE;
283 0 return 0;
284 }
285  
286 12 values = (AV *)SvRV(config);
287 12 endpoint = av_fetch(values, 0, 0);
288 12 limit = av_fetch(values, 1, 0);
289 12 ▶ if (endpoint == NULL || limit == NULL
Branch: 2 branches
BranchTrueFalseTotal
if (endpoint == NULL || limit == NULL 12 0 12
if (endpoint == NULL || limit == NULL 12 0 12
290 12 ▶ || !SvOK(*endpoint) || !SvOK(*limit)) {
Branch: 2 branches
BranchTrueFalseTotal
|| !SvOK(*endpoint) || !SvOK(*limit)) { 12 0 12
|| !SvOK(*endpoint) || !SvOK(*limit)) { 0 12 12
291 0 PUTBACK;
292 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
293 0 LEAVE;
294 0 return 0;
295 }
296  
297 12 value = SvPV(*endpoint, value_len);
298 12 ▶ if (value_len != 6
Branch: 1 branch
BranchTrueFalseTotal
if (value_len != 6 12 0 12
299 12 ▶ || (!memEQ(value, "client", 6) && !memEQ(value, "server", 6))) {
Branch: 2 branches
BranchTrueFalseTotal
|| (!memEQ(value, "client", 6) && !memEQ(value, "server", 6))) { 7 5 12
|| (!memEQ(value, "client", 6) && !memEQ(value, "server", 6))) { 0 7 7
300 0 PUTBACK;
301 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
302 0 LEAVE;
303 0 return 0;
304 }
305  
306 12 *max_message_size = SvUV(*limit);
307 12 ▶ if (*max_message_size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (*max_message_size == 0) { 0 12 12
308 0 PUTBACK;
309 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
310 0 LEAVE;
311 0 return 0;
312 }
313  
314 12 Copy(value, endpoint_type, 6, char);
315 12 endpoint_type[6] = '\0';
316  
317 12 PUTBACK;
318 12 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 12 0 12
319 12 LEAVE;
320 12 return 1;
321 }
322  
323 static void
324 38 lews_engine_set_in_feed(SV *engine, int value)
325 {
326 HV *state;
327 SV **slot;
328  
329 38 ▶ if (!SvROK(engine) || SvTYPE(SvRV(engine)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(engine) || SvTYPE(SvRV(engine)) != SVt_PVHV) 38 0 38
if (!SvROK(engine) || SvTYPE(SvRV(engine)) != SVt_PVHV) 0 38 38
330 0 croak("invalid Linux::Event::WebSocket::_Engine object");
331  
332 38 state = (HV *)SvRV(engine);
333 38 slot = hv_fetchs(state, "in_feed", 0);
334 38 ▶ if (slot == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (slot == NULL) 0 38 38
335 0 croak("WebSocket Engine is missing in_feed state");
336  
337 38 sv_setiv(*slot, value ? 1 : 0);
338 38 }
339  
340 static SV *
341 0 lews_raw_call_engine_error(SV *engine, bqws_error error_code)
342 {
343 0 SV *error = NULL;
344 0 dSP;
345  
346 0 ENTER;
347 0 SAVETMPS;
348 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
349 0 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 0 0
350 0 PUSHs(engine);
351 0 PUSHs(sv_2mortal(newSViv((IV)error_code)));
352 0 PUSHs(sv_2mortal(newSVpv(bqws_error_str(error_code), 0)));
353 0 PUTBACK;
354 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
355 0 call_method("_handle_bq_error", G_DISCARD | G_EVAL);
356 0 SPAGAIN;
357  
358 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
359 0 ▶ error = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
error = newSVsv(ERRSV); 0 0 0
360 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
361 }
362  
363 0 PUTBACK;
364 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
365 0 LEAVE;
366 0 return error;
367 }
368  
369 static SV *
370 15 lews_raw_call_write(SV *stream, SV *wire)
371 {
372 15 SV *error = NULL;
373 15 dSP;
374  
375 15 ENTER;
376 15 SAVETMPS;
377 15 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 15 15
378 15 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 15 15
379 15 ▶ XPUSHs(stream);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(stream); 0 15 15
380 15 ▶ XPUSHs(wire);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(wire); 0 15 15
381 15 PUTBACK;
382 15 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 15 0 15
383 15 call_method("write", G_DISCARD | G_EVAL);
384 15 SPAGAIN;
385  
386 15 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 15 0 15
if (SvTRUE(ERRSV)) { 0 15 15
387 0 ▶ error = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
error = newSVsv(ERRSV); 0 0 0
388 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
389 }
390  
391 15 PUTBACK;
392 15 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 15 0 15
393 15 LEAVE;
394 15 return error;
395 }
396  
397 static SV *
398 12 lews_raw_call_native_ready(SV *stream, SV *native)
399 {
400 12 SV *engine = NULL;
401 int count;
402 12 dSP;
403  
404 12 ENTER;
405 12 SAVETMPS;
406 12 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 12 12
407 12 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 12 12
408 12 ▶ XPUSHs(stream);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(stream); 0 12 12
409 12 ▶ XPUSHs(native);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(native); 0 12 12
410 12 PUTBACK;
411 12 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 12 0 12
412 12 count = call_method("_websocket_raw_native_ready", G_SCALAR | G_EVAL);
413 12 SPAGAIN;
414  
415 24 ▶ if (!SvTRUE(ERRSV) && count == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count == 1) { 12 0 12
if (!SvTRUE(ERRSV) && count == 1) { 12 0 12
if (!SvTRUE(ERRSV) && count == 1) { 12 0 12
416 12 SV *result = POPs;
417 12 ▶ if (SvROK(result)
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(result) 12 0 12
418 12 ▶ && sv_derived_from(result, "Linux::Event::WebSocket::_Engine"))
Branch: 1 branch
BranchTrueFalseTotal
&& sv_derived_from(result, "Linux::Event::WebSocket::_Engine")) 12 0 12
419 12 engine = newSVsv(result);
420 } else {
421 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
422 }
423  
424 12 PUTBACK;
425 12 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 12 0 12
426 12 LEAVE;
427 12 return engine;
428 }
429 static SV *
430 12 lews_engine_native(SV *engine)
431 {
432 HV *state;
433 SV **slot;
434  
435 12 ▶ if (!SvROK(engine) || SvTYPE(SvRV(engine)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(engine) || SvTYPE(SvRV(engine)) != SVt_PVHV) 12 0 12
if (!SvROK(engine) || SvTYPE(SvRV(engine)) != SVt_PVHV) 0 12 12
436 0 return NULL;
437  
438 12 state = (HV *)SvRV(engine);
439 12 slot = hv_fetchs(state, "native", 0);
440 12 ▶ if (slot == NULL || !SvOK(*slot)
Branch: 2 branches
BranchTrueFalseTotal
if (slot == NULL || !SvOK(*slot) 12 0 12
if (slot == NULL || !SvOK(*slot) 12 0 12
441 12 ▶ || !SvROK(*slot)
Branch: 1 branch
BranchTrueFalseTotal
|| !SvROK(*slot) 12 0 12
442 12 ▶ || !sv_derived_from(*slot, "Linux::Event::WebSocket::_BQ"))
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from(*slot, "Linux::Event::WebSocket::_BQ")) 0 12 12
443 0 return NULL;
444  
445 12 return newSVsv(*slot);
446 }
447  
448 static SV *
449 8 lews_raw_call_complete(SV *engine, SV *stream)
450 {
451 8 SV *error = NULL;
452 8 dSP;
453  
454 8 ENTER;
455 8 SAVETMPS;
456 8 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 8 8
457 8 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 8 8
458 8 PUSHs(engine);
459 8 PUSHs(stream);
460 8 PUTBACK;
461 8 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 8 0 8
462 8 call_method("_finish_feed", G_DISCARD | G_EVAL);
463 8 SPAGAIN;
464  
465 8 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 8 0 8
if (SvTRUE(ERRSV)) { 0 8 8
466 0 ▶ error = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
error = newSVsv(ERRSV); 0 0 0
467 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
468 }
469  
470 8 PUTBACK;
471 8 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 8 0 8
472 8 LEAVE;
473 8 return error;
474 }
475  
476  
477 static int
478 23 lews_raw_consumer_initialize(pTHX_ lews_raw_consumer *context)
479 {
480 char endpoint_type[7];
481 UV max_message_size;
482  
483 23 ▶ if (context->bq != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context->bq != NULL) 11 12 23
484 11 return 1;
485  
486 12 ▶ if (!lews_raw_stream_config(
Branch: 1 branch
BranchTrueFalseTotal
if (!lews_raw_stream_config( 0 12 12
487 aTHX_ context->stream, endpoint_type, &max_message_size))
488 0 return -1;
489  
490 12 context->native = lews_bq_new_object(
491 "Linux::Event::WebSocket::_BQ",
492 endpoint_type,
493 max_message_size
494 );
495 12 ▶ if (context->native == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context->native == NULL) 0 12 12
496 0 return -2;
497  
498 12 context->bq = lews_bq_from_sv(context->native);
499 12 context->engine =
500 12 lews_raw_call_native_ready(context->stream, context->native);
501  
502 12 ▶ if (context->engine == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (context->engine == NULL) { 0 12 12
503 0 context->bq = NULL;
504 0 SvREFCNT_dec(context->native);
505 0 context->native = NULL;
506 0 return -3;
507 }
508  
509 {
510 12 SV *engine_native = lews_engine_native(context->engine);
511 12 ▶ if (engine_native == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (engine_native == NULL) { 0 12 12
512 0 context->bq = NULL;
513 0 SvREFCNT_dec(context->engine);
514 0 context->engine = NULL;
515 0 SvREFCNT_dec(context->native);
516 0 context->native = NULL;
517 0 return -3;
518 }
519  
520 12 SvREFCNT_dec(context->native);
521 12 context->native = engine_native;
522 12 context->bq = lews_bq_from_sv(context->native);
523 }
524  
525 12 return 1;
526 }
527  
528 static void *
529 17 lews_raw_consumer_create(
530 pTHX_
531 const les_consumer_host_api_v1_t *host,
532 void *host_context,
533 SV *stream
534 )
535 {
536 lews_raw_consumer *context;
537  
538 PERL_UNUSED_CONTEXT;
539  
540 17 ▶ if (host == NULL
Branch: 1 branch
BranchTrueFalseTotal
if (host == NULL 17 0 17
541 17 ▶ || host->abi_version != LES_CONSUMER_ABI_VERSION
Branch: 1 branch
BranchTrueFalseTotal
|| host->abi_version != LES_CONSUMER_ABI_VERSION 17 0 17
542 17 ▶ || host->struct_size < LES_CONSUMER_HOST_V1_RETAIN_REQUIRED_SIZE
Branch: 1 branch
BranchTrueFalseTotal
|| host->struct_size < LES_CONSUMER_HOST_V1_RETAIN_REQUIRED_SIZE 17 0 17
543 17 ▶ || host->retain == NULL || host->release == NULL)
Branch: 2 branches
BranchTrueFalseTotal
|| host->retain == NULL || host->release == NULL) 17 0 17
|| host->retain == NULL || host->release == NULL) 0 17 17
544 0 return NULL;
545  
546 17 Newxz(context, 1, lews_raw_consumer);
547 17 ▶ if (context == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL) 0 17 17
548 0 return NULL;
549  
550 17 context->host = host;
551 17 context->host_context = host_context;
552 17 context->stream = SvREFCNT_inc(stream);
553 17 return context;
554 }
555  
556 static int
557 23 lews_raw_consumer_input(
558 pTHX_
559 void *opaque,
560 const char *data,
561 size_t length,
562 size_t *consumed
563 )
564 {
565 23 lews_raw_consumer *context = (lews_raw_consumer *)opaque;
566 23 size_t used = 0;
567 bqws_msg *msg;
568 bqws_error error_code;
569 23 SV *callback_error = NULL;
570 23 SV *wire = NULL;
571 23 int needs_complete = 0;
572 23 int result = LES_CONSUMER_CONTINUE;
573  
574 23 *consumed = 0;
575  
576 23 ▶ if (context == NULL
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL 23 0 23
577 23 ▶ || context->host == NULL || context->host->retain == NULL
Branch: 2 branches
BranchTrueFalseTotal
|| context->host == NULL || context->host->retain == NULL 23 0 23
|| context->host == NULL || context->host->retain == NULL 23 0 23
578 23 ▶ || context->host->release == NULL)
Branch: 1 branch
BranchTrueFalseTotal
|| context->host->release == NULL) 0 23 23
579 0 return LES_CONSUMER_ERROR;
580  
581 23 ▶ if (!context->host->retain(aTHX_ context->host_context))
Branch: 1 branch
BranchTrueFalseTotal
if (!context->host->retain(aTHX_ context->host_context)) 0 23 23
582 0 return LES_CONSUMER_ERROR;
583  
584 {
585 23 int init_status = lews_raw_consumer_initialize(aTHX_ context);
586 23 ▶ if (init_status <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (init_status <= 0) { 0 23 23
587 0 context->host->release(aTHX_ context->host_context);
588 0 ▶ if (init_status == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (init_status == -1) 0 0 0
589 0 croak("WebSocket raw consumer configuration failed");
590 0 ▶ if (init_status == -2)
Branch: 1 branch
BranchTrueFalseTotal
if (init_status == -2) 0 0 0
591 0 croak("WebSocket raw consumer bq initialization failed");
592 0 croak("WebSocket raw consumer Engine handoff failed");
593 }
594 }
595  
596 23 ▶ if (context->host->is_closed(aTHX_ context->host_context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (context->host->is_closed(aTHX_ context->host_context)) { 0 23 23
597 0 context->host->release(aTHX_ context->host_context);
598 0 return LES_CONSUMER_CONTINUE;
599 }
600  
601 23 lews_engine_set_in_feed(context->engine, 1);
602  
603 37 ▶ while (used < length) {
Branch: 1 branch
BranchTrueFalseTotal
while (used < length) { 23 14 37
604 23 size_t n = bqws_read_from(
605 23 context->bq->ws,
606 23 data + used,
607 length - used
608 );
609 23 ▶ if (n == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) 0 23 23
610 0 break;
611 23 used += n;
612 23 ▶ if (bqws_get_error(context->bq->ws) != BQWS_OK
Branch: 1 branch
BranchTrueFalseTotal
if (bqws_get_error(context->bq->ws) != BQWS_OK 23 0 23
613 23 ▶ || bqws_get_state(context->bq->ws) >= BQWS_STATE_CLOSING)
Branch: 1 branch
BranchTrueFalseTotal
|| bqws_get_state(context->bq->ws) >= BQWS_STATE_CLOSING) 14 9 23
614 break;
615 }
616 23 *consumed = used;
617  
618 23 while (callback_error == NULL
619 45 ▶ && (msg = bqws_recv(context->bq->ws)) != NULL) {
Branch: 2 branches
BranchTrueFalseTotal
&& (msg = bqws_recv(context->bq->ws)) != NULL) { 45 0 45
&& (msg = bqws_recv(context->bq->ws)) != NULL) { 24 21 45
620 24 switch (msg->type) {
621 9 case BQWS_MSG_TEXT:
622 9 callback_error = lews_bq_call_event(
623 context->engine, context->stream,
624 9 1, msg->data, msg->size, &needs_complete
625 );
626 9 break;
627 3 case BQWS_MSG_BINARY:
628 3 callback_error = lews_bq_call_event(
629 context->engine, context->stream,
630 3 2, msg->data, msg->size, &needs_complete
631 );
632 3 break;
633 9 case BQWS_MSG_CONTROL_CLOSE:
634 9 needs_complete = 1;
635 9 callback_error = lews_bq_call_event(
636 context->engine, context->stream,
637 9 8, msg->data, msg->size, &needs_complete
638 );
639 9 break;
640 3 case BQWS_MSG_CONTROL_PING:
641 case BQWS_MSG_CONTROL_PONG:
642 3 break;
643 0 default:
644 0 callback_error = newSVpvf(
645 "unexpected bq_websocket message type %d",
646 (int)msg->type
647 );
648 0 break;
649 }
650 24 bqws_free_msg(msg);
651  
652 24 ▶ if (context->host->is_closed(aTHX_ context->host_context))
Branch: 1 branch
BranchTrueFalseTotal
if (context->host->is_closed(aTHX_ context->host_context)) 2 22 24
653 2 break;
654 }
655  
656 23 error_code = bqws_get_error(context->bq->ws);
657 23 ▶ if (callback_error == NULL && error_code != BQWS_OK) {
Branch: 2 branches
BranchTrueFalseTotal
if (callback_error == NULL && error_code != BQWS_OK) { 23 0 23
if (callback_error == NULL && error_code != BQWS_OK) { 0 23 23
658 0 needs_complete = 1;
659 callback_error =
660 0 lews_raw_call_engine_error(context->engine, error_code);
661 }
662  
663 23 ▶ if (callback_error == NULL
Branch: 1 branch
BranchTrueFalseTotal
if (callback_error == NULL 23 0 23
664 23 ▶ && !context->host->is_closed(aTHX_ context->host_context)) {
Branch: 1 branch
BranchTrueFalseTotal
&& !context->host->is_closed(aTHX_ context->host_context)) { 21 2 23
665 21 wire = lews_bq_flush(context->bq);
666 21 ▶ if (SvCUR(wire) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(wire) != 0) 15 6 21
667 15 callback_error = lews_raw_call_write(context->stream, wire);
668 }
669  
670 23 ▶ if (wire != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (wire != NULL) 21 2 23
671 21 SvREFCNT_dec(wire);
672  
673 23 ▶ if (callback_error == NULL && needs_complete
Branch: 2 branches
BranchTrueFalseTotal
if (callback_error == NULL && needs_complete 23 0 23
if (callback_error == NULL && needs_complete 9 14 23
674 9 ▶ && !context->host->is_closed(aTHX_ context->host_context))
Branch: 1 branch
BranchTrueFalseTotal
&& !context->host->is_closed(aTHX_ context->host_context)) 8 1 9
675 8 callback_error =
676 8 lews_raw_call_complete(context->engine, context->stream);
677 else
678 15 lews_engine_set_in_feed(context->engine, 0);
679  
680 23 ▶ if (used != length && error_code == BQWS_OK
Branch: 2 branches
BranchTrueFalseTotal
if (used != length && error_code == BQWS_OK 0 23 23
if (used != length && error_code == BQWS_OK 0 0 0
681 0 ▶ && bqws_get_state(context->bq->ws) < BQWS_STATE_CLOSING
Branch: 1 branch
BranchTrueFalseTotal
&& bqws_get_state(context->bq->ws) < BQWS_STATE_CLOSING 0 0 0
682 0 ▶ && callback_error == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
&& callback_error == NULL) { 0 0 0
683 0 callback_error = newSVpvf(
684 "bq_websocket consumed only %lu of %lu raw input bytes",
685 (unsigned long)used,
686 (unsigned long)length
687 );
688 }
689  
690 23 context->host->release(aTHX_ context->host_context);
691  
692 23 ▶ if (callback_error != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (callback_error != NULL) 0 23 23
693 0 croak_sv(callback_error);
694  
695 23 return result;
696 }
697  
698 static void
699 13 lews_raw_consumer_event(
700 pTHX_
701 void *opaque,
702 uint32_t event,
703 int error,
704 const char *message
705 )
706 {
707 PERL_UNUSED_ARG(opaque);
708 PERL_UNUSED_ARG(event);
709 PERL_UNUSED_ARG(error);
710 PERL_UNUSED_ARG(message);
711 PERL_UNUSED_CONTEXT;
712 13 }
713  
714 static void
715 17 lews_raw_consumer_destroy(pTHX_ void *opaque)
716 {
717 17 lews_raw_consumer *context = (lews_raw_consumer *)opaque;
718  
719 PERL_UNUSED_CONTEXT;
720 17 ▶ if (context == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL) 0 17 17
721 0 return;
722  
723 17 context->bq = NULL;
724 17 ▶ if (context->engine != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context->engine != NULL) 12 5 17
725 12 SvREFCNT_dec(context->engine);
726 17 ▶ if (context->native != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context->native != NULL) 12 5 17
727 12 SvREFCNT_dec(context->native);
728 17 ▶ if (context->stream != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context->stream != NULL) 17 0 17
729 17 SvREFCNT_dec(context->stream);
730 17 Safefree(context);
731 }
732  
733 typedef struct {
734 const les_consumer_host_api_v1_t *host;
735 void *host_context;
736 SV *stream;
737 } lews_http_bridge_consumer;
738  
739 static void *
740 7 lews_http_bridge_create(
741 pTHX_
742 const les_consumer_host_api_v1_t *host,
743 void *host_context,
744 SV *stream
745 )
746 {
747 lews_http_bridge_consumer *context;
748  
749 PERL_UNUSED_CONTEXT;
750  
751 7 ▶ if (host == NULL
Branch: 1 branch
BranchTrueFalseTotal
if (host == NULL 7 0 7
752 7 ▶ || host->abi_version != LES_CONSUMER_ABI_VERSION
Branch: 1 branch
BranchTrueFalseTotal
|| host->abi_version != LES_CONSUMER_ABI_VERSION 7 0 7
753 7 ▶ || host->struct_size < LES_CONSUMER_HOST_V1_RETAIN_REQUIRED_SIZE
Branch: 1 branch
BranchTrueFalseTotal
|| host->struct_size < LES_CONSUMER_HOST_V1_RETAIN_REQUIRED_SIZE 7 0 7
754 7 ▶ || host->retain == NULL || host->release == NULL)
Branch: 2 branches
BranchTrueFalseTotal
|| host->retain == NULL || host->release == NULL) 7 0 7
|| host->retain == NULL || host->release == NULL) 0 7 7
755 0 return NULL;
756  
757 7 Newxz(context, 1, lews_http_bridge_consumer);
758 7 ▶ if (context == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL) 0 7 7
759 0 return NULL;
760  
761 7 context->host = host;
762 7 context->host_context = host_context;
763 7 context->stream = SvREFCNT_inc(stream);
764 7 return context;
765 }
766  
767 static int
768 7 lews_http_bridge_input(
769 pTHX_
770 void *opaque,
771 const char *data,
772 size_t length,
773 size_t *consumed
774 )
775 {
776 7 lews_http_bridge_consumer *context =
777 (lews_http_bridge_consumer *)opaque;
778 7 SV *callback_error = NULL;
779 7 dSP;
780  
781 7 *consumed = 0;
782  
783 7 ▶ if (context == NULL || context->host == NULL
Branch: 2 branches
BranchTrueFalseTotal
if (context == NULL || context->host == NULL 7 0 7
if (context == NULL || context->host == NULL 7 0 7
784 7 ▶ || context->host->retain == NULL
Branch: 1 branch
BranchTrueFalseTotal
|| context->host->retain == NULL 7 0 7
785 7 ▶ || context->host->release == NULL)
Branch: 1 branch
BranchTrueFalseTotal
|| context->host->release == NULL) 0 7 7
786 0 return LES_CONSUMER_ERROR;
787  
788 7 ▶ if (!context->host->retain(aTHX_ context->host_context))
Branch: 1 branch
BranchTrueFalseTotal
if (!context->host->retain(aTHX_ context->host_context)) 0 7 7
789 0 return LES_CONSUMER_ERROR;
790  
791 7 *consumed = length;
792  
793 7 ENTER;
794 7 SAVETMPS;
795 7 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 7 7
796 7 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 7 7
797 7 PUSHs(context->stream);
798 7 PUSHs(sv_2mortal(newSVpvn(data, (STRLEN)length)));
799 7 PUTBACK;
800 7 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 7 0 7
801 7 call_method("_websocket_http_input", G_DISCARD | G_EVAL);
802 7 SPAGAIN;
803  
804 7 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 7 0 7
if (SvTRUE(ERRSV)) { 0 7 7
805 0 ▶ callback_error = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
callback_error = newSVsv(ERRSV); 0 0 0
806 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
807 }
808  
809 7 PUTBACK;
810 7 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 7 0 7
811 7 LEAVE;
812  
813 7 context->host->release(aTHX_ context->host_context);
814  
815 7 ▶ if (callback_error != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (callback_error != NULL) 0 7 7
816 0 croak_sv(callback_error);
817  
818 7 return LES_CONSUMER_CONTINUE;
819 }
820  
821 static void
822 0 lews_http_bridge_event(
823 pTHX_
824 void *opaque,
825 uint32_t event,
826 int error,
827 const char *message
828 )
829 {
830 PERL_UNUSED_ARG(opaque);
831 PERL_UNUSED_ARG(event);
832 PERL_UNUSED_ARG(error);
833 PERL_UNUSED_ARG(message);
834 PERL_UNUSED_CONTEXT;
835 0 }
836  
837 static void
838 7 lews_http_bridge_destroy(pTHX_ void *opaque)
839 {
840 7 lews_http_bridge_consumer *context =
841 (lews_http_bridge_consumer *)opaque;
842  
843 PERL_UNUSED_CONTEXT;
844 7 ▶ if (context == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL) 0 7 7
845 0 return;
846  
847 7 ▶ if (context->stream != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context->stream != NULL) 7 0 7
848 7 SvREFCNT_dec(context->stream);
849 7 Safefree(context);
850 }
851  
852 static const les_consumer_ops_v1_t lews_http_bridge_consumer_ops = {
853 LES_CONSUMER_ABI_VERSION,
854 sizeof(les_consumer_ops_v1_t),
855 "Linux::Event::WebSocket HTTP handoff bridge",
856 LES_CONSUMER_F_RAW_INPUT,
857 lews_http_bridge_create,
858 NULL,
859 lews_http_bridge_event,
860 lews_http_bridge_destroy,
861 NULL,
862 lews_http_bridge_input
863 };
864  
865 static const les_consumer_ops_v1_t lews_raw_consumer_ops = {
866 LES_CONSUMER_ABI_VERSION,
867 sizeof(les_consumer_ops_v1_t),
868 "Linux::Event::WebSocket bq raw input",
869 LES_CONSUMER_F_RAW_INPUT,
870 lews_raw_consumer_create,
871 NULL,
872 lews_raw_consumer_event,
873 lews_raw_consumer_destroy,
874 NULL,
875 lews_raw_consumer_input
876 };
877  
878 MODULE = Linux::Event::WebSocket PACKAGE = Linux::Event::WebSocket::_BQ
879  
880 PROTOTYPES: DISABLE
881  
882 UV
883 _raw_consumer_operations_address()
884 CODE:
885 8 ▶ RETVAL = PTR2UV(&lews_raw_consumer_ops);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(&lews_raw_consumer_ops); 0 8 8
886 OUTPUT:
887 RETVAL
888  
889 UV
890 _http_bridge_consumer_operations_address()
891 CODE:
892 6 ▶ RETVAL = PTR2UV(&lews_http_bridge_consumer_ops);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(&lews_http_bridge_consumer_ops); 0 6 6
893 OUTPUT:
894 RETVAL
895  
896 SV *
897 new(class, endpoint_type, max_message_size)
898 const char *class
899 const char *endpoint_type
900 UV max_message_size
901 CODE:
902 43 RETVAL = lews_bq_new_object(class, endpoint_type, max_message_size);
903 43 ▶ if (RETVAL == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL == NULL) 0 43 43
904 0 croak("bq_websocket context initialization failed");
905 OUTPUT:
906 RETVAL
907  
908 void
909 feed(self, callback_target, connection, bytes)
910 SV *self
911 SV *callback_target
912 SV *connection
913 SV *bytes
914 PREINIT:
915 lews_bq *state;
916 STRLEN len;
917 const char *data;
918 size_t used;
919 bqws_msg *msg;
920 bqws_error error;
921 SV *callback_error;
922 PPCODE:
923 24 state = lews_bq_from_sv(self);
924 24 data = SvPVbyte(bytes, len);
925 24 used = 0;
926 42 ▶ while (used < (size_t)len) {
Branch: 1 branch
BranchTrueFalseTotal
while (used < (size_t)len) { 24 18 42
927 24 size_t n = bqws_read_from(
928 state->ws,
929 24 data + used,
930 24 (size_t)len - used
931 );
932 24 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 0 24 24
933 0 break;
934 }
935 24 used += n;
936 24 ▶ if (bqws_get_error(state->ws) != BQWS_OK
Branch: 1 branch
BranchTrueFalseTotal
if (bqws_get_error(state->ws) != BQWS_OK 19 5 24
937 19 ▶ || bqws_get_state(state->ws) >= BQWS_STATE_CLOSING) {
Branch: 1 branch
BranchTrueFalseTotal
|| bqws_get_state(state->ws) >= BQWS_STATE_CLOSING) { 18 1 19
938 break;
939 }
940 }
941  
942 45 ▶ while ((msg = bqws_recv(state->ws)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((msg = bqws_recv(state->ws)) != NULL) { 21 24 45
943 21 callback_error = NULL;
944  
945 21 switch (msg->type) {
946 17 case BQWS_MSG_TEXT:
947 17 callback_error = lews_bq_call_event(
948 17 callback_target, connection, 1, msg->data, msg->size, NULL
949 );
950 17 break;
951 1 case BQWS_MSG_BINARY:
952 1 callback_error = lews_bq_call_event(
953 1 callback_target, connection, 2, msg->data, msg->size, NULL
954 );
955 1 break;
956 1 case BQWS_MSG_CONTROL_CLOSE:
957 1 callback_error = lews_bq_call_event(
958 1 callback_target, connection, 8, msg->data, msg->size, NULL
959 );
960 1 break;
961 2 case BQWS_MSG_CONTROL_PING:
962 case BQWS_MSG_CONTROL_PONG:
963 2 break;
964 0 default:
965 0 bqws_free_msg(msg);
966 0 croak("unexpected bq_websocket message type %d", (int)msg->type);
967 }
968  
969 21 bqws_free_msg(msg);
970  
971 21 ▶ if (callback_error != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (callback_error != NULL) { 0 21 21
972 0 croak_sv(callback_error);
973 }
974 }
975  
976 24 error = bqws_get_error(state->ws);
977 24 ▶ if (used != (size_t)len && error == BQWS_OK
Branch: 2 branches
BranchTrueFalseTotal
if (used != (size_t)len && error == BQWS_OK 0 24 24
if (used != (size_t)len && error == BQWS_OK 0 0 0
978 0 ▶ && bqws_get_state(state->ws) < BQWS_STATE_CLOSING) {
Branch: 1 branch
BranchTrueFalseTotal
&& bqws_get_state(state->ws) < BQWS_STATE_CLOSING) { 0 0 0
979 0 croak("bq_websocket consumed only %lu of %lu input bytes",
980 (unsigned long)used, (unsigned long)len);
981 }
982  
983 24 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 24 24
984 24 PUSHs(sv_2mortal(newSViv((IV)error)));
985 24 PUSHs(sv_2mortal(newSVpv(bqws_error_str(error), 0)));
986 24 XSRETURN(2);
987  
988 UV
989 _memory_used(self)
990 SV *self
991 PREINIT:
992 lews_bq *state;
993 CODE:
994 3 state = lews_bq_from_sv(self);
995 3 RETVAL = (UV)bqws_get_memory_used(state->ws);
996 OUTPUT:
997 RETVAL
998  
999 SV *
1000 flush(self)
1001 SV *self
1002 PREINIT:
1003 lews_bq *state;
1004 CODE:
1005 41 state = lews_bq_from_sv(self);
1006 41 RETVAL = lews_bq_flush(state);
1007 OUTPUT:
1008 RETVAL
1009  
1010 void
1011 queue_message(self, opcode, bytes)
1012 SV *self
1013 IV opcode
1014 SV *bytes
1015 PREINIT:
1016 lews_bq *state;
1017 STRLEN len;
1018 const char *data;
1019 CODE:
1020 16 state = lews_bq_from_sv(self);
1021 16 ▶ if (opcode == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (opcode == 1) { 12 4 16
1022 12 ▶ if (SvUTF8(bytes)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(bytes)) { 5 7 12
1023 5 data = SvPVutf8(bytes, len);
1024 } else {
1025 7 data = SvPVbyte(bytes, len);
1026 }
1027  
1028 12 ▶ if (!is_utf8_string_flags(
Branch: 1 branch
BranchTrueFalseTotal
if (!is_utf8_string_flags( 3 9 12
1029 (const U8 *)data,
1030 len,
1031 UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE
1032 )) {
1033 3 croak("send_text(): payload contains invalid UTF-8");
1034 }
1035  
1036 9 bqws_send(state->ws, BQWS_MSG_TEXT, data, (size_t)len);
1037 4 ▶ } else if (opcode == 2) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (opcode == 2) { 3 1 4
1038 3 data = SvPVbyte(bytes, len);
1039 3 bqws_send(state->ws, BQWS_MSG_BINARY, data, (size_t)len);
1040 1 ▶ } else if (opcode == 9) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (opcode == 9) { 1 0 1
1041 1 data = SvPVbyte(bytes, len);
1042 1 bqws_send_ping(state->ws, data, (size_t)len);
1043 } else {
1044 0 croak("unsupported bq_websocket opcode %ld", (long)opcode);
1045 }
1046  
1047 13 ▶ if (bqws_get_error(state->ws) != BQWS_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (bqws_get_error(state->ws) != BQWS_OK) { 0 13 13
1048 0 croak("bq_websocket send failed: %s",
1049 bqws_error_str(bqws_get_error(state->ws)));
1050 }
1051  
1052 void
1053 queue_close(self, status_code, reason)
1054 SV *self
1055 UV status_code
1056 SV *reason
1057 PREINIT:
1058 lews_bq *state;
1059 STRLEN len;
1060 const char *data;
1061 CODE:
1062 16 state = lews_bq_from_sv(self);
1063 16 data = SvPVbyte(reason, len);
1064 16 bqws_close(
1065 state->ws,
1066 (bqws_close_reason)status_code,
1067 data,
1068 (size_t)len
1069 );
1070  
1071 void
1072 DESTROY(self)
1073 SV *self
1074 PREINIT:
1075 lews_bq *state;
1076 CODE:
1077 55 ▶ if (SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(self)) { 55 0 55
1078 55 state = INT2PTR(lews_bq *, SvIV((SV *)SvRV(self)));
1079 55 ▶ if (state != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state != NULL) { 55 0 55
1080 55 lews_bq_free(state);
1081 55 sv_setiv((SV *)SvRV(self), 0);
1082 }
1083 }
« vendor/bq_websocket/bq_websocket.c ↑ Summary blib/lib/Linux/Event/WebSocket.pm »