Debian Codegraph @

test/handlers/wt_echo_h.erl

raw
1 %% This module echoes client events back,
2 %% including creating new streams.
3
4 -module(wt_echo_h).
5 -behavior(cowboy_webtransport).
6
7 -export([init/2]).
8 -export([webtransport_handle/2]).
9 -export([webtransport_info/2]).
10 -export([terminate/3]).
11
12 %% -define(DEBUG, 1).
13 -ifdef(DEBUG).
14 -define(LOG(Fmt, Args), ct:pal(Fmt, Args)).
15 -else.
16 -define(LOG(Fmt, Args), _ = Fmt, _ = Args, ok).
17 -endif.
18
19 init(Req0, _) ->
20 ?LOG("WT init ~p~n", [Req0]),
21 Req = case cowboy_req:parse_header(<<"wt-available-protocols">>, Req0) of
22 undefined ->
23 Req0;
24 [Protocol|_] ->
25 cowboy_req:set_resp_header(<<"wt-protocol">>, cow_http_hd:wt_protocol(Protocol), Req0)
26 end,
27 {cowboy_webtransport, Req, #{}}.
28
29 webtransport_handle(Event = {stream_open, StreamID, bidi}, Streams) ->
30 ?LOG("WT handle ~p~n", [Event]),
31 {[], Streams#{StreamID => bidi}};
32 webtransport_handle(Event = {stream_open, StreamID, unidi}, Streams) ->
33 ?LOG("WT handle ~p~n", [Event]),
34 OpenStreamRef = make_ref(),
35 {[{open_stream, OpenStreamRef, unidi, <<>>}], Streams#{
36 StreamID => {unidi_remote, OpenStreamRef},
37 OpenStreamRef => {unidi_local, StreamID}}};
38 webtransport_handle(Event = {opened_stream_id, OpenStreamRef, OpenStreamID}, Streams) ->
39 ?LOG("WT handle ~p~n", [Event]),
40 case Streams of
41 #{OpenStreamRef := bidi} ->
42 {[], maps:remove(OpenStreamRef, Streams#{
43 OpenStreamID => bidi
44 })};
45 #{OpenStreamRef := {unidi_local, RemoteStreamID}} ->
46 #{RemoteStreamID := {unidi_remote, OpenStreamRef}} = Streams,
47 {[], maps:remove(OpenStreamRef, Streams#{
48 RemoteStreamID => {unidi_remote, OpenStreamID},
49 OpenStreamID => {unidi_local, RemoteStreamID}
50 })}
51 end;
52 webtransport_handle(Event = {stream_data, StreamID, _IsFin, <<"TEST:", Test/bits>>}, Streams) ->
53 ?LOG("WT handle ~p~n", [Event]),
54 case Test of
55 <<"open_bidi">> ->
56 OpenStreamRef = make_ref(),
57 {[{open_stream, OpenStreamRef, bidi, <<>>}],
58 Streams#{OpenStreamRef => bidi}};
59 <<"initiate_close">> ->
60 {[initiate_close], Streams};
61 <<"close">> ->
62 {[close], Streams};
63 <<"close_app_code">> ->
64 {[{close, 1234567890}], Streams};
65 <<"close_app_code_msg">> ->
66 {[{close, 1234567890, <<"onetwothreefourfivesixseveneightnineten">>}], Streams};
67 <<"event_pid:", EventPidBin/bits>> ->
68 {[{send, StreamID, nofin, <<"event_pid_received">>}],
69 Streams#{event_pid => binary_to_term(EventPidBin)}}
70 end;
71 webtransport_handle(Event = {stream_data, StreamID, IsFin, Data}, Streams) ->
72 ?LOG("WT handle ~p~n", [Event]),
73 case Streams of
74 #{StreamID := bidi} ->
75 {[{send, StreamID, IsFin, Data}], Streams};
76 #{StreamID := {unidi_remote, Ref}} when is_reference(Ref) ->
77 %% The stream isn't ready. We try again later.
78 erlang:send_after(100, self(), {try_again, Event}),
79 {[], Streams};
80 #{StreamID := {unidi_remote, LocalStreamID}} ->
81 {[{send, LocalStreamID, IsFin, Data}], Streams}
82 end;
83 webtransport_handle(Event = {datagram, Data}, Streams) ->
84 ?LOG("WT handle ~p~n", [Event]),
85 {[{send, datagram, Data}], Streams};
86 webtransport_handle(Event = close_initiated, Streams) ->
87 ?LOG("WT handle ~p~n", [Event]),
88 {[{send, datagram, <<"TEST:close_initiated">>}], Streams};
89 webtransport_handle(Event, Streams) ->
90 ?LOG("WT handle ignore ~p~n", [Event]),
91 {[], Streams}.
92
93 webtransport_info({try_again, Event}, Streams) ->
94 ?LOG("WT try_again ~p", [Event]),
95 webtransport_handle(Event, Streams).
96
97 terminate(Reason, Req, State=#{event_pid := EventPid}) ->
98 ?LOG("WT terminate ~0p~n~0p~n~0p", [Reason, Req, State]),
99 EventPid ! {'$wt_echo_h', terminate, Reason, Req, State},
100 ok;
101 terminate(Reason, Req, State) ->
102 ?LOG("WT terminate ~0p~n~0p~n~0p", [Reason, Req, State]),
103 ok.