WebSocket.xs

filter: Statementstmt 78.0% 420 / 538 Branchbran 48.9% 188 / 384 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 65.9% 608 / 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 123 lews_bq_from_sv(SV *self)
94 {
95 lews_bq *state;
96  
97 123 ▶ if (!SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self)) { 0 123 123
98 0 croak("invalid Linux::Event::WebSocket::_BQ object");
99 }
100  
101 123 state = INT2PTR(lews_bq *, SvIV((SV *)SvRV(self)));
102 123 ▶ if (state == NULL || state->ws == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (state == NULL || state->ws == NULL) { 123 0 123
if (state == NULL || state->ws == NULL) { 0 123 123
103 0 croak("invalid Linux::Event::WebSocket::_BQ state");
104 }
105 123 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 10 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 10 dSP;
260  
261 10 ENTER;
262 10 SAVETMPS;
263 10 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 10 10
264 10 ▶ XPUSHs(stream);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(stream); 0 10 10
265 10 PUTBACK;
266 10 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 10 0 10
267 10 count = call_method("_websocket_raw_config", G_SCALAR | G_EVAL);
268 10 SPAGAIN;
269  
270 10 ▶ if (SvTRUE(ERRSV) || count != 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) || count != 1) { 10 0 10
if (SvTRUE(ERRSV) || count != 1) { 10 0 10
if (SvTRUE(ERRSV) || count != 1) { 0 10 10
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 10 config = POPs;
279 10 ▶ if (!SvROK(config) || SvTYPE(SvRV(config)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(config) || SvTYPE(SvRV(config)) != SVt_PVAV) { 10 0 10
if (!SvROK(config) || SvTYPE(SvRV(config)) != SVt_PVAV) { 0 10 10
280 0 PUTBACK;
281 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
282 0 LEAVE;
283 0 return 0;
284 }
285  
286 10 values = (AV *)SvRV(config);
287 10 endpoint = av_fetch(values, 0, 0);
288 10 limit = av_fetch(values, 1, 0);
289 10 ▶ if (endpoint == NULL || limit == NULL
Branch: 2 branches
BranchTrueFalseTotal
if (endpoint == NULL || limit == NULL 10 0 10
if (endpoint == NULL || limit == NULL 10 0 10
290 10 ▶ || !SvOK(*endpoint) || !SvOK(*limit)) {
Branch: 2 branches
BranchTrueFalseTotal
|| !SvOK(*endpoint) || !SvOK(*limit)) { 10 0 10
|| !SvOK(*endpoint) || !SvOK(*limit)) { 0 10 10
291 0 PUTBACK;
292 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
293 0 LEAVE;
294 0 return 0;
295 }
296  
297 10 value = SvPV(*endpoint, value_len);
298 10 ▶ if (value_len != 6
Branch: 1 branch
BranchTrueFalseTotal
if (value_len != 6 10 0 10
299 10 ▶ || (!memEQ(value, "client", 6) && !memEQ(value, "server", 6))) {
Branch: 2 branches
BranchTrueFalseTotal
|| (!memEQ(value, "client", 6) && !memEQ(value, "server", 6))) { 6 4 10
|| (!memEQ(value, "client", 6) && !memEQ(value, "server", 6))) { 0 6 6
300 0 PUTBACK;
301 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
302 0 LEAVE;
303 0 return 0;
304 }
305  
306 10 *max_message_size = SvUV(*limit);
307 10 ▶ if (*max_message_size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (*max_message_size == 0) { 0 10 10
308 0 PUTBACK;
309 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
310 0 LEAVE;
311 0 return 0;
312 }
313  
314 10 Copy(value, endpoint_type, 6, char);
315 10 endpoint_type[6] = '\0';
316  
317 10 PUTBACK;
318 10 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 10 0 10
319 10 LEAVE;
320 10 return 1;
321 }
322  
323 static void
324 34 lews_engine_set_in_feed(SV *engine, int value)
325 {
326 HV *state;
327 SV **slot;
328  
329 34 ▶ if (!SvROK(engine) || SvTYPE(SvRV(engine)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(engine) || SvTYPE(SvRV(engine)) != SVt_PVHV) 34 0 34
if (!SvROK(engine) || SvTYPE(SvRV(engine)) != SVt_PVHV) 0 34 34
330 0 croak("invalid Linux::Event::WebSocket::_Engine object");
331  
332 34 state = (HV *)SvRV(engine);
333 34 slot = hv_fetchs(state, "in_feed", 0);
334 34 ▶ if (slot == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (slot == NULL) 0 34 34
335 0 croak("WebSocket Engine is missing in_feed state");
336  
337 34 sv_setiv(*slot, value ? 1 : 0);
338 34 }
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 14 lews_raw_call_write(SV *stream, SV *wire)
371 {
372 14 SV *error = NULL;
373 14 dSP;
374  
375 14 ENTER;
376 14 SAVETMPS;
377 14 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 14 14
378 14 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 14 14
379 14 ▶ XPUSHs(stream);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(stream); 0 14 14
380 14 ▶ XPUSHs(wire);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(wire); 0 14 14
381 14 PUTBACK;
382 14 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 14 0 14
383 14 call_method("write", G_DISCARD | G_EVAL);
384 14 SPAGAIN;
385  
386 14 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 14 0 14
if (SvTRUE(ERRSV)) { 0 14 14
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 14 PUTBACK;
392 14 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 14 0 14
393 14 LEAVE;
394 14 return error;
395 }
396  
397 static SV *
398 10 lews_raw_call_native_ready(SV *stream, SV *native)
399 {
400 10 SV *engine = NULL;
401 int count;
402 10 dSP;
403  
404 10 ENTER;
405 10 SAVETMPS;
406 10 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 10 10
407 10 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 10 10
408 10 ▶ XPUSHs(stream);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(stream); 0 10 10
409 10 ▶ XPUSHs(native);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(native); 0 10 10
410 10 PUTBACK;
411 10 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 10 0 10
412 10 count = call_method("_websocket_raw_native_ready", G_SCALAR | G_EVAL);
413 10 SPAGAIN;
414  
415 20 ▶ if (!SvTRUE(ERRSV) && count == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count == 1) { 10 0 10
if (!SvTRUE(ERRSV) && count == 1) { 10 0 10
if (!SvTRUE(ERRSV) && count == 1) { 10 0 10
416 10 SV *result = POPs;
417 10 ▶ if (SvROK(result)
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(result) 10 0 10
418 10 ▶ && sv_derived_from(result, "Linux::Event::WebSocket::_Engine"))
Branch: 1 branch
BranchTrueFalseTotal
&& sv_derived_from(result, "Linux::Event::WebSocket::_Engine")) 10 0 10
419 10 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 10 PUTBACK;
425 10 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 10 0 10
426 10 LEAVE;
427 10 return engine;
428 }
429 static SV *
430 10 lews_engine_native(SV *engine)
431 {
432 HV *state;
433 SV **slot;
434  
435 10 ▶ if (!SvROK(engine) || SvTYPE(SvRV(engine)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(engine) || SvTYPE(SvRV(engine)) != SVt_PVHV) 10 0 10
if (!SvROK(engine) || SvTYPE(SvRV(engine)) != SVt_PVHV) 0 10 10
436 0 return NULL;
437  
438 10 state = (HV *)SvRV(engine);
439 10 slot = hv_fetchs(state, "native", 0);
440 10 ▶ if (slot == NULL || !SvOK(*slot)
Branch: 2 branches
BranchTrueFalseTotal
if (slot == NULL || !SvOK(*slot) 10 0 10
if (slot == NULL || !SvOK(*slot) 10 0 10
441 10 ▶ || !SvROK(*slot)
Branch: 1 branch
BranchTrueFalseTotal
|| !SvROK(*slot) 10 0 10
442 10 ▶ || !sv_derived_from(*slot, "Linux::Event::WebSocket::_BQ"))
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from(*slot, "Linux::Event::WebSocket::_BQ")) 0 10 10
443 0 return NULL;
444  
445 10 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 21 lews_raw_consumer_initialize(pTHX_ lews_raw_consumer *context)
479 {
480 char endpoint_type[7];
481 UV max_message_size;
482  
483 21 ▶ if (context->bq != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context->bq != NULL) 11 10 21
484 11 return 1;
485  
486 10 ▶ if (!lews_raw_stream_config(
Branch: 1 branch
BranchTrueFalseTotal
if (!lews_raw_stream_config( 0 10 10
487 aTHX_ context->stream, endpoint_type, &max_message_size))
488 0 return -1;
489  
490 10 context->native = lews_bq_new_object(
491 "Linux::Event::WebSocket::_BQ",
492 endpoint_type,
493 max_message_size
494 );
495 10 ▶ if (context->native == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context->native == NULL) 0 10 10
496 0 return -2;
497  
498 10 context->bq = lews_bq_from_sv(context->native);
499 10 context->engine =
500 10 lews_raw_call_native_ready(context->stream, context->native);
501  
502 10 ▶ if (context->engine == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (context->engine == NULL) { 0 10 10
503 0 context->bq = NULL;
504 0 SvREFCNT_dec(context->native);
505 0 context->native = NULL;
506 0 return -3;
507 }
508  
509 {
510 10 SV *engine_native = lews_engine_native(context->engine);
511 10 ▶ if (engine_native == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (engine_native == NULL) { 0 10 10
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 10 SvREFCNT_dec(context->native);
521 10 context->native = engine_native;
522 10 context->bq = lews_bq_from_sv(context->native);
523 }
524  
525 10 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 21 lews_raw_consumer_input(
558 pTHX_
559 void *opaque,
560 const char *data,
561 size_t length,
562 size_t *consumed
563 )
564 {
565 21 lews_raw_consumer *context = (lews_raw_consumer *)opaque;
566 21 size_t used = 0;
567 bqws_msg *msg;
568 bqws_error error_code;
569 21 SV *callback_error = NULL;
570 21 SV *wire = NULL;
571 21 int needs_complete = 0;
572 21 int result = LES_CONSUMER_CONTINUE;
573  
574 21 *consumed = 0;
575  
576 21 ▶ if (context == NULL
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL 21 0 21
577 21 ▶ || context->host == NULL || context->host->retain == NULL
Branch: 2 branches
BranchTrueFalseTotal
|| context->host == NULL || context->host->retain == NULL 21 0 21
|| context->host == NULL || context->host->retain == NULL 21 0 21
578 21 ▶ || context->host->release == NULL)
Branch: 1 branch
BranchTrueFalseTotal
|| context->host->release == NULL) 0 21 21
579 0 return LES_CONSUMER_ERROR;
580  
581 21 ▶ if (!context->host->retain(aTHX_ context->host_context))
Branch: 1 branch
BranchTrueFalseTotal
if (!context->host->retain(aTHX_ context->host_context)) 0 21 21
582 0 return LES_CONSUMER_ERROR;
583  
584 {
585 21 int init_status = lews_raw_consumer_initialize(aTHX_ context);
586 21 ▶ if (init_status <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (init_status <= 0) { 0 21 21
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 21 ▶ if (context->host->is_closed(aTHX_ context->host_context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (context->host->is_closed(aTHX_ context->host_context)) { 0 21 21
597 0 context->host->release(aTHX_ context->host_context);
598 0 return LES_CONSUMER_CONTINUE;
599 }
600  
601 21 lews_engine_set_in_feed(context->engine, 1);
602  
603 33 ▶ while (used < length) {
Branch: 1 branch
BranchTrueFalseTotal
while (used < length) { 21 12 33
604 21 size_t n = bqws_read_from(
605 21 context->bq->ws,
606 21 data + used,
607 length - used
608 );
609 21 ▶ if (n == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) 0 21 21
610 0 break;
611 21 used += n;
612 21 ▶ if (bqws_get_error(context->bq->ws) != BQWS_OK
Branch: 1 branch
BranchTrueFalseTotal
if (bqws_get_error(context->bq->ws) != BQWS_OK 21 0 21
613 21 ▶ || bqws_get_state(context->bq->ws) >= BQWS_STATE_CLOSING)
Branch: 1 branch
BranchTrueFalseTotal
|| bqws_get_state(context->bq->ws) >= BQWS_STATE_CLOSING) 12 9 21
614 break;
615 }
616 21 *consumed = used;
617  
618 21 while (callback_error == NULL
619 42 ▶ && (msg = bqws_recv(context->bq->ws)) != NULL) {
Branch: 2 branches
BranchTrueFalseTotal
&& (msg = bqws_recv(context->bq->ws)) != NULL) { 42 0 42
&& (msg = bqws_recv(context->bq->ws)) != NULL) { 22 20 42
620 22 switch (msg->type) {
621 7 case BQWS_MSG_TEXT:
622 7 callback_error = lews_bq_call_event(
623 context->engine, context->stream,
624 7 1, msg->data, msg->size, &needs_complete
625 );
626 7 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 22 bqws_free_msg(msg);
651  
652 22 ▶ if (context->host->is_closed(aTHX_ context->host_context))
Branch: 1 branch
BranchTrueFalseTotal
if (context->host->is_closed(aTHX_ context->host_context)) 1 21 22
653 1 break;
654 }
655  
656 21 error_code = bqws_get_error(context->bq->ws);
657 21 ▶ if (callback_error == NULL && error_code != BQWS_OK) {
Branch: 2 branches
BranchTrueFalseTotal
if (callback_error == NULL && error_code != BQWS_OK) { 21 0 21
if (callback_error == NULL && error_code != BQWS_OK) { 0 21 21
658 0 needs_complete = 1;
659 callback_error =
660 0 lews_raw_call_engine_error(context->engine, error_code);
661 }
662  
663 21 ▶ if (callback_error == NULL
Branch: 1 branch
BranchTrueFalseTotal
if (callback_error == NULL 21 0 21
664 21 ▶ && !context->host->is_closed(aTHX_ context->host_context)) {
Branch: 1 branch
BranchTrueFalseTotal
&& !context->host->is_closed(aTHX_ context->host_context)) { 20 1 21
665 20 wire = lews_bq_flush(context->bq);
666 20 ▶ if (SvCUR(wire) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(wire) != 0) 14 6 20
667 14 callback_error = lews_raw_call_write(context->stream, wire);
668 }
669  
670 21 ▶ if (wire != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (wire != NULL) 20 1 21
671 20 SvREFCNT_dec(wire);
672  
673 21 ▶ if (callback_error == NULL && needs_complete
Branch: 2 branches
BranchTrueFalseTotal
if (callback_error == NULL && needs_complete 21 0 21
if (callback_error == NULL && needs_complete 9 12 21
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 13 lews_engine_set_in_feed(context->engine, 0);
679  
680 21 ▶ if (used != length && error_code == BQWS_OK
Branch: 2 branches
BranchTrueFalseTotal
if (used != length && error_code == BQWS_OK 0 21 21
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 21 context->host->release(aTHX_ context->host_context);
691  
692 21 ▶ if (callback_error != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (callback_error != NULL) 0 21 21
693 0 croak_sv(callback_error);
694  
695 21 return result;
696 }
697  
698 static void
699 12 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 12 }
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) 10 7 17
725 10 SvREFCNT_dec(context->engine);
726 17 ▶ if (context->native != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context->native != NULL) 10 7 17
727 10 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 14 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 14 ▶ if (host == NULL
Branch: 1 branch
BranchTrueFalseTotal
if (host == NULL 14 0 14
752 14 ▶ || host->abi_version != LES_CONSUMER_ABI_VERSION
Branch: 1 branch
BranchTrueFalseTotal
|| host->abi_version != LES_CONSUMER_ABI_VERSION 14 0 14
753 14 ▶ || host->struct_size < LES_CONSUMER_HOST_V1_RETAIN_REQUIRED_SIZE
Branch: 1 branch
BranchTrueFalseTotal
|| host->struct_size < LES_CONSUMER_HOST_V1_RETAIN_REQUIRED_SIZE 14 0 14
754 14 ▶ || host->retain == NULL || host->release == NULL)
Branch: 2 branches
BranchTrueFalseTotal
|| host->retain == NULL || host->release == NULL) 14 0 14
|| host->retain == NULL || host->release == NULL) 0 14 14
755 0 return NULL;
756  
757 14 Newxz(context, 1, lews_http_bridge_consumer);
758 14 ▶ if (context == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL) 0 14 14
759 0 return NULL;
760  
761 14 context->host = host;
762 14 context->host_context = host_context;
763 14 context->stream = SvREFCNT_inc(stream);
764 14 return context;
765 }
766  
767 static int
768 14 lews_http_bridge_input(
769 pTHX_
770 void *opaque,
771 const char *data,
772 size_t length,
773 size_t *consumed
774 )
775 {
776 14 lews_http_bridge_consumer *context =
777 (lews_http_bridge_consumer *)opaque;
778 14 SV *callback_error = NULL;
779 14 dSP;
780  
781 14 *consumed = 0;
782  
783 14 ▶ if (context == NULL || context->host == NULL
Branch: 2 branches
BranchTrueFalseTotal
if (context == NULL || context->host == NULL 14 0 14
if (context == NULL || context->host == NULL 14 0 14
784 14 ▶ || context->host->retain == NULL
Branch: 1 branch
BranchTrueFalseTotal
|| context->host->retain == NULL 14 0 14
785 14 ▶ || context->host->release == NULL)
Branch: 1 branch
BranchTrueFalseTotal
|| context->host->release == NULL) 0 14 14
786 0 return LES_CONSUMER_ERROR;
787  
788 14 ▶ if (!context->host->retain(aTHX_ context->host_context))
Branch: 1 branch
BranchTrueFalseTotal
if (!context->host->retain(aTHX_ context->host_context)) 0 14 14
789 0 return LES_CONSUMER_ERROR;
790  
791 14 *consumed = length;
792  
793 14 ENTER;
794 14 SAVETMPS;
795 14 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 14 14
796 14 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 14 14
797 14 PUSHs(context->stream);
798 14 PUSHs(sv_2mortal(newSVpvn(data, (STRLEN)length)));
799 14 PUTBACK;
800 14 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 14 0 14
801 14 call_method("_websocket_http_input", G_DISCARD | G_EVAL);
802 14 SPAGAIN;
803  
804 14 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 14 0 14
if (SvTRUE(ERRSV)) { 0 14 14
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 14 PUTBACK;
810 14 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 14 0 14
811 14 LEAVE;
812  
813 14 context->host->release(aTHX_ context->host_context);
814  
815 14 ▶ if (callback_error != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (callback_error != NULL) 0 14 14
816 0 croak_sv(callback_error);
817  
818 14 return LES_CONSUMER_CONTINUE;
819 }
820  
821 static void
822 1 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 1 }
836  
837 static void
838 14 lews_http_bridge_destroy(pTHX_ void *opaque)
839 {
840 14 lews_http_bridge_consumer *context =
841 (lews_http_bridge_consumer *)opaque;
842  
843 PERL_UNUSED_CONTEXT;
844 14 ▶ if (context == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL) 0 14 14
845 0 return;
846  
847 14 ▶ if (context->stream != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context->stream != NULL) 14 0 14
848 14 SvREFCNT_dec(context->stream);
849 14 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 12 ▶ RETVAL = PTR2UV(&lews_http_bridge_consumer_ops);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(&lews_http_bridge_consumer_ops); 6 6 12
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 45 RETVAL = lews_bq_new_object(class, endpoint_type, max_message_size);
903 45 ▶ if (RETVAL == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL == NULL) 0 45 45
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 26 state = lews_bq_from_sv(self);
924 26 data = SvPVbyte(bytes, len);
925 26 used = 0;
926 46 ▶ while (used < (size_t)len) {
Branch: 1 branch
BranchTrueFalseTotal
while (used < (size_t)len) { 26 20 46
927 26 size_t n = bqws_read_from(
928 state->ws,
929 26 data + used,
930 26 (size_t)len - used
931 );
932 26 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 0 26 26
933 0 break;
934 }
935 26 used += n;
936 26 ▶ if (bqws_get_error(state->ws) != BQWS_OK
Branch: 1 branch
BranchTrueFalseTotal
if (bqws_get_error(state->ws) != BQWS_OK 21 5 26
937 21 ▶ || bqws_get_state(state->ws) >= BQWS_STATE_CLOSING) {
Branch: 1 branch
BranchTrueFalseTotal
|| bqws_get_state(state->ws) >= BQWS_STATE_CLOSING) { 20 1 21
938 break;
939 }
940 }
941  
942 49 ▶ while ((msg = bqws_recv(state->ws)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((msg = bqws_recv(state->ws)) != NULL) { 23 26 49
943 23 callback_error = NULL;
944  
945 23 switch (msg->type) {
946 19 case BQWS_MSG_TEXT:
947 19 callback_error = lews_bq_call_event(
948 19 callback_target, connection, 1, msg->data, msg->size, NULL
949 );
950 19 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 23 bqws_free_msg(msg);
970  
971 23 ▶ if (callback_error != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (callback_error != NULL) { 0 23 23
972 0 croak_sv(callback_error);
973 }
974 }
975  
976 26 error = bqws_get_error(state->ws);
977 26 ▶ if (used != (size_t)len && error == BQWS_OK
Branch: 2 branches
BranchTrueFalseTotal
if (used != (size_t)len && error == BQWS_OK 0 26 26
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 26 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 26 26
984 26 PUSHs(sv_2mortal(newSViv((IV)error)));
985 26 PUSHs(sv_2mortal(newSVpv(bqws_error_str(error), 0)));
986 26 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 42 state = lews_bq_from_sv(self);
1006 42 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 »