include/punk/punk_wsroom.h

filter: Statementstmt 85.1% 46 / 54 Branchbran 51.8% 31 / 54 (7 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 68.5% 77 / 108 (7 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_bus.h ↑ Summary xs/conditionalget.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 /*
27 # uncoverable branch true
28 # uncoverable condition left count:1 class:ignore_covered_err
29 # uncoverable condition left count:2 class:ignore_covered_err */
30 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
31 0 croak("Punk::WebSocket::Room: not a room"); /* # uncoverable statement */
32 20 return (HV *)SvRV(self);
33 }
34  
35 16 static HV *pwr_members(pTHX_ SV *self) {
36 16 HV *h = pwr_hv(aTHX_ self);
37 16 SV **m = hv_fetchs(h, "members", 0);
38 /*
39 # uncoverable branch false
40 # uncoverable condition right count:1 class:ignore_covered_err
41 # uncoverable condition right count:2 class:ignore_covered_err
42 # uncoverable condition right count:3 class:ignore_covered_err
43 # uncoverable condition right count:4 class:ignore_covered_err */
44 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
45 {
46 0 HV *nm = newHV(); /* # uncoverable statement */
47 0 (void)hv_stores(h, "members", newRV_noinc((SV *)nm)); /* # uncoverable statement */
48 0 return nm; /* # uncoverable statement */
49 }
50 }
51  
52 /* the wsconn state of a member SV, or -1 if it is not a live connection */
53 14 static int pwr_ws_state(pTHX_ SV *ws) {
54 /*
55 # uncoverable branch true
56 # uncoverable condition left count:1 class:ignore_covered_err
57 # uncoverable condition left count:2 class:ignore_covered_err
58 # uncoverable condition left count:3 class:ignore_covered_err
59 # uncoverable condition left count:4 class:ignore_covered_err */
60 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
61 14 return punk_ws_of(aTHX_ ws)->state;
62 }
63  
64 /* refaddr($sv) as a raw pointer - the member-hash key (binary, length-counted) */
65 #define PWR_ADDR(sv) ((SvROK(sv)) ? (void *)SvRV(sv) : (void *)NULL)
66 #define PWR_KEYLEN (sizeof(void *))
67  
68 /* Fill out with a strong ref to every live member, pruning anything gone
69 * (a weakref that went undef) or closed. Keys are snapshotted first so the
70 * hash can be modified as we go. */
71 6 static void pwr_live(pTHX_ HV *members, AV *out) {
72 6 AV *keys = (AV *)sv_2mortal((SV *)newAV());
73 HE *he;
74 SSize_t i, n;
75 6 hv_iterinit(members);
76 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
77 6 n = av_len(keys) + 1;
78 15 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 9 6 15
79 9 SV *ksv = *av_fetch(keys, i, 0);
80 9 STRLEN kl; const char *k = SvPV_const(ksv, kl);
81 9 SV **mp = hv_fetch(members, k, (I32)kl, 0);
82 /*
83 # uncoverable condition right count:1 class:ignore_covered_err
84 # uncoverable condition right count:2 class:ignore_covered_err */
85 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
86 int st;
87 /*
88 # uncoverable branch true
89 # uncoverable condition left count:1 class:ignore_covered_err
90 # uncoverable condition left count:2 class:ignore_covered_err */
91 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
92 9 st = pwr_ws_state(aTHX_ ws);
93 /*
94 # uncoverable branch true
95 # uncoverable condition left count:1 class:ignore_covered_err
96 # uncoverable condition left count:2 class:ignore_covered_err */
97 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
98 0 (void)hv_delete(members, k, (I32)kl, 0); /* # uncoverable statement */
99 0 continue; /* # uncoverable statement */
100 }
101 9 av_push(out, newSVsv(ws)); /* a strong ref for the loop's life */
102 }
103 6 }
104  
105 /* Encode once (via _encode_text / _encode_binary), queue to every open member
106 * bar `except`; returns the number sent. */
107 4 static IV pwr_fan_skip(pTHX_ SV *self, SV *frame, void *skip) {
108 4 HV *members = pwr_members(aTHX_ self);
109 4 AV *live = (AV *)sv_2mortal((SV *)newAV());
110 4 IV sent = 0;
111 SSize_t i, n;
112 STRLEN flen;
113 4 const char *fp = SvPV_const(frame, flen);
114 4 pwr_live(aTHX_ members, live);
115 4 n = av_len(live) + 1;
116 10 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 6 4 10
117 6 SV *ws = *av_fetch(live, i, 0);
118 punk_wsconn *w;
119 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
120 /*
121 # uncoverable branch true
122 # uncoverable condition left count:1 class:ignore_covered_err */
123 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
124 /* what Punk::WebSocket::_send_raw does, without the method call
125 * per member: `live` holds a strong reference to each, so a member
126 * that fails here outlives the loop */
127 5 w = punk_ws_of(aTHX_ ws);
128 /*
129 # uncoverable branch true
130 # uncoverable condition left count:1 class:ignore_covered_err
131 # uncoverable condition right count:1 class:ignore_covered_err
132 # uncoverable condition left count:2 class:ignore_covered_err */
133 5 ▶ if (w->write_buffer_limit
Branch: 1 branch
BranchTrueFalseTotal
if (w->write_buffer_limit 5 0 5
134 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
135 0 pw_fail(aTHX_ w, PW_CLOSE_POLICY_VIOLATION, "send buffer full"); /* # uncoverable statement */
136 0 continue; /* # uncoverable statement */
137 }
138 5 pw_append(&w->wbuf, &w->wlen, &w->wcap, fp, flen);
139 5 pw_flush(aTHX_ w);
140 5 sent++;
141 }
142 4 return sent;
143 }
144  
145 1 static IV pwr_fan(pTHX_ SV *self, SV *frame, SV *except) {
146 2 ▶ return pwr_fan_skip(aTHX_ self, frame,
Branch: 1 branch
BranchTrueFalseTotal
return pwr_fan_skip(aTHX_ self, frame, 1 0 1
147 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
148 }
149  
150 #endif /* PUNK_WSROOM_H */
« include/punk/punk_bus.h ↑ Summary xs/conditionalget.xs »