-
Notifications
You must be signed in to change notification settings - Fork 35
/
quicer_echo_server_stream_callback.erl
149 lines (131 loc) · 4.09 KB
/
quicer_echo_server_stream_callback.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
%%--------------------------------------------------------------------
%% Copyright (c) 2021-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(quicer_echo_server_stream_callback).
-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,
handle_call/4
]).
-export([handle_stream_data/4]).
%% @doc handle handoff from other stream owner.
init_handoff(Stream, _StreamOpts, Conn, #{is_orphan := true, flags := Flags}) ->
InitState = #{
stream => Stream,
conn => Conn,
is_local => false,
is_unidir => quicer:is_unidirectional(Flags),
echo_stream => undefined,
sent_bytes => 0
},
%ct:pal("init_handoff ~p", [{InitState, StreamOpts}]),
{ok, InitState}.
post_handoff(Stream, _PostData, State) ->
quicer:setopt(Stream, active, true),
{ok, State}.
%% @doc accepted new stream.
new_stream(Stream, #{flags := Flags} = StreamOpts, Conn) ->
InitState = #{
stream => Stream,
conn => Conn,
is_local => false,
echo_stream => undefined,
stream_opts => StreamOpts,
is_unidir => quicer:is_unidirectional(Flags),
sent_bytes => 0
},
{ok, InitState}.
peer_accepted(_Stream, _Flags, S) ->
{ok, S}.
peer_receive_aborted(_Stream, _Flags, S) ->
{ok, S}.
peer_send_aborted(Stream, _Flags, S) ->
quicer:async_close_stream(Stream),
{ok, S}.
peer_send_shutdown(Stream, _Flags, S) ->
quicer:async_close_stream(Stream),
{ok, S}.
send_complete(_Stream, _Flags, S) ->
{ok, S}.
send_shutdown_complete(_Stream, _Flags, S) ->
{ok, S}.
start_completed(_Stream, _Flags, S) ->
{ok, S}.
handle_stream_data(
Stream,
Bin,
_Opts,
#{
sent_bytes := Cnt,
stream_opts := StreamOpts,
conn := Conn,
echo_stream := undefined,
stream := Stream
} = State
) ->
case maps:get(is_echo_new_stream, StreamOpts, false) of
false ->
case quicer:send(Stream, echo_msg(Bin, State)) of
{ok, Size} ->
{ok, State#{sent_bytes => Cnt + Size}};
_ ->
%% handle error in test aborted.
{ok, State}
end;
true ->
%% echo reply with a new stream from server to client.
{ok, EchoStream} = quicer:start_stream(Conn, StreamOpts),
case quicer:send(EchoStream, echo_msg(Bin, State)) of
{ok, Size} ->
{ok, State#{sent_bytes => Cnt + Size, echo_stream => EchoStream}};
_ ->
%% handle error in test aborted.
{ok, State}
end
end;
handle_stream_data(
Stream,
Bin,
_Opts,
#{
sent_bytes := Cnt,
echo_stream := _Ignore
} = State
) ->
{ok, Size} = quicer:send(Stream, Bin),
{ok, State#{sent_bytes => Cnt + Size}}.
passive(_Stream, undefined, S) ->
ct:fail("Steam go into passive mode"),
{stop, no_passive, S}.
handle_call(_Stream, _Request, _Opts, _CBState) ->
ok.
stream_closed(_Stream, _Flags, S) ->
{stop, normal, S}.
%% For snabbkaffe, RID is meaningful on the same node.
echo_msg(<<"__STATE__">>, State) ->
quicer_test_lib:encode_stream_term(State);
echo_msg(Msg, _State) ->
Msg.