Merge pull request #44 from ckiee/remove-dead-peers

rkvm-{server,client}: Remove dead peers quickly
This commit is contained in:
htrefil 2023-08-24 08:47:29 +02:00 committed by GitHub
commit aa5def347a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 8 deletions

13
Cargo.lock generated
View file

@ -812,6 +812,7 @@ dependencies = [
"rkvm-input",
"serde",
"sha2",
"socket2 0.5.3",
"thiserror",
"tokio",
]
@ -970,6 +971,16 @@ dependencies = [
"winapi",
]
[[package]]
name = "socket2"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2538b18701741680e0322a2302176d3253a35388e2e62f172f64f4f16605f877"
dependencies = [
"libc",
"windows-sys 0.48.0",
]
[[package]]
name = "spin"
version = "0.5.2"
@ -1054,7 +1065,7 @@ dependencies = [
"num_cpus",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"socket2 0.4.9",
"tokio-macros",
"windows-sys 0.48.0",
]

View file

@ -2,7 +2,7 @@ use rkvm_input::writer::Writer;
use rkvm_net::auth::{AuthChallenge, AuthStatus};
use rkvm_net::message::Message;
use rkvm_net::version::Version;
use rkvm_net::Update;
use rkvm_net::{socket, Update};
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::io;
@ -40,8 +40,7 @@ pub async fn run(
_ => unimplemented!("Unhandled rustls ServerName variant: {:?}", hostname),
};
stream.set_linger(None).map_err(Error::Network)?;
stream.set_nodelay(false).map_err(Error::Network)?;
socket::configure(&stream).map_err(Error::Network)?;
log::info!("Connected to server");

View file

@ -16,4 +16,5 @@ thiserror = "1.0.40"
hmac = "0.12.1"
sha2 = "0.10.6"
rand = "0.8.5"
log = "0.4.11"
log = "0.4.11"
socket2 = { version = "0.5.3", features = ["all"] }

View file

@ -1,5 +1,6 @@
pub mod auth;
pub mod message;
pub mod socket;
pub mod version;
use rkvm_input::abs::{AbsAxis, AbsInfo};

18
rkvm-net/src/socket.rs Normal file
View file

@ -0,0 +1,18 @@
use socket2::{SockRef, TcpKeepalive};
use std::io::Error;
use std::time::Duration;
use tokio::net::TcpStream;
pub fn configure(stream: &TcpStream) -> Result<(), Error> {
stream.set_linger(None)?;
stream.set_nodelay(false)?;
SockRef::from(&stream).set_tcp_keepalive(
&TcpKeepalive::new()
.with_time(Duration::from_secs(1))
.with_interval(Duration::from_secs(10))
.with_retries(1),
)?;
Ok(())
}

View file

@ -7,7 +7,7 @@ use rkvm_input::sync::SyncEvent;
use rkvm_net::auth::{AuthChallenge, AuthResponse, AuthStatus};
use rkvm_net::message::Message;
use rkvm_net::version::Version;
use rkvm_net::Update;
use rkvm_net::{socket, Update};
use slab::Slab;
use std::collections::{HashMap, HashSet, VecDeque};
use std::ffi::CString;
@ -288,8 +288,7 @@ async fn client(
password: &str,
) -> Result<(), ClientError> {
let negotiate = async {
stream.set_linger(None)?;
stream.set_nodelay(false)?;
socket::configure(&stream)?;
let stream = acceptor.accept(stream).await?;
log::info!("{}: TLS connected", addr);