1 |
use std::{str::FromStr, sync::atomic::Ordering};
|
2 |
|
3 |
use anyhow::Error;
|
4 |
use giterated_models::messages::handshake::{
|
5 |
HandshakeFinalize, HandshakeResponse, InitiateHandshake,
|
6 |
};
|
7 |
use semver::Version;
|
8 |
|
9 |
use crate::{
|
10 |
connection::ConnectionError,
|
11 |
message::{HandshakeMessage, Message, MessageHandler, NetworkMessage, State},
|
12 |
validate_version, version,
|
13 |
};
|
14 |
|
15 |
use super::{wrapper::ConnectionState, HandlerUnhandled};
|
16 |
|
17 |
pub async fn handshake_handle(
|
18 |
message: &NetworkMessage,
|
19 |
state: &ConnectionState,
|
20 |
) -> Result<(), Error> {
|
21 |
if initiate_handshake
|
22 |
.handle_message(&message, state)
|
23 |
.await
|
24 |
.is_ok()
|
25 |
{
|
26 |
Ok(())
|
27 |
} else if handshake_response
|
28 |
.handle_message(&message, state)
|
29 |
.await
|
30 |
.is_ok()
|
31 |
{
|
32 |
Ok(())
|
33 |
} else if handshake_finalize
|
34 |
.handle_message(&message, state)
|
35 |
.await
|
36 |
.is_ok()
|
37 |
{
|
38 |
Ok(())
|
39 |
} else {
|
40 |
Err(Error::from(HandlerUnhandled))
|
41 |
}
|
42 |
}
|
43 |
|
44 |
async fn initiate_handshake(
|
45 |
HandshakeMessage(initiation): HandshakeMessage<InitiateHandshake>,
|
46 |
State(connection_state): State<ConnectionState>,
|
47 |
) -> Result<(), HandshakeError> {
|
48 |
info!("meow!");
|
49 |
connection_state
|
50 |
.send(HandshakeResponse {
|
51 |
identity: connection_state.instance.clone(),
|
52 |
version: version(),
|
53 |
})
|
54 |
.await
|
55 |
.map_err(|e| HandshakeError::SendError(e))?;
|
56 |
|
57 |
Ok(())
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
}
|
83 |
|
84 |
async fn handshake_response(
|
85 |
HandshakeMessage(initiation): HandshakeMessage<HandshakeResponse>,
|
86 |
State(connection_state): State<ConnectionState>,
|
87 |
) -> Result<(), HandshakeError> {
|
88 |
connection_state
|
89 |
.send(HandshakeFinalize { success: true })
|
90 |
.await
|
91 |
.map_err(|e| HandshakeError::SendError(e))?;
|
92 |
|
93 |
Ok(())
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
}
|
116 |
|
117 |
async fn handshake_finalize(
|
118 |
HandshakeMessage(finalize): HandshakeMessage<HandshakeFinalize>,
|
119 |
State(connection_state): State<ConnectionState>,
|
120 |
) -> Result<(), HandshakeError> {
|
121 |
connection_state.handshaked.store(true, Ordering::SeqCst);
|
122 |
|
123 |
connection_state
|
124 |
.send(HandshakeFinalize { success: true })
|
125 |
.await
|
126 |
.map_err(|e| HandshakeError::SendError(e))?;
|
127 |
|
128 |
Ok(())
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
}
|
143 |
|
144 |
#[derive(Debug, thiserror::Error)]
|
145 |
pub enum HandshakeError {
|
146 |
#[error("version mismatch during handshake, ours: {0}, theirs: {1}")]
|
147 |
VersionMismatch(Version, Version),
|
148 |
#[error("while sending message: {0}")]
|
149 |
SendError(Error),
|
150 |
#[error("{0}")]
|
151 |
Other(#[from] Error),
|
152 |
}
|
153 |
|