-
-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add TransportSocket trait for injecting the socket into netcode transports #145
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use std::net::{SocketAddr, UdpSocket}; | ||
|
||
use super::{NetcodeError, NetcodeTransportError, TransportSocket}; | ||
|
||
/// Implementation of [`TransportSocket`] for `UdpSockets`. | ||
#[derive(Debug)] | ||
pub struct NativeSocket { | ||
socket: UdpSocket, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe implement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't do it, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see. |
||
} | ||
|
||
impl NativeSocket { | ||
/// Makes a new native socket. | ||
pub fn new(socket: UdpSocket) -> Result<Self, NetcodeError> { | ||
socket.set_nonblocking(true)?; | ||
Ok(Self { socket }) | ||
} | ||
} | ||
|
||
impl TransportSocket for NativeSocket { | ||
fn addr(&self) -> std::io::Result<SocketAddr> { | ||
self.socket.local_addr() | ||
} | ||
|
||
fn is_closed(&mut self) -> bool { | ||
false | ||
} | ||
|
||
fn close(&mut self) {} | ||
fn disconnect(&mut self, _: SocketAddr) {} | ||
fn preupdate(&mut self) {} | ||
|
||
fn try_recv(&mut self, buffer: &mut [u8]) -> std::io::Result<(usize, SocketAddr)> { | ||
self.socket.recv_from(buffer) | ||
} | ||
|
||
fn postupdate(&mut self) {} | ||
|
||
fn send(&mut self, addr: SocketAddr, packet: &[u8]) -> Result<(), NetcodeTransportError> { | ||
self.socket.send_to(packet, addr)?; | ||
Ok(()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered using generic instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah generic would get rid of the dynamic dispatch, but then all user code that gets the netcode transport resources has to parameterize on the socket type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense.