1+ % %%-------------------------------------------------------------------
2+ % % @doc
3+ % % == Libp2p Ack Stream ==
4+ % % @see libp2p_framed_stream
5+ % % @end
6+ % %%-------------------------------------------------------------------
17-module (libp2p_ack_stream ).
28
39-include (" pb/libp2p_ack_stream_pb.hrl" ).
1016 {ok , Ref :: any ()} | {error , term ()}.
1117-callback handle_ack (State :: any (), Ref :: any (), Seq :: [pos_integer ()], Reset :: boolean ()) -> ok .
1218
13- % % API
19+ % % ------------------------------------------------------------------
20+ % % API Function Exports
21+ % % ------------------------------------------------------------------
1422-export ([send_ack /3 ]).
15- % % libp2p_framed_stream
23+
24+ % % ------------------------------------------------------------------
25+ % % libp2p_framed_stream Function Exports
26+ % % ------------------------------------------------------------------
1627-export ([server /4 , client /2 , init /3 , handle_data /3 , handle_send /5 , handle_info /3 ]).
1728
1829
2536
2637-define (ACK_STREAM_TIMEOUT , timer :minutes (5 )).
2738
28- % % API
29- % %
39+ % % ------------------------------------------------------------------
40+ % % API Function Definitions
41+ % % ------------------------------------------------------------------
3042
43+ % %%-------------------------------------------------------------------
44+ % % @doc
45+ % % Send ack message
46+ % % @end
47+ % %%-------------------------------------------------------------------
3148-spec send_ack (pid (), pos_integer (), boolean ()) -> ok .
3249send_ack (Pid , Seq , Reset ) ->
3350 Pid ! {send_ack , Seq , Reset },
3451 ok .
3552
36- % % libp2p_framed_stream
37- % %
53+ % % ------------------------------------------------------------------
54+ % % libp2p_framed_stream Function Definitions
55+ % % ------------------------------------------------------------------
56+
57+ % % @hidden
3858client (Connection , Args ) ->
3959 libp2p_framed_stream :client (? MODULE , Connection , Args ).
4060
61+ % % @hidden
4162server (Connection , Path , _TID , Args ) ->
4263 libp2p_framed_stream :server (? MODULE , Connection , [Path | Args ]).
4364
65+ % % @hidden
4466init (server , Connection , [Path , AckModule , AckState | _ ]) ->
4567 case AckModule :accept_stream (AckState , self (), Path ) of
4668 {ok , AckRef } ->
@@ -55,6 +77,7 @@ init(client, Connection, [AckRef, AckModule, AckState | _]) ->
5577 {ok , # state {connection = Connection ,
5678 ack_ref = AckRef , ack_module = AckModule , ack_state = AckState }}.
5779
80+ % % @hidden
5881handle_data (_Kind , Data , State = # state {ack_ref = AckRef , ack_module = AckModule , ack_state = AckState }) ->
5982 case libp2p_ack_stream_pb :decode_msg (Data , libp2p_ack_frame_pb ) of
6083 # libp2p_ack_frame_pb {messages = Bin , seqs = Seq } when Bin /= [] ->
@@ -71,6 +94,7 @@ handle_data(_Kind, Data, State=#state{ack_ref=AckRef, ack_module=AckModule, ack_
7194 {noreply , State }
7295 end .
7396
97+ % % @hidden
7498handle_send (_Kind , From , Msgs , Timeout , State = # state {}) when is_list (Msgs ) ->
7599 {Seqs , Data } = lists :unzip (Msgs ),
76100 Msg = # libp2p_ack_frame_pb {messages = Data , seqs = Seqs },
@@ -79,6 +103,7 @@ handle_send(_Kind, From, {Data, Seq}, Timeout, State=#state{}) ->
79103 Msg = # libp2p_ack_frame_pb {messages = [Data ], seqs = [Seq ]},
80104 {ok , {reply , From , pending }, libp2p_ack_stream_pb :encode_msg (Msg ), Timeout , State # state {}}.
81105
106+ % % @hidden
82107handle_info (_Kind , {send_ack , Seq , Reset }, State = # state {}) ->
83108 Msg = # libp2p_ack_frame_pb {seqs = Seq , reset = Reset },
84109 {noreply , State , libp2p_ack_stream_pb :encode_msg (Msg )}.
0 commit comments