-
Notifications
You must be signed in to change notification settings - Fork 35
/
example_server_stream.erl
189 lines (168 loc) · 5.95 KB
/
example_server_stream.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
%%--------------------------------------------------------------------
%% Copyright (c) 2022-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
-module(example_server_stream).
-behavior(quicer_stream).
-export([
init_handoff/4,
post_handoff/3,
new_stream/3,
start_completed/3,
send_complete/3,
peer_send_shutdown/3,
peer_send_aborted/3,
peer_receive_aborted/3,
send_shutdown_complete/3,
stream_closed/3,
peer_accepted/3,
passive/3
]).
-export([handle_stream_data/4]).
-export([handle_call/3]).
-export([handle_info/2]).
-include("quicer.hrl").
-include_lib("snabbkaffe/include/snabbkaffe.hrl").
init_handoff(Stream, _StreamOpts, Conn, #{flags := Flags}) ->
InitState = #{
stream => Stream,
conn => Conn,
peer_stream => undefined,
is_local => false,
is_unidir => quicer:is_unidirectional(Flags)
},
ct:pal("init_handoff ~p", [{InitState, _StreamOpts}]),
{ok, InitState}.
post_handoff(Stream, _PostData, State) ->
case quicer:setopt(Stream, active, true) of
ok ->
{ok, State};
{error, closed} ->
{stop, closed, State}
end.
new_stream(Stream, #{flags := Flags}, Conn) ->
InitState = #{
stream => Stream,
conn => Conn,
peer_stream => undefined,
is_local => false,
is_unidir => quicer:is_unidirectional(Flags)
},
{ok, InitState}.
peer_accepted(_Stream, _Flags, S) ->
%% we just ignore it
{ok, S}.
peer_receive_aborted(Stream, ErrorCode, #{is_unidir := false} = S) ->
%% we abort send with same reason
quicer:async_shutdown_stream(Stream, ?QUIC_STREAM_SHUTDOWN_FLAG_ABORT, ErrorCode),
{ok, S};
peer_receive_aborted(Stream, ErrorCode, #{is_unidir := true, is_local := true} = S) ->
quicer:async_shutdown_stream(Stream, ?QUIC_STREAM_SHUTDOWN_FLAG_ABORT, ErrorCode),
{ok, S}.
peer_send_aborted(Stream, ErrorCode, #{is_unidir := false} = S) ->
%% we abort receive with same reason
quicer:async_shutdown_stream(Stream, ?QUIC_STREAM_SHUTDOWN_FLAG_ABORT_RECEIVE, ErrorCode),
{ok, S};
peer_send_aborted(Stream, ErrorCode, #{is_unidir := true, is_local := false} = S) ->
quicer:async_shutdown_stream(Stream, ?QUIC_STREAM_SHUTDOWN_FLAG_ABORT_RECEIVE, ErrorCode),
{ok, S}.
peer_send_shutdown(Stream, _Flags, S) ->
case quicer:async_shutdown_stream(Stream, ?QUIC_STREAM_SHUTDOWN_FLAG_GRACEFUL, 0) of
ok -> ok;
{error, _} -> ok
end,
{ok, S}.
send_complete(_Stream, false, S) ->
{ok, S};
send_complete(_Stream, true = _IsCanceled, S) ->
ct:pal("~p : send is canceled", [?FUNCTION_NAME]),
{ok, S}.
send_shutdown_complete(_Stream, _Flags, S) ->
ct:pal("~p : stream send is complete", [?FUNCTION_NAME]),
{ok, S}.
start_completed(_Stream, #{status := success, stream_id := StreamId}, S) ->
{ok, S#{stream_id => StreamId}};
start_completed(_Stream, #{status := Other}, S) ->
%% or we could retry
{stop, {start_fail, Other}, S}.
handle_stream_data(
Stream, <<"flow_control.enable_bidi">> = Bin, _Flags, #{is_unidir := true, conn := Conn} = State
) ->
?tp(debug, #{stream => Stream, data => Bin, module => ?MODULE, dir => unidir}),
ok = quicer:setopt(Conn, settings, #{peer_bidi_stream_count => 2}),
{ok, State};
handle_stream_data(Stream, Bin, _Flags, #{is_unidir := false} = State) ->
%% for bidir stream, we just echo in place.
?tp(debug, #{stream => Stream, data => Bin, module => ?MODULE, dir => bidir}),
ct:pal("Server recv: ~p from ~p", [Bin, Stream]),
case quicer:send(Stream, Bin) of
{ok, _} ->
{ok, State};
{error, closed} ->
{stop, {shutdown, closed}, State}
end;
handle_stream_data(
Stream, Bin, _Flags, #{is_unidir := true, peer_stream := PeerStream, conn := Conn} = State
) ->
?tp(debug, #{stream => Stream, data => Bin, module => ?MODULE, dir => unidir}),
ct:pal("Server recv: ~p from ~p", [Bin, Stream]),
case PeerStream of
undefined ->
case
quicer_local_stream:start(
?MODULE,
Conn,
[{open_flag, ?QUIC_STREAM_OPEN_FLAG_UNIDIRECTIONAL}],
[]
)
of
{ok, StreamProc} ->
catch quicer_stream:send(StreamProc, Bin),
{ok, State#{peer_stream := StreamProc}};
{error, {_, _}} ->
{ok, State};
{error, closed} ->
{ok, State}
end;
StreamProc when is_pid(StreamProc) ->
{ok, _} = quicer_stream:send(StreamProc, Bin),
{ok, State}
end.
passive(_Stream, undefined, S) ->
ct:fail("Steam go into passive mode"),
{ok, S}.
stream_closed(
_Stream,
#{
is_conn_shutdown := IsConnShutdown,
is_app_closing := IsAppClosing,
is_shutdown_by_app := IsAppShutdown,
is_closed_remotely := IsRemote,
status := Status,
error := Code
},
S
) when
is_boolean(IsConnShutdown) andalso
is_boolean(IsAppClosing) andalso
is_boolean(IsAppShutdown) andalso
is_boolean(IsRemote) andalso
is_atom(Status) andalso
is_integer(Code)
->
{stop, normal, S}.
handle_call(_Request, _From, S) ->
{reply, {error, not_impl}, S}.
handle_info(Info, S) ->
{ok, S}.