| 1 |
|
| MODULE = Fetch PACKAGE = Fetch |
| 2 |
|
| |
| 3 |
|
| # $ua->websocket($url, %opt) -> Fetch::Future resolving to a Fetch::WebSocket. |
| 4 |
|
| # The Upgrade handshake runs on the same non-blocking connection/loop as any |
| 5 |
|
| # request; on a verified 101 the Future yields the live socket. |
| 6 |
|
| SV * |
| 7 |
|
| websocket(self, url, ...) |
| 8 |
|
| SV *self |
| 9 |
|
| const char *url |
| 10 |
|
| CODE: |
| 11 |
|
| { |
| 12 |
1 |
| HV *opt = newHV(); |
| 13 |
|
| int i; |
| 14 |
1 |
▶ | for (i = 2; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 2; i + 1 < items; i += 2) { |
0 |
1 |
1 |
|
| 15 |
|
| STRLEN kl; |
| 16 |
0 |
| const char *k = SvPV_const(ST(i), kl); |
| 17 |
0 |
| (void)hv_store(opt, k, (I32)kl, newSVsv(ST(i + 1)), 0); |
| 18 |
|
| } |
| 19 |
1 |
| RETVAL = ft_websocket(aTHX_ ft_ua_of(aTHX_ self), url, opt); |
| 20 |
1 |
| SvREFCNT_dec((SV *)opt); |
| 21 |
|
| } |
| 22 |
|
| OUTPUT: |
| 23 |
|
| RETVAL |
| 24 |
|
| |
| 25 |
|
| MODULE = Fetch PACKAGE = Fetch::WebSocket |
| 26 |
|
| |
| 27 |
|
| # Send a text message (opcode 0x1); the payload is encoded as UTF-8. |
| 28 |
|
| void |
| 29 |
|
| send(self, data) |
| 30 |
|
| SV *self |
| 31 |
|
| SV *data |
| 32 |
|
| CODE: |
| 33 |
|
| { |
| 34 |
4 |
| ft_conn *c = ft_ws_of(aTHX_ self); |
| 35 |
|
| STRLEN len; |
| 36 |
4 |
| const char *d = SvPVutf8(data, len); |
| 37 |
4 |
| ft_ws_send(aTHX_ c, 0x1, d, len); |
| 38 |
|
| } |
| 39 |
|
| |
| 40 |
|
| # Send a binary message (opcode 0x2) - raw bytes, no encoding. |
| 41 |
|
| void |
| 42 |
|
| send_binary(self, data) |
| 43 |
|
| SV *self |
| 44 |
|
| SV *data |
| 45 |
|
| CODE: |
| 46 |
|
| { |
| 47 |
1 |
| ft_conn *c = ft_ws_of(aTHX_ self); |
| 48 |
|
| STRLEN len; |
| 49 |
1 |
| const char *d = SvPVbyte(data, len); |
| 50 |
1 |
| ft_ws_send(aTHX_ c, 0x2, d, len); |
| 51 |
|
| } |
| 52 |
|
| |
| 53 |
|
| # A Future resolving to the next inbound message (text decoded, binary raw). |
| 54 |
|
| SV * |
| 55 |
|
| next_message(self) |
| 56 |
|
| SV *self |
| 57 |
|
| CODE: |
| 58 |
|
| { |
| 59 |
4 |
| ft_conn *c = ft_ws_of(aTHX_ self); |
| 60 |
4 |
| RETVAL = hmf_new(aTHX_ "Fetch::Future"); |
| 61 |
4 |
▶ | if (c->ws_inbox && av_len(c->ws_inbox) >= 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c->ws_inbox && av_len(c->ws_inbox) >= 0) { |
0 |
4 |
4 |
| if (c->ws_inbox && av_len(c->ws_inbox) >= 0) { |
0 |
0 |
0 |
|
| 62 |
0 |
| SV *msg = av_shift(c->ws_inbox); /* buffered already */ |
| 63 |
0 |
| hmf_settle(aTHX_ RETVAL, HMF_DONE, &msg, 1); |
| 64 |
0 |
| SvREFCNT_dec(msg); |
| 65 |
4 |
▶ | } else if (c->ws_closed) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c->ws_closed) { |
0 |
4 |
4 |
|
| 66 |
0 |
| SV *e = sv_2mortal(newSVpvs("Fetch: websocket closed")); |
| 67 |
0 |
| hmf_settle(aTHX_ RETVAL, HMF_FAILED, &e, 1); |
| 68 |
|
| } else { |
| 69 |
4 |
▶ | if (c->ws_waiter) SvREFCNT_dec(c->ws_waiter); |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->ws_waiter) SvREFCNT_dec(c->ws_waiter); |
0 |
4 |
4 |
|
| 70 |
4 |
| c->ws_waiter = SvREFCNT_inc(RETVAL); /* resolved on arrival */ |
| 71 |
|
| } |
| 72 |
|
| } |
| 73 |
|
| OUTPUT: |
| 74 |
|
| RETVAL |
| 75 |
|
| |
| 76 |
|
| # Install a persistent callback for each inbound message; any already buffered |
| 77 |
|
| # are delivered immediately. |
| 78 |
|
| void |
| 79 |
|
| on_message(self, cb) |
| 80 |
|
| SV *self |
| 81 |
|
| SV *cb |
| 82 |
|
| CODE: |
| 83 |
|
| { |
| 84 |
1 |
| ft_conn *c = ft_ws_of(aTHX_ self); |
| 85 |
1 |
▶ | if (c->ws_on_message) SvREFCNT_dec(c->ws_on_message); |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->ws_on_message) SvREFCNT_dec(c->ws_on_message); |
0 |
1 |
1 |
|
| 86 |
1 |
| c->ws_on_message = SvREFCNT_inc(cb); |
| 87 |
1 |
▶ | if (c->ws_inbox) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->ws_inbox) { |
0 |
1 |
1 |
|
| 88 |
0 |
▶ | while (av_len(c->ws_inbox) >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (av_len(c->ws_inbox) >= 0) { |
0 |
0 |
0 |
|
| 89 |
0 |
| SV *msg = av_shift(c->ws_inbox); |
| 90 |
0 |
| dSP; |
| 91 |
0 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
0 |
0 |
|
| 92 |
0 |
▶ | XPUSHs(sv_2mortal(msg)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(msg)); |
0 |
0 |
0 |
|
| 93 |
0 |
| PUTBACK; |
| 94 |
0 |
| call_sv(cb, G_DISCARD | G_EVAL); |
| 95 |
0 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 96 |
|
| } |
| 97 |
|
| } |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
|
| # Install a callback fired once when the socket closes. |
| 101 |
|
| void |
| 102 |
|
| on_close(self, cb) |
| 103 |
|
| SV *self |
| 104 |
|
| SV *cb |
| 105 |
|
| CODE: |
| 106 |
|
| { |
| 107 |
0 |
| ft_conn *c = ft_ws_of(aTHX_ self); |
| 108 |
0 |
▶ | if (c->ws_on_close) SvREFCNT_dec(c->ws_on_close); |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->ws_on_close) SvREFCNT_dec(c->ws_on_close); |
0 |
0 |
0 |
|
| 109 |
0 |
| c->ws_on_close = SvREFCNT_inc(cb); |
| 110 |
|
| } |
| 111 |
|
| |
| 112 |
|
| # True once the socket has closed. |
| 113 |
|
| int |
| 114 |
|
| is_closed(self) |
| 115 |
|
| SV *self |
| 116 |
|
| CODE: |
| 117 |
1 |
▶ | RETVAL = ft_ws_of(aTHX_ self)->ws_closed; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = ft_ws_of(aTHX_ self)->ws_closed; |
0 |
1 |
1 |
|
| 118 |
|
| OUTPUT: |
| 119 |
|
| RETVAL |
| 120 |
|
| |
| 121 |
|
| # Send a close frame and shut the socket down. |
| 122 |
|
| void |
| 123 |
|
| close(self) |
| 124 |
|
| SV *self |
| 125 |
|
| CODE: |
| 126 |
1 |
| ft_ws_close(aTHX_ ft_ws_of(aTHX_ self)); |
| 127 |
|
| |
| 128 |
|
| void |
| 129 |
|
| DESTROY(self) |
| 130 |
|
| SV *self |
| 131 |
|
| CODE: |
| 132 |
|
| { |
| 133 |
1 |
| ft_conn *c = ft_ws_of(aTHX_ self); |
| 134 |
1 |
▶ | if (c) ft_conn_free(aTHX_ c); |
Branch: 1 branch
| Branch | True | False | Total |
| if (c) ft_conn_free(aTHX_ c); |
1 |
0 |
1 |
|
| 135 |
|
| } |