/// Listens for incoming client connections. Does not return until the
/// server shuts down.
Listens for incoming client connections. Does not return until the
server shuts down.
Listens for incoming client connections . Does not return until the server shuts down .
pub async fn listen_on(&self, port: u16) -> Result<(), Error> {
        let certificate = self.tls_certificate().await?;
        let keypair =
            KeyPair::from_parts(certificate.certificate_chain, certificate.private_key.0)?;
        let mut builder = Endpoint::builder();
        builder.set_protocols([CURRENT_PROTOCOL_VERSION.as_bytes().to_vec()]);
        builder.set_address(([0; 8], port).into());
        builder
            .set_max_idle_timeout(None)
            .map_err(|err| Error::Core(bonsaidb_core::Error::Transport(err.to_string())))?;
        builder.set_server_key_pair(Some(keypair));
        let mut server = builder
            .build()
            .map_err(|err| Error::Core(bonsaidb_core::Error::Transport(err.to_string())))?;
        {
            let mut endpoint = fast_async_write!(self.data.endpoint);
            *endpoint = Some(server.clone());
        }
        let mut shutdown_watcher = self
            .data
            .shutdown
            .watcher()
            .await
            .expect("server already shut down");
        while let Some(result) = tokio::select! {
            shutdown_state = shutdown_watcher.wait_for_shutdown() => {
                drop(server.close_incoming());
                if matches!(shutdown_state, ShutdownState::GracefulShutdown) {
                    server.wait_idle().await;
                }
                drop(server.close());
                None
            },
            msg = server.next() => msg
        } {
            let connection = result.accept::<()>().await?;
            let task_self = self.clone();
            tokio::spawn(async move {
                let address = connection.remote_address();
                if let Err(err) = task_self.handle_bonsai_connection(connection).await {
                    log::error!("[server] closing connection {}: {:?}", address, err);
                }
            });
        }
        Ok(())
    }
pub async fn listen_on ( & self , port : u16 ) -> Result < ( ) , Error > { let certificate = self . tls_certificate ( ) . await ? ; let keypair = KeyPair :: from_parts ( certificate . certificate_chain , certificate . private_key . 0 ) ? ; let mut builder = Endpoint :: builder ( ) ; builder . set_protocols ( [ CURRENT_PROTOCOL_VERSION . as_bytes ( ) . to_vec ( ) ] ) ; builder . set_address ( ( [ 0 ; 8 ] , port ) . into ( ) ) ; builder . set_max_idle_timeout ( None ) . map_err ( | err | Error :: Core ( bonsaidb_core :: Error :: Transport ( err . to_string ( ) ) ) ) ? ; builder . set_server_key_pair ( Some ( keypair ) ) ; let mut server = builder . build ( ) . map_err ( | err | Error :: Core ( bonsaidb_core :: Error :: Transport ( err . to_string ( ) ) ) ) ? ; { let mut endpoint = fast_async_write ! ( self data endpoint ) ; * endpoint = Some ( server . clone ( ) ) ; } let mut shutdown_watcher = self . data . shutdown . watcher ( ) . await . expect ( " " ) ; while let Some ( result ) = tokio :: select ! { shutdown_state shutdown_watcher wait_for_shutdown ( ) { drop ( server close_incoming ( ) ) if matches ( shutdown_state ShutdownState GracefulShutdown ) { server wait_idle ( ) await } drop ( server close ( ) ) None } msg server next ( ) msg } { let connection = result . accept :: < ( ) > ( ) . await ? ; let task_self = self . clone ( ) ; tokio :: spawn ( async move { let address = connection . remote_address ( ) ; if let Err ( err ) = task_self . handle_bonsai_connection ( connection ) . await { log :: error ! ( " " address err ) ; } } ) ; } Ok ( ( ) ) }
Listens for incoming client connections.
Listens for incoming client connections .
&self
port
u16