Skip to content

Commit

Permalink
g3-socket: fix winsock usage
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-jq-b committed Oct 23, 2024
1 parent 85e4ed2 commit 83919ec
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
4 changes: 1 addition & 3 deletions lib/g3-socket/src/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,13 @@ impl BindAddr {
#[cfg(any(target_os = "linux", target_os = "android"))]
set_bind_address_no_port(socket, true)?;
#[cfg(windows)]
set_reuse_unicastport(socket, true)?;
let _ = set_reuse_unicastport(socket, true)?;
let addr: SockAddr = SocketAddr::new(*ip, 0).into();
socket.bind(&addr)
}
#[cfg(any(target_os = "linux", target_os = "android"))]
BindAddr::Interface(name) => {
set_bind_address_no_port(socket, true)?;
#[cfg(windows)]
set_reuse_unicastport(socket, true)?;
socket.bind_device(Some(name.as_bytes()))
}
}
Expand Down
3 changes: 1 addition & 2 deletions lib/g3-socket/src/raw/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use std::io::{self, IoSlice};
use std::net::SocketAddr;
use std::os::windows::io::{AsRawSocket, FromRawSocket};
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket};

use socket2::{MsgHdr, SockAddr, Socket};

Expand All @@ -37,7 +37,6 @@ impl RawSocket {
}
}

#[cfg(unix)]
impl Drop for RawSocket {
fn drop(&mut self) {
if let Some(s) = self.inner.take() {
Expand Down
12 changes: 8 additions & 4 deletions lib/g3-socket/src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,15 @@ mod tests {
Default::default(),
)
.unwrap();
let local_addr = socket.local_addr().unwrap();
assert_eq!(local_addr.port(), 0);
let local_addr1 = socket.local_addr().unwrap();
#[cfg(unix)]
assert_eq!(local_addr1.port(), 0);
#[cfg(windows)]
assert_ne!(local_addr1.port(), 0);
socket.connect(peer_addr).unwrap();
let local_addr = socket.local_addr().unwrap();
assert_ne!(local_addr.port(), 0);
let local_addr2 = socket.local_addr().unwrap();
assert_ne!(local_addr2.port(), 0);
assert_ne!(local_addr1, local_addr2);
}

#[test]
Expand Down

0 comments on commit 83919ec

Please sign in to comment.