Skip to content

Commit 8763b39

Browse files
committed
feat(net_crypto): send packets via TCP
1 parent b9e7e7e commit 8763b39

File tree

3 files changed

+160
-65
lines changed

3 files changed

+160
-65
lines changed

src/toxcore/net_crypto/crypto_connection.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub const REQUEST_PACKETS_COMPARE_CONSTANT: f64 = 0.125 * 100.0;
6565
/// Packet that should be sent every second. Depending on `ConnectionStatus` it
6666
/// can be `CookieRequest` or `CryptoHandshake`
6767
#[derive(Clone, Debug, Eq, PartialEq)]
68-
enum StatusPacketEnum {
68+
pub enum StatusPacketEnum {
6969
/// `CookieRequest` packet
7070
CookieRequest(CookieRequest),
7171
/// `CryptoHandshake` packet
@@ -78,7 +78,7 @@ enum StatusPacketEnum {
7878
pub struct StatusPacket {
7979
/// Packet that should be sent every second. Depending on `ConnectionStatus`
8080
/// it can be `CookieRequest` or `CryptoHandshake`
81-
packet: StatusPacketEnum,
81+
pub packet: StatusPacketEnum,
8282
/// When packet was sent last time
8383
pub sent_time: Instant,
8484
/// How many times packet was sent
@@ -104,14 +104,6 @@ impl StatusPacket {
104104
}
105105
}
106106

107-
/// Get `Packet` that should be sent every second
108-
pub fn dht_packet(&self) -> Packet {
109-
match self.packet {
110-
StatusPacketEnum::CookieRequest(ref packet) => Packet::CookieRequest(packet.clone()),
111-
StatusPacketEnum::CryptoHandshake(ref packet) => Packet::CryptoHandshake(packet.clone()),
112-
}
113-
}
114-
115107
/// Check if one second is elapsed since last time when the packet was sent
116108
fn is_time_elapsed(&self) -> bool {
117109
clock_elapsed(self.sent_time) > CRYPTO_SEND_PACKET_INTERVAL
@@ -431,15 +423,15 @@ impl CryptoConnection {
431423

432424
/// Get `CookieRequest` or `CryptoHandshake` if it should be sent depending
433425
/// on connection status and update sent counter
434-
pub fn packet_to_send(&mut self) -> Option<Packet> {
426+
pub fn packet_to_send(&mut self) -> Option<StatusPacketEnum> {
435427
match self.status {
436428
ConnectionStatus::CookieRequesting { ref mut packet, .. }
437429
| ConnectionStatus::HandshakeSending { ref mut packet, .. }
438430
| ConnectionStatus::NotConfirmed { ref mut packet, .. } => {
439431
if packet.should_be_sent() {
440432
packet.num_sent += 1;
441433
packet.sent_time = clock_now();
442-
Some(packet.dht_packet())
434+
Some(packet.packet.clone())
443435
} else {
444436
None
445437
}

src/toxcore/net_crypto/errors.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,19 @@ error_kind! {
232232
SendToConnectionStatus,
233233
}
234234
}
235+
236+
error_kind! {
237+
#[doc = "Error that can happen during a packet sending."]
238+
#[derive(Debug)]
239+
SendPacketError,
240+
#[doc = "The specific kind of error that can occur."]
241+
#[derive(Debug, Eq, PartialEq, Fail)]
242+
SendPacketErrorKind {
243+
#[doc = "Failed to send TCP packet."]
244+
#[fail(display = "Failed to send TCP packet")]
245+
Tcp,
246+
#[doc = "Failed to send UDP packet."]
247+
#[fail(display = "Failed to send UDP packet")]
248+
Udp,
249+
}
250+
}

0 commit comments

Comments
 (0)