xs/wsroom.xs

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