KCP.xs

filter: Statementstmt 86.9% 107 / 123 Branchbran 60.1% 65 / 108 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 74.4% 172 / 231 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/KCP/Test.pm »
1 /*
2 * Copyright (c) homqyy
3 */
4  
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8  
9 #include "ppport.h"
10  
11 #include <ikcp.h>
12  
13 #define KCP_NORMAL_MODE "normal"
14 #define KCP_FAST_MODE "fast"
15  
16 typedef struct {
17 ikcpcb *ikcp;
18 SV *output;
19 SV *user;
20 } kcp_t;
21  
22 static int
23 7 _output(const char *buf, int len, ikcpcb *ikcp, void *user)
24 {
25 7 kcp_t *kcp = (kcp_t *)user;
26  
27 7 ▶ if (kcp->output)
Branch: 1 branch
BranchTrueFalseTotal
if (kcp->output) 6 1 7
28 {
29 6 dSP;
30  
31 6 ENTER;
32 6 SAVETMPS;
33  
34 6 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 6 6
35 6 ▶ XPUSHs(sv_2mortal(newSVpvn(buf, len)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(buf, len))); 0 6 6
36 6 ▶ if (kcp->user != NULL) XPUSHs(sv_2mortal(newSVsv(kcp->user)));
Branch: 2 branches
BranchTrueFalseTotal
if (kcp->user != NULL) XPUSHs(sv_2mortal(newSVsv(kcp->user))); 6 0 6
if (kcp->user != NULL) XPUSHs(sv_2mortal(newSVsv(kcp->user))); 0 6 6
37 6 PUTBACK;
38  
39 6 call_sv(kcp->output, G_VOID);
40  
41 6 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 6 0 6
42 6 LEAVE;
43 }
44  
45 7 return 0;
46 }
47  
48 MODULE = KCP PACKAGE = KCP PREFIX = ikcp_
49  
50 IUINT32
51 ikcp_get_conv(kcp_t *kcp, char *ptr = NULL)
52 CODE:
53 3 ▶ if (ptr == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (ptr == NULL) 1 2 3
54 {
55 1 RETVAL = kcp->ikcp->conv;
56 }
57 else
58 {
59 2 RETVAL = ikcp_getconv(ptr);
60 }
61  
62 OUTPUT:
63 RETVAL
64  
65 IUINT32
66 ikcp_get_interval(kcp_t *kcp)
67 CODE:
68 10 ▶ RETVAL = kcp->ikcp->interval;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = kcp->ikcp->interval; 3 7 10
69  
70 OUTPUT:
71 RETVAL
72  
73 kcp_t *
74 ikcp_new(IUINT32 conv, SV *user = &PL_sv_undef)
75 INIT:
76 20 ▶ if (conv <= 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (conv <= 0) XSRETURN_UNDEF; 1 19 20
77  
78 19 kcp_t *kcp = malloc(sizeof(kcp_t));
79 19 ▶ if (kcp == NULL) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (kcp == NULL) XSRETURN_UNDEF; 0 19 19
80 19 memzero(kcp, sizeof(kcp_t));
81  
82  
83 19 ikcpcb * ikcp = ikcp_create(conv, kcp);
84 19 ▶ if (kcp == NULL) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (kcp == NULL) XSRETURN_UNDEF; 0 19 19
85  
86 19 kcp->ikcp = ikcp;
87  
88 19 ▶ if (user)
Branch: 1 branch
BranchTrueFalseTotal
if (user) 19 0 19
89 {
90 19 kcp->user = newSVsv(user);
91 }
92  
93 CODE:
94 19 ikcp_nodelay(kcp->ikcp, 0, 40, 0, 0); // normal
95 19 ikcp_setoutput(kcp->ikcp, _output);
96 19 RETVAL = kcp;
97  
98 OUTPUT:
99 RETVAL
100  
101 int
102 ikcp_input(kcp_t *kcp, SV *data)
103 CODE:
104 char *buffer;
105 STRLEN len;
106  
107 3 ▶ if (!SvPOK(data)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(data)) XSRETURN_UNDEF; 0 3 3
108  
109 3 buffer = SvPV(data, len);
110  
111 3 RETVAL = ikcp_input(kcp->ikcp, buffer, len);
112  
113 POSTCALL:
114 3 ▶ if (RETVAL < 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL < 0) XSRETURN_UNDEF; 0 3 3
115  
116 3 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 2 1 3
117  
118 OUTPUT:
119 RETVAL
120  
121 int
122 ikcp_recv(kcp_t *kcp, SV *data, int len)
123 CODE:
124 4 ▶ if (len <= 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (len <= 0) XSRETURN_UNDEF; 0 3 3
125  
126 3 char buffer[len];
127 3 RETVAL = ikcp_recv(kcp->ikcp, buffer, len);
128  
129 POSTCALL:
130 3 ▶ if (RETVAL < 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL < 0) XSRETURN_UNDEF; 1 2 3
131  
132 2 sv_setpvn(data, buffer, RETVAL);
133  
134 OUTPUT:
135 RETVAL
136 data
137  
138 int
139 ikcp_send(kcp_t *kcp, SV *data)
140 CODE:
141 char *buffer;
142 STRLEN len;
143  
144 8 ▶ if (!SvPOK(data)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(data)) XSRETURN_UNDEF; 0 8 8
145  
146 8 buffer = SvPV(data, len);
147  
148 8 RETVAL = ikcp_send(kcp->ikcp, buffer, len);
149  
150 POSTCALL:
151 8 ▶ if (RETVAL < 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL < 0) XSRETURN_UNDEF; 0 8 8
152  
153 8 ▶ RETVAL = len;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = len; 0 8 8
154  
155 OUTPUT:
156 RETVAL
157  
158 SV *
159 ikcp_set_output(kcp_t *kcp, SV *output)
160 CODE:
161 5 ▶ if (kcp->output) sv_2mortal(kcp->output);
Branch: 1 branch
BranchTrueFalseTotal
if (kcp->output) sv_2mortal(kcp->output); 0 5 5
162  
163 5 kcp->output = newSVsv(output);
164  
165 5 ▶ if (kcp->output == NULL) croak("KCP::setoutput: no enough memory");
Branch: 1 branch
BranchTrueFalseTotal
if (kcp->output == NULL) croak("KCP::setoutput: no enough memory"); 0 5 5
166  
167 5 RETVAL = SvREFCNT_inc(ST(0));
168  
169 OUTPUT:
170 RETVAL
171  
172 SV *
173 ikcp_update(kcp_t *kcp, long current)
174 CODE:
175 15 ikcp_update(kcp->ikcp, current);
176  
177 15 RETVAL = SvREFCNT_inc(ST(0));
178  
179 OUTPUT:
180 RETVAL
181  
182 SV *
183 ikcp_flush(kcp_t *kcp)
184 CODE:
185 1 ikcp_flush(kcp->ikcp);
186  
187 1 RETVAL = SvREFCNT_inc(ST(0));
188  
189 OUTPUT:
190 RETVAL
191  
192 IUINT32
193 ikcp_mtu(kcp_t *kcp, int mtu = 0)
194 INIT:
195 4 IUINT32 old_mtu = kcp->ikcp->mtu;
196  
197 CODE:
198 4 ▶ if (mtu != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (mtu != 0) 2 2 4
199 {
200 2 int ret = ikcp_setmtu(kcp->ikcp, mtu);
201  
202 2 ▶ if (ret < 0) croak("KCP::mtu: invalid mtu");
Branch: 1 branch
BranchTrueFalseTotal
if (ret < 0) croak("KCP::mtu: invalid mtu"); 1 1 2
203 }
204  
205 3 ▶ RETVAL = old_mtu;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = old_mtu; 0 3 3
206  
207 OUTPUT:
208 RETVAL
209  
210 IUINT32
211 ikcp_sndwnd(kcp_t *kcp, int sndwnd = 0)
212 INIT:
213 3 IUINT32 old_wnd = kcp->ikcp->snd_wnd;
214  
215 CODE:
216 3 ▶ if (sndwnd != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (sndwnd != 0) 1 2 3
217 {
218 1 ikcp_wndsize(kcp->ikcp, sndwnd, kcp->ikcp->rcv_wnd);
219 }
220  
221 3 ▶ RETVAL = old_wnd;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = old_wnd; 0 3 3
222  
223 OUTPUT:
224 RETVAL
225  
226 IUINT32
227 ikcp_rcvwnd(kcp_t *kcp, int rcvwnd = 0)
228 INIT:
229 3 IUINT32 old_wnd = kcp->ikcp->rcv_wnd;
230  
231 CODE:
232 3 ▶ if (rcvwnd != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rcvwnd != 0) 1 2 3
233 {
234 1 ikcp_wndsize(kcp->ikcp, kcp->ikcp->snd_wnd, rcvwnd);
235 }
236  
237 3 ▶ RETVAL = old_wnd;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = old_wnd; 0 3 3
238  
239 OUTPUT:
240 RETVAL
241  
242 int
243 ikcp_get_waitsnd(kcp_t *kcp)
244 CODE:
245 2 RETVAL = ikcp_waitsnd(kcp->ikcp);
246  
247 OUTPUT:
248 RETVAL
249  
250 SV *
251 ikcp_set_mode(kcp_t *kcp, SV *mode)
252 CODE:
253 3 ▶ if (mode == NULL) croak("KCP::setmode: invalid mode\n");
Branch: 1 branch
BranchTrueFalseTotal
if (mode == NULL) croak("KCP::setmode: invalid mode\n"); 0 3 3
254  
255 char *m;
256 STRLEN len;
257  
258 3 ▶ if (!SvPOK(mode)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(mode)) XSRETURN_UNDEF; 0 3 3
259  
260 3 m = SvPV(mode, len);
261  
262 3 ▶ if (len + 1 == sizeof(KCP_NORMAL_MODE)
Branch: 1 branch
BranchTrueFalseTotal
if (len + 1 == sizeof(KCP_NORMAL_MODE) 1 2 3
263 1 ▶ && memcmp(m, KCP_NORMAL_MODE, len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(m, KCP_NORMAL_MODE, len) == 0) 1 0 1
264 {
265 1 ikcp_nodelay(kcp->ikcp, 0, 40, 0, 0);
266 }
267 2 ▶ else if (len + 1 == sizeof(KCP_FAST_MODE)
Branch: 1 branch
BranchTrueFalseTotal
else if (len + 1 == sizeof(KCP_FAST_MODE) 1 1 2
268 1 ▶ && memcmp(m, KCP_FAST_MODE, len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(m, KCP_FAST_MODE, len) == 0) 1 0 1
269 {
270 1 ikcp_nodelay(kcp->ikcp, 1, 10, 2, 1);
271 }
272 else
273 {
274 1 croak("KCP::setmode: invalid mode\n");
275 }
276  
277 2 RETVAL = SvREFCNT_inc(ST(0));
278  
279 OUTPUT:
280 RETVAL
281  
282 SV *
283 ikcp_nodelay(kcp_t *kcp, ...)
284 CODE:
285 // arguments
286 10 int nodelay = 0;
287 10 int interval = 40;
288 10 int resend = 0;
289 10 int nc = 0;
290  
291 // parse arguments
292 int i;
293 char *key;
294 STRLEN len;
295 13 ▶ for (i = 1; i < items; i+=2)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i+=2) 9 4 13
296 {
297 9 ▶ if (!SvPOK(ST(i)))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(ST(i))) 0 9 9
298 {
299 0 croak("key must is a 'string'");
300 }
301  
302 9 key = SvPV(ST(i), len);
303  
304 9 ▶ if (i + 1 != items)
Branch: 1 branch
BranchTrueFalseTotal
if (i + 1 != items) 9 0 9
305 {
306 9 SV * value = ST(i + 1);
307  
308 9 ▶ if (len + 1 == sizeof("nodelay") &&
Branch: 1 branch
BranchTrueFalseTotal
if (len + 1 == sizeof("nodelay") && 8 1 9
309 8 ▶ memcmp(key, "nodelay", len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
memcmp(key, "nodelay", len) == 0) 8 0 8
310 {
311 8 ▶ if (!SvIOK(value))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvIOK(value)) 5 3 8
312 {
313 5 croak("invalid value of nodelay. 0:disable(default), 1:enable");
314 }
315  
316 3 nodelay = SvIV(value);
317  
318 3 ▶ if (nodelay != 0 && nodelay != 1)
Branch: 2 branches
BranchTrueFalseTotal
if (nodelay != 0 && nodelay != 1) 3 0 3
if (nodelay != 0 && nodelay != 1) 1 2 3
319 {
320 1 croak("invalid value of nodelay. 0:disable(default), 1:enable");
321 }
322  
323 2 continue;
324 }
325 1 ▶ else if (len + 1 == sizeof("interval") &&
Branch: 1 branch
BranchTrueFalseTotal
else if (len + 1 == sizeof("interval") && 1 0 1
326 1 ▶ memcmp(key, "interval", len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
memcmp(key, "interval", len) == 0) 1 0 1
327 {
328 1 ▶ if (!SvIOK(value))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvIOK(value)) 0 1 1
329 {
330 0 croak("invalid value of interval. Internal update timer interval in millisec, default is 100ms ");
331 }
332  
333 1 interval = SvIV(value);
334 1 continue;
335 }
336 0 ▶ else if (len + 1 == sizeof("resend") &&
Branch: 1 branch
BranchTrueFalseTotal
else if (len + 1 == sizeof("resend") && 0 0 0
337 0 ▶ memcmp(key, "resend", len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
memcmp(key, "resend", len) == 0) 0 0 0
338 {
339 0 ▶ if (!SvIOK(value))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvIOK(value)) 0 0 0
340 {
341 0 croak("invalid value of resend. 0:disable fast resend(default), 1:enable fast resend");
342 }
343  
344 0 resend = SvIV(value);
345 0 continue;
346 }
347 0 ▶ else if (len + 1 == sizeof("disable_congestion_control") &&
Branch: 1 branch
BranchTrueFalseTotal
else if (len + 1 == sizeof("disable_congestion_control") && 0 0 0
348 0 ▶ memcmp(key, "disable_congestion_control", len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
memcmp(key, "disable_congestion_control", len) == 0) 0 0 0
349 {
350 0 ▶ if (!SvIOK(value))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvIOK(value)) 0 0 0
351 {
352 0 croak("invalid value of nc. nc: 0:normal congestion control(default), 1:disable congestion control");
353 }
354  
355 0 nc = SvIV(value);
356 0 continue;
357 }
358 else
359 {
360 0 croak("KCP::nodelay: invalid arguments '%s'\n", key);
361 }
362 }
363 else
364 {
365  
366 0 croak("KCP::nodelay: invalid arguments '%s'\n", key);
367 }
368 }
369  
370 // call c function
371 4 ikcp_nodelay(kcp->ikcp, nodelay, interval, resend, nc);
372  
373 4 RETVAL = SvREFCNT_inc(ST(0));
374  
375 OUTPUT:
376 RETVAL
377  
378 int
379 peeksize(kcp_t *kcp)
380 CODE:
381 1 RETVAL = ikcp_peeksize(kcp->ikcp);
382  
383 OUTPUT:
384 RETVAL
385  
386 void
387 ikcp_DESTROY(kcp_t *kcp)
388 CODE:
389 19 ▶ if (kcp->output) sv_2mortal(kcp->output);
Branch: 1 branch
BranchTrueFalseTotal
if (kcp->output) sv_2mortal(kcp->output); 5 14 19
390  
391 19 ▶ if (kcp->user) sv_2mortal(kcp->user);
Branch: 1 branch
BranchTrueFalseTotal
if (kcp->user) sv_2mortal(kcp->user); 19 0 19
392  
393 19 ikcp_release(kcp->ikcp);
394  
395 19 free(kcp);
↑ Summary blib/lib/KCP/Test.pm »