@@ -8,14 +8,27 @@ use crate::twamp_control::StartAck;
88use crate :: twamp_control:: StartSessions ;
99use crate :: twamp_control:: StopSessions ;
1010use crate :: twamp_control:: { ServerGreeting , SetUpResponse } ;
11- use anyhow:: Result ;
1211use deku:: prelude:: * ;
12+ use std:: io;
1313use std:: time:: Duration ;
14+ use thiserror:: Error ;
1415use tokio:: io:: { AsyncReadExt , AsyncWriteExt } ;
1516use tokio:: net:: TcpStream ;
1617use tokio:: sync:: oneshot;
1718use tracing:: * ;
1819
20+ #[ derive( Error , Debug ) ]
21+ pub enum ServerError {
22+ #[ error( "failed to read TWAMP-Control message" ) ]
23+ ReadError ( ControlMessage , #[ source] io:: Error ) ,
24+
25+ #[ error( "failed to send TWAMP-Control message" ) ]
26+ WriteError ( ControlMessage , #[ source] io:: Error ) ,
27+
28+ #[ error( "failed to convert b/w rust and wire format." ) ]
29+ WireConversionError ( ControlMessage , #[ source] DekuError ) ,
30+ }
31+
1932/// Server is responsible for handling incoming [TWAMP-Control](crate::twamp_control) connection from a
2033/// [Control-Client](crate::control_client::ControlClient).
2134#[ derive( Debug ) ]
@@ -35,115 +48,166 @@ impl Server {
3548 start_ack_tx : oneshot:: Sender < ( ) > ,
3649 stop_session_tx : oneshot:: Sender < ( ) > ,
3750 timeout_tx : oneshot:: Sender < u64 > ,
38- ) -> Result < ( ) > {
51+ ) -> anyhow :: Result < ( ) > {
3952 self . send_server_greeting ( ) . await ?;
4053
4154 self . read_set_up_response ( ) . await ?;
4255 self . send_server_start ( ) . await ?;
4356
4457 let request_tw_session = self . read_request_tw_session ( ) . await ?;
45- req_tw_tx. send ( request_tw_session. clone ( ) ) . unwrap ( ) ;
46- let final_port = ref_port_rx. await . unwrap ( ) ;
58+ req_tw_tx
59+ . send ( request_tw_session. clone ( ) )
60+ . expect ( "RequestTwSession should be sent over channel." ) ;
61+ let final_port = ref_port_rx
62+ . await
63+ . expect ( "should have received the final port over channel." ) ;
4764 self . send_accept_session ( final_port) . await ?;
48- timeout_tx. send ( request_tw_session. timeout ) . unwrap ( ) ;
65+ timeout_tx
66+ . send ( request_tw_session. timeout )
67+ . expect ( "timeout should be sent over channel." ) ;
4968
5069 self . read_start_sessions ( ) . await ?;
5170 self . send_start_ack ( ) . await ?;
52- start_ack_tx. send ( ( ) ) . unwrap ( ) ;
71+ start_ack_tx
72+ . send ( ( ) )
73+ . expect ( "StartAck should be sent over channel" ) ;
5374
5475 self . read_stop_sessions ( ) . await ?;
55- stop_session_tx. send ( ( ) ) . unwrap ( ) ;
76+ stop_session_tx
77+ . send ( ( ) )
78+ . expect ( "StopSession should be sent over channel." ) ;
5679
5780 Ok ( ( ) )
5881 }
5982
6083 /// Creates a `ServerGreeting`, converts to bytes and sends it out on `TWAMP-Control`.
61- pub async fn send_server_greeting ( & mut self ) -> Result < ServerGreeting > {
84+ pub async fn send_server_greeting ( & mut self ) -> anyhow :: Result < ServerGreeting > {
6285 info ! ( "Sending ServerGreeting" ) ;
6386 let server_greeting = ServerGreeting :: new ( & [ SecurityMode :: Unauthenticated ] ) ;
6487 debug ! ( "ServerGreeting: {:?}" , server_greeting) ;
65- let encoded = server_greeting. to_bytes ( ) . unwrap ( ) ;
66- self . socket . write_all ( & encoded[ ..] ) . await ?;
88+ let encoded = server_greeting
89+ . to_bytes ( )
90+ . map_err ( |err| ServerError :: WireConversionError ( ControlMessage :: ServerGreeting , err) ) ?;
91+ self . socket
92+ . write_all ( & encoded[ ..] )
93+ . await
94+ . map_err ( |err| ServerError :: WriteError ( ControlMessage :: ServerGreeting , err) ) ?;
6795 info ! ( "Sent ServerGreeting" ) ;
6896 Ok ( server_greeting)
6997 }
7098
7199 /// Reads from `TWAMP-Control` stream assuming the bytes to be received will be of a
72100 /// `Set-Up-Response`. Converts those bytes into a `Set-Up-Response` struct and returns it.
73- pub async fn read_set_up_response ( & mut self ) -> Result < SetUpResponse > {
101+ pub async fn read_set_up_response ( & mut self ) -> anyhow :: Result < SetUpResponse > {
74102 let mut buf = [ 0 ; SetUpResponse :: SERIALIZED_SIZE ] ;
75103 info ! ( "Reading Set-Up-Response" ) ;
76- self . socket . read_exact ( & mut buf) . await ?;
77- let ( _rest, set_up_response) = SetUpResponse :: from_bytes ( ( & buf, 0 ) ) . unwrap ( ) ;
104+ self . socket
105+ . read_exact ( & mut buf)
106+ . await
107+ . map_err ( |err| ServerError :: ReadError ( ControlMessage :: SetUpResponse , err) ) ?;
108+ let ( _rest, set_up_response) = SetUpResponse :: from_bytes ( ( & buf, 0 ) )
109+ . map_err ( |err| ServerError :: WireConversionError ( ControlMessage :: SetUpResponse , err) ) ?;
78110 debug ! ( "Set-Up-Response: {:?}" , set_up_response) ;
79111 info ! ( "Read Set-Up-Response" ) ;
80112 Ok ( set_up_response)
81113 }
82114
83115 /// Creates a `Server-Start`, converts to bytes and sends it out on `TWAMP-Control`.
84- pub async fn send_server_start ( & mut self ) -> Result < ServerStart > {
116+ pub async fn send_server_start ( & mut self ) -> anyhow :: Result < ServerStart > {
85117 info ! ( "Sending Server-Start" ) ;
86118 let server_start = ServerStart :: new ( Accept :: Ok , Duration :: new ( 123456 , 789 ) ) ;
87119 debug ! ( "Server-Start: {:?}" , server_start) ;
88- let encoded = server_start. to_bytes ( ) . unwrap ( ) ;
89- self . socket . write_all ( & encoded[ ..] ) . await ?;
120+ let encoded = server_start
121+ . to_bytes ( )
122+ . map_err ( |err| ServerError :: WireConversionError ( ControlMessage :: ServerStart , err) ) ?;
123+ self . socket
124+ . write_all ( & encoded[ ..] )
125+ . await
126+ . map_err ( |err| ServerError :: WriteError ( ControlMessage :: ServerStart , err) ) ?;
90127 info ! ( "Sent Server-Start" ) ;
91128 Ok ( server_start)
92129 }
93130
94131 /// Reads from `TWAMP-Control` stream assuming the bytes to be received will be of a
95132 /// `Request-TW-Session`. Converts those bytes into a `Request-TW-Session` struct and returns it.
96- pub async fn read_request_tw_session ( & mut self ) -> Result < RequestTwSession > {
133+ pub async fn read_request_tw_session ( & mut self ) -> anyhow :: Result < RequestTwSession > {
97134 let mut buf = [ 0 ; RequestTwSession :: SERIALIZED_SIZE ] ;
98135 debug ! ( "Reading Request-TW-Session" ) ;
99- self . socket . read_exact ( & mut buf) . await ?;
100- let ( _rest, request_tw_session) = RequestTwSession :: from_bytes ( ( & buf, 0 ) ) . unwrap ( ) ;
136+ self . socket
137+ . read_exact ( & mut buf)
138+ . await
139+ . map_err ( |err| ServerError :: ReadError ( ControlMessage :: RequestTwSession , err) ) ?;
140+ let ( _rest, request_tw_session) =
141+ RequestTwSession :: from_bytes ( ( & buf, 0 ) ) . map_err ( |err| {
142+ ServerError :: WireConversionError ( ControlMessage :: RequestTwSession , err)
143+ } ) ?;
101144 debug ! ( "Request-TW-Session: {:?}" , request_tw_session) ;
102145 info ! ( "Read Request-TW-Session" ) ;
103146 Ok ( request_tw_session)
104147 }
105148
106149 /// Creates a `Accept-Session`, converts to bytes and sends it out on `TWAMP-Control`.
107- pub async fn send_accept_session ( & mut self , receiver_port : u16 ) -> Result < AcceptSession > {
150+ pub async fn send_accept_session (
151+ & mut self ,
152+ receiver_port : u16 ,
153+ ) -> anyhow:: Result < AcceptSession > {
108154 info ! ( "Sending Accept-Session" ) ;
109155 let accept_session = AcceptSession :: new ( Accept :: Ok , receiver_port, 0 , 0 ) ;
110156 debug ! ( "Accept-Session: {:?}" , accept_session) ;
111- let encoded = accept_session. to_bytes ( ) . unwrap ( ) ;
112- self . socket . write_all ( & encoded[ ..] ) . await ?;
157+ let encoded = accept_session
158+ . to_bytes ( )
159+ . map_err ( |err| ServerError :: WireConversionError ( ControlMessage :: AcceptSession , err) ) ?;
160+ self . socket
161+ . write_all ( & encoded[ ..] )
162+ . await
163+ . map_err ( |err| ServerError :: WriteError ( ControlMessage :: AcceptSession , err) ) ?;
113164 debug ! ( "Sent Accept-Session" ) ;
114165 Ok ( accept_session)
115166 }
116167
117168 /// Reads from `TWAMP-Control` stream assuming the bytes to be received will be of a
118169 /// `Start-Sessions`. Converts those bytes into a `Start-Sessions` struct and returns it.
119- pub async fn read_start_sessions ( & mut self ) -> Result < StartSessions > {
170+ pub async fn read_start_sessions ( & mut self ) -> anyhow :: Result < StartSessions > {
120171 let mut buf = [ 0 ; StartSessions :: SERIALIZED_SIZE ] ;
121172 debug ! ( "Reading Start-Sessions" ) ;
122- self . socket . read_exact ( & mut buf) . await ?;
123- let ( _rest, start_sessions) = StartSessions :: from_bytes ( ( & buf, 0 ) ) . unwrap ( ) ;
173+ self . socket
174+ . read_exact ( & mut buf)
175+ . await
176+ . map_err ( |err| ServerError :: ReadError ( ControlMessage :: StartSessions , err) ) ?;
177+ let ( _rest, start_sessions) = StartSessions :: from_bytes ( ( & buf, 0 ) )
178+ . map_err ( |err| ServerError :: WireConversionError ( ControlMessage :: StartSessions , err) ) ?;
124179 debug ! ( "Start-Sessions: {:?}" , start_sessions) ;
125180 info ! ( "Read Start-Sessions" ) ;
126181 Ok ( start_sessions)
127182 }
128183
129184 /// Creates a `Start-Ack`, converts to bytes and sends it out on `TWAMP-Control`.
130- pub async fn send_start_ack ( & mut self ) -> Result < StartAck > {
185+ pub async fn send_start_ack ( & mut self ) -> anyhow :: Result < StartAck > {
131186 info ! ( "Sending Start-Ack" ) ;
132187 let start_ack = StartAck :: new ( Accept :: Ok ) ;
133188 debug ! ( "Start-Ack: {:?}" , start_ack) ;
134- let encoded = start_ack. to_bytes ( ) . unwrap ( ) ;
135- self . socket . write_all ( & encoded[ ..] ) . await ?;
189+ let encoded = start_ack
190+ . to_bytes ( )
191+ . map_err ( |err| ServerError :: WireConversionError ( ControlMessage :: StartAck , err) ) ?;
192+ self . socket
193+ . write_all ( & encoded[ ..] )
194+ . await
195+ . map_err ( |err| ServerError :: WriteError ( ControlMessage :: StartAck , err) ) ?;
136196 info ! ( "Sent Start-Ack" ) ;
137197 Ok ( start_ack)
138198 }
139199
140200 /// Reads from `TWAMP-Control` stream assuming the bytes to be received will be of a
141201 /// `Stop-Sessions`. Converts those bytes into a `Stop-Sessions` struct and returns it.
142- pub async fn read_stop_sessions ( & mut self ) -> Result < StopSessions > {
202+ pub async fn read_stop_sessions ( & mut self ) -> anyhow :: Result < StopSessions > {
143203 let mut buf = [ 0 ; StopSessions :: SERIALIZED_SIZE ] ;
144204 debug ! ( "Reading Stop-Sessions" ) ;
145- self . socket . read_exact ( & mut buf) . await ?;
146- let ( _rest, stop_sessions) = StopSessions :: from_bytes ( ( & buf, 0 ) ) . unwrap ( ) ;
205+ self . socket
206+ . read_exact ( & mut buf)
207+ . await
208+ . map_err ( |err| ServerError :: ReadError ( ControlMessage :: StopSessions , err) ) ?;
209+ let ( _rest, stop_sessions) = StopSessions :: from_bytes ( ( & buf, 0 ) )
210+ . map_err ( |err| ServerError :: WireConversionError ( ControlMessage :: StopSessions , err) ) ?;
147211 debug ! ( "Stop-Sessions: {:?}" , stop_sessions) ;
148212 info ! ( "Read Stop-Sessions" ) ;
149213 Ok ( stop_sessions)
0 commit comments