28 lines
828 B
Rust
28 lines
828 B
Rust
use std::{
|
|
io::Result,
|
|
net::{Ipv6Addr, UdpSocket},
|
|
time::Duration,
|
|
};
|
|
|
|
use crossbeam_channel::unbounded;
|
|
|
|
use super::{
|
|
queues::{ReceiveQueue, SendQueue},
|
|
thread::{start_receive_thread, start_send_thread},
|
|
};
|
|
|
|
fn configure_socket(socket: &UdpSocket) -> Result<()> {
|
|
socket.set_read_timeout(None)?;
|
|
socket.set_write_timeout(Some(Duration::from_secs(1)))?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn bind_socket(port: u16) -> Result<(SendQueue, ReceiveQueue)> {
|
|
let socket = UdpSocket::bind((Ipv6Addr::UNSPECIFIED, port))?;
|
|
configure_socket(&socket)?;
|
|
let (send_inbound, receive_inbound) = unbounded();
|
|
start_receive_thread(send_inbound, socket.try_clone()?);
|
|
let (send_outbound, receive_outbound) = unbounded();
|
|
start_send_thread(receive_outbound, socket);
|
|
Ok((send_outbound, receive_inbound))
|
|
}
|