mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-02 18:55:32 +00:00
Move things to aquatic_common_tcp
This commit is contained in:
parent
720596dfb4
commit
82378e71f6
8 changed files with 61 additions and 45 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -63,6 +63,7 @@ dependencies = [
|
||||||
"mio",
|
"mio",
|
||||||
"native-tls",
|
"native-tls",
|
||||||
"serde",
|
"serde",
|
||||||
|
"socket2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -91,7 +92,6 @@ dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
"serde_urlencoded",
|
"serde_urlencoded",
|
||||||
"simplelog",
|
"simplelog",
|
||||||
"socket2",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
||||||
|
|
@ -13,4 +13,5 @@ anyhow = "1"
|
||||||
aquatic_common = { path = "../aquatic_common" }
|
aquatic_common = { path = "../aquatic_common" }
|
||||||
mio = { version = "0.7", features = ["tcp", "os-poll", "os-util"] }
|
mio = { version = "0.7", features = ["tcp", "os-poll", "os-util"] }
|
||||||
native-tls = "0.2"
|
native-tls = "0.2"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
|
socket2 = { version = "0.3", features = ["reuseport"] }
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -29,6 +31,14 @@ pub struct HandlerConfig {
|
||||||
pub channel_recv_timeout_microseconds: u64,
|
pub channel_recv_timeout_microseconds: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
#[serde(default)]
|
||||||
|
pub struct SocketConfig {
|
||||||
|
/// Bind to this address
|
||||||
|
pub address: SocketAddr,
|
||||||
|
pub ipv6_only: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
|
|
@ -73,6 +83,16 @@ impl Default for HandlerConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Default for SocketConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
address: SocketAddr::from(([0, 0, 0, 0], 3000)),
|
||||||
|
ipv6_only: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
impl Default for TlsConfig {
|
impl Default for TlsConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,9 @@ use std::io::Read;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use native_tls::{Identity, TlsAcceptor};
|
use native_tls::{Identity, TlsAcceptor};
|
||||||
|
use socket2::{Socket, Domain, Type, Protocol};
|
||||||
|
|
||||||
use crate::config::TlsConfig;
|
use crate::config::{TlsConfig, SocketConfig};
|
||||||
|
|
||||||
|
|
||||||
pub fn create_tls_acceptor(
|
pub fn create_tls_acceptor(
|
||||||
|
|
@ -32,4 +33,33 @@ pub fn create_tls_acceptor(
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// will be almost identical to ws version
|
||||||
|
pub fn create_listener(
|
||||||
|
config: &SocketConfig
|
||||||
|
) -> ::anyhow::Result<::std::net::TcpListener> {
|
||||||
|
let builder = if config.address.is_ipv4(){
|
||||||
|
Socket::new(Domain::ipv4(), Type::stream(), Some(Protocol::tcp()))
|
||||||
|
} else {
|
||||||
|
Socket::new(Domain::ipv6(), Type::stream(), Some(Protocol::tcp()))
|
||||||
|
}.context("Couldn't create socket2::Socket")?;
|
||||||
|
|
||||||
|
if config.ipv6_only {
|
||||||
|
builder.set_only_v6(true)
|
||||||
|
.context("Couldn't put socket in ipv6 only mode")?
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.set_nonblocking(true)
|
||||||
|
.context("Couldn't put socket in non-blocking mode")?;
|
||||||
|
builder.set_reuse_port(true)
|
||||||
|
.context("Couldn't put socket in reuse_port mode")?;
|
||||||
|
builder.bind(&config.address.into()).with_context(||
|
||||||
|
format!("Couldn't bind socket to address {}", config.address)
|
||||||
|
)?;
|
||||||
|
builder.listen(128)
|
||||||
|
.context("Couldn't listen for connections on socket")?;
|
||||||
|
|
||||||
|
Ok(builder.into_tcp_listener())
|
||||||
}
|
}
|
||||||
|
|
@ -33,7 +33,6 @@ privdrop = "0.3"
|
||||||
rand = { version = "0.7", features = ["small_rng"] }
|
rand = { version = "0.7", features = ["small_rng"] }
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_urlencoded = "0.6"
|
serde_urlencoded = "0.6"
|
||||||
socket2 = { version = "0.3", features = ["reuseport"] }
|
|
||||||
simplelog = "0.8"
|
simplelog = "0.8"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
use std::net::SocketAddr;
|
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
pub use aquatic_common_tcp::config::*;
|
pub use aquatic_common_tcp::config::*;
|
||||||
|
|
@ -21,12 +19,12 @@ pub struct Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub struct NetworkConfig {
|
pub struct NetworkConfig {
|
||||||
/// Bind to this address
|
#[serde(flatten)]
|
||||||
pub address: SocketAddr,
|
pub socket: SocketConfig,
|
||||||
pub ipv6_only: bool,
|
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub tls: TlsConfig,
|
pub tls: TlsConfig,
|
||||||
pub poll_event_capacity: usize,
|
pub poll_event_capacity: usize,
|
||||||
|
|
@ -34,7 +32,6 @@ pub struct NetworkConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub struct ProtocolConfig {
|
pub struct ProtocolConfig {
|
||||||
|
|
@ -66,8 +63,7 @@ impl Default for Config {
|
||||||
impl Default for NetworkConfig {
|
impl Default for NetworkConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
address: SocketAddr::from(([0, 0, 0, 0], 3000)),
|
socket: SocketConfig::default(),
|
||||||
ipv6_only: false,
|
|
||||||
tls: TlsConfig::default(),
|
tls: TlsConfig::default(),
|
||||||
poll_event_capacity: 4096,
|
poll_event_capacity: 4096,
|
||||||
poll_timeout_milliseconds: 50,
|
poll_timeout_milliseconds: 50,
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@ use native_tls::TlsAcceptor;
|
||||||
use mio::{Events, Poll, Interest, Token};
|
use mio::{Events, Poll, Interest, Token};
|
||||||
use mio::net::TcpListener;
|
use mio::net::TcpListener;
|
||||||
|
|
||||||
|
use aquatic_common_tcp::network::create_listener;
|
||||||
|
|
||||||
use crate::common::*;
|
use crate::common::*;
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::protocol::*;
|
use crate::protocol::*;
|
||||||
|
|
@ -67,7 +69,7 @@ pub fn run_socket_worker(
|
||||||
response_channel_receiver: ResponseChannelReceiver,
|
response_channel_receiver: ResponseChannelReceiver,
|
||||||
opt_tls_acceptor: Option<TlsAcceptor>,
|
opt_tls_acceptor: Option<TlsAcceptor>,
|
||||||
){
|
){
|
||||||
match create_listener(&config){
|
match create_listener(&config.network.socket){
|
||||||
Ok(listener) => {
|
Ok(listener) => {
|
||||||
socket_worker_statuses.lock()[socket_worker_index] = Some(Ok(()));
|
socket_worker_statuses.lock()[socket_worker_index] = Some(Ok(()));
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,42 +2,10 @@ use std::time::Instant;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use mio::Token;
|
use mio::Token;
|
||||||
use socket2::{Socket, Domain, Type, Protocol};
|
|
||||||
|
|
||||||
use crate::config::Config;
|
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
|
||||||
// will be almost identical to ws version
|
|
||||||
pub fn create_listener(
|
|
||||||
config: &Config
|
|
||||||
) -> ::anyhow::Result<::std::net::TcpListener> {
|
|
||||||
let builder = if config.network.address.is_ipv4(){
|
|
||||||
Socket::new(Domain::ipv4(), Type::stream(), Some(Protocol::tcp()))
|
|
||||||
} else {
|
|
||||||
Socket::new(Domain::ipv6(), Type::stream(), Some(Protocol::tcp()))
|
|
||||||
}.context("Couldn't create socket2::Socket")?;
|
|
||||||
|
|
||||||
if config.network.ipv6_only {
|
|
||||||
builder.set_only_v6(true)
|
|
||||||
.context("Couldn't put socket in ipv6 only mode")?
|
|
||||||
}
|
|
||||||
|
|
||||||
builder.set_nonblocking(true)
|
|
||||||
.context("Couldn't put socket in non-blocking mode")?;
|
|
||||||
builder.set_reuse_port(true)
|
|
||||||
.context("Couldn't put socket in reuse_port mode")?;
|
|
||||||
builder.bind(&config.network.address.into()).with_context(||
|
|
||||||
format!("Couldn't bind socket to address {}", config.network.address)
|
|
||||||
)?;
|
|
||||||
builder.listen(128)
|
|
||||||
.context("Couldn't listen for connections on socket")?;
|
|
||||||
|
|
||||||
Ok(builder.into_tcp_listener())
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Don't bother with deregistering from Poll. In my understanding, this is
|
/// Don't bother with deregistering from Poll. In my understanding, this is
|
||||||
/// done automatically when the stream is dropped, as long as there are no
|
/// done automatically when the stream is dropped, as long as there are no
|
||||||
/// other references to the file descriptor, such as when it is accessed
|
/// other references to the file descriptor, such as when it is accessed
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue