include/punk/punk_wsroom.h

filter: Statementstmt 85.1% 46 / 54 Branchbran 57.4% 31 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 71.3% 77 / 108 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/dbi.xs ↑ Summary xs/sendfile.xs »
1 /* punk_wsroom.h - Punk::WebSocket::Room, in C.
2 *
3 * A per-worker named group of connections. Rooms live in one registry per
4 * process (the fork-guarded static below), each a blessed hash { name,
5 * members }, and members are held weakly: a live connection is kept alive by
6 * the C self-reference it holds while open, so a room never resurrects a
7 * closed one and never leaks a disconnected one. A broadcast encodes the frame
8 * once (the _encode_text/_encode_binary XSUB) and queues the same bytes to
9 * every open member. State and refaddr checks are done straight off the
10 * punk_wsconn struct; the send and the encode reuse the connection XSUBs.
11 *
12 * Must be included after punk_wsconn.h (punk_ws_of, PW_ST_*) and
13 * punk_context.h (pcx_call_meth).
14 */
15  
16 #ifndef PUNK_WSROOM_H
17 #define PUNK_WSROOM_H
18  
19 /* The registry is per process: workers fork, and each owns the sockets it
20 * accepted (the pid guard resets an inherited registry, as the Perl's
21 * `$PID != $$` did). Not per-ithread - the server model is fork-per-worker. */
22 static HV *pwr_rooms = NULL;
23 static IV pwr_rooms_pid = 0;
24  
25 20 static HV *pwr_hv(pTHX_ SV *self) {
26 20 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 20 0 20
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 0 20 20
27 0 croak("Punk::WebSocket::Room: not a room");
28 20 return (HV *)SvRV(self);
29 }
30  
31 16 static HV *pwr_members(pTHX_ SV *self) {
32 16 HV *h = pwr_hv(aTHX_ self);
33 16 SV **m = hv_fetchs(h, "members", 0);
34 16 ▶ if (m && *m && SvROK(*m) && SvTYPE(SvRV(*m)) == SVt_PVHV) return (HV *)SvRV(*m);
Branch: 4 branches
BranchTrueFalseTotal
if (m && *m && SvROK(*m) && SvTYPE(SvRV(*m)) == SVt_PVHV) return (HV *)SvRV(*m); 16 0 16
if (m && *m && SvROK(*m) && SvTYPE(SvRV(*m)) == SVt_PVHV) return (HV *)SvRV(*m); 16 0 16
if (m && *m && SvROK(*m) && SvTYPE(SvRV(*m)) == SVt_PVHV) return (HV *)SvRV(*m); 16 0 16
if (m && *m && SvROK(*m) && SvTYPE(SvRV(*m)) == SVt_PVHV) return (HV *)SvRV(*m); 16 0 16
35 {
36 0 HV *nm = newHV();
37 0 (void)hv_stores(h, "members", newRV_noinc((SV *)nm));
38 0 return nm;
39 }
40 }
41  
42 /* the wsconn state of a member SV, or -1 if it is not a live connection */
43 14 static int pwr_ws_state(pTHX_ SV *ws) {
44 14 ▶ if (!ws || !SvOK(ws) || !SvROK(ws) || !SvIOK(SvRV(ws))) return -1;
Branch: 4 branches
BranchTrueFalseTotal
if (!ws || !SvOK(ws) || !SvROK(ws) || !SvIOK(SvRV(ws))) return -1; 14 0 14
if (!ws || !SvOK(ws) || !SvROK(ws) || !SvIOK(SvRV(ws))) return -1; 14 0 14
if (!ws || !SvOK(ws) || !SvROK(ws) || !SvIOK(SvRV(ws))) return -1; 14 0 14
if (!ws || !SvOK(ws) || !SvROK(ws) || !SvIOK(SvRV(ws))) return -1; 0 14 14
45 14 return punk_ws_of(aTHX_ ws)->state;
46 }
47  
48 /* refaddr($sv) as a raw pointer - the member-hash key (binary, length-counted) */
49 #define PWR_ADDR(sv) ((SvROK(sv)) ? (void *)SvRV(sv) : (void *)NULL)
50 #define PWR_KEYLEN (sizeof(void *))
51  
52 /* Fill out with a strong ref to every live member, pruning anything gone
53 * (a weakref that went undef) or closed. Keys are snapshotted first so the
54 * hash can be modified as we go. */
55 6 static void pwr_live(pTHX_ HV *members, AV *out) {
56 6 AV *keys = (AV *)sv_2mortal((SV *)newAV());
57 HE *he;
58 SSize_t i, n;
59 6 hv_iterinit(members);
60 15 ▶ while ((he = hv_iternext(members))) av_push(keys, newSVsv(hv_iterkeysv(he)));
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(members))) av_push(keys, newSVsv(hv_iterkeysv(he))); 9 6 15
61 6 n = av_len(keys) + 1;
62 15 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 9 6 15
63 9 SV *ksv = *av_fetch(keys, i, 0);
64 9 STRLEN kl; const char *k = SvPV_const(ksv, kl);
65 9 SV **mp = hv_fetch(members, k, (I32)kl, 0);
66 9 ▶ SV *ws = (mp && *mp) ? *mp : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV *ws = (mp && *mp) ? *mp : NULL; 9 0 9
SV *ws = (mp && *mp) ? *mp : NULL; 9 0 9
67 int st;
68 9 ▶ if (!ws || !SvOK(ws)) { (void)hv_delete(members, k, (I32)kl, 0); continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (!ws || !SvOK(ws)) { (void)hv_delete(members, k, (I32)kl, 0); continue; } 9 0 9
if (!ws || !SvOK(ws)) { (void)hv_delete(members, k, (I32)kl, 0); continue; } 0 9 9
69 9 st = pwr_ws_state(aTHX_ ws);
70 9 ▶ if (st < 0 || st == PW_ST_CLOSED) {
Branch: 2 branches
BranchTrueFalseTotal
if (st < 0 || st == PW_ST_CLOSED) { 9 0 9
if (st < 0 || st == PW_ST_CLOSED) { 0 9 9
71 0 (void)hv_delete(members, k, (I32)kl, 0);
72 0 continue;
73 }
74 9 av_push(out, newSVsv(ws)); /* a strong ref for the loop's life */
75 }
76 6 }
77  
78 /* Encode once (via _encode_text / _encode_binary), queue to every open member
79 * bar `except`; returns the number sent. */
80 4 static IV pwr_fan_skip(pTHX_ SV *self, SV *frame, void *skip) {
81 4 HV *members = pwr_members(aTHX_ self);
82 4 AV *live = (AV *)sv_2mortal((SV *)newAV());
83 4 IV sent = 0;
84 SSize_t i, n;
85 STRLEN flen;
86 4 const char *fp = SvPV_const(frame, flen);
87 4 pwr_live(aTHX_ members, live);
88 4 n = av_len(live) + 1;
89 10 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 6 4 10
90 6 SV *ws = *av_fetch(live, i, 0);
91 punk_wsconn *w;
92 6 ▶ if (skip && (void *)SvRV(ws) == skip) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (skip && (void *)SvRV(ws) == skip) continue; 2 4 6
if (skip && (void *)SvRV(ws) == skip) continue; 1 1 2
93 5 ▶ if (pwr_ws_state(aTHX_ ws) != PW_ST_OPEN) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (pwr_ws_state(aTHX_ ws) != PW_ST_OPEN) continue; 0 5 5
94 /* what Punk::WebSocket::_send_raw does, without the method call
95 * per member: `live` holds a strong reference to each, so a member
96 * that fails here outlives the loop */
97 5 w = punk_ws_of(aTHX_ ws);
98 5 ▶ if (w->write_buffer_limit
Branch: 1 branch
BranchTrueFalseTotal
if (w->write_buffer_limit 5 0 5
99 5 ▶ && w->wlen - w->woff + flen > w->write_buffer_limit) {
Branch: 1 branch
BranchTrueFalseTotal
&& w->wlen - w->woff + flen > w->write_buffer_limit) { 0 5 5
100 0 pw_fail(aTHX_ w, PW_CLOSE_POLICY_VIOLATION, "send buffer full");
101 0 continue;
102 }
103 5 pw_append(&w->wbuf, &w->wlen, &w->wcap, fp, flen);
104 5 pw_flush(aTHX_ w);
105 5 sent++;
106 }
107 4 return sent;
108 }
109  
110 1 static IV pwr_fan(pTHX_ SV *self, SV *frame, SV *except) {
111 2 ▶ return pwr_fan_skip(aTHX_ self, frame,
Branch: 1 branch
BranchTrueFalseTotal
return pwr_fan_skip(aTHX_ self, frame, 1 0 1
112 1 ▶ (except && SvOK(except)) ? PWR_ADDR(except) : NULL);
Branch: 2 branches
BranchTrueFalseTotal
(except && SvOK(except)) ? PWR_ADDR(except) : NULL); 0 1 1
(except && SvOK(except)) ? PWR_ADDR(except) : NULL); 0 0 0
113 }
114  
115 #endif /* PUNK_WSROOM_H */
« xs/dbi.xs ↑ Summary xs/sendfile.xs »