forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudp.rs
More file actions
41 lines (34 loc) · 1.1 KB
/
udp.rs
File metadata and controls
41 lines (34 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::net::UdpSocket;
/// A generic UDP client
pub struct Client {
pub socket: Arc<UdpSocket>,
}
impl Client {
#[allow(dead_code)]
pub async fn connected(remote_socket_addr: &SocketAddr, local_socket_addr: &SocketAddr) -> Client {
let client = Client::bind(local_socket_addr).await;
client.connect(remote_socket_addr).await;
client
}
pub async fn bind(local_socket_addr: &SocketAddr) -> Self {
let socket = UdpSocket::bind(local_socket_addr).await.unwrap();
Self {
socket: Arc::new(socket),
}
}
pub async fn connect(&self, remote_address: &SocketAddr) {
self.socket.connect(remote_address).await.unwrap();
}
#[allow(dead_code)]
pub async fn send(&self, bytes: &[u8]) -> usize {
self.socket.writable().await.unwrap();
self.socket.send(bytes).await.unwrap()
}
#[allow(dead_code)]
pub async fn receive(&self, bytes: &mut [u8]) -> usize {
self.socket.readable().await.unwrap();
self.socket.recv(bytes).await.unwrap()
}
}