xs/wsroom.xs

filter: Statementstmt 77.9% 53 / 68 Branchbran 39.7% 28 / 68 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 58.8% 81 / 136 (1 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/core.xs ↑ Summary xs/cors.xs »
1 MODULE = Punk PACKAGE = Punk::WebSocket::Room
2  
3 PROTOTYPES: DISABLE
4  
5 # Per-worker pub/sub groups of connections, in C (punk_wsroom.h). Members are
6 # held weakly and pruned on access; a broadcast encodes once and queues the
7 # same bytes to every open member. lib/Punk/WebSocket/Room.pm is documentation.
8  
9 # named($name): the worker's room of that name, created on first use.
10 SV *
11 named(class, name)
12 SV *class
13 SV *name
14 CODE:
15 {
16 6 IV pid = (IV)PerlProc_getpid();
17 6 STRLEN nl; const char *n = SvPV_const(name, nl);
18 SV **rp;
19 6 ▶ if (!pwr_rooms) { pwr_rooms = newHV(); pwr_rooms_pid = pid; }
Branch: 1 branch
BranchTrueFalseTotal
if (!pwr_rooms) { pwr_rooms = newHV(); pwr_rooms_pid = pid; } 2 4 6
20 4 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 4 4
21 6 # uncoverable condition left count:1 class:ignore_covered_err
22 6 ▶ else if (pwr_rooms_pid != pid) { hv_clear(pwr_rooms); pwr_rooms_pid = pid; }
Branch: 3 branches
BranchTrueFalseTotal
else if (pwr_rooms_pid != pid) { hv_clear(pwr_rooms); pwr_rooms_pid = pid; } 3 3 6
else if (pwr_rooms_pid != pid) { hv_clear(pwr_rooms); pwr_rooms_pid = pid; } 3 0 3
else if (pwr_rooms_pid != pid) { hv_clear(pwr_rooms); pwr_rooms_pid = pid; } 3 0 3
23 rp = hv_fetch(pwr_rooms, n, (I32)nl, 0);
24 3 # uncoverable condition right count:2 class:ignore_covered_err
25 # uncoverable condition right count:3 class:ignore_covered_err
26 3 if (rp && *rp && SvROK(*rp)) RETVAL = newSVsv(*rp);
27 3 else {
28 3 HV *room = newHV();
29 3 SV *rv;
30 3 (void)hv_stores(room, "name", newSVsv(name));
31 (void)hv_stores(room, "members", newRV_noinc((SV *)newHV()));
32 rv = sv_bless(newRV_noinc((SV *)room), gv_stashsv(class, GV_ADD));
33 (void)hv_store(pwr_rooms, n, (I32)nl, rv, 0); /* registry owns it */
34 RETVAL = newSVsv(rv);
35 }
36 }
37 OUTPUT:
38 RETVAL
39  
40 SV *
41 # uncoverable statement
42 # uncoverable subroutine
43 name(self)
44 SV *self
45 CODE:
46 {
47 0 SV **nm = hv_fetchs(pwr_hv(aTHX_ self), "name", 0); /* # uncoverable statement */
48 0 ▶ # uncoverable statement
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable statement 0 0 0
# uncoverable statement 0 0 0
49 # uncoverable condition left count:1
50 # uncoverable condition right count:1
51 # uncoverable condition left count:2
52 # uncoverable condition right count:2
53 RETVAL = (nm && *nm) ? newSVsv(*nm) : &PL_sv_undef;
54 }
55 OUTPUT:
56 RETVAL
57  
58 # join($ws): add a member, held weakly. Chains.
59 SV *
60 join(self, ws)
61 SV *self
62 SV *ws
63 CODE:
64 {
65 5 ▶ void *addr = PWR_ADDR(ws);
Branch: 1 branch
BranchTrueFalseTotal
void *addr = PWR_ADDR(ws); 5 0 5
66 5 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 5 0 5
67 5 # uncoverable condition right count:1 class:ignore_covered_err
68 5 if (addr) {
69 5 HV *members = pwr_members(aTHX_ self);
70 5 SV *copy = newSVsv(ws);
71 (void)hv_store(members, (const char *)&addr, PWR_KEYLEN, copy, 0);
72 5 sv_rvweaken(copy);
73 }
74 RETVAL = newSVsv(self);
75 }
76 OUTPUT:
77 RETVAL
78  
79 # leave($ws): drop a member. Chains.
80 SV *
81 leave(self, ws)
82 SV *self
83 SV *ws
84 CODE:
85 {
86 5 ▶ void *addr = PWR_ADDR(ws);
Branch: 1 branch
BranchTrueFalseTotal
void *addr = PWR_ADDR(ws); 5 0 5
87 5 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 5 0 5
88 5 # uncoverable condition right count:1 class:ignore_covered_err
89 if (addr)
90 5 (void)hv_delete(pwr_members(aTHX_ self), (const char *)&addr,
91 PWR_KEYLEN, 0);
92 RETVAL = newSVsv(self);
93 }
94 OUTPUT:
95 RETVAL
96  
97 # has($ws): is $ws a member and open?
98 IV
99 # uncoverable statement
100 # uncoverable subroutine
101 has(self, ws)
102 SV *self
103 SV *ws
104 CODE:
105 {
106 0 ▶ void *addr = PWR_ADDR(ws); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
void *addr = PWR_ADDR(ws); /* # uncoverable statement */ 0 0 0
107 0 SV **mp = addr ? hv_fetch(pwr_members(aTHX_ self), /* # uncoverable statement */
108 0 ▶ (const char *)&addr, PWR_KEYLEN, 0) : NULL; /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
(const char *)&addr, PWR_KEYLEN, 0) : NULL; /* # uncoverable statement */ 0 0 0
109 0 ▶ # uncoverable statement
Branch: 4 branches
BranchTrueFalseTotal
# uncoverable statement 0 0 0
# uncoverable statement 0 0 0
# uncoverable statement 0 0 0
# uncoverable statement 0 0 0
110 # uncoverable condition left count:1
111 # uncoverable condition right count:1
112 # uncoverable condition left count:2
113 # uncoverable condition right count:2
114 # uncoverable condition left count:3
115 # uncoverable condition right count:3
116 RETVAL = (mp && *mp && pwr_ws_state(aTHX_ *mp) == PW_ST_OPEN) ? 1 : 0;
117 }
118 OUTPUT:
119 RETVAL
120  
121 # clients: the live members (pruning as it goes).
122 void
123 # uncoverable statement
124 # uncoverable subroutine
125 clients(self)
126 SV *self
127 PPCODE:
128 {
129 0 AV *live = (AV *)sv_2mortal((SV *)newAV()); /* # uncoverable statement */
130 SSize_t i, n;
131 0 pwr_live(aTHX_ pwr_members(aTHX_ self), live); /* # uncoverable statement */
132 0 n = av_len(live) + 1; /* # uncoverable statement */
133 0 ▶ EXTEND(SP, n); /* # uncoverable statement */
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); /* # uncoverable statement */ 0 0 0
EXTEND(SP, n); /* # uncoverable statement */ 0 0 0
134 0 ▶ # uncoverable statement
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable statement 0 0 0
135 0 # uncoverable branch true
136 # uncoverable branch false
137 # uncoverable condition left count:1
138 # uncoverable condition right count:1
139 for (i = 0; i < n; i++)
140 PUSHs(sv_2mortal(newSVsv(*av_fetch(live, i, 0)))); /* # uncoverable statement */
141 }
142  
143 IV
144 count(self)
145 SV *self
146 CODE:
147 {
148 1 AV *live = (AV *)sv_2mortal((SV *)newAV());
149 1 pwr_live(aTHX_ pwr_members(aTHX_ self), live);
150 1 ▶ RETVAL = av_len(live) + 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = av_len(live) + 1; 0 1 1
151 }
152 OUTPUT:
153 RETVAL
154  
155 # broadcast($text, $except?) / broadcast_binary($bytes, $except?): encode once,
156 # queue to every open member bar $except; returns the number sent.
157 SV *
158 broadcast(self, message, except = &PL_sv_undef)
159 SV *self
160 SV *message
161 SV *except
162 ALIAS:
163 broadcast_binary = 1
164 CODE:
165 {
166 SV *frame; int count;
167 4 dSP;
168 4 ENTER; SAVETMPS;
169 4 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(message); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(message); PUTBACK; 0 4 4
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(message); PUTBACK; 0 4 4
170 4 ▶ count = call_pv(ix ? PK_WEBSOCKET "::_encode_binary"
Branch: 1 branch
BranchTrueFalseTotal
count = call_pv(ix ? PK_WEBSOCKET "::_encode_binary" 2 2 4
171 : PK_WEBSOCKET "::_encode_text", G_SCALAR);
172 4 SPAGAIN;
173 4 ▶ frame = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
frame = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 4 0 4
174 4 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 4 0 4
175 4 sv_2mortal(frame);
176  
177 /* ACROSS THE POOL, when there is one.
178 *
179 * A room is per worker; under `workers => 4` a local-only broadcast
180 * reaches roughly a quarter of the people in it, succeeds, and returns
181 * a plausible number. So the frame goes on the bus instead, and every
182 * worker's subscriber - INCLUDING this one's - fans it to its members.
183 *
184 * ONE delivery path. Sending locally as well would send this worker's
185 * members the frame twice, and would leave two paths to drift apart,
186 * so a bug in the shared one would be invisible to anybody testing on
187 * a single worker.
188 *
189 * `except` travels with it: a connection belongs to one worker, so
190 * the frame names that worker and the connection, this worker's
191 * subscriber leaves it out, and every other worker delivers to all
192 * of its members. Leaving `except` off the bus instead meant the
193 * usual chat broadcast, everyone but the sender, reached this worker
194 * alone. */
195 {
196 4 SV **np = hv_fetchs(pwr_hv(aTHX_ self), "name", 0);
197 4 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 4 0 4
# uncoverable condition left count:1 class:ignore_covered_err 4 0 4
198 4 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 1 3 4
199 1 if (!np || !*np
200 || !punk_bus_room_publish(aTHX_ *np, frame, except))
201 RETVAL = newSViv(pwr_fan(aTHX_ self, frame, except));
202 else
203 /* what THIS worker delivered. The rest of the pool is served
204 3 * from its own wakeup, and a total across processes is not
205 * knowable here without waiting for it. */
206 RETVAL = newSViv(PUNK_BUS_LAST_FAN);
207 }
208 }
209 OUTPUT:
210 RETVAL
211  
212 # close_all($code = 1000, $reason = ''): close every member, empty the room.
213 IV
214 close_all(self, code = &PL_sv_undef, reason = &PL_sv_undef)
215 SV *self
216 SV *code
217 SV *reason
218 CODE:
219 {
220 1 HV *members = pwr_members(aTHX_ self);
221 1 AV *live = (AV *)sv_2mortal((SV *)newAV());
222 SSize_t i, n;
223 1 IV closed = 0;
224 1 pwr_live(aTHX_ members, live);
225 1 n = av_len(live) + 1;
226 3 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2 1 3
227 2 SV *ws = *av_fetch(live, i, 0);
228 SV *argv[2], *r;
229 2 ▶ argv[0] = sv_2mortal(newSViv(SvOK(code) ? SvIV(code) : 1000)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
argv[0] = sv_2mortal(newSViv(SvOK(code) ? SvIV(code) : 1000)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
230 2 ▶ argv[1] = SvOK(reason) ? reason : sv_2mortal(newSVpvs("")); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
argv[1] = SvOK(reason) ? reason : sv_2mortal(newSVpvs("")); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 2 2
231 2 r = pcx_call_meth(aTHX_ ws, "close", argv, 2, 0);
232 2 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 2 2
233 2 # uncoverable condition left count:1 class:ignore_covered_err
234 if (r) SvREFCNT_dec(r);
235 1 closed++;
236 1 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 0 1 1
237 hv_clear(members);
238 RETVAL = closed;
239 }
240 OUTPUT:
241 RETVAL
242  
243 # clear: empty the room without closing anything. Chains.
244 SV *
245 # uncoverable statement
246 # uncoverable subroutine
247 clear(self)
248 SV *self
249 CODE:
250 0 hv_clear(pwr_members(aTHX_ self)); /* # uncoverable statement */
251 0 RETVAL = newSVsv(self); /* # uncoverable statement */
252 OUTPUT:
253 RETVAL
254  
255 MODULE = Punk PACKAGE = Punk
256  
257 # Register this process's bus subscriptions.
258 #
259 # compile() asks Hyperman to run this in every worker, which is where it
260 # belongs. Exposed because a test - and anything driving Punk outside a real
261 # server - has no worker hook to fire it. Idempotent.
262 void
263 # uncoverable statement
264 # uncoverable subroutine
265 _bus_attach()
266 CODE:
267 0 punk_bus_attach(aTHX); /* # uncoverable statement */
« xs/core.xs ↑ Summary xs/cors.xs »