Skip to main content

Networking

Struct Networking 

Source
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>

Source

pub fn new(driver_factory: impl NetDriverFactory<D>) -> Self

Create a new Networking stack using a particular NetDriver implementation.

Source

pub fn event_bus(&self) -> &EventBusRegistry

Get the EventBusRegistry to register event handlers.

Available events:

Source

pub fn insert_msg_handler<M, E>(&self, handler: impl MessageHandler<M, E>)
where M: MessageRecv, E: Error + Send + Sync + 'static,

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.

Source

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.

Source

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.

Source

pub fn close_blocking(&self)

Convenience wrapper for a sync version of Self::close().

Source§

impl<D: NetDriverConnect> Networking<D>

Source

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.

Source

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>

Source

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>

Source

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>

Source

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.

Source

pub fn send_recv<M>( &self, msg: impl Into<Message<M>>, ) -> Result<MessageReplyFuture<M::Reply>, NetworkingError>
where M: MessageSend + MessageRepliable, M::Reply: MessageRecv + Send + 'static,

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>

Source

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).

Source

pub fn send_recv_to<M>( &self, connection: Connection, msg: impl Into<Message<M>>, ) -> Result<MessageReplyFuture<M::Reply>, NetworkingError>
where M: MessageSend + MessageRepliable, M::Reply: MessageRecv + Send + 'static,

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>

Source

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>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more