Struct cobalt::Connection
[−]
[src]
pub struct Connection<R: RateLimiter, M: PacketModifier> { /* fields omitted */ }
Implementation of a reliable, virtual socket connection.
Methods
impl<R: RateLimiter, M: PacketModifier> Connection<R, M>
[src]
fn new(config: Config,
local_addr: SocketAddr,
peer_addr: SocketAddr,
rate_limiter: R,
packet_modifier: M)
-> Connection<R, M>
local_addr: SocketAddr,
peer_addr: SocketAddr,
rate_limiter: R,
packet_modifier: M)
-> Connection<R, M>
Creates a new Virtual Connection over the given SocketAddr
.
Examples
use std::net::SocketAddr; use cobalt::{ BinaryRateLimiter, Connection, ConnectionState, Config, NoopPacketModifier, PacketModifier, RateLimiter }; let config = Config::default(); let local_address: SocketAddr = "127.0.0.1:0".parse().unwrap(); let peer_address: SocketAddr = "255.0.0.1:0".parse().unwrap(); let limiter = BinaryRateLimiter::new(config); let modifier = NoopPacketModifier::new(config); let conn = Connection::new(config, local_address, peer_address, limiter, modifier); assert!(conn.state() == ConnectionState::Connecting); assert_eq!(conn.open(), true);
fn id_from_packet(config: &Config, packet: &[u8]) -> Option<ConnectionID>
Extracts a ConnectionID
from packet with a valid protocol header.
Examples
use cobalt::{ BinaryRateLimiter, Connection, ConnectionID, Config, NoopPacketModifier }; let config = Config { protocol_header: [11, 22, 33, 44], ..Config::default() }; let packet = [ 11, 22, 33, 44, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0 ]; let conn_id = Connection::<BinaryRateLimiter, NoopPacketModifier>::id_from_packet(&config, &packet); assert!(conn_id == Some(ConnectionID(16909060)));
fn open(&self) -> bool
Returns whether the connection is currently accepting any incoming packets.
fn congested(&self) -> bool
Returns whether the connection is currently congested and should be sending less packets per second in order to resolve the congestion.
fn id(&self) -> ConnectionID
Returns the id of the connection.
fn set_id(&mut self, id: ConnectionID)
Overrides the id of the connection.
fn state(&self) -> ConnectionState
Returns the current state of the connection.
fn rtt(&self) -> u32
Returns the average roundtrip time for the connection.
fn packet_loss(&self) -> f32
Returns the percent of packets that were sent and never acknowledged over the total number of packets that have been send across the connection.
fn local_addr(&self) -> SocketAddr
Returns the socket address for the local end of this connection.
fn peer_addr(&self) -> SocketAddr
Returns the socket address for the remote end of this connection.
fn set_peer_addr(&mut self, peer_addr: SocketAddr)
Sets the socket address of the remote peer of this connection.
fn set_config(&mut self, config: Config)
Overrides the connection's existing configuration.
fn send(&mut self, kind: MessageKind, payload: Vec<u8>)
Sends a message of the specified kind
along with its payload
over
the connection.
How exactly the message is send and whether it is guaranteed to be
delivered eventually is determined by its MessageKind
.
fn events(&mut self) -> Drain<ConnectionEvent>
Returns a drain iterator over all queued events from this connection.
fn receive_packet(&mut self, packet: Vec<u8>) -> bool
Receives a incoming UDP packet.
fn send_packet<S: Socket>(&mut self, socket: &mut S, addr: &SocketAddr) -> u32
Send a new outgoing UDP packet.
fn reset(&mut self)
Resets the connection for re-use with another address.
fn close(&mut self)
Closes the connection, no further packets will be received or send.