p2p/network/kad/stream/
p2p_network_kad_stream_state.rs

1use malloc_size_of_derive::MallocSizeOf;
2use serde::{Deserialize, Serialize};
3
4use crate::{
5    P2pNetworkKademliaRpcFromMessageError, P2pNetworkKademliaRpcReply,
6    P2pNetworkKademliaRpcRequest, P2pNetworkStreamProtobufError,
7};
8
9#[derive(Clone, Debug, Serialize, Deserialize, MallocSizeOf)]
10pub enum P2pNetworkKadStreamState {
11    Incoming(P2pNetworkKadIncomingStreamState),
12    Outgoing(P2pNetworkKadOutgoingStreamState),
13}
14
15impl P2pNetworkKadStreamState {
16    pub fn new(incoming: bool) -> Self {
17        if incoming {
18            P2pNetworkKadStreamState::Incoming(Default::default())
19        } else {
20            P2pNetworkKadStreamState::Outgoing(Default::default())
21        }
22    }
23}
24
25/// Incoming Kademlia stream is used by a remote peer to perform a Kademlia request.
26#[derive(Clone, Debug, Default, Serialize, Deserialize, MallocSizeOf)]
27pub enum P2pNetworkKadIncomingStreamState {
28    #[default]
29    Default,
30    /// Waiting for the incoming request.
31    WaitingForRequest { expect_close: bool },
32    /// A portion of data from the stream is received.
33    PartialRequestReceived { len: usize, data: Vec<u8> },
34    /// Request from the stream is received.
35    RequestIsReady { data: P2pNetworkKademliaRpcRequest },
36    /// Waiting for an outgoing data, or for finalization of the stream (iff `expect_fin` is `true`)
37    WaitingForReply,
38    /// Response bytes for the remote request is ready to be written into the stream.
39    ResponseBytesAreReady { bytes: Vec<u8> },
40    /// Remote peer half-closed the stream.
41    Closing,
42    /// The stream is closed.
43    Closed,
44    /// Error handling the stream.
45    /// TODO: use enum for errors.
46    Error(P2pNetworkStreamProtobufError<P2pNetworkKademliaRpcFromMessageError>),
47}
48
49#[derive(Clone, Debug, Default, Serialize, Deserialize, MallocSizeOf)]
50pub enum P2pNetworkKadOutgoingStreamState {
51    #[default]
52    Default,
53    /// Waiting for an outgoing data, or for finalization of the stream (iff `expect_close` is `true`)
54    WaitingForRequest { expect_close: bool },
55    /// Response bytes for the remote request are ready to be written into the stream.
56    RequestBytesAreReady { bytes: Vec<u8> },
57    /// Waiting for the incoming reply.
58    WaitingForReply,
59    /// A portion of data from the stream is received.
60    PartialReplyReceived { len: usize, data: Vec<u8> },
61    /// Response from the stream is received.
62    ResponseIsReady { data: P2pNetworkKademliaRpcReply },
63    /// Closing the stream.
64    Closing,
65    /// The stream is closed.
66    Closed,
67    /// Error handling the stream.
68    Error(P2pNetworkStreamProtobufError<P2pNetworkKademliaRpcFromMessageError>),
69}
70
71#[derive(Debug, Clone, PartialEq, thiserror::Error, Serialize, Deserialize)]
72#[error("kademlia incoming stream: {0}")]
73pub struct P2pNetworkKadIncomingStreamError(
74    #[from] P2pNetworkStreamProtobufError<P2pNetworkKademliaRpcFromMessageError>,
75);
76
77#[derive(Debug, Clone, PartialEq, thiserror::Error, Serialize, Deserialize)]
78#[error("kademlia outgoing stream: {0}")]
79pub struct P2pNetworkKadOutgoingStreamError(
80    #[from] P2pNetworkStreamProtobufError<P2pNetworkKademliaRpcFromMessageError>,
81);