p2p/network/identify/stream/
p2p_network_identify_stream_state.rs

1use crate::{
2    network::identify::{P2pNetworkIdentify, P2pNetworkIdentifyFromMessageError},
3    P2pNetworkStreamProtobufError,
4};
5use malloc_size_of_derive::MallocSizeOf;
6use serde::{Deserialize, Serialize};
7
8#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
9pub enum P2pNetworkIdentifyStreamKind {
10    Incoming,
11    Outgoing,
12}
13
14impl From<bool> for P2pNetworkIdentifyStreamKind {
15    fn from(incoming: bool) -> Self {
16        if incoming {
17            P2pNetworkIdentifyStreamKind::Incoming
18        } else {
19            P2pNetworkIdentifyStreamKind::Outgoing
20        }
21    }
22}
23
24#[derive(Clone, Debug, Default, Serialize, Deserialize, MallocSizeOf)]
25pub enum P2pNetworkIdentifyStreamState {
26    #[default]
27    Default,
28    /// Prepare to receive the Identify message from the remote peer
29    RecvIdentify,
30    /// Prepare to send the Identify messge to the remote peer.
31    SendIdentify,
32    /// A portion of data from the stream is received.
33    IncomingPartialData {
34        len: usize,
35        data: Vec<u8>,
36    },
37    // Identify message fully received from remote peer
38    IdentifyReceived {
39        data: Box<P2pNetworkIdentify>,
40    },
41    /// Error handling the stream.
42    Error(
43        #[ignore_malloc_size_of = "error message"]
44        P2pNetworkStreamProtobufError<P2pNetworkIdentifyFromMessageError>,
45    ),
46}
47
48impl P2pNetworkIdentifyStreamState {
49    pub fn new() -> Self {
50        P2pNetworkIdentifyStreamState::Default
51    }
52}
53
54impl From<P2pNetworkIdentify> for P2pNetworkIdentifyStreamState {
55    fn from(data: P2pNetworkIdentify) -> Self {
56        P2pNetworkIdentifyStreamState::IdentifyReceived {
57            data: Box::new(data),
58        }
59    }
60}
61
62#[derive(Debug, Clone, PartialEq, thiserror::Error, Serialize, Deserialize)]
63#[error("identify stream: {0}")]
64pub struct P2pNetworkIdentifyStreamError(
65    #[from] P2pNetworkStreamProtobufError<P2pNetworkIdentifyFromMessageError>,
66);