pub struct Networking<D: NetDriver> { /* private fields */ }Expand description
Networking stack for managing OSI host layers 5 (session) and 6 (presentation).
This structure maintains user-side networking sessions and connections. It also manages the presentation layer, by accepting layer 7 (application) Messages to present downwards to layer 4 (transport) and below.
See module-level documentation for more details.
Implementations§
Source§impl<D: NetDriver> Networking<D>
impl<D: NetDriver> Networking<D>
Sourcepub fn new(driver_factory: impl NetDriverFactory<D>) -> Self
pub fn new(driver_factory: impl NetDriverFactory<D>) -> Self
Create a new Networking stack using a particular NetDriver implementation.
Sourcepub fn event_bus(&self) -> &EventBusRegistry
pub fn event_bus(&self) -> &EventBusRegistry
Get the EventBusRegistry to register event handlers.
Available events:
Sourcepub fn insert_msg_handler<M, E>(&self, handler: impl MessageHandler<M, E>)
pub fn insert_msg_handler<M, E>(&self, handler: impl MessageHandler<M, E>)
Insert a message handler into this Networking stack.
The handler is associated with the message type’s key, and inserting multiple handlers with the same key will override the old one.
Sourcepub fn connection_info(&self, connection: &Connection) -> Option<ConnectionInfo>
pub fn connection_info(&self, connection: &Connection) -> Option<ConnectionInfo>
Retrieve detailed ConnectionInfo for a particular Connection.
If there is no Connection or it has been closed, yields None.
Sourcepub async fn close(&self)
pub async fn close(&self)
Close the Networking stack.
Closes the stack if it is open (e.g. has been connected or listened). It is likely that any future operations will yield NetworkingError::Closed until the stack is reset.
Note that the Networking stack will automatically close itself when dropped, so it is not necessary to explicitly call this before dropping the stack.
Sourcepub fn close_blocking(&self)
pub fn close_blocking(&self)
Convenience wrapper for a sync version of Self::close().
Source§impl<D: NetDriverConnect> Networking<D>
impl<D: NetDriverConnect> Networking<D>
Sourcepub async fn connect(
&self,
socket_addr: SocketAddr,
) -> Result<ClientConnectContext, NetworkingError>
pub async fn connect( &self, socket_addr: SocketAddr, ) -> Result<ClientConnectContext, NetworkingError>
Attempt to connect to the remote socket_addr.
How many endpoints can be connected to is up to the particular driver. For example, a client/server architecture may only allow a client to connect to one server at a time, whereas a peer-to-peer architecture may allow clients to connect to many other clients at once. See the driver’s documentation for more details.
§Errors
NetworkingError::InvalidAddress if the socket_addr could not be
connected to.
NetworkingError::Cancelled if the connection was cancelled by event handling.
Sourcepub fn connect_sync(
&self,
socket_addr: SocketAddr,
) -> Result<ClientConnectContext, NetworkingError>
pub fn connect_sync( &self, socket_addr: SocketAddr, ) -> Result<ClientConnectContext, NetworkingError>
Convenience wrapper for a sync version of Self::connect().
Source§impl<D: NetDriverCloseConnection> Networking<D>
impl<D: NetDriverCloseConnection> Networking<D>
Sourcepub async fn close_connection(&self, connection: Connection)
pub async fn close_connection(&self, connection: Connection)
Close the specified Connection.
This method is idempotent, and specifying a non-existing Connection ID will do nothing.
Source§impl<D: NetDriverListen> Networking<D>
impl<D: NetDriverListen> Networking<D>
Sourcepub async fn listen(
&self,
socket_addr: SocketAddr,
) -> Result<(), NetworkingError>
pub async fn listen( &self, socket_addr: SocketAddr, ) -> Result<(), NetworkingError>
Bind to and listen on a socket.
§Errors
NetworkingError::InvalidAddress if the socket_addr could not be bound.
Source§impl<D: NetDriverSend> Networking<D>
impl<D: NetDriverSend> Networking<D>
Sourcepub fn send<M>(&self, msg: impl Into<Message<M>>) -> Result<(), NetworkingError>where
M: MessageSend,
pub fn send<M>(&self, msg: impl Into<Message<M>>) -> Result<(), NetworkingError>where
M: MessageSend,
Send a Message.
Messages are dispatched immediately. This method does not block or otherwise wait.
§Errors
NetworkingError::MalformedMessage if the Message couldn’t be encoded into bytes.
NetworkingError::Closed if the connection is closed.
Sourcepub fn send_recv<M>(
&self,
msg: impl Into<Message<M>>,
) -> Result<MessageReplyFuture<M::Reply>, NetworkingError>
pub fn send_recv<M>( &self, msg: impl Into<Message<M>>, ) -> Result<MessageReplyFuture<M::Reply>, NetworkingError>
Send a repliable Message and get a reply future.
Messages are dispatched immediately. This method does not block or otherwise wait, however the returned reply future can be waited on.
§Errors
NetworkingError::MalformedMessage if the Message couldn’t be encoded into bytes.
NetworkingError::Closed if the connection is closed.
Source§impl<D: NetDriverSendTo> Networking<D>
impl<D: NetDriverSendTo> Networking<D>
Sourcepub fn send_to<M>(
&self,
connection: Connection,
msg: impl Into<Message<M>>,
) -> Result<(), NetworkingError>where
M: MessageSend,
pub fn send_to<M>(
&self,
connection: Connection,
msg: impl Into<Message<M>>,
) -> Result<(), NetworkingError>where
M: MessageSend,
Send a Message to a specific Connection.
Messages are dispatched immediately. This method does not block or otherwise wait.
§Errors
NetworkingError::InvalidConnection if the Connection does not exist.
NetworkingError::MalformedMessage if the Message couldn’t be encoded into bytes.
NetworkingError::Closed if the driver is closed (e.g. no longer listening on a socket in the case of a server).
Sourcepub fn send_recv_to<M>(
&self,
connection: Connection,
msg: impl Into<Message<M>>,
) -> Result<MessageReplyFuture<M::Reply>, NetworkingError>
pub fn send_recv_to<M>( &self, connection: Connection, msg: impl Into<Message<M>>, ) -> Result<MessageReplyFuture<M::Reply>, NetworkingError>
Send a repliable Message to a specific Connection and get a reply future.
Messages are dispatched immediately. This method does not block or otherwise wait, however the returned reply future can be waited on.
§Errors
NetworkingError::InvalidConnection if the Connection does not exist.
NetworkingError::MalformedMessage if the Message couldn’t be encoded into bytes.
NetworkingError::Closed if the driver is closed (e.g. no longer listening on a socket in the case of a server).
Source§impl<D: NetDriverBroadcast> Networking<D>
impl<D: NetDriverBroadcast> Networking<D>
Sourcepub fn broadcast<M>(
&self,
msg: impl Into<Message<M>>,
) -> Result<(), NetworkingError>where
M: MessageSend,
pub fn broadcast<M>(
&self,
msg: impl Into<Message<M>>,
) -> Result<(), NetworkingError>where
M: MessageSend,
Broadcast a Message to all Connections.
Messages are dispatched immediately. This method does not block or otherwise wait.
§Errors
NetworkingError::MalformedMessage if the Message couldn’t be encoded into bytes.
NetworkingError::Closed if the driver is closed (e.g. no longer listening on a socket in the case of a server).
Trait Implementations§
Source§impl<D: NetDriver> Drop for Networking<D>
impl<D: NetDriver> Drop for Networking<D>
Auto Trait Implementations§
impl<D> !RefUnwindSafe for Networking<D>
impl<D> !UnwindSafe for Networking<D>
impl<D> Freeze for Networking<D>
impl<D> Send for Networking<D>
impl<D> Sync for Networking<D>
impl<D> Unpin for Networking<D>
impl<D> UnsafeUnpin for Networking<D>
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.