// Bind a new socket with a local address
Bind a new socket with a local address
Bind a new socket with a local address
fn bind(
    endpoint_cfg: quinn::ServerConfig,
    local_addr: SocketAddr,
    allow_random_port: bool,
) -> Result<(quinn::Endpoint, quinn::Incoming)> {
    let mut endpoint_builder = quinn::Endpoint::builder();
    let _ = endpoint_builder.listen(endpoint_cfg);
    match UdpSocket::bind(&local_addr) {
        Ok(udp) => endpoint_builder.with_socket(udp).map_err(Error::Endpoint),
        Err(err) if allow_random_port => {
            info!(
                "Failed to bind to local address: {} - Error: {}. Trying random port instead.",
                local_addr, err
            );
            let bind_addr = SocketAddr::new(local_addr.ip(), 0);
            endpoint_builder.bind(&bind_addr).map_err(|e| {
                error!("Failed to bind to random port too: {}", e);
                Error::Endpoint(e)
            })
        }
        Err(err) => {
            error!("{}", err);
            Err(Error::CannotAssignPort(local_addr.port()))
        }
    }
}
fn bind ( endpoint_cfg : quinn :: ServerConfig , local_addr : SocketAddr , allow_random_port : bool , ) -> Result < ( quinn :: Endpoint , quinn :: Incoming ) > { let mut endpoint_builder = quinn :: Endpoint :: builder ( ) ; let _ = endpoint_builder . listen ( endpoint_cfg ) ; match UdpSocket :: bind ( & local_addr ) { Ok ( udp ) => endpoint_builder . with_socket ( udp ) . map_err ( Error :: Endpoint ) , Err ( err ) if allow_random_port => { info ! ( " " local_addr err ) ; let bind_addr = SocketAddr :: new ( local_addr . ip ( ) , 0 ) ; endpoint_builder . bind ( & bind_addr ) . map_err ( | e | { error ! ( " " e ) ; Error :: Endpoint ( e ) } ) } Err ( err ) => { error ! ( " " err ) ; Err ( Error :: CannotAssignPort ( local_addr . port ( ) ) ) } } }
Bind a new socket with a local address
Bind a new socket with a local address
endpoint_cfg
quinn::ServerConfig
local_addr
SocketAddr
allow_random_port
bool