Crate veloren_network_protocol
source ·Expand description
Network Protocol
a I/O-Free protocol for the veloren network crate.
This crate defines multiple different protocols over UnreliableDrain
and
UnreliableSink
traits, which allows it to define the behavior of a
protocol separated from the actual io.
For example we define the TCP protocol on top of Drains and Sinks that can send chunks of bytes. You can now implement your own Drain And Sink that sends the data via tokio’s or std’s implementation. Or you just use a std::mpsc::channel for unit tests without needing a actual tcp socket.
This crate currently defines:
- TCP
- MPSC
- QUIC
eventually a pure UDP implementation will follow
warning: don’t mix protocol, using the TCP variant for actual UDP socket will result in dropped data using UDP with a TCP socket will be a waste of resources.
A channel in this crate is defined as a combination of read and write protocol.
§adding a protocol
We start by defining our DataFormat. For most this is prob Vec<u8>
or
Bytes
. MPSC can directly send a msg without serialisation.
Create 2 structs, one for the receiving and sending end. Based on a generic
Drain/Sink with your required DataFormat.
Implement the SendProtocol
and RecvProtocol
traits respectively.
Implement the Handshake: InitProtocol
, alternatively you can also
implement ReliableDrain
and ReliableSink
, by this, you use the default
Handshake.
This crate also contains consts and definitions for the network protocol.
For an example see TcpDrain
and TcpSink
in the tcp.rs
Modules§
- use at own risk, might change any time, for internal benchmarks
- error 🔒
- event 🔒
- frame 🔒
- message 🔒
- metrics 🔒
- mpsc 🔒
- prio 🔒
- quic 🔒
- tcp 🔒
- types 🔒
- util 🔒
Structs§
- MPSC implementation of
RecvProtocol
- MPSC implementation of
SendProtocol
- Support struct used for uniquely identifying
Participant
over theNetwork
. - use promises to modify the behavior of
Streams
. see the consts in thisstruct
for - Cache for
ProtocolMetrics
, more optimized and cleared up after channel disconnect. - QUIC implementation of
RecvProtocol
- QUIC implementation of
SendProtocol
- Unique ID per Stream, in one Channel. one side will always start with 0, while the other start with u64::MAX / 2. number increases for each created Stream.
- TCP implementation of
RecvProtocol
- TCP implementation of
SendProtocol
Enums§
- All possible Errors that can happen during Handshake
InitProtocol
- used for implementing your own MPSC
Sink
andDrain
- When you return closed you must stay closed!
- used for communication with
SendProtocol
andRecvProtocol
Constants§
- Maximal possible Prio to choose (for performance reasons)
- When this semver differs, 2 Networks can’t communicate.
Traits§
- Handshake: Used to connect 2 Channels.
- Generic Network Recv Protocol. See:
SendProtocol
- Generic Network Send Protocol. Implement this for your Protocol of choice ( tcp, udp, mpsc, quic) Allows the creation/deletions of
Streams
and sending messages viaProtocolEvent
. - This crate makes use of UnreliableDrains, they are expected to provide the same guarantees like their IO-counterpart. E.g. ordered messages for TCP and nothing for UDP. The respective Protocol needs then to handle this. This trait is an abstraction above multiple Drains, e.g.
tokio
async-std
std
or evenasync-channel
- Sink counterpart of
UnreliableDrain
Type Aliases§
- guaranteed
Bandwidth
. SeePrio
- ChannelID, unique ID per Channel (Protocol)
- Every Stream has a
Prio
and guaranteedBandwidth
. Every send, the guarantees part is used first. If there is still bandwidth left, it will be shared by all Streams with the same priority. Prio 0 will be send first, then 1, … till the last prio 7 is send. Prio must be < 8!