|
3 | 3 |
|
4 | 4 | use tox_crypto::*; |
5 | 5 | use tox_packet::relay::*; |
6 | | -use tox_packet::relay::connection_id::ConnectionId; |
7 | 6 | use crate::relay::links::Links; |
8 | | -use tox_packet::onion::InnerOnionResponse; |
9 | 7 | use crate::time::*; |
10 | 8 | use crate::utils::*; |
11 | 9 |
|
12 | | -use std::io::{Error, ErrorKind}; |
13 | 10 | use std::net::IpAddr; |
14 | 11 | use std::time::{Instant, Duration}; |
15 | 12 |
|
16 | 13 | use futures::channel::mpsc; |
17 | | -use futures::SinkExt; |
18 | 14 | use rand::thread_rng; |
19 | 15 |
|
20 | 16 | /// Interval of time for sending TCP PingRequest |
21 | 17 | pub const TCP_PING_FREQUENCY: Duration = Duration::from_secs(30); |
22 | 18 | /// Interval of time for waiting response of PingRequest sent |
23 | 19 | pub const TCP_PING_TIMEOUT: Duration = Duration::from_secs(10); |
24 | | -/// Interval of time for packet sending |
25 | | -pub const TCP_SEND_TIMEOUT: Duration = Duration::from_secs(1); |
26 | 20 |
|
27 | 21 | /** Structure that represents how Server keeps connected clients. A write-only socket with |
28 | 22 | human interface. A client cannot send a message directly to another client, whereas server can. |
@@ -125,93 +119,16 @@ impl Client { |
125 | 119 | &mut self.links |
126 | 120 | } |
127 | 121 |
|
128 | | - /** Send a packet. This method does not ignore IO error |
129 | | - */ |
130 | | - async fn send(&self, packet: Packet) -> Result<(), Error> { |
131 | | - let mut tx = self.tx.clone(); |
132 | | - |
133 | | - let timeout = tokio::time::timeout( |
134 | | - TCP_SEND_TIMEOUT, |
135 | | - tx.send(packet) |
136 | | - ); |
137 | | - |
138 | | - match timeout.await { |
139 | | - Err(e) => Err(Error::new( |
140 | | - ErrorKind::Other, |
141 | | - format!("Failed to send packet: {:?}", e) |
142 | | - )), |
143 | | - Ok(Err(e)) => Err(Error::new( |
144 | | - ErrorKind::Other, |
145 | | - format!("Failed to send packet: {:?}", e) |
146 | | - )), |
147 | | - Ok(_) => Ok(()) |
148 | | - } |
149 | | - } |
150 | | - /** Send a packet. This method ignores IO error |
151 | | - */ |
152 | | - async fn send_ignore_error(&self, packet: Packet) { |
153 | | - // ignore if somehow failed to send it |
154 | | - self.send(packet).await.ok(); |
155 | | - } |
156 | | - /** Construct RouteResponse and send it to Client |
157 | | - */ |
158 | | - pub async fn send_route_response(&self, pk: PublicKey, connection_id: ConnectionId) -> Result<(), Error> { |
159 | | - self.send( |
160 | | - Packet::RouteResponse(RouteResponse { connection_id, pk }) |
161 | | - ).await |
162 | | - } |
163 | | - /** Construct ConnectNotification and send it to Client ignoring IO error |
164 | | - */ |
165 | | - pub async fn send_connect_notification(&self, connection_id: ConnectionId) { |
166 | | - self.send_ignore_error( |
167 | | - Packet::ConnectNotification(ConnectNotification { connection_id }) |
168 | | - ).await; |
169 | | - } |
170 | | - /** Construct DisconnectNotification and send it to Client ignoring IO error |
171 | | - */ |
172 | | - pub async fn send_disconnect_notification(&self, connection_id: ConnectionId) { |
173 | | - self.send_ignore_error( |
174 | | - Packet::DisconnectNotification(DisconnectNotification { connection_id }) |
175 | | - ).await; |
| 122 | + pub fn tx(&self) -> mpsc::Sender<Packet> { |
| 123 | + self.tx.clone() |
176 | 124 | } |
177 | | - /** Construct PongResponse and send it to Client |
178 | | - */ |
179 | | - pub async fn send_pong_response(&self, ping_id: u64) -> Result<(), Error> { |
180 | | - self.send( |
181 | | - Packet::PongResponse(PongResponse { ping_id }) |
182 | | - ).await |
183 | | - } |
184 | | - /** Construct OobReceive and send it to Client ignoring IO error |
185 | | - */ |
186 | | - pub async fn send_oob(&self, sender_pk: PublicKey, data: Vec<u8>) { |
187 | | - self.send_ignore_error( |
188 | | - Packet::OobReceive(OobReceive { sender_pk, data }) |
189 | | - ).await; |
190 | | - } |
191 | | - /** Construct OnionResponse and send it to Client |
192 | | - */ |
193 | | - pub async fn send_onion_response(&self, payload: InnerOnionResponse) -> Result<(), Error> { |
194 | | - self.send( |
195 | | - Packet::OnionResponse(OnionResponse { payload }) |
196 | | - ).await |
197 | | - } |
198 | | - /** Construct Data and send it to Client |
199 | | - */ |
200 | | - pub async fn send_data(&self, connection_id: ConnectionId, data: DataPayload) -> Result<(), Error> { |
201 | | - self.send( |
202 | | - Packet::Data(Data { connection_id, data }) |
203 | | - ).await |
204 | | - } |
205 | | - /** Construct PingRequest and send it to Client |
206 | | - */ |
207 | | - pub async fn send_ping_request(&mut self) -> Result<(), Error> { |
| 125 | + |
| 126 | + pub fn new_ping_id(&mut self) -> u64 { |
208 | 127 | let ping_id = gen_ping_id(&mut thread_rng()); |
209 | 128 |
|
210 | 129 | self.last_pinged = Instant::now(); |
211 | 130 | self.ping_id = ping_id; |
212 | 131 |
|
213 | | - self.send( |
214 | | - Packet::PingRequest(PingRequest { ping_id }) |
215 | | - ).await |
| 132 | + ping_id |
216 | 133 | } |
217 | 134 | } |
0 commit comments