some basic code
This commit is contained in:
+16
-2
@@ -1,3 +1,17 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use std::net::UdpSocket;
|
||||
fn main() -> std::io::Result<()> {
|
||||
{
|
||||
let socket: UdpSocket = (|| -> std::io::Result<UdpSocket> {
|
||||
let mut port: u16 = 59999;
|
||||
loop {
|
||||
port += 1;
|
||||
match UdpSocket::bind(format!("0.0.0.0:{}", port)) {
|
||||
Ok(socket) => return Ok(socket),
|
||||
Err(_) => continue, // Retry on error
|
||||
}
|
||||
}
|
||||
})()
|
||||
.expect("Failed to bind to any available port");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
pub const SERVER_PORT: u16 = 3543;
|
||||
pub const BUFFER_SIZE: usize = 65535;
|
||||
pub const DEFAULT_TIMEOUT: u64 = 30;
|
||||
pub const VERSION: &str = "v0.1";
|
||||
|
||||
pub enum ServerMetods {
|
||||
REGISTER,
|
||||
GET,
|
||||
}
|
||||
|
||||
+34
-2
@@ -1,3 +1,35 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
mod net;
|
||||
use std::{net::UdpSocket, process::exit, sync::Arc};
|
||||
fn main() -> std::io::Result<()> {
|
||||
{
|
||||
let socket: Arc<UdpSocket> = Arc::new(
|
||||
(|| -> std::io::Result<UdpSocket> {
|
||||
let listen_port: u16 = 60000;
|
||||
match UdpSocket::bind(format!("0.0.0.0:{}", listen_port)) {
|
||||
Ok(socket) => return Ok(socket),
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
})()
|
||||
.expect("Failed to bind to any available port"),
|
||||
);
|
||||
|
||||
let mut buf: [u8; pea_2_pea::BUFFER_SIZE] = [0; pea_2_pea::BUFFER_SIZE];
|
||||
smol::block_on(async {
|
||||
loop {
|
||||
match socket.recv_from(&mut buf) {
|
||||
Ok((data_length, src)) => {
|
||||
smol::spawn(net::handle_request(buf, socket.clone(), src, data_length))
|
||||
.detach();
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error receiving data: {}", e);
|
||||
exit(-4);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// socket.send_to(&buf, &src)?;
|
||||
} // the socket is closed here
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
pub async fn handle_request(
|
||||
mut buf: [u8; pea_2_pea::BUFFER_SIZE],
|
||||
socket: std::sync::Arc<std::net::UdpSocket>,
|
||||
src: core::net::SocketAddr,
|
||||
data_len: usize,
|
||||
) {
|
||||
#[cfg(target_endian = "little")]
|
||||
buf.reverse();
|
||||
}
|
||||
Reference in New Issue
Block a user