2021-11-26 21:46:27 +00:00
|
|
|
use std::{
|
|
|
|
env,
|
|
|
|
net::{
|
|
|
|
Ipv4Addr,
|
2021-12-05 03:39:53 +00:00
|
|
|
SocketAddrV4,
|
2021-11-26 21:46:27 +00:00
|
|
|
UdpSocket,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-12-05 03:39:53 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
#[derive (Debug, Error)]
|
|
|
|
enum AppError {
|
|
|
|
#[error (transparent)]
|
|
|
|
CliArgs (#[from] CliArgError),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Debug, Error)]
|
|
|
|
enum CliArgError {
|
|
|
|
#[error ("First argument should be a subcommand")]
|
|
|
|
MissingSubcommand,
|
|
|
|
#[error ("Unknown subcommand `{0}`")]
|
|
|
|
UnknownSubcommand (String),
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CommonParams {
|
|
|
|
// Servers bind on this port, clients must send to the port
|
|
|
|
server_port: u16,
|
|
|
|
|
|
|
|
// Clients and servers will all join the same multicast addr
|
|
|
|
multicast_addr: Ipv4Addr,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CommonParams {
|
|
|
|
fn default () -> Self {
|
|
|
|
Self {
|
|
|
|
server_port: 9040,
|
|
|
|
multicast_addr: Ipv4Addr::new (225, 100, 99, 98),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 21:46:27 +00:00
|
|
|
|
2021-12-05 03:39:53 +00:00
|
|
|
fn main () -> Result <(), AppError> {
|
2021-11-26 21:46:27 +00:00
|
|
|
let mut args = env::args ();
|
|
|
|
|
|
|
|
let _exe_name = args.next ();
|
|
|
|
|
|
|
|
match args.next ().as_ref ().map (|s| &s[..]) {
|
2021-12-05 03:39:53 +00:00
|
|
|
None => return Err (CliArgError::MissingSubcommand.into ()),
|
2021-11-26 21:46:27 +00:00
|
|
|
Some ("client") => client ()?,
|
|
|
|
Some ("server") => server ()?,
|
2021-12-05 03:39:53 +00:00
|
|
|
Some (x) => return Err (CliArgError::UnknownSubcommand (x.to_string ()).into ()),
|
2021-11-26 21:46:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
|
2021-12-05 03:39:53 +00:00
|
|
|
fn client () -> Result <(), AppError> {
|
|
|
|
let params = CommonParams::default ();
|
|
|
|
let socket = UdpSocket::bind ("0.0.0.0:0").unwrap ();
|
2021-11-26 21:46:27 +00:00
|
|
|
|
2021-12-05 03:39:53 +00:00
|
|
|
socket.join_multicast_v4 (¶ms.multicast_addr, &([0u8, 0, 0, 0].into ())).unwrap ();
|
2021-11-26 21:46:27 +00:00
|
|
|
|
2021-12-05 03:39:53 +00:00
|
|
|
socket.send_to ("hi there".as_bytes (), (params.multicast_addr, params.server_port)).unwrap ();
|
2021-11-26 21:46:27 +00:00
|
|
|
|
2021-12-05 03:43:12 +00:00
|
|
|
let mut buf = vec! [0u8; 4096];
|
|
|
|
let (bytes_recved, remote_addr) = socket.recv_from (&mut buf).unwrap ();
|
|
|
|
buf.truncate (bytes_recved);
|
|
|
|
let _buf = buf;
|
|
|
|
dbg! (remote_addr);
|
|
|
|
|
2021-11-26 21:46:27 +00:00
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
|
2021-12-05 03:39:53 +00:00
|
|
|
fn server () -> Result <(), AppError> {
|
|
|
|
let params = CommonParams::default ();
|
|
|
|
let socket = UdpSocket::bind (SocketAddrV4::new (Ipv4Addr::UNSPECIFIED, params.server_port)).unwrap ();
|
2021-11-26 21:46:27 +00:00
|
|
|
|
2021-12-05 03:39:53 +00:00
|
|
|
socket.join_multicast_v4 (¶ms.multicast_addr, &([0u8, 0, 0, 0].into ())).unwrap ();
|
2021-11-26 21:46:27 +00:00
|
|
|
|
|
|
|
let mut buf = vec! [0u8; 4096];
|
|
|
|
let (bytes_recved, remote_addr) = socket.recv_from (&mut buf).unwrap ();
|
2021-12-05 03:43:12 +00:00
|
|
|
buf.truncate (bytes_recved);
|
|
|
|
let _buf = buf;
|
|
|
|
dbg! (remote_addr);
|
2021-11-26 21:46:27 +00:00
|
|
|
|
2021-12-05 03:43:12 +00:00
|
|
|
socket.send_to ("hi there".as_bytes (), remote_addr).unwrap ();
|
2021-11-26 21:46:27 +00:00
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|