@@ -8,9 +8,7 @@ use std::{
88 time:: Instant ,
99} ;
1010
11- use fraktor_remote_core_rs:: {
12- config:: RemoteCompressionConfig , extension:: RemoteEvent , transport:: TransportError , wire:: CompressionTableKind ,
13- } ;
11+ use fraktor_remote_core_rs:: { config:: RemoteCompressionConfig , extension:: RemoteEvent , transport:: TransportError } ;
1412use futures:: { SinkExt as _, StreamExt as _} ;
1513use tokio:: {
1614 net:: { TcpListener , TcpStream } ,
@@ -21,11 +19,9 @@ use tokio::{
2119use tokio_util:: codec:: Framed ;
2220
2321use super :: {
22+ WireFrame ,
2423 client:: inbound_lane_index,
25- compression:: {
26- InboundCompressionAction , TcpCompressionTables , compression_advertisement_interval,
27- next_compression_advertisement_tick,
28- } ,
24+ compression:: { InboundCompressionAction , TcpCompressionTables } ,
2925 connection_loss_reporter:: ConnectionLossReporter ,
3026 frame_codec:: WireFrameCodec ,
3127 inbound_frame_event:: InboundFrameEvent ,
@@ -80,19 +76,22 @@ impl TcpServer {
8076 }
8177 }
8278
83- pub ( crate ) fn start_with_remote_events (
79+ pub ( crate ) fn start_with_remote_events < F > (
8480 & mut self ,
8581 inbound_txs : Vec < UnboundedSender < InboundFrameEvent > > ,
8682 remote_event_tx : Option < Sender < RemoteEvent > > ,
8783 monotonic_epoch : Instant ,
88- local_authority : String ,
89- ) -> Result < SocketAddr , TransportError > {
84+ local_authority_for_bound_port : F ,
85+ ) -> Result < SocketAddr , TransportError >
86+ where
87+ F : FnOnce ( u16 ) -> String , {
9088 if self . accept_task . is_some ( ) {
9189 return Err ( TransportError :: AlreadyRunning ) ;
9290 }
9391 let handle = Handle :: try_current ( ) . map_err ( |_| TransportError :: NotAvailable ) ?;
9492 let listener = StdTcpListener :: bind ( & self . bind_addr ) . map_err ( |_| TransportError :: SendFailed ) ?;
9593 let bound_addr = listener. local_addr ( ) . map_err ( |_| TransportError :: SendFailed ) ?;
94+ let local_authority = local_authority_for_bound_port ( bound_addr. port ( ) ) ;
9695 listener. set_nonblocking ( true ) . map_err ( |_| TransportError :: SendFailed ) ?;
9796 let listener = TcpListener :: from_std ( listener) . map_err ( |_| TransportError :: SendFailed ) ?;
9897 let frame_codec = self . frame_codec ;
@@ -164,69 +163,43 @@ async fn read_loop(
164163 let mut framed = Framed :: new ( stream, frame_codec) ;
165164 let mut authority = None ;
166165 let mut compression_tables = TcpCompressionTables :: new ( compression_config) ;
167- let mut actor_ref_advertisement_interval = compression_advertisement_interval (
168- compression_config. actor_ref_max ( ) ,
169- compression_config. actor_ref_advertisement_interval ( ) ,
170- ) ;
171- let mut manifest_advertisement_interval = compression_advertisement_interval (
172- compression_config. manifest_max ( ) ,
173- compression_config. manifest_advertisement_interval ( ) ,
174- ) ;
175166 let exit_cause = loop {
176- tokio:: select! {
177- next = framed. next( ) => match next {
178- | Some ( Ok ( decoded) ) => {
179- let decoded = match compression_tables. handle_inbound_frame( decoded, & local_authority) {
180- | Ok ( InboundCompressionAction :: Forward ( frame) ) => frame,
181- | Ok ( InboundCompressionAction :: Reply ( pdu) ) => {
182- if let Err ( err) = framed. send( crate :: transport:: tcp:: WireFrame :: Control ( pdu) ) . await {
183- tracing:: warn!( ?err, peer = %peer, "tcp server compression ack write error" ) ;
184- break Some ( TransportError :: SendFailed ) ;
185- }
186- continue ;
187- } ,
188- | Ok ( InboundCompressionAction :: Consumed ) => continue ,
189- | Err ( err) => {
190- tracing:: warn!( ?err, peer = %peer, "tcp server compression frame error" ) ;
167+ match framed. next ( ) . await {
168+ | Some ( Ok ( decoded) ) => {
169+ let decoded = match compression_tables. handle_inbound_frame ( decoded, & local_authority) {
170+ | Ok ( InboundCompressionAction :: Forward ( frame) ) => frame,
171+ | Ok ( InboundCompressionAction :: Reply ( pdu) ) => {
172+ if let Err ( err) = framed. send ( WireFrame :: Control ( pdu) ) . await {
173+ tracing:: warn!( ?err, peer = %peer, "tcp server compression ack write error" ) ;
191174 break Some ( TransportError :: SendFailed ) ;
192- } ,
193- } ;
194- if let Some ( frame_authority) = authority_for_frame( & decoded) {
195- authority = Some ( frame_authority) ;
196- }
197- let lane_index = inbound_lane_index( & peer, authority. as_ref( ) , & decoded, inbound_txs. len( ) ) ;
198- let inbound_tx =
199- inbound_txs. get( lane_index) . expect( "inbound_lane_index returns an index within the inbound_txs lane count" ) ;
200- if inbound_tx
201- . send( InboundFrameEvent { peer: peer. clone( ) , authority: authority. clone( ) , frame: decoded } )
202- . is_err( )
203- {
204- // Receiver dropped — the transport is shutting down.
205- break None ;
206- }
207- } ,
208- | Some ( Err ( err) ) => {
209- tracing:: warn!( ?err, peer = %peer, "tcp frame decode error" ) ;
210- break Some ( TransportError :: SendFailed ) ;
211- } ,
212- | None => break Some ( TransportError :: ConnectionClosed ) ,
213- } ,
214- _ = next_compression_advertisement_tick( & mut actor_ref_advertisement_interval) => {
215- if let Some ( frame) = compression_tables. create_advertisement( CompressionTableKind :: ActorRef , & local_authority)
216- && let Err ( err) = framed. send( frame) . await
217- {
218- tracing:: warn!( ?err, peer = %peer, "tcp server actor-ref compression advertisement write error" ) ;
219- break Some ( TransportError :: SendFailed ) ;
175+ }
176+ continue ;
177+ } ,
178+ | Ok ( InboundCompressionAction :: Consumed ) => continue ,
179+ | Err ( err) => {
180+ tracing:: warn!( ?err, peer = %peer, "tcp server compression frame error" ) ;
181+ break Some ( TransportError :: SendFailed ) ;
182+ } ,
183+ } ;
184+ if let Some ( frame_authority) = authority_for_frame ( & decoded) {
185+ authority = Some ( frame_authority) ;
220186 }
221- } ,
222- _ = next_compression_advertisement_tick( & mut manifest_advertisement_interval) => {
223- if let Some ( frame) = compression_tables. create_advertisement( CompressionTableKind :: Manifest , & local_authority)
224- && let Err ( err) = framed. send( frame) . await
187+ let lane_index = inbound_lane_index ( & peer, authority. as_ref ( ) , & decoded, inbound_txs. len ( ) ) ;
188+ let inbound_tx =
189+ inbound_txs. get ( lane_index) . expect ( "inbound_lane_index returns an index within the inbound_txs lane count" ) ;
190+ if inbound_tx
191+ . send ( InboundFrameEvent { peer : peer. clone ( ) , authority : authority. clone ( ) , frame : decoded } )
192+ . is_err ( )
225193 {
226- tracing :: warn! ( ?err , peer = %peer , "tcp server manifest compression advertisement write error" ) ;
227- break Some ( TransportError :: SendFailed ) ;
194+ // Receiver dropped — the transport is shutting down.
195+ break None ;
228196 }
229197 } ,
198+ | Some ( Err ( err) ) => {
199+ tracing:: warn!( ?err, peer = %peer, "tcp frame decode error" ) ;
200+ break Some ( TransportError :: SendFailed ) ;
201+ } ,
202+ | None => break Some ( TransportError :: ConnectionClosed ) ,
230203 }
231204 } ;
232205 if let ( Some ( cause) , Some ( authority) , Some ( sender) ) = ( exit_cause, authority, remote_event_tx) {
0 commit comments