Skip to content

Commit 7e3ebf6

Browse files
authored
Merge pull request #445 from tox-rs/tokio-1
feat(tokio): update version to 1.0
2 parents 9ead890 + 65ccaff commit 7e3ebf6

File tree

21 files changed

+123
-124
lines changed

21 files changed

+123
-124
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
os: [ubuntu-latest, macos-latest, windows-latest]
1515
rust:
1616
- stable
17-
- 1.42.0
17+
- 1.44.0
1818
steps:
1919
- uses: actions/checkout@v2
2020
- uses: actions-rs/toolchain@v1

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ issue / pull request should be filled on the reference repository.
3030
[CONTRIBUTING.md](/CONTRIBUTING.md).
3131

3232
## Building
33-
Fairly simple. First, install [Rust] >= 1.42.0 and a C compiler ([Build Tools
33+
Fairly simple. First, install [Rust] >= 1.44.0 and a C compiler ([Build Tools
3434
for Visual Studio][VSBuild] on Windows, GCC or Clang on other platforms).
3535

3636
Then you can build the debug version with

examples/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ tox_core = { version = "0.1.1", path = "../tox_core" }
1212

1313
log = "0.4"
1414
futures = { version = "0.3", default-features = false, features = ["std", "async-await"] }
15-
env_logger = "0.7"
15+
env_logger = "0.8"
1616
hex = "0.4"
1717
failure = "0.1"
1818

1919
[dev-dependencies.tokio]
20-
version = "0.2"
20+
version = "1.0"
2121
default-features = false
22-
features = ["macros", "test-util", "net", "rt-core", "rt-threaded", "sync", "stream", "time"]
22+
features = ["macros", "test-util", "net", "rt", "rt-multi-thread", "sync", "time"]
2323

2424
[dev-dependencies.tokio-util]
25-
version = "0.3"
26-
features = ["codec", "udp"]
25+
version = "0.6"
26+
features = ["codec", "net"]
2727

2828
[[example]]
2929
name = "dht_server"

examples/tcp_client.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use failure::{Error, err_msg};
1414
use hex::FromHex;
1515

1616
use futures::prelude::*;
17-
use futures::future;
1817
use futures::channel::mpsc;
1918

2019
use tokio_util::codec::Framed;
@@ -103,7 +102,8 @@ async fn create_client(mut rx: mpsc::Receiver<Packet>, tx: mpsc::Sender<Packet>)
103102
futures::try_join!(reader, writer).map(drop)
104103
}
105104

106-
fn main() {
105+
#[tokio::main]
106+
async fn main() -> Result<(), Error> {
107107
env_logger::init();
108108

109109
let (mut tx, rx) = mpsc::channel(1);
@@ -138,8 +138,8 @@ fn main() {
138138
Result::<(), Error>::Ok(())
139139
};
140140

141-
let client = future::try_select(client.boxed(), packet_sender.boxed());
142-
143-
let mut runtime = tokio::runtime::Runtime::new().unwrap();
144-
runtime.block_on(client).map_err(|e| e.into_inner().0).unwrap();
141+
futures::select! {
142+
res = client.fuse() => res,
143+
res = packet_sender.fuse() => res,
144+
}
145145
}

examples/tcp_server.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#[macro_use]
22
extern crate log;
33

4+
use failure::Error;
45
use tox_crypto::*;
56
use tox_core::relay::server::{Server, tcp_run};
67
use tox_core::stats::Stats;
@@ -9,7 +10,8 @@ use tokio::net::TcpListener;
910

1011
const TCP_CONNECTIONS_LIMIT: usize = 1024;
1112

12-
fn main() {
13+
#[tokio::main]
14+
async fn main() -> Result<(), Error> {
1315
env_logger::init();
1416
// Server constant PK for examples/tests
1517
// Use `gen_keypair` to generate random keys
@@ -33,11 +35,6 @@ fn main() {
3335
let server = Server::new();
3436

3537
let stats = Stats::new();
36-
let future = async {
37-
let listener = TcpListener::bind(&addr).await.unwrap();
38-
drop(tcp_run(&server, listener, server_sk, stats, TCP_CONNECTIONS_LIMIT).await);
39-
};
40-
41-
let mut runtime = tokio::runtime::Runtime::new().unwrap();
42-
runtime.block_on(future)
38+
let listener = TcpListener::bind(&addr).await.unwrap();
39+
tcp_run(&server, listener, server_sk, stats, TCP_CONNECTIONS_LIMIT).await.map_err(Error::from)
4340
}

tox_core/Cargo.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ tox_binary_io = { version = "0.1.1", path = "../tox_binary_io" }
2121
tox_crypto = { version = "0.1.1", path = "../tox_crypto" }
2222
tox_packet = { version = "0.1.1", path = "../tox_packet" }
2323

24-
bytes = "0.5"
24+
bytes = "1.0"
2525
futures = { version = "0.3", default-features = false, features = ["std", "async-await"] }
2626
log = "0.4"
2727
nom = "5.1"
@@ -30,18 +30,18 @@ get_if_addrs = "0.5"
3030
failure = "0.1"
3131
lru = "0.6"
3232
bitflags = "1.0"
33-
itertools = "0.9"
33+
itertools = "0.10"
3434

3535
[dependencies.tokio]
36-
version = "0.2"
36+
version = "1.0"
3737
default-features = false
38-
features = ["net", "sync", "stream", "time"]
38+
features = ["net", "sync", "time"]
3939

4040
[dependencies.tokio-util]
41-
version = "0.3"
42-
features = ["codec", "udp"]
41+
version = "0.6"
42+
features = ["codec", "net"]
4343

4444
[dev-dependencies.tokio]
45-
version = "0.2"
45+
version = "1.0"
4646
default-features = false
47-
features = ["macros", "test-util", "net", "rt-core", "rt-threaded", "sync", "stream", "time"]
47+
features = ["macros", "test-util", "net", "rt", "rt-multi-thread", "sync", "time"]

tox_core/src/dht/codec.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,16 @@ impl Decoder for DhtCodec {
114114
type Error = DecodeError;
115115

116116
fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
117+
if buf.is_empty() {
118+
return Ok(None);
119+
}
120+
117121
let len = buf.len();
118122
if len > MAX_DHT_PACKET_SIZE {
119123
return Err(DecodeError::too_big_packet(len))
120124
}
121125

122-
match Packet::from_bytes(buf) {
126+
let result = match Packet::from_bytes(buf) {
123127
Err(error) => {
124128
Err(DecodeError::deserialize(error, buf.to_vec()))
125129
},
@@ -129,7 +133,11 @@ impl Decoder for DhtCodec {
129133

130134
Ok(Some(packet))
131135
}
132-
}
136+
};
137+
138+
buf.clear();
139+
140+
result
133141
}
134142
}
135143

@@ -154,7 +162,6 @@ impl Encoder<Packet> for DhtCodec {
154162
#[cfg(test)]
155163
mod tests {
156164
use super::*;
157-
use nom::Needed;
158165
use tox_packet::onion::*;
159166
use tox_crypto::*;
160167

@@ -347,10 +354,8 @@ mod tests {
347354
let mut codec = DhtCodec::new(stats);
348355
let mut buf = BytesMut::new();
349356

350-
// not enought bytes to decode EncryptedPacket
351-
let res = codec.decode(&mut buf);
352-
let error = res.err().unwrap();
353-
assert_eq!(*error.kind(), DecodeErrorKind::Deserialize { error: Err::Incomplete(Needed::Size(1)), packet: Vec::new() });
357+
// we can't distinguish 0-length UDP packets from completely consumed packets
358+
assert!(codec.decode(&mut buf).unwrap().is_none());
354359
}
355360

356361
#[test]

tox_core/src/dht/lan_discovery.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::net::{IpAddr, SocketAddr};
55
use std::time::{Duration};
66

77
use failure::Fail;
8-
use futures::{stream, StreamExt, SinkExt};
8+
use futures::{stream, SinkExt};
99
use futures::channel::mpsc;
1010
use get_if_addrs::IfAddr;
1111

@@ -144,21 +144,22 @@ impl LanDiscoverySender {
144144
let interval = LAN_DISCOVERY_INTERVAL;
145145
let mut wakeups = tokio::time::interval(interval);
146146

147-
while wakeups.next().await.is_some() {
147+
loop {
148+
wakeups.tick().await;
149+
148150
if let Err(e) = tokio::time::timeout(interval, self.send()).await {
149151
warn!("Failed to send LAN discovery packets: {}", e);
150152

151153
return Err(e.context(LanDiscoveryErrorKind::SendTo).into())
152154
}
153155
}
154-
155-
Ok(())
156156
}
157157
}
158158

159159
#[cfg(test)]
160160
mod tests {
161161
use super::*;
162+
use futures::StreamExt;
162163
use tox_binary_io::*;
163164

164165
fn broadcast_addrs_count() -> usize {

tox_core/src/dht/server/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,9 @@ impl Server {
383383
let interval = BOOTSTRAP_INTERVAL;
384384
let mut wakeups = tokio::time::interval(interval);
385385

386-
while wakeups.next().await.is_some() {
386+
loop {
387+
wakeups.tick().await;
388+
387389
trace!("Bootstrap wake up");
388390
let send_res = tokio::time::timeout(
389391
interval,
@@ -405,8 +407,6 @@ impl Server {
405407
return res
406408
}
407409
}
408-
409-
Ok(())
410410
}
411411

412412
/// Check if all nodes in Ktree are discarded (including the case when
@@ -438,7 +438,9 @@ impl Server {
438438
let interval = Duration::from_secs(MAIN_LOOP_INTERVAL);
439439
let mut wakeups = tokio::time::interval(interval);
440440

441-
while wakeups.next().await.is_some() {
441+
loop {
442+
wakeups.tick().await;
443+
442444
trace!("DHT server wake up");
443445

444446
let loop_res =
@@ -457,8 +459,6 @@ impl Server {
457459
return res
458460
}
459461
}
460-
461-
Ok(())
462462
}
463463

464464
/// Refresh onion symmetric key periodically. Result future will never be
@@ -467,12 +467,12 @@ impl Server {
467467
let interval = ONION_REFRESH_KEY_INTERVAL;
468468
let mut wakeups = tokio::time::interval_at(tokio::time::Instant::now() + interval, interval);
469469

470-
while wakeups.next().await.is_some() {
470+
loop {
471+
wakeups.tick().await;
472+
471473
trace!("Refreshing onion key");
472474
self.refresh_onion_key().await;
473475
}
474-
475-
Ok(())
476476
}
477477

478478
/// Run ping sending periodically. Result future will never be completed
@@ -481,12 +481,12 @@ impl Server {
481481
let interval = TIME_TO_PING;
482482
let mut wakeups = tokio::time::interval_at(tokio::time::Instant::now() + interval, interval);
483483

484-
while wakeups.next().await.is_some() {
484+
loop {
485+
wakeups.tick().await;
486+
485487
self.send_pings().await
486488
.map_err(|e| e.context(RunErrorKind::SendTo))?;
487489
}
488-
489-
Ok(())
490490
}
491491

492492
/// Send `PingRequest` packets to nodes from `nodes_to_ping` list.

tox_core/src/dht/server_ext.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ mod tests {
116116

117117
let client_future = async {
118118
// Send invalid request first to ensure that the server won't crash
119-
let mut client_socket = client_socket;
120119
client_socket.send_to(&[42; 123][..], &server_addr)
121120
.await
122121
.map_err(|e| Error::new(ErrorKind::Other, e.compat()))?;

tox_core/src/friend_connection/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,9 @@ impl FriendConnections {
367367
async fn run_main_loop(&self) -> Result<(), RunError> {
368368
let mut wakeups = tokio::time::interval(MAIN_LOOP_INTERVAL);
369369

370-
while wakeups.next().await.is_some() {
370+
loop {
371+
wakeups.tick().await;
372+
371373
let fut = tokio::time::timeout(MAIN_LOOP_INTERVAL, self.main_loop());
372374
let res = match fut.await {
373375
Err(e) => Err(e.context(RunErrorKind::Timeout).into()),
@@ -380,8 +382,6 @@ impl FriendConnections {
380382
return res
381383
}
382384
}
383-
384-
Ok(())
385385
}
386386

387387
/// Run friends connection module. This will add handlers for DHT

tox_core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Repo: https://github.com/tox-rs/tox
99
#![forbid(unsafe_code)]
1010
#![doc(html_logo_url = "https://raw.githubusercontent.com/tox-rs/logo/master/logo.png")]
1111
// Remove it when it will be fixed in nom parser
12-
#![allow(clippy::redundant_closure)]
12+
#![allow(clippy::redundant_closure, clippy::result_unit_err)]
1313

1414
#[macro_use]
1515
extern crate log;

tox_core/src/net_crypto/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::time::{Duration, Instant};
2525
use std::u16;
2626

2727
use failure::Fail;
28-
use futures::{TryFutureExt, StreamExt, SinkExt};
28+
use futures::{TryFutureExt, SinkExt};
2929
use futures::future;
3030
use futures::channel::mpsc;
3131
use tokio::sync::RwLock;
@@ -885,7 +885,7 @@ impl NetCrypto {
885885
connection.packets_received += 1;
886886
self.process_ready_lossless_packets(&mut connection.recv_array, connection.peer_real_pk).await
887887
.map_err(|e| e.context(HandlePacketErrorKind::SendToLossless))?;
888-
} else if packet_id >= PACKET_ID_LOSSY_RANGE_START && packet_id <= PACKET_ID_LOSSY_RANGE_END {
888+
} else if (PACKET_ID_LOSSY_RANGE_START..=PACKET_ID_LOSSY_RANGE_END).contains(&packet_id) {
889889
// Update end index of received buffer ignoring the error - we still
890890
// want to handle this packet even if connection is too slow
891891
connection.recv_array.set_buffer_end(payload.packet_number).ok();
@@ -1091,7 +1091,9 @@ impl NetCrypto {
10911091
pub async fn run(&self) -> Result<(), RunError> {
10921092
let mut wakeups = tokio::time::interval(PACKET_COUNTER_AVERAGE_INTERVAL);
10931093

1094-
while wakeups.next().await.is_some() {
1094+
loop {
1095+
wakeups.tick().await;
1096+
10951097
let fut = tokio::time::timeout(
10961098
PACKET_COUNTER_AVERAGE_INTERVAL, self.main_loop()
10971099
);
@@ -1108,8 +1110,6 @@ impl NetCrypto {
11081110
return res
11091111
}
11101112
}
1111-
1112-
Ok(())
11131113
}
11141114

11151115
/// Set sink to send DHT `PublicKey` when it gets known.
@@ -1133,7 +1133,7 @@ impl NetCrypto {
11331133
mod tests {
11341134
// https://github.com/rust-lang/rust/issues/61520
11351135
use super::{*, Packet};
1136-
use futures::Future;
1136+
use futures::{Future, StreamExt};
11371137

11381138
impl NetCrypto {
11391139
pub async fn has_friend(&self, pk: &PublicKey) -> bool {

0 commit comments

Comments
 (0)