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
|
|
@ -1,3 +1,5 @@
|
|||
use std::net::SocketAddr;
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
|
||||
|
|
@ -29,6 +31,14 @@ pub struct HandlerConfig {
|
|||
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)]
|
||||
#[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 {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
|
|
|
|||
|
|
@ -5,8 +5,9 @@ use std::io::Read;
|
|||
|
||||
use anyhow::Context;
|
||||
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(
|
||||
|
|
@ -32,4 +33,33 @@ pub fn create_tls_acceptor(
|
|||
} else {
|
||||
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())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue