11use std:: { error:: Error , sync:: Arc } ;
2+ use axum:: Json ;
23use tokio:: sync:: { broadcast, Mutex } ;
3- use wtransport:: { endpoint:: IncomingSession , Endpoint , Identity , ServerConfig } ;
4+ use wtransport:: { endpoint:: IncomingSession , Connection , Endpoint , Identity , ServerConfig } ;
45use crate :: services:: docker;
56
6- pub async fn start_webtransport ( ) -> Result < ( ) , Box < dyn Error > > {
7+ pub async fn start_webtransport ( ) -> Result < ( ) , Box < dyn Error + Send + Sync > > {
78 let identity = match Identity :: load_pemfiles ( "localhost.pem" , "localhost-key.pem" ) . await {
89 Ok ( identity) => identity,
910 Err ( e) => {
@@ -44,7 +45,7 @@ pub async fn start_webtransport() -> Result<(), Box<dyn Error>> {
4445 }
4546}
4647
47- async fn handle_connection ( incoming_session : IncomingSession , tx : broadcast:: Sender < String > ) -> Result < ( ) , Box < dyn Error > > {
48+ async fn handle_connection ( incoming_session : IncomingSession , tx : broadcast:: Sender < String > ) -> Result < ( ) , Box < dyn Error + Send + Sync > > {
4849 let request = match incoming_session. await {
4950 Ok ( request) => request,
5051 Err ( e) => {
@@ -63,6 +64,39 @@ async fn handle_connection(incoming_session: IncomingSession, tx: broadcast::Sen
6364
6465 log:: info!( "Accepted connection from {:?}" , connection. remote_address( ) ) ;
6566
67+ let datagram_handle = tokio:: spawn ( handle_datagram ( connection. clone ( ) ) ) ;
68+
69+ let bidirectional_handle = tokio:: spawn ( handle_bidirectionnal ( connection, tx) ) ;
70+
71+ let _ = tokio:: join!( datagram_handle, bidirectional_handle) ;
72+ Ok ( ( ) )
73+ }
74+
75+ async fn handle_datagram ( connection : Connection ) -> Result < ( ) , Box < dyn Error + Send + Sync > > {
76+ loop {
77+ let datagram = match connection. receive_datagram ( ) . await {
78+ Ok ( datagram) => datagram,
79+ Err ( e) => {
80+ log:: error!( "Failed to receive datagram: {:?}" , e) ;
81+ return Err ( Box :: new ( e) ) ;
82+ }
83+ } ;
84+ let received_message = String :: from_utf8_lossy ( & datagram) ;
85+ log:: info!( "Received message: {:?}" , received_message) ;
86+
87+ let response = b"Hello from server via datagram!" ;
88+ match connection. send_datagram ( response) {
89+ Ok ( _) => { } ,
90+ Err ( e) => {
91+ log:: error!( "Failed to send datagram: {:?}" , e) ;
92+ return Err ( Box :: new ( e) ) ;
93+ }
94+ } ;
95+ }
96+ }
97+
98+ async fn handle_bidirectionnal ( connection : Connection , tx : broadcast:: Sender < String > ) -> Result < ( ) , Box < dyn Error + Send + Sync > > {
99+ log:: info!( "Accepted connection from {:?}" , connection. remote_address( ) ) ;
66100
67101 while let Ok ( ( send_stream, mut recv_stream) ) = connection. accept_bi ( ) . await {
68102 log:: trace!( "Accepted bidirectional stream" ) ;
@@ -80,8 +114,8 @@ async fn handle_connection(incoming_session: IncomingSession, tx: broadcast::Sen
80114 log:: info!( "Received message: {:?}" , received_message) ;
81115
82116 let mut send_stream = send_stream. lock ( ) . await ;
117+
83118 let _ = send_stream. write_all ( b"Hello from server!" ) . await ;
84- //TODO: Send response
85119 }
86120 } ) ;
87121 }
@@ -98,4 +132,10 @@ async fn handle_connection(incoming_session: IncomingSession, tx: broadcast::Sen
98132 }
99133
100134 Ok ( ( ) )
135+ }
136+
137+ pub fn handle_event ( event : String , data : Json < String > ) {
138+ log:: info!( "Received event: {:?}" , event) ;
139+ log:: info!( "Received data: {:?}" , data) ;
140+
101141}
0 commit comments