Struct MultiMessage

Source
pub struct MultiMessage<P, R> {
    pub protocol_id: P,
    pub payload: R,
}
Expand description

A multiplexed message, consisting of the ID of the protocol and dynamic content that only the corresponding session will know how to handle, and check if it is indeed the next expected message.

A message like this is coming from or going to a specific network connection. It is assumed that there is no need to identify the remote party, since the protocol would only be talking to one party at a time that already got determined at connection time.

We can probably assume that there will be only one instance of each protocol per remote party, so P will be a simple static identifier for the protocol. If that’s not the case, it could be more complex, like a unique session ID.

It is up to some higher level component to handle the wire format of the message, including turning the payload and the protocol ID into binary or JSON for example. This is what the Repr allows us to do, e.g. to have each message implement serde, wrap then in an enum, that gets serialised at the edge of the system to/from JSON.

A network connection will be either incoming or outgoing, i.e. for Alice to follow Bob and for Bob to follow Alice they will need 2 TCP connections, one from Alice to Bob and another from Bob to Alice. If it wasn’t so we would need a way to differentiate between two instances of the same protocol, a unique session ID for each message.

Another way of doing it would be to add two flags to the message:

  1. indicate the direction of the connection, incoming or outgoing
  2. indicate whether the protocol was initiated by the source or the target of the connection.

This would facilitate all communication over a single TCP connection, however we would need to start with protocol negotiation to find out if the other side even supports serving certain protocols. It would also make it more difficult to tell if a connection can be closed, because we wouldn’t know if the other side has interest in keeping it open even if currently there are no sessions.

Fields§

§protocol_id: P

The protocol ID features explicitly in the message, rather than as a type class, because for example with DynMessage we don’t have a unique mapping between a message type and its protocol. For example multiple protocols can expect a String.

§payload: R

The payload is the Repr representation of the message, to be (un)wrapped by the session.

Implementations§

Source§

impl<P, R> MultiMessage<P, R>

Source

pub fn new<T: Send + 'static>(protocol_id: P, msg: T) -> Self
where R: Repr<T>,

Wrap a raw message that has a Repr into a MultiMessage.

Trait Implementations§

Source§

impl<P: Debug, R: Debug> Debug for MultiMessage<P, R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<P, R> Freeze for MultiMessage<P, R>
where P: Freeze, R: Freeze,

§

impl<P, R> RefUnwindSafe for MultiMessage<P, R>

§

impl<P, R> Send for MultiMessage<P, R>
where P: Send, R: Send,

§

impl<P, R> Sync for MultiMessage<P, R>
where P: Sync, R: Sync,

§

impl<P, R> Unpin for MultiMessage<P, R>
where P: Unpin, R: Unpin,

§

impl<P, R> UnwindSafe for MultiMessage<P, R>
where P: UnwindSafe, R: UnwindSafe,

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