Skip to content

Commit

Permalink
Replace bind_{tcp,unix} with a single function
Browse files Browse the repository at this point in the history
Same reasons as the connect_{tcp,unix} commit. Although there is
currently only one call site.
  • Loading branch information
EliteTK committed Jun 4, 2024
1 parent adde765 commit 854cfbc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
8 changes: 2 additions & 6 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ use tokio::task;
use tracing::{error, info, info_span, warn, Instrument};

use crate::client;
use crate::config::{Address, Config};
use crate::config::Config;
use crate::instance::InstanceMap;
use crate::socketwrapper::Listener;

pub async fn run(config: &Config) -> Result<()> {
let instance_map = InstanceMap::new(config).await;
let next_client_id = AtomicUsize::new(0);

let listener = match config.listen {
Address::Tcp(ip_addr, port) => Listener::bind_tcp((ip_addr, port).into()).await,
#[cfg(target_family = "unix")]
Address::Unix(ref path) => Listener::bind_unix(path),
}
let listener = Listener::bind(&config.listen).await
.context("listen")?;
loop {
match listener.accept().await {
Expand Down
25 changes: 12 additions & 13 deletions src/socketwrapper.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#[cfg(target_family = "unix")]
use std::fs;
#[cfg(target_family = "unix")]
use std::path::Path;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::{io, net};
Expand Down Expand Up @@ -235,18 +233,19 @@ pub enum Listener {
}

impl Listener {
pub async fn bind_tcp(addr: net::SocketAddr) -> io::Result<Listener> {
Ok(Listener::Tcp(TcpListener::bind(addr).await?))
}

#[cfg(target_family = "unix")]
pub fn bind_unix<T: AsRef<Path>>(addr: T) -> io::Result<Listener> {
match fs::remove_file(&addr) {
Ok(()) => (),
Err(e) if e.kind() == io::ErrorKind::NotFound => (),
Err(e) => return Err(e),
pub async fn bind(addr: &Address) -> io::Result<Listener> {
match addr {
Address::Tcp(ip_addr, port) => Ok(Listener::Tcp(TcpListener::bind((*ip_addr, *port)).await?)),
#[cfg(target_family = "unix")]
Address::Unix(path) => {
match fs::remove_file(&path) {
Ok(()) => (),
Err(e) if e.kind() == io::ErrorKind::NotFound => (),
Err(e) => return Err(e),
}
Ok(Listener::Unix(UnixListener::bind(path)?))
}
}
Ok(Listener::Unix(UnixListener::bind(addr)?))
}

pub async fn accept(&self) -> io::Result<(Stream, SocketAddr)> {
Expand Down

0 comments on commit 854cfbc

Please sign in to comment.