Skip to content

Commit 4cbf40b

Browse files
committed
windows issues
1 parent 5365d90 commit 4cbf40b

File tree

8 files changed

+59
-31
lines changed

8 files changed

+59
-31
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
host_os:
1414
- ubuntu-latest
1515
- macos-latest
16-
# - windows-latest
16+
- windows-latest
1717

1818
runs-on: ${{ matrix.host_os }}
1919

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ categories = ["network-utilities"]
1111
readme = "README.md"
1212

1313
[dependencies]
14+
cfg-if = "1.0"
1415
chrono = "0.4"
1516
clap = { version = "4.4", features = ["derive", "wrap_help"] }
1617
core_affinity = "0.8"

src/client.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ fn connect_to_server(address: &str, port: &u16) -> BoxResult<TcpStream> {
6767
let socket: socket2::Socket = socket2::Socket::from(stream);
6868
let keepalive = socket2::TcpKeepalive::new()
6969
.with_time(KEEPALIVE_DURATION)
70-
.with_interval(KEEPALIVE_DURATION)
71-
.with_retries(4);
70+
.with_interval(KEEPALIVE_DURATION);
71+
cfg_if::cfg_if! {
72+
if #[cfg(unix)] {
73+
let keepalive = keepalive.with_retries(4);
74+
}
75+
}
7276
socket.set_tcp_keepalive(&keepalive)?;
7377
socket.set_nodelay(true)?;
7478

src/main.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ fn main() -> BoxResult<()> {
4141
std::process::exit(3);
4242
}
4343
true
44-
})
45-
.expect("unable to set SIGINT handler");
44+
})?;
4645

4746
log::debug!("beginning normal operation...");
4847
server::serve(&args)?;
@@ -57,8 +56,7 @@ fn main() -> BoxResult<()> {
5756
std::process::exit(3);
5857
}
5958
true
60-
})
61-
.expect("unable to set SIGINT handler");
59+
})?;
6260

6361
log::debug!("connecting to server...");
6462
client::execute(&args)?;

src/protocol/communication.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use mio::{Events, Interest, Poll};
2727
use crate::BoxResult;
2828

2929
/// how long to wait for keepalive events
30-
// the communications channels typically exchange data every second, so 2s is reasonable to avoid excess noise
30+
/// the communications channels typically exchange data every second, so 2s is reasonable to avoid excess noise
3131
pub const KEEPALIVE_DURATION: Duration = Duration::from_secs(2);
3232

3333
/// how long to block on polling operations
@@ -101,8 +101,8 @@ fn receive_length(stream: &mut TcpStream, alive_check: fn() -> bool, results_han
101101
};
102102
poll.registry().deregister(stream)?;
103103
result
104-
// Err(Box::new(simple_error::simple_error!("system shutting down")))
105104
}
105+
106106
/// receives the data-value of a pending message over a client-server communications stream
107107
fn receive_payload(
108108
stream: &mut TcpStream,
@@ -166,8 +166,8 @@ fn receive_payload(
166166
};
167167
poll.registry().deregister(stream)?;
168168
result
169-
// Err(Box::new(simple_error::simple_error!("system shutting down")))
170169
}
170+
171171
/// handles the full process of retrieving a message from a client-server communications stream
172172
pub fn receive(
173173
stream: &mut TcpStream,

src/server.rs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,20 @@ static ALIVE: AtomicBool = AtomicBool::new(true);
4545
static CLIENTS: AtomicU16 = AtomicU16::new(0);
4646

4747
fn tcp_stream_try_clone(stream: &TcpStream) -> BoxResult<TcpStream> {
48-
use std::os::fd::{AsRawFd, BorrowedFd};
49-
let fd = unsafe { BorrowedFd::borrow_raw(stream.as_raw_fd()) };
50-
let fd = fd.try_clone_to_owned()?;
51-
let socket: socket2::Socket = socket2::Socket::from(fd);
48+
cfg_if::cfg_if! {
49+
if #[cfg(unix)] {
50+
use std::os::fd::{AsRawFd, BorrowedFd};
51+
let fd = unsafe { BorrowedFd::borrow_raw(stream.as_raw_fd()) };
52+
let fd = fd.try_clone_to_owned()?;
53+
let socket: socket2::Socket = socket2::Socket::from(fd);
54+
} else {
55+
use std::os::windows::io::{AsRawSocket, BorrowedSocket};
56+
let socket = unsafe { BorrowedSocket::borrow_raw(stream.as_raw_socket()) };
57+
let socket = socket.try_clone_to_owned()?;
58+
let socket: socket2::Socket = socket2::Socket::from(socket);
59+
}
60+
}
61+
5262
let stream: std::net::TcpStream = socket.into();
5363
let socket = TcpStream::from_std(stream);
5464
Ok(socket)
@@ -359,14 +369,26 @@ pub fn serve(args: &Args) -> BoxResult<()> {
359369
log::info!("connection from {}", address);
360370

361371
let mut stream = {
362-
use std::os::fd::{FromRawFd, IntoRawFd};
363-
let fd = stream.into_raw_fd();
364-
let socket: socket2::Socket = unsafe { socket2::Socket::from_raw_fd(fd) };
365-
366-
let keepalive = socket2::TcpKeepalive::new()
367-
.with_time(KEEPALIVE_DURATION)
368-
.with_interval(KEEPALIVE_DURATION)
369-
.with_retries(4);
372+
cfg_if::cfg_if! {
373+
if #[cfg(unix)] {
374+
use std::os::fd::{FromRawFd, IntoRawFd};
375+
let fd = stream.into_raw_fd();
376+
let socket: socket2::Socket = unsafe { socket2::Socket::from_raw_fd(fd) };
377+
378+
let keepalive = socket2::TcpKeepalive::new()
379+
.with_time(KEEPALIVE_DURATION)
380+
.with_interval(KEEPALIVE_DURATION)
381+
.with_retries(4);
382+
} else {
383+
// Only Windows supports raw sockets
384+
use std::os::windows::io::{FromRawSocket, IntoRawSocket};
385+
let socket = stream.into_raw_socket();
386+
let socket: socket2::Socket = unsafe { socket2::Socket::from_raw_socket(socket) };
387+
let keepalive = socket2::TcpKeepalive::new()
388+
.with_time(KEEPALIVE_DURATION)
389+
.with_interval(KEEPALIVE_DURATION);
390+
}
391+
}
370392
socket.set_tcp_keepalive(&keepalive)?;
371393

372394
socket.set_nodelay(true)?;

src/stream/tcp.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
* along with rperf. If not, see <https://www.gnu.org/licenses/>.
1919
*/
2020

21-
use nix::sys::socket::{setsockopt, sockopt::RcvBuf, sockopt::SndBuf};
22-
2321
use crate::protocol::results::{get_unix_timestamp, IntervalResult, TcpReceiveResult, TcpSendResult};
2422
use crate::BoxResult;
2523

@@ -192,6 +190,7 @@ pub mod receiver {
192190
}
193191
}
194192

193+
#[allow(dead_code)]
195194
pub struct TcpReceiver {
196195
active: AtomicBool,
197196
test_definition: super::TcpTestDefinition,
@@ -300,7 +299,8 @@ pub mod receiver {
300299

301300
use std::os::fd::{FromRawFd, IntoRawFd};
302301
let raw_stream = unsafe { std::net::TcpStream::from_raw_fd(stream.into_raw_fd()) };
303-
super::setsockopt(&raw_stream, super::RcvBuf, &self.receive_buffer)?;
302+
use nix::sys::socket::{setsockopt, sockopt::RcvBuf};
303+
setsockopt(&raw_stream, RcvBuf, &self.receive_buffer)?;
304304
stream = unsafe { TcpStream::from_raw_fd(raw_stream.into_raw_fd()) };
305305
}
306306

@@ -457,6 +457,7 @@ pub mod sender {
457457
const WRITE_TIMEOUT: Duration = Duration::from_millis(50);
458458
const BUFFER_FULL_TIMEOUT: Duration = Duration::from_millis(1);
459459

460+
#[allow(dead_code)]
460461
pub struct TcpSender {
461462
active: bool,
462463
test_definition: super::TcpTestDefinition,
@@ -530,7 +531,8 @@ pub mod sender {
530531
#[cfg(unix)]
531532
if self.send_buffer != 0 {
532533
log::debug!("setting send-buffer to {}...", self.send_buffer);
533-
super::setsockopt(&raw_stream, super::SndBuf, &self.send_buffer)?;
534+
use nix::sys::socket::{setsockopt, sockopt::SndBuf};
535+
setsockopt(&raw_stream, SndBuf, &self.send_buffer)?;
534536
}
535537

536538
raw_stream.set_write_timeout(Some(WRITE_TIMEOUT))?;

src/stream/udp.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
* along with rperf. If not, see <https://www.gnu.org/licenses/>.
1919
*/
2020

21-
use nix::sys::socket::{setsockopt, sockopt::RcvBuf, sockopt::SndBuf};
22-
2321
use crate::protocol::results::{get_unix_timestamp, IntervalResult, UdpReceiveResult, UdpSendResult};
2422
use crate::BoxResult;
2523

@@ -217,6 +215,7 @@ pub mod receiver {
217215
socket: UdpSocket,
218216
}
219217
impl UdpReceiver {
218+
#[allow(unused_variables)]
220219
pub fn new(
221220
test_definition: super::UdpTestDefinition,
222221
stream_idx: &u8,
@@ -231,7 +230,8 @@ pub mod receiver {
231230
#[cfg(unix)]
232231
if *receive_buffer != 0 {
233232
log::debug!("setting receive-buffer to {}...", receive_buffer);
234-
super::setsockopt(&socket, super::RcvBuf, receive_buffer)?;
233+
use nix::sys::socket::{setsockopt, sockopt::RcvBuf};
234+
setsockopt(&socket, RcvBuf, receive_buffer)?;
235235
}
236236
log::debug!("bound UDP receive socket for stream {}: {}", stream_idx, socket.local_addr()?);
237237

@@ -514,7 +514,7 @@ pub mod sender {
514514
staged_packet: Vec<u8>,
515515
}
516516
impl UdpSender {
517-
#[allow(clippy::too_many_arguments)]
517+
#[allow(clippy::too_many_arguments, unused_variables)]
518518
pub fn new(
519519
test_definition: super::UdpTestDefinition,
520520
stream_idx: &u8,
@@ -538,7 +538,8 @@ pub mod sender {
538538
#[cfg(unix)]
539539
if *send_buffer != 0 {
540540
log::debug!("setting send-buffer to {}...", send_buffer);
541-
super::setsockopt(&socket, super::SndBuf, send_buffer)?;
541+
use nix::sys::socket::{setsockopt, sockopt::SndBuf};
542+
setsockopt(&socket, SndBuf, send_buffer)?;
542543
}
543544
socket.connect(socket_addr_receiver)?;
544545
log::debug!("connected UDP stream {} to {}", stream_idx, socket_addr_receiver);

0 commit comments

Comments
 (0)