EVx.xs

filter: Statementstmt 93.2% 468 / 502 Branchbran 65.0% 247 / 380 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.0% 715 / 882 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Net/WebSocket/EVx.pm ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "wslay/wslay.h"
5 #include "EVAPI.h"
6  
7 //windows
8 #ifdef WIN32
9 #ifndef EWOULDBLOCK
10 #define EWOULDBLOCK WSAEWOULDBLOCK
11 #endif
12 #else
13 #include <fcntl.h>
14 #ifndef EWOULDBLOCK
15 #define EWOULDBLOCK EAGAIN
16 #endif
17 #endif
18  
19 #define FRAGMENTED_EOF 0
20 #define FRAGMENTED_ERROR -1
21 #define FRAGMENTED_DATA 1
22 #define MASK_BUFFER_SIZE 1024
23  
24 #define REQUIRE_CTX(ws) if (!(ws)->ctx) { croak("WebSocket connection already closed"); }
25  
26 /* croak_sv is perl 5.13.1+; before that, rethrow via $@ */
27 #ifdef croak_sv
28 #define RETHROW(sv) croak_sv(sv)
29 #else
30 #define RETHROW(sv) STMT_START { sv_setsv(ERRSV, (sv)); croak(NULL); } STMT_END
31 #endif
32  
33 typedef struct {
34 wslay_event_context_ptr ctx;
35 HV* perl_callbacks;
36 ev_io io;
37 ev_cleanup loop_cleanup;
38 struct ev_loop* loop;
39 SV* queue_wait_cb;
40 SV* pending_error;
41 SV** frag_sources;
42 size_t frag_count;
43 size_t frag_size;
44 uint8_t* mask_buf;
45 size_t mask_remaining;
46 struct wslay_event_callbacks callbacks;
47 uint8_t default_rsv;
48 char read_stopped;
49 char write_stopped;
50 char busy;
51 } websocket_object;
52  
53 /* no handlers; its address just identifies our magic among other PERL_MAGIC_ext */
54 static MGVTBL wslay_magic_vtbl;
55  
56 static void wait_io_event(websocket_object* websock_object);
57  
58 /* a watcher cannot ask whether its loop is alive, and EV::default_destroy()
59 frees one out from under us; this fires while it is still usable */
60 1 static void loop_cleanup_event(struct ev_loop* loop, ev_cleanup* w, int revents) {
61 1 websocket_object* websock_object = (websocket_object*) w->data;
62 1 ▶ if (ev_is_active(&(websock_object->io))) { ev_io_stop(loop, &(websock_object->io)); }
Branch: 1 branch
BranchTrueFalseTotal
if (ev_is_active(&(websock_object->io))) { ev_io_stop(loop, &(websock_object->io)); } 1 0 1
63 1 websock_object->loop = NULL;
64 1 }
65  
66 279 static void stop_io_watcher(websocket_object* websock_object) {
67 279 ▶ if (websock_object->loop && ev_is_active(&(websock_object->io))) {
Branch: 2 branches
BranchTrueFalseTotal
if (websock_object->loop && ev_is_active(&(websock_object->io))) { 278 1 279
if (websock_object->loop && ev_is_active(&(websock_object->io))) { 184 94 278
68 184 ev_io_stop(websock_object->loop, &(websock_object->io));
69 }
70 279 }
71  
72 79 static int duplicate_socket(int sock) {
73 79 int fd = dup(sock);
74 79 ▶ if (fd < 0) { croak("Net::WebSocket::EVx - dup(%d) failed: %s", sock, strerror(errno)); }
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { croak("Net::WebSocket::EVx - dup(%d) failed: %s", sock, strerror(errno)); } 0 79 79
75 #ifndef WIN32
76 {
77 79 int fd_flags = fcntl(sock, F_GETFD);
78 79 ▶ if (fd_flags >= 0) { (void) fcntl(fd, F_SETFD, fd_flags); }
Branch: 1 branch
BranchTrueFalseTotal
if (fd_flags >= 0) { (void) fcntl(fd, F_SETFD, fd_flags); } 79 0 79
79 }
80 #endif
81 #if defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL)
82 {
83 int enabled = 1;
84 if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &enabled, sizeof(enabled)) < 0) {
85 int saved_errno = errno;
86 close(fd);
87 croak("Net::WebSocket::EVx - SO_NOSIGPIPE: %s", strerror(saved_errno));
88 }
89 }
90 #endif
91 79 return fd;
92 }
93  
94 103 static ssize_t recv_callback(wslay_event_context_ptr ctx, uint8_t* buf, size_t len, int flags, void* data) {
95 103 websocket_object* websock_object = (websocket_object*) data;
96 ssize_t r;
97 103 ▶ if (websock_object->read_stopped) {
Branch: 1 branch
BranchTrueFalseTotal
if (websock_object->read_stopped) { 0 103 103
98 0 wslay_event_set_error(ctx, WSLAY_ERR_WOULDBLOCK);
99 0 return -1;
100 }
101 103 ▶ while ((r = recv(websock_object->io.fd, buf, len, 0)) == -1 && errno == EINTR);
Branch: 2 branches
BranchTrueFalseTotal
while ((r = recv(websock_object->io.fd, buf, len, 0)) == -1 && errno == EINTR); 38 65 103
while ((r = recv(websock_object->io.fd, buf, len, 0)) == -1 && errno == EINTR); 0 38 38
102 103 ▶ if (r == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (r == -1) { 38 65 103
103 38 ▶ if (errno == EAGAIN || errno == EWOULDBLOCK) {
Branch: 2 branches
BranchTrueFalseTotal
if (errno == EAGAIN || errno == EWOULDBLOCK) { 0 38 38
if (errno == EAGAIN || errno == EWOULDBLOCK) { 0 0 0
104 38 wslay_event_set_error(ctx, WSLAY_ERR_WOULDBLOCK);
105 } else {
106 0 wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);
107 }
108 65 ▶ } else if (r == 0) { /* Unexpected EOF is also treated as an error */
Branch: 1 branch
BranchTrueFalseTotal
} else if (r == 0) { /* Unexpected EOF is also treated as an error */ 1 64 65
109 1 wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);
110 1 r = -1;
111 }
112 103 return r;
113 }
114  
115 1338 static ssize_t send_callback(wslay_event_context_ptr ctx, const uint8_t* buf, size_t len, int flags, void* data) {
116 1338 websocket_object* websock_object = (websocket_object*) data;
117 ssize_t r;
118 1338 int sflags = 0;
119 #ifdef MSG_NOSIGNAL
120 1338 sflags |= MSG_NOSIGNAL;
121 #endif
122 1338 ▶ if (websock_object->write_stopped) {
Branch: 1 branch
BranchTrueFalseTotal
if (websock_object->write_stopped) { 0 1338 1338
123 0 wslay_event_set_error(ctx, WSLAY_ERR_WOULDBLOCK);
124 0 return -1;
125 }
126 #ifdef MSG_MORE
127 1338 ▶ if(flags & WSLAY_MSG_MORE) { sflags |= MSG_MORE; }
Branch: 1 branch
BranchTrueFalseTotal
if(flags & WSLAY_MSG_MORE) { sflags |= MSG_MORE; } 660 678 1338
128 #endif // MSG_MORE
129 1338 ▶ while ((r = send(websock_object->io.fd, buf, len, sflags)) == -1 && errno == EINTR);
Branch: 2 branches
BranchTrueFalseTotal
while ((r = send(websock_object->io.fd, buf, len, sflags)) == -1 && errno == EINTR); 4 1334 1338
while ((r = send(websock_object->io.fd, buf, len, sflags)) == -1 && errno == EINTR); 0 4 4
130 1338 ▶ if (r == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (r == -1) { 4 1334 1338
131 4 ▶ if(errno == EAGAIN || errno == EWOULDBLOCK) {
Branch: 2 branches
BranchTrueFalseTotal
if(errno == EAGAIN || errno == EWOULDBLOCK) { 0 4 4
if(errno == EAGAIN || errno == EWOULDBLOCK) { 0 0 0
132 4 wslay_event_set_error(ctx, WSLAY_ERR_WOULDBLOCK);
133 } else {
134 0 wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);
135 }
136 }
137 1338 return r;
138 }
139  
140 861 static MAGIC* get_wslay_magic (HV* hv) {
141 MAGIC* mg;
142 861 ▶ for (mg = SvMAGIC((SV*) hv); mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC((SV*) hv); mg; mg = mg->mg_moremagic) { 861 0 861
143 861 ▶ if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &wslay_magic_vtbl) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &wslay_magic_vtbl) { 861 0 861
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &wslay_magic_vtbl) { 861 0 861
144 861 return mg;
145 }
146 }
147 0 return NULL;
148 }
149  
150 778 static websocket_object* get_wslay_context (HV* hv) {
151 778 MAGIC* mg = get_wslay_magic(hv);
152 778 ▶ if (!mg || !mg->mg_ptr) {
Branch: 2 branches
BranchTrueFalseTotal
if (!mg || !mg->mg_ptr) { 778 0 778
if (!mg || !mg->mg_ptr) { 1 777 778
153 1 croak("Net::WebSocket::EVx - object is not initialized or has already been destroyed");
154 }
155 777 return (websocket_object*) mg->mg_ptr;
156 }
157  
158 /* hold a callback's exception instead of letting it longjmp out of wslay/libev */
159 709 static void catch_perl_error(websocket_object* websock_object) {
160 709 ▶ if (!SvTRUE(ERRSV)) { return; }
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) { return; } 709 0 709
if (!SvTRUE(ERRSV)) { return; } 702 7 709
161 7 ▶ if (!websock_object->pending_error) { websock_object->pending_error = newSVsv(ERRSV); }
Branch: 2 branches
BranchTrueFalseTotal
if (!websock_object->pending_error) { websock_object->pending_error = newSVsv(ERRSV); } 7 0 7
if (!websock_object->pending_error) { websock_object->pending_error = newSVsv(ERRSV); } 7 0 7
162 7 ▶ sv_setpvn(ERRSV, "", 0);
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvn(ERRSV, "", 0); 7 0 7
163 }
164  
165 871 static SV* take_perl_error(websocket_object* websock_object) {
166 871 SV* error = websock_object->pending_error;
167 871 websock_object->pending_error = NULL;
168 871 return error;
169 }
170  
171 /* a callback may drop the last reference to its own object (undef $ws), freeing
172 the struct we are using; UNGUARD is what then runs DESTROY, so nothing may
173 touch the object after it */
174 #define GUARD(ws) SvREFCNT_inc((SV*) (ws)->perl_callbacks)
175 #define UNGUARD(ws) SvREFCNT_dec((SV*) (ws)->perl_callbacks)
176  
177 18 static SV* retain_callback(SV* cb) {
178 18 SvGETMAGIC(cb);
179 18 ▶ if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) { return SvREFCNT_inc(SvRV(cb)); }
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) { return SvREFCNT_inc(SvRV(cb)); } 18 0 18
if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) { return SvREFCNT_inc(SvRV(cb)); } 18 0 18
180 0 return newSVsv(cb);
181 }
182  
183 7 static SV* track_fragmented_source(websocket_object* websock_object, SV* cb) {
184 7 ▶ if (websock_object->frag_count == websock_object->frag_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (websock_object->frag_count == websock_object->frag_size) { 7 0 7
185 7 ▶ size_t size = websock_object->frag_size ? websock_object->frag_size * 2 : 4;
Branch: 1 branch
BranchTrueFalseTotal
size_t size = websock_object->frag_size ? websock_object->frag_size * 2 : 4; 0 7 7
186 7 SV** sources = (SV**) realloc(websock_object->frag_sources, size * sizeof(SV*));
187 7 ▶ if (!sources) { croak("Net::WebSocket::EVx - out of memory"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!sources) { croak("Net::WebSocket::EVx - out of memory"); } 0 7 7
188 7 websock_object->frag_sources = sources;
189 7 websock_object->frag_size = size;
190 }
191 7 cb = retain_callback(cb);
192 7 websock_object->frag_sources[websock_object->frag_count++] = cb;
193 7 return cb;
194 }
195  
196 5 static void release_fragmented_source(websocket_object* websock_object, SV* cb) {
197 size_t i;
198 5 ▶ for (i = 0; i < websock_object->frag_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < websock_object->frag_count; i++) { 5 0 5
199 5 ▶ if (websock_object->frag_sources[i] == cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (websock_object->frag_sources[i] == cb) { 5 0 5
200 5 websock_object->frag_sources[i] = websock_object->frag_sources[--websock_object->frag_count];
201 5 SvREFCNT_dec(cb);
202 5 return;
203 }
204 }
205 }
206  
207 638 static int genmask_callback(wslay_event_context_ptr ctx, uint8_t* buf, size_t len, void* data) {
208 638 websocket_object* websock_object = (websocket_object*) data;
209 SV** cb;
210 638 ▶ if (websock_object->pending_error) { return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (websock_object->pending_error) { return -1; } 0 638 638
211 638 ▶ if ((cb = hv_fetch(websock_object->perl_callbacks , "genmask", 7, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((cb = hv_fetch(websock_object->perl_callbacks , "genmask", 7, 0))) { 3 635 638
212 3 int count, failed = 0;
213 SV* sv_data;
214 STRLEN source_len;
215 char *source_buf;
216 3 dSP;
217 3 ENTER;
218 3 SAVETMPS;
219 3 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
220 3 ▶ XPUSHs(sv_2mortal(newSVuv(len)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVuv(len))); 0 3 3
221 3 PUTBACK;
222 3 count = call_sv(*cb, G_SCALAR|G_EVAL);
223 3 SPAGAIN;
224 3 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 3 0 3
if (SvTRUE(ERRSV)) { 0 3 3
225 0 catch_perl_error(websock_object);
226 0 failed = 1;
227 } else {
228 3 ▶ sv_data = count ? SP[0] : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
sv_data = count ? SP[0] : &PL_sv_undef; 3 0 3
229 /* an undef return is a length error, not something to warn about */
230 5 ▶ if (SvOK(sv_data)) { source_buf = SvPV(sv_data, source_len); }
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(sv_data)) { source_buf = SvPV(sv_data, source_len); } 2 1 3
231 1 else { source_buf = NULL; source_len = 0; }
232 3 ▶ if (source_len != len) {
Branch: 1 branch
BranchTrueFalseTotal
if (source_len != len) { 2 1 3
233 2 websock_object->pending_error = newSVpvf(
234 "Net::WebSocket::EVx - genmask callback must return exactly %" UVuf " bytes, got %" UVuf,
235 (UV) len, (UV) source_len
236 );
237 2 failed = 1;
238 } else {
239 1 memcpy(buf, source_buf, len);
240 }
241 }
242 3 SP -= count;
243 3 PUTBACK;
244 3 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 3 0 3
245 3 LEAVE;
246 3 ▶ if (failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (failed) { 2 1 3
247 2 wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);
248 2 return -1;
249 }
250 } else {
251 635 ▶ if (len > websock_object->mask_remaining) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > websock_object->mask_remaining) { 28 607 635
252 28 int fd, saved_errno = 0;
253 28 size_t used = 0;
254 28 int oflags = O_RDONLY;
255 #ifdef O_CLOEXEC
256 28 oflags |= O_CLOEXEC;
257 #endif
258 28 ▶ if (!websock_object->mask_buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (!websock_object->mask_buf) { 26 2 28
259 26 websock_object->mask_buf = (uint8_t*) malloc(MASK_BUFFER_SIZE);
260 26 ▶ if (!websock_object->mask_buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (!websock_object->mask_buf) { 0 26 26
261 0 websock_object->pending_error = newSVpv("Net::WebSocket::EVx - out of memory", 0);
262 0 return -1;
263 }
264 }
265 28 ▶ do { fd = open("/dev/urandom", oflags); } while (fd < 0 && errno == EINTR);
Branch: 2 branches
BranchTrueFalseTotal
do { fd = open("/dev/urandom", oflags); } while (fd < 0 && errno == EINTR); 0 28 28
do { fd = open("/dev/urandom", oflags); } while (fd < 0 && errno == EINTR); 0 0 0
266 28 ▶ if (fd < 0) { saved_errno = errno; }
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { saved_errno = errno; } 0 28 28
267 else {
268 56 ▶ while (used < MASK_BUFFER_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
while (used < MASK_BUFFER_SIZE) { 28 28 56
269 28 ssize_t r = read(fd, websock_object->mask_buf + used, MASK_BUFFER_SIZE - used);
270 28 ▶ if (r > 0) { used += r; }
Branch: 1 branch
BranchTrueFalseTotal
if (r > 0) { used += r; } 28 0 28
271 0 ▶ else if (r < 0 && errno == EINTR) { continue; }
Branch: 2 branches
BranchTrueFalseTotal
else if (r < 0 && errno == EINTR) { continue; } 0 0 0
else if (r < 0 && errno == EINTR) { continue; } 0 0 0
272 0 ▶ else { saved_errno = r < 0 ? errno : EIO; break; }
Branch: 1 branch
BranchTrueFalseTotal
else { saved_errno = r < 0 ? errno : EIO; break; } 0 0 0
273 }
274 28 close(fd);
275 }
276 28 ▶ if (saved_errno) {
Branch: 1 branch
BranchTrueFalseTotal
if (saved_errno) { 0 28 28
277 0 websock_object->pending_error = newSVpvf("Net::WebSocket::EVx - mask entropy: %s", strerror(saved_errno));
278 0 return -1;
279 }
280 28 websock_object->mask_remaining = MASK_BUFFER_SIZE;
281 }
282 635 websock_object->mask_remaining -= len;
283 635 memcpy(buf, websock_object->mask_buf + websock_object->mask_remaining, len);
284 }
285 636 return 0;
286 }
287  
288 647 static void on_frame_recv_start_callback (wslay_event_context_ptr ctx, const struct wslay_event_on_frame_recv_start_arg* frame, void* data) {
289 647 websocket_object* websock_object = (websocket_object*) data;
290 SV** cb;
291 647 ▶ if (websock_object->pending_error) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (websock_object->pending_error) { return; } 0 647 647
292 647 ▶ if (!(cb = hv_fetch(websock_object->perl_callbacks, "on_frame_recv_start", 19, 0)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(cb = hv_fetch(websock_object->perl_callbacks, "on_frame_recv_start", 19, 0)) ) { 640 7 647
293 640 return;
294 }
295 {
296 7 dSP;
297 7 ENTER;
298 7 SAVETMPS;
299 7 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 7 7
300 7 ▶ EXTEND(SP, 4);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 4); 0 7 7
301 7 PUSHs(sv_2mortal(newSViv(frame->fin)));
302 7 PUSHs(sv_2mortal(newSViv(frame->rsv)));
303 7 PUSHs(sv_2mortal(newSViv(frame->opcode)));
304 7 PUSHs(sv_2mortal(newSVuv(frame->payload_length)));
305 7 PUTBACK;
306 7 call_sv(*cb, G_VOID|G_DISCARD|G_EVAL);
307 7 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 7 0 7
308 7 LEAVE;
309 }
310 7 catch_perl_error(websock_object);
311 }
312  
313 663 static void on_frame_recv_chunk_callback (wslay_event_context_ptr ctx, const struct wslay_event_on_frame_recv_chunk_arg* chunk, void* data) {
314 663 websocket_object* websock_object = (websocket_object*) data;
315 SV** cb;
316 663 ▶ if (websock_object->pending_error) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (websock_object->pending_error) { return; } 1 662 663
317 662 ▶ if (!(cb = hv_fetch(websock_object->perl_callbacks, "on_frame_recv_chunk", 19, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(cb = hv_fetch(websock_object->perl_callbacks, "on_frame_recv_chunk", 19, 0))) { 657 5 662
318 657 return;
319 }
320 {
321 5 dSP;
322 5 ENTER;
323 5 SAVETMPS;
324 5 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 5 5
325 5 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 5 5
326 5 PUSHs(sv_2mortal(newSVpvn((const char*) chunk->data, chunk->data_length)));
327 5 PUTBACK;
328 5 call_sv(*cb, G_VOID|G_DISCARD|G_EVAL);
329 5 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 5 0 5
330 5 LEAVE;
331 }
332 5 catch_perl_error(websock_object);
333 }
334  
335 646 static void on_frame_recv_end_callback(wslay_event_context_ptr ctx, void* data) {
336 646 websocket_object* websock_object = (websocket_object*) data;
337 SV** cb;
338 646 ▶ if (websock_object->pending_error) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (websock_object->pending_error) { return; } 2 644 646
339 644 ▶ if (!(cb = hv_fetch(websock_object->perl_callbacks, "on_frame_recv_end", 17, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(cb = hv_fetch(websock_object->perl_callbacks, "on_frame_recv_end", 17, 0))) { 639 5 644
340 639 return;
341 }
342 {
343 5 dSP;
344 5 ENTER;
345 5 SAVETMPS;
346 5 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 5 5
347 5 call_sv(*cb, G_DISCARD|G_NOARGS|G_EVAL);
348 5 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 5 5
349 5 LEAVE;
350 }
351 5 catch_perl_error(websock_object);
352 }
353  
354 644 static void on_msg_recv_callback(wslay_event_context_ptr ctx, const struct wslay_event_on_msg_recv_arg* msg, void* data) {
355 644 websocket_object* websock_object = (websocket_object*) data;
356 SV** cb;
357 SV* msg_data;
358 644 ▶ if (msg->opcode == 0x08) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (msg->opcode == 0x08) { return; } 6 638 644
359 638 ▶ if (websock_object->pending_error) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (websock_object->pending_error) { return; } 3 635 638
360 635 ▶ if (!(cb = hv_fetch(websock_object->perl_callbacks, "on_msg_recv", 11, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(cb = hv_fetch(websock_object->perl_callbacks, "on_msg_recv", 11, 0))) { 5 630 635
361 5 return;
362 }
363 630 ▶ msg_data = newSVpvn(msg->msg ? (const char*) msg->msg : "", msg->msg_length);
Branch: 1 branch
BranchTrueFalseTotal
msg_data = newSVpvn(msg->msg ? (const char*) msg->msg : "", msg->msg_length); 624 6 630
364 630 ▶ if (!(msg->rsv & WSLAY_RSV1_BIT) && msg->opcode == 1) { SvUTF8_on(msg_data); }
Branch: 2 branches
BranchTrueFalseTotal
if (!(msg->rsv & WSLAY_RSV1_BIT) && msg->opcode == 1) { SvUTF8_on(msg_data); } 628 2 630
if (!(msg->rsv & WSLAY_RSV1_BIT) && msg->opcode == 1) { SvUTF8_on(msg_data); } 615 13 628
365 {
366 630 dSP;
367 630 ENTER;
368 630 SAVETMPS;
369 630 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 630 630
370 630 ▶ EXTEND(SP, 4);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 4); 0 630 630
371 630 PUSHs(sv_2mortal(newSViv(msg->rsv)));
372 630 PUSHs(sv_2mortal(newSViv(msg->opcode)));
373 630 PUSHs(sv_2mortal(msg_data));
374 630 PUSHs(sv_2mortal(newSViv(msg->status_code)));
375 630 PUTBACK;
376 630 call_sv(*cb, G_VOID|G_DISCARD|G_EVAL);
377 630 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 630 0 630
378 630 LEAVE;
379 }
380 630 catch_perl_error(websock_object);
381 }
382  
383 9 static ssize_t fragmented_msg_callback(wslay_event_context_ptr ctx, uint8_t* buf, size_t len, const union wslay_event_msg_source* source, int* eof, void* userdata) {
384 9 websocket_object* websock_object = (websocket_object*) userdata;
385 9 SV* cb = (SV*) source->data;
386 9 ssize_t bytes_written = 0;
387 9 int count, failed = 0;
388 9 SV* data = &PL_sv_undef;
389 9 int status = FRAGMENTED_DATA;
390 STRLEN source_len;
391 char* source_buf;
392 9 ▶ if (websock_object->pending_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (websock_object->pending_error) { 0 9 9
393 0 release_fragmented_source(websock_object, cb);
394 0 wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);
395 0 return -1;
396 }
397 {
398 9 dSP;
399 9 ENTER;
400 9 SAVETMPS;
401 9 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 9 9
402 9 ▶ XPUSHs(sv_2mortal(newSVuv(len)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVuv(len))); 0 9 9
403 9 PUTBACK;
404 9 count = call_sv(cb, G_ARRAY|G_EVAL);
405 9 SPAGAIN;
406 9 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 9 0 9
if (SvTRUE(ERRSV)) { 0 9 9
407 0 catch_perl_error(websock_object);
408 0 failed = 1;
409 9 ▶ } else if (count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (count == 1) { 4 5 9
410 4 data = SP[0];
411 5 ▶ } else if (count == 2) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (count == 2) { 5 0 5
412 5 status = SvIV(SP[0]);
413 5 data = SP[-1];
414 } else {
415 0 websock_object->pending_error = newSVpv(
416 "Net::WebSocket::EVx - fragmented msg cb MUST return one or two elements!", 0
417 );
418 0 failed = 1;
419 }
420 9 ▶ if (!failed && SvOK(data)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!failed && SvOK(data)) { 9 0 9
if (!failed && SvOK(data)) { 9 0 9
421 9 source_buf = SvPV(data, source_len);
422 9 ▶ if (source_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (source_len) { 6 3 9
423 6 bytes_written = (ssize_t) (source_len < len ? source_len : len);
424 6 memcpy(buf, source_buf, bytes_written);
425 }
426 }
427 9 SP -= count;
428 9 PUTBACK;
429 9 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 9 0 9
430 9 LEAVE;
431 }
432 9 ▶ if (failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (failed) { 0 9 9
433 0 release_fragmented_source(websock_object, cb);
434 0 wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE);
435 0 return -1;
436 }
437 9 ▶ if (status == FRAGMENTED_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == FRAGMENTED_EOF) { 4 5 9
438 4 *eof = 1;
439 4 release_fragmented_source(websock_object, cb);
440 5 ▶ } else if (status == FRAGMENTED_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (status == FRAGMENTED_ERROR) { 1 4 5
441 1 bytes_written = -1;
442 1 wslay_event_set_error(websock_object->ctx, WSLAY_ERR_CALLBACK_FAILURE);
443 1 release_fragmented_source(websock_object, cb);
444 }
445 // else - FRAGMENTED_DATA
446 9 return bytes_written;
447 }
448  
449 //////////////////////
450 93 static void close_connection(websocket_object* websock_object) {
451 int status;
452 size_t i, frag_count;
453 SV** sources;
454 SV* waiter;
455 SV** cb;
456 93 ▶ if (!websock_object->ctx) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!websock_object->ctx) { return; } 14 79 93
457 79 status = wslay_event_get_status_code_received(websock_object->ctx);
458 79 wslay_event_context_free(websock_object->ctx);
459 79 websock_object->ctx = NULL;
460 79 stop_io_watcher(websock_object);
461 79 ▶ if (websock_object->io.fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (websock_object->io.fd >= 0) { 79 0 79
462 79 close(websock_object->io.fd); /* our own dup(), never the caller's descriptor */
463 79 websock_object->io.fd = -1;
464 }
465 79 free(websock_object->mask_buf);
466 79 websock_object->mask_buf = NULL;
467 79 websock_object->mask_remaining = 0;
468 79 frag_count = websock_object->frag_count;
469 79 sources = websock_object->frag_sources;
470 79 websock_object->frag_count = 0;
471 79 websock_object->frag_size = 0;
472 79 websock_object->frag_sources = NULL;
473 81 ▶ for (i = 0; i < frag_count; i++) { SvREFCNT_dec(sources[i]); }
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < frag_count; i++) { SvREFCNT_dec(sources[i]); } 2 79 81
474 79 free(sources);
475 79 waiter = websock_object->queue_wait_cb;
476 79 websock_object->queue_wait_cb = NULL;
477 79 ▶ if (waiter) { SvREFCNT_dec(waiter); }
Branch: 1 branch
BranchTrueFalseTotal
if (waiter) { SvREFCNT_dec(waiter); } 1 78 79
478 /* no perl during global destruction: callbacks and what they close over may be gone */
479 79 ▶ if (PL_dirty) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (PL_dirty) { return; } 10 69 79
480 69 ▶ if ((cb = hv_fetch(websock_object->perl_callbacks, "on_close", 8, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((cb = hv_fetch(websock_object->perl_callbacks, "on_close", 8, 0))) { 53 16 69
481 53 dSP;
482 53 ENTER;
483 53 SAVETMPS;
484 53 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 53 53
485 53 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 53 53
486 53 PUSHs(sv_2mortal(newSViv(status)));
487 53 PUTBACK;
488 53 call_sv(*cb, G_VOID|G_DISCARD|G_EVAL);
489 53 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 53 0 53
490 53 LEAVE;
491 53 catch_perl_error(websock_object);
492 };
493 }
494  
495 109 static void wslay_io_event (struct ev_loop* loop, struct ev_io* w, int revents) {
496 109 websocket_object* websock_object = (websocket_object*) w->data;
497 SV* error;
498 109 GUARD(websock_object); /* wslay parses into the struct across every callback */
499 109 websock_object->busy = 1;
500 109 ▶ if ((revents & EV_READ) && !websock_object->read_stopped) {
Branch: 2 branches
BranchTrueFalseTotal
if ((revents & EV_READ) && !websock_object->read_stopped) { 48 61 109
if ((revents & EV_READ) && !websock_object->read_stopped) { 48 0 48
501 48 ▶ if (wslay_event_recv(websock_object->ctx)) {
Branch: 1 branch
BranchTrueFalseTotal
if (wslay_event_recv(websock_object->ctx)) { 1 47 48
502 1 close_connection(websock_object);
503 1 goto done;
504 }
505 }
506 108 ▶ if (!websock_object->ctx) { goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!websock_object->ctx) { goto done; } 0 108 108
507 108 ▶ if ((revents & EV_WRITE) && !websock_object->write_stopped && !websock_object->pending_error) {
Branch: 3 branches
BranchTrueFalseTotal
if ((revents & EV_WRITE) && !websock_object->write_stopped && !websock_object->pending_error) { 43 65 108
if ((revents & EV_WRITE) && !websock_object->write_stopped && !websock_object->pending_error) { 0 65 65
if ((revents & EV_WRITE) && !websock_object->write_stopped && !websock_object->pending_error) { 4 61 65
508 61 ▶ if (wslay_event_send(websock_object->ctx)) {
Branch: 1 branch
BranchTrueFalseTotal
if (wslay_event_send(websock_object->ctx)) { 58 3 61
509 3 close_connection(websock_object);
510 3 goto done;
511 }
512 }
513 105 done:
514 109 websock_object->busy = 0;
515 109 wait_io_event(websock_object);
516 109 error = take_perl_error(websock_object);
517 109 UNGUARD(websock_object);
518 109 ▶ if (error) { RETHROW(sv_2mortal(error)); }
Branch: 1 branch
BranchTrueFalseTotal
if (error) { RETHROW(sv_2mortal(error)); } 9 100 109
519 100 };
520  
521 871 static void wait_io_event(websocket_object* websock_object) {
522 871 int events = 0;
523 871 ▶ if (websock_object->busy) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (websock_object->busy) { return; } 0 871 871
524 871 websock_object->busy = 1;
525 871 ▶ if (!websock_object->loop || !websock_object->ctx) { goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!websock_object->loop || !websock_object->ctx) { goto done; } 871 0 871
if (!websock_object->loop || !websock_object->ctx) { goto done; } 4 867 871
526 867 while (
527 897 websock_object->queue_wait_cb &&
528 21 ▶ websock_object->loop && websock_object->ctx &&
Branch: 2 branches
BranchTrueFalseTotal
websock_object->loop && websock_object->ctx && 21 0 21
websock_object->loop && websock_object->ctx && 21 0 21
529 21 ▶ !websock_object->pending_error &&
Branch: 1 branch
BranchTrueFalseTotal
!websock_object->pending_error && 20 1 21
530 40 ▶ !(websock_object->read_stopped && websock_object->write_stopped) &&
Branch: 1 branch
BranchTrueFalseTotal
!(websock_object->read_stopped && websock_object->write_stopped) && 0 20 20
531 905 ▶ !wslay_event_want_write(websock_object->ctx) &&
Branch: 2 branches
BranchTrueFalseTotal
!wslay_event_want_write(websock_object->ctx) && 21 855 876
!wslay_event_want_write(websock_object->ctx) && 9 0 9
532 9 !wslay_event_get_queued_msg_count(websock_object->ctx)
533 ) {
534 9 SV* wait_cb = websock_object->queue_wait_cb;
535 9 websock_object->queue_wait_cb = NULL;
536 {
537 9 dSP;
538 9 ENTER;
539 9 SAVETMPS;
540 9 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 9 9
541 9 call_sv(wait_cb, G_DISCARD|G_NOARGS|G_EVAL);
542 9 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 9 9
543 9 LEAVE;
544 }
545 9 SvREFCNT_dec(wait_cb);
546 9 catch_perl_error(websock_object);
547 }
548 867 ▶ if (!websock_object->loop || !websock_object->ctx) { goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!websock_object->loop || !websock_object->ctx) { goto done; } 867 0 867
if (!websock_object->loop || !websock_object->ctx) { goto done; } 0 867 867
549 867 ▶ if (!websock_object->read_stopped && wslay_event_want_read(websock_object->ctx)) { events |= EV_READ; }
Branch: 2 branches
BranchTrueFalseTotal
if (!websock_object->read_stopped && wslay_event_want_read(websock_object->ctx)) { events |= EV_READ; } 864 3 867
if (!websock_object->read_stopped && wslay_event_want_read(websock_object->ctx)) { events |= EV_READ; } 845 19 864
550 867 ▶ if (!websock_object->write_stopped && wslay_event_want_write(websock_object->ctx)) { events |= EV_WRITE; }
Branch: 2 branches
BranchTrueFalseTotal
if (!websock_object->write_stopped && wslay_event_want_write(websock_object->ctx)) { events |= EV_WRITE; } 849 18 867
if (!websock_object->write_stopped && wslay_event_want_write(websock_object->ctx)) { events |= EV_WRITE; } 678 171 849
551 867 ▶ if (events) {
Branch: 1 branch
BranchTrueFalseTotal
if (events) { 852 15 867
552 852 ▶ if (!ev_is_active(&(websock_object->io)) || (websock_object->io.events & (EV_READ | EV_WRITE)) != events) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ev_is_active(&(websock_object->io)) || (websock_object->io.events & (EV_READ | EV_WRITE)) != events) { 769 83 852
if (!ev_is_active(&(websock_object->io)) || (websock_object->io.events & (EV_READ | EV_WRITE)) != events) { 102 667 769
553 185 stop_io_watcher(websock_object);
554 185 ev_io_set(&(websock_object->io), websock_object->io.fd, events);
555 185 ev_io_start(websock_object->loop, &(websock_object->io));
556 }
557 } else {
558 15 stop_io_watcher(websock_object);
559 15 ▶ if (!wslay_event_get_read_enabled(websock_object->ctx) && !wslay_event_get_write_enabled(websock_object->ctx)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!wslay_event_get_read_enabled(websock_object->ctx) && !wslay_event_get_write_enabled(websock_object->ctx)) { 3 12 15
if (!wslay_event_get_read_enabled(websock_object->ctx) && !wslay_event_get_write_enabled(websock_object->ctx)) { 2 10 12
560 10 close_connection(websock_object);
561 }
562 }
563 5 done:
564 871 websock_object->busy = 0;
565 };
566  
567 /* wait_io_event for the XSUBs: guarded, and rethrows what a callback died with */
568 768 static void wait_io_event_guarded(websocket_object* websock_object) {
569 SV* error;
570 768 ▶ if (websock_object->busy) { return; } /* finish wslay before changing watchers or freeing its context */
Branch: 1 branch
BranchTrueFalseTotal
if (websock_object->busy) { return; } /* finish wslay before changing watchers or freeing its context */ 6 762 768
571 762 GUARD(websock_object);
572 762 wait_io_event(websock_object);
573 762 error = take_perl_error(websock_object);
574 762 UNGUARD(websock_object);
575 762 ▶ if (error) { RETHROW(sv_2mortal(error)); }
Branch: 1 branch
BranchTrueFalseTotal
if (error) { RETHROW(sv_2mortal(error)); } 0 762 762
576 }
577  
578  
579 MODULE = Net::WebSocket::EVx PACKAGE = Net::WebSocket::EVx
580  
581  
582 BOOT:
583 {
584 6 ▶ I_EV_API("Net::WebSocket::EVx");
Branch: 3 branches
BranchTrueFalseTotal
I_EV_API("Net::WebSocket::EVx"); 0 6 6
I_EV_API("Net::WebSocket::EVx"); 6 0 6
I_EV_API("Net::WebSocket::EVx"); 0 6 6
585 #ifdef WIN32
586 _setmaxstdio(2048);
587 #endif
588 }
589  
590 PROTOTYPES: DISABLE
591  
592 void _wslay_event_context_init(object, sock, is_server)
593 HV* object
594 int sock
595 int is_server
596 CODE:
597 websocket_object* websock_object;
598 int fd;
599 79 ▶ if (sock < 0) { croak("Net::WebSocket::EVx - invalid file descriptor: %d", sock); }
Branch: 1 branch
BranchTrueFalseTotal
if (sock < 0) { croak("Net::WebSocket::EVx - invalid file descriptor: %d", sock); } 0 79 79
600 /* own a private copy: closing the caller's would have perl close it
601 again when their handle is reaped, killing whatever reused the number */
602 79 fd = duplicate_socket(sock);
603 79 websock_object = calloc(1, sizeof(websocket_object));
604 79 ▶ if (!websock_object) { close(fd); croak("Net::WebSocket::EVx - out of memory"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!websock_object) { close(fd); croak("Net::WebSocket::EVx - out of memory"); } 0 79 79
605 79 ev_io_init(&(websock_object->io), wslay_io_event, fd, EV_READ);
606 79 websock_object->io.data = (void*) websock_object;
607 79 ev_cleanup_init(&(websock_object->loop_cleanup), loop_cleanup_event);
608 79 websock_object->loop_cleanup.data = (void*) websock_object;
609 79 websock_object->loop = EV_DEFAULT;
610 79 websock_object->perl_callbacks = object;
611 79 websock_object->default_rsv = WSLAY_RSV1_BIT;
612 79 websock_object->callbacks.recv_callback = recv_callback;
613 79 websock_object->callbacks.send_callback = send_callback;
614 79 websock_object->callbacks.genmask_callback = genmask_callback;
615 79 websock_object->callbacks.on_frame_recv_start_callback = on_frame_recv_start_callback;
616 79 websock_object->callbacks.on_frame_recv_chunk_callback = on_frame_recv_chunk_callback;
617 79 websock_object->callbacks.on_frame_recv_end_callback = on_frame_recv_end_callback;
618 79 websock_object->callbacks.on_msg_recv_callback = on_msg_recv_callback;
619 158 ▶ if (is_server
Branch: 2 branches
BranchTrueFalseTotal
if (is_server 43 36 79
if (is_server 0 79 79
620 43 ? wslay_event_context_server_init(&(websock_object->ctx), &(websock_object->callbacks), websock_object)
621 36 : wslay_event_context_client_init(&(websock_object->ctx), &(websock_object->callbacks), websock_object)
622 ) {
623 0 close(fd);
624 0 free(websock_object);
625 0 croak("Can't initialize! WSLAY_ERR_NOMEM \n");
626 }
627 79 sv_magicext((SV*) object, NULL, PERL_MAGIC_ext, &wslay_magic_vtbl, (const char *) websock_object, 0);
628 79 ev_cleanup_start(websock_object->loop, &(websock_object->loop_cleanup));
629 79 wslay_event_config_set_allowed_rsv_bits(websock_object->ctx, WSLAY_RSV1_BIT);
630 79 wait_io_event_guarded(websock_object);
631  
632 void _wslay_event_config_set_no_buffering (object, buffering)
633 HV* object
634 int buffering
635 CODE:
636 79 websocket_object* websock_object = get_wslay_context(object);
637 79 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 79 79
638 79 wslay_event_config_set_no_buffering(websock_object->ctx, buffering);
639  
640 void _wslay_event_config_set_max_recv_msg_length(object, len)
641 HV* object
642 UV len
643 CODE:
644 0 websocket_object* websock_object = get_wslay_context(object);
645 0 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 0 0
646 0 wslay_event_config_set_max_recv_msg_length(websock_object->ctx, len);
647  
648 void _wslay_event_config_set_allowed_rsv_bits(object, rsv)
649 HV* object
650 int rsv
651 CODE:
652 2 websocket_object* websock_object = get_wslay_context(object);
653 2 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 2 2
654 2 wslay_event_config_set_allowed_rsv_bits(websock_object->ctx, (uint8_t) rsv);
655  
656 void _set_default_rsv(object, rsv)
657 HV* object
658 int rsv
659 CODE:
660 5 websocket_object* websock_object = get_wslay_context(object);
661 5 websock_object->default_rsv = (uint8_t) rsv;
662  
663 void shutdown_read(object)
664 HV* object
665 CODE:
666 2 websocket_object* websock_object = get_wslay_context(object);
667 2 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 2 2
668 2 wslay_event_shutdown_read(websock_object->ctx);
669 2 wait_io_event_guarded(websock_object);
670  
671 void shutdown_write(object)
672 HV* object
673 CODE:
674 2 websocket_object* websock_object = get_wslay_context(object);
675 2 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 2 2
676 2 wslay_event_shutdown_write(websock_object->ctx);
677 2 wait_io_event_guarded(websock_object);
678  
679 void stop(object)
680 HV* object
681 CODE:
682 1 websocket_object* websock_object = get_wslay_context(object);
683 1 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 1 1
684 1 websock_object->read_stopped = 1;
685 1 websock_object->write_stopped = 1;
686 1 wait_io_event_guarded(websock_object);
687  
688 void stop_read(object)
689 HV* object
690 CODE:
691 2 websocket_object* websock_object = get_wslay_context(object);
692 2 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 2 2
693 2 websock_object->read_stopped = 1;
694 2 wait_io_event_guarded(websock_object);
695  
696 void stop_write(object)
697 HV* object
698 CODE:
699 7 websocket_object* websock_object = get_wslay_context(object);
700 7 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 7 7
701 7 websock_object->write_stopped = 1;
702 7 wait_io_event_guarded(websock_object);
703  
704 void start(object)
705 HV* object
706 CODE:
707 1 websocket_object* websock_object = get_wslay_context(object);
708 1 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 1 1
709 1 websock_object->read_stopped = 0;
710 1 websock_object->write_stopped = 0;
711 1 wait_io_event_guarded(websock_object);
712  
713 void start_read(object)
714 HV* object
715 CODE:
716 2 websocket_object* websock_object = get_wslay_context(object);
717 2 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 2 2
718 2 websock_object->read_stopped = 0;
719 2 wait_io_event_guarded(websock_object);
720  
721 void start_write(object)
722 HV* object
723 CODE:
724 5 websocket_object* websock_object = get_wslay_context(object);
725 5 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 5 5
726 5 websock_object->write_stopped = 0;
727 5 wait_io_event_guarded(websock_object);
728  
729 void _set_waiter(object, waiter)
730 HV* object
731 SV* waiter
732 CODE:
733 11 websocket_object* websock_object = get_wslay_context(object);
734 11 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 11 11
735 11 SV* cb = retain_callback(waiter);
736 11 ▶ if (websock_object->queue_wait_cb) { SvREFCNT_dec(websock_object->queue_wait_cb); }
Branch: 1 branch
BranchTrueFalseTotal
if (websock_object->queue_wait_cb) { SvREFCNT_dec(websock_object->queue_wait_cb); } 1 10 11
737 11 websock_object->queue_wait_cb = cb;
738 11 wait_io_event_guarded(websock_object);
739  
740 int queue_msg (object, data, opcode=1)
741 HV* object
742 SV* data
743 int opcode
744 CODE:
745 640 websocket_object* websock_object = get_wslay_context(object);
746 639 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 639 639
747 STRLEN len;
748 struct wslay_event_msg msg;
749 639 msg.msg = (const uint8_t*) SvPV(data, len);
750 639 msg.msg_length = len;
751 639 msg.opcode = opcode;
752 639 int result = wslay_event_queue_msg(websock_object->ctx, &msg);
753 639 ▶ if (result == WSLAY_ERR_INVALID_ARGUMENT) { croak("Wslay queue_msg - WSLAY_ERR_INVALID_ARGUMENT"); }
Branch: 1 branch
BranchTrueFalseTotal
if (result == WSLAY_ERR_INVALID_ARGUMENT) { croak("Wslay queue_msg - WSLAY_ERR_INVALID_ARGUMENT"); } 0 639 639
754 639 ▶ if (result == WSLAY_ERR_NOMEM) { croak("Wslay queue_msg - WSLAY_ERR_NOMEM"); }
Branch: 1 branch
BranchTrueFalseTotal
if (result == WSLAY_ERR_NOMEM) { croak("Wslay queue_msg - WSLAY_ERR_NOMEM"); } 0 639 639
755 639 wait_io_event_guarded(websock_object);
756 639 ▶ RETVAL = result;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = result; 608 31 639
757 OUTPUT:
758 RETVAL
759  
760 int queue_msg_ex (object, data, opcode=1, rsv=-1)
761 HV* object
762 SV* data
763 int opcode
764 int rsv
765 CODE:
766 6 websocket_object* websock_object = get_wslay_context(object);
767 6 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 6 6
768 STRLEN len;
769 struct wslay_event_msg msg;
770 6 ▶ if (rsv < 0) { rsv = websock_object->default_rsv; }
Branch: 1 branch
BranchTrueFalseTotal
if (rsv < 0) { rsv = websock_object->default_rsv; } 2 4 6
771 6 msg.msg = (const uint8_t*) SvPV(data, len);
772 6 msg.msg_length = len;
773 6 msg.opcode = opcode;
774 6 int result = wslay_event_queue_msg_ex(websock_object->ctx, &msg, (uint8_t) rsv);
775 6 ▶ if (result == WSLAY_ERR_INVALID_ARGUMENT) { croak("Wslay queue_msg_ex - WSLAY_ERR_INVALID_ARGUMENT"); }
Branch: 1 branch
BranchTrueFalseTotal
if (result == WSLAY_ERR_INVALID_ARGUMENT) { croak("Wslay queue_msg_ex - WSLAY_ERR_INVALID_ARGUMENT"); } 0 6 6
776 6 ▶ if (result == WSLAY_ERR_NOMEM) { croak("Wslay queue_msg_ex - WSLAY_ERR_NOMEM"); }
Branch: 1 branch
BranchTrueFalseTotal
if (result == WSLAY_ERR_NOMEM) { croak("Wslay queue_msg_ex - WSLAY_ERR_NOMEM"); } 0 6 6
777 6 wait_io_event_guarded(websock_object);
778 6 ▶ RETVAL = result;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = result; 3 3 6
779 OUTPUT:
780 RETVAL
781  
782 int queue_fragmented (object, cb, opcode=2)
783 HV* object
784 SV* cb
785 int opcode
786 CODE:
787 5 websocket_object* websock_object = get_wslay_context(object);
788 5 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 5 5
789 struct wslay_event_fragmented_msg msg;
790 5 msg.opcode = opcode;
791 5 msg.source.data = track_fragmented_source(websock_object, cb);
792 5 msg.read_callback = fragmented_msg_callback;
793 5 int result = wslay_event_queue_fragmented_msg(websock_object->ctx, &msg);
794 5 ▶ if (result) { release_fragmented_source(websock_object, (SV*) msg.source.data); }
Branch: 1 branch
BranchTrueFalseTotal
if (result) { release_fragmented_source(websock_object, (SV*) msg.source.data); } 0 5 5
795 5 ▶ if (result == WSLAY_ERR_INVALID_ARGUMENT) { croak("Wslay queue_fragmented - WSLAY_ERR_INVALID_ARGUMENT"); }
Branch: 1 branch
BranchTrueFalseTotal
if (result == WSLAY_ERR_INVALID_ARGUMENT) { croak("Wslay queue_fragmented - WSLAY_ERR_INVALID_ARGUMENT"); } 0 5 5
796 5 ▶ if (result == WSLAY_ERR_NOMEM) { croak("Wslay queue_fragmented - WSLAY_ERR_NOMEM"); }
Branch: 1 branch
BranchTrueFalseTotal
if (result == WSLAY_ERR_NOMEM) { croak("Wslay queue_fragmented - WSLAY_ERR_NOMEM"); } 0 5 5
797 5 wait_io_event_guarded(websock_object);
798 5 ▶ RETVAL = result;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = result; 0 5 5
799 OUTPUT:
800 RETVAL
801  
802 int queue_fragmented_ex (object, cb, opcode=2, rsv=-1)
803 HV* object
804 SV* cb
805 int opcode
806 int rsv
807 CODE:
808 2 websocket_object* websock_object = get_wslay_context(object);
809 2 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 2 2
810 struct wslay_event_fragmented_msg msg;
811 2 ▶ if (rsv < 0) { rsv = websock_object->default_rsv; }
Branch: 1 branch
BranchTrueFalseTotal
if (rsv < 0) { rsv = websock_object->default_rsv; } 2 0 2
812 2 msg.opcode = opcode;
813 2 msg.source.data = track_fragmented_source(websock_object, cb);
814 2 msg.read_callback = fragmented_msg_callback;
815 2 int result = wslay_event_queue_fragmented_msg_ex(websock_object->ctx, &msg, (uint8_t) rsv);
816 2 ▶ if (result) { release_fragmented_source(websock_object, (SV*) msg.source.data); }
Branch: 1 branch
BranchTrueFalseTotal
if (result) { release_fragmented_source(websock_object, (SV*) msg.source.data); } 0 2 2
817 2 ▶ if (result == WSLAY_ERR_INVALID_ARGUMENT) { croak("Wslay queue_fragmented_ex - WSLAY_ERR_INVALID_ARGUMENT"); }
Branch: 1 branch
BranchTrueFalseTotal
if (result == WSLAY_ERR_INVALID_ARGUMENT) { croak("Wslay queue_fragmented_ex - WSLAY_ERR_INVALID_ARGUMENT"); } 0 2 2
818 2 ▶ if (result == WSLAY_ERR_NOMEM) { croak("Wslay queue_fragmented_ex - WSLAY_ERR_NOMEM"); }
Branch: 1 branch
BranchTrueFalseTotal
if (result == WSLAY_ERR_NOMEM) { croak("Wslay queue_fragmented_ex - WSLAY_ERR_NOMEM"); } 0 2 2
819 2 wait_io_event_guarded(websock_object);
820 2 ▶ RETVAL = result;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = result; 2 0 2
821 OUTPUT:
822 RETVAL
823  
824 int close (object, status_code = 0, data = NULL)
825 HV* object
826 int status_code
827 SV* data
828 CODE:
829 5 websocket_object* websock_object = get_wslay_context(object);
830 5 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 5 5
831 5 STRLEN reason_length = 0;
832 5 char *reason = NULL;
833 8 ▶ if (data) { reason = SvPV(data, reason_length); }
Branch: 1 branch
BranchTrueFalseTotal
if (data) { reason = SvPV(data, reason_length); } 3 2 5
834 5 int result = wslay_event_queue_close(websock_object->ctx, status_code, (const uint8_t*) reason, reason_length);
835 5 ▶ if (result == WSLAY_ERR_INVALID_ARGUMENT) {croak("Wslay close - WSLAY_ERR_INVALID_ARGUMENT"); }
Branch: 1 branch
BranchTrueFalseTotal
if (result == WSLAY_ERR_INVALID_ARGUMENT) {croak("Wslay close - WSLAY_ERR_INVALID_ARGUMENT"); } 1 4 5
836 4 ▶ if (result == WSLAY_ERR_NOMEM) { croak("Wslay close - WSLAY_ERR_NOMEM"); }
Branch: 1 branch
BranchTrueFalseTotal
if (result == WSLAY_ERR_NOMEM) { croak("Wslay close - WSLAY_ERR_NOMEM"); } 0 4 4
837 4 wait_io_event_guarded(websock_object);
838 4 ▶ RETVAL = result;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = result; 0 4 4
839 OUTPUT:
840 RETVAL
841  
842 UV queued_count (object)
843 HV* object
844 CODE:
845 1 websocket_object* websock_object = get_wslay_context(object);
846 1 ▶ REQUIRE_CTX(websock_object);
Branch: 1 branch
BranchTrueFalseTotal
REQUIRE_CTX(websock_object); 0 1 1
847 1 RETVAL = wslay_event_get_queued_msg_count(websock_object->ctx);
848 OUTPUT:
849 RETVAL
850  
851 void DESTROY (object)
852 HV* object
853 CODE:
854 83 MAGIC* mg = get_wslay_magic(object);
855 websocket_object* websock_object;
856 83 ▶ if (!mg || !mg->mg_ptr) { XSRETURN_EMPTY; } /* already destroyed */
Branch: 2 branches
BranchTrueFalseTotal
if (!mg || !mg->mg_ptr) { XSRETURN_EMPTY; } /* already destroyed */ 83 0 83
if (!mg || !mg->mg_ptr) { XSRETURN_EMPTY; } /* already destroyed */ 4 79 83
857 79 websock_object = (websocket_object*) mg->mg_ptr;
858 /* detach first: an explicit DESTROY followed by GC must not free twice */
859 79 mg->mg_ptr = NULL;
860 79 close_connection(websock_object);
861 79 ▶ if (websock_object->loop && ev_is_active(&(websock_object->loop_cleanup))) {
Branch: 2 branches
BranchTrueFalseTotal
if (websock_object->loop && ev_is_active(&(websock_object->loop_cleanup))) { 78 1 79
if (websock_object->loop && ev_is_active(&(websock_object->loop_cleanup))) { 78 0 78
862 78 ev_cleanup_stop(websock_object->loop, &(websock_object->loop_cleanup));
863 }
864 79 ▶ if (websock_object->pending_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (websock_object->pending_error) { 0 79 79
865 0 ▶ if (!PL_dirty) { warn("%s", SvPV_nolen(websock_object->pending_error)); }
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_dirty) { warn("%s", SvPV_nolen(websock_object->pending_error)); } 0 0 0
866 0 SvREFCNT_dec(websock_object->pending_error);
867 }
868 79 free(websock_object);
« blib/lib/Net/WebSocket/EVx.pm ↑ Summary