pub struct Connection {
pub address: SocketAddr,
pub state: Arc<Mutex<ConnectionState>>,
/* private fields */
}
Expand description
The connection struct contains the logic for a connection to the server. The following methods are the most important:
Connection::recv()
: This is used to recieve packets from the client.Connection::send()
: This is used to send packets to the client.Connection::close()
: This is used to disconnect the client.
This struct does not provide an API for connecting to other peers, for that you should use the Client struct.
Fields§
§address: SocketAddr
The address of the connection This is internally tokenized by rak-rs
state: Arc<Mutex<ConnectionState>>
Implementations§
Source§impl Connection
impl Connection
Sourcepub async fn new(
address: SocketAddr,
socket: &Arc<UdpSocket>,
net: Receiver<Vec<u8>>,
notifier: Arc<Sender<SocketAddr>>,
mtu: u16,
) -> Self
pub async fn new( address: SocketAddr, socket: &Arc<UdpSocket>, net: Receiver<Vec<u8>>, notifier: Arc<Sender<SocketAddr>>, mtu: u16, ) -> Self
Initializes a new Connection instance.
Sourcepub async fn recv(&mut self) -> Result<Vec<u8>, RecvError>
pub async fn recv(&mut self) -> Result<Vec<u8>, RecvError>
This method is used to recieve packets from the client connection. Packets that are recieved here are packets sent by the peer expected to be handled by the server.
This is where you would handle your packets from the client.
§Example
This is a snippet of code you would use after you’ve accepted a connection from the server with
[Listener::accept()
].
use rakrs::connection::Connection;
async fn handle(mut conn: Connection) {
loop {
// get a packet sent from the client
if let Ok(pk) = conn.recv().await {
println!("Got a connection packet {:?} ", pk);
}
}
}
pub async fn is_closed(&self) -> bool
Sourcepub async fn send(
&self,
buffer: &[u8],
immediate: bool,
) -> Result<(), SendQueueError>
pub async fn send( &self, buffer: &[u8], immediate: bool, ) -> Result<(), SendQueueError>
This method is used to send payloads to the connection. This method internally will encode your payload into a RakNet packet, and send it to the client.
§Example
This is a snippet of code you would use when you need to send a payload to the client.
use rakrs::connection::Connection;
async fn send_payload(mut conn: Connection) {
conn.send(&[0x01, 0x02, 0x03], true).await.unwrap();
}