Skip to content

Instantly share code, notes, and snippets.

@turnage
Last active March 10, 2020 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turnage/2a495c539d023a66b2c8c45291d06df4 to your computer and use it in GitHub Desktop.
Save turnage/2a495c539d023a66b2c8c45291d06df4 to your computer and use it in GitHub Desktop.
Network Interface
/// An identifier for a stream which is unique on a single connection.
///
/// A stream is a logical sequence of datagrams.
///
/// A stream may be strictly ordered, gauranteeing that all packets
/// are received at the endpoint in order (e.g. 1, 2, 3, 4, 5, ...).
///
/// Alternatively a stream may be only sequenced, a weaker property
/// that gaurantees all packets received by the endpoint are new,
/// but some may be missed (e.g. 1, 2, 5, ...).
///
/// Different stream types have independent id spaces. Datagrams
/// can be sent strictly ordered with `StreamId(9)` and other
/// datagrams can be sent sequenced with `StreamId(9)` without
/// conflict.
pub struct StreamId(u8);
/// A position of a datagram in a connection.
pub struct GramPosition {
/// An identifier for the stream in which the datagram arrived.
pub stream_id: StreamId,
pub index: StreamIndex,
}
/// A position in a datagram stream.
enum StreamIndex {
/// The ordinal number of a datagram which arrived in a strictly
/// ordered stream. Ordinal indices are gauranteed to count up
/// by steps of `1`.
Ordinal(u32),
/// The sequence number of a datagram which arrived in a
/// sequenced stream. Sequential indices are gauranteed to be
/// larger than preceding indices.
Sequence(u32)
}
/// The mode of delivery for a datagram.
pub enum DeliveryMode {
/// Reliable ordered delivery ensures that all datagrams are
/// received by the endpoint exactly once, in order.
ReliableOrdered(StreamId),
/// Reliable sequenced delivery ensures that datagrams are
/// received by the endpoint at most once, in order. Some
/// datagrams might not be received by the endpoint at all
/// if a newer datagram arrives first.
ReliableSequenced(StreamId),
/// Reliable unordered delivery ensures that datagrams are
/// received by the endpoint exactly once, in arbitrary order.
ReliableUnordered,
///
UnreliableSequenced(StreamId),
UnreliableUnordered
}
pub struct TxGram<'a> {
pub data: Box<dyn std::io::Read>,
pub delivery_mode: DeliveryMode,
}
pub struct RxGram<'a> {
pub data: Box<dyn std::io::Read>,
pub stream: StreamId,
}
pub struct Peer {
outbound: Box<dyn futures::sink::Sink>,
inbound: Box<dyn futures::stream::Stream>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment