#[repr(C)]pub struct UdpSocket<S: SocketState = Unbound, B: SocketBuffer = Vec<u8>> { /* private fields */ }Expand description
A UDP socket
§Notes
- Remote host (
Self::remote) is set when the socket is bound callingbind() - In addition to supporting the creation (with
new) and manual management of the socket, this struct implementsEasySockettrait, which allows for an easier management of the socket, providing theopenmethod as an alternative tonew. This method return aUdpSocketalready connected, and ready to send/receive data (using thewriteandreadmethods). - The socket is closed when the struct is dropped. Closing via drop is best-effort.
Implementations§
Source§impl UdpSocket
impl UdpSocket
Sourcepub fn new() -> Result<UdpSocket<Unbound>, SocketError>
pub fn new() -> Result<UdpSocket<Unbound>, SocketError>
Create a socket
§Notes
- Creating a new socket is not sufficient to start sending/receiving data.
You must call
Self::open()(if you’re using it inEasySocketmode), orSelf::bind()and/orSelf::connect().
§Errors
SocketError::Errnoif the socket could not be created
Source§impl<S: SocketState> UdpSocket<S>
impl<S: SocketState> UdpSocket<S>
Sourcepub fn remote(&self) -> Option<SocketAddr>
pub fn remote(&self) -> Option<SocketAddr>
Get the remote address of the socket
Sourcepub fn send_flags(&self) -> SocketSendFlags
pub fn send_flags(&self) -> SocketSendFlags
Flags used when sending data
Sourcepub fn set_send_flags(&mut self, send_flags: SocketSendFlags)
pub fn set_send_flags(&mut self, send_flags: SocketSendFlags)
Set the flags used when sending data
Sourcepub fn recv_flags(&self) -> SocketRecvFlags
pub fn recv_flags(&self) -> SocketRecvFlags
Flags used when receiving data
Sourcepub fn set_recv_flags(&mut self, recv_flags: SocketRecvFlags)
pub fn set_recv_flags(&mut self, recv_flags: SocketRecvFlags)
Set the flags used when receiving data
Source§impl UdpSocket<Unbound>
impl UdpSocket<Unbound>
Sourcepub fn bind(
self,
addr: Option<SocketAddr>,
) -> Result<UdpSocket<Bound>, SocketError>
pub fn bind( self, addr: Option<SocketAddr>, ) -> Result<UdpSocket<Bound>, SocketError>
Bind the socket
§Parameters
addr: The address to bind to, ifNonebinds to0.0.0.0:0
§Returns
Ok(UdpSocket<Bound>)if the binding was successfulErr(SocketError)if the binding was unsuccessful.
§Errors
SocketError::Errnoif the binding was unsuccessful
Source§impl UdpSocket<Bound>
impl UdpSocket<Bound>
Sourcepub fn connect(
self,
addr: SocketAddr,
) -> Result<UdpSocket<Connected>, SocketError>
pub fn connect( self, addr: SocketAddr, ) -> Result<UdpSocket<Connected>, SocketError>
Connect to a remote host
§Notes
The socket must be in state [UdpSocketState::Bound] to connect to a remote host.
To bind the socket use bind().
§Returns
Ok(UdpSocket<Connected>)if the connection was successfulErr(SocketError)if the connection was unsuccessful
§Errors
- Any
SocketErrorif the connection was unsuccessful
Sourcepub fn _read_from(
self,
buf: &mut [u8],
) -> Result<(usize, UdpSocket<Connected>), SocketError>
pub fn _read_from( self, buf: &mut [u8], ) -> Result<(usize, UdpSocket<Connected>), SocketError>
Read from a bound socket
§Parameters
buf: The buffer where to store the received data
§Returns
Ok((usize, UdpSocket<Connected>))if the write was successful. The number of bytes readErr(SocketError)if the read was unsuccessful.
§Errors
- Any
SocketErrorif the read was unsuccessful
Sourcepub fn _write_to(
self,
buf: &[u8],
len: usize,
to: SocketAddr,
) -> Result<(usize, UdpSocket<Connected>), SocketError>
pub fn _write_to( self, buf: &[u8], len: usize, to: SocketAddr, ) -> Result<(usize, UdpSocket<Connected>), SocketError>
Write to a bound socket
§Parameters
buf: The buffer containing the data to send
§Returns
Ok((usize, UdpSocket<Connected>))if the send was successful. The number of bytes sentErr(SocketError)if the send was unsuccessful.
§Errors
- Any
SocketErrorif the send was unsuccessful
Source§impl UdpSocket<Connected>
impl UdpSocket<Connected>
Sourcepub fn internal_read(&mut self, buf: &mut [u8]) -> Result<usize, SocketError>
pub fn internal_read(&mut self, buf: &mut [u8]) -> Result<usize, SocketError>
Read from a socket
§Parameters
buf: The buffer where to store the received data
§Returns
Ok(usize)if the read was successful. The number of bytes readErr(SocketError)if the read was unsuccessful.
§Errors
- Any
SocketErrorif the read was unsuccessful
Sourcepub fn internal_write(&mut self, buf: &[u8]) -> Result<usize, SocketError>
pub fn internal_write(&mut self, buf: &[u8]) -> Result<usize, SocketError>
Write to a socket
§Returns
Ok(usize)if the send was successful. The number of bytes sentErr(SocketError)if the send was unsuccessful.
§Errors
- Any
SocketErrorif the send was unsuccessful
Sourcepub fn internal_flush(&mut self) -> Result<(), SocketError>
pub fn internal_flush(&mut self) -> Result<(), SocketError>
Trait Implementations§
Source§impl<S: Clone + SocketState, B: Clone + SocketBuffer> Clone for UdpSocket<S, B>
impl<S: Clone + SocketState, B: Clone + SocketBuffer> Clone for UdpSocket<S, B>
Source§impl<S: SocketState> ErrorType for UdpSocket<S>
impl<S: SocketState> ErrorType for UdpSocket<S>
Source§type Error = SocketError
type Error = SocketError
Error type of all the IO operations on this type.
Source§impl Open<'_, '_> for UdpSocket<Unbound>
impl Open<'_, '_> for UdpSocket<Unbound>
Source§impl<S: SocketState> OptionType for UdpSocket<S>
impl<S: SocketState> OptionType for UdpSocket<S>
type Options<'a> = SocketOptions
Source§impl Read for UdpSocket<Connected>
impl Read for UdpSocket<Connected>
Source§impl Write for UdpSocket<Connected>
impl Write for UdpSocket<Connected>
impl EasySocket for UdpSocket<Connected>
Auto Trait Implementations§
impl<S, B> Freeze for UdpSocket<S, B>where
B: Freeze,
impl<S, B> RefUnwindSafe for UdpSocket<S, B>where
B: RefUnwindSafe,
S: RefUnwindSafe,
impl<S = Unbound, B = Vec<u8>> !Send for UdpSocket<S, B>
impl<S = Unbound, B = Vec<u8>> !Sync for UdpSocket<S, B>
impl<S, B> Unpin for UdpSocket<S, B>
impl<S, B> UnwindSafe for UdpSocket<S, B>where
B: UnwindSafe,
S: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more