-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Platform specific directory handling
- Loading branch information
Showing
8 changed files
with
118 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod socket; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use anyhow::{Context, Result}; | ||
use async_std::io::{Read, Write}; | ||
use async_std::net::{TcpListener, TcpStream}; | ||
use async_std::os::unix::net::{UnixListener, UnixStream}; | ||
use async_trait::async_trait; | ||
|
||
pub trait GenericSocket: Read + Write + Unpin + Send + Sync {} | ||
pub type SocketBox = Box<dyn GenericSocket>; | ||
|
||
#[async_trait] | ||
pub trait GenericListener: Sync + Send { | ||
async fn accept<'a>(&'a self) -> Result<Box<dyn GenericSocket>>; | ||
} | ||
|
||
#[async_trait] | ||
impl GenericListener for TcpListener { | ||
async fn accept<'a>(&'a self) -> Result<Box<dyn GenericSocket>> { | ||
let (socket, _) = self.accept().await?; | ||
Ok((Box::new(socket))) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl GenericListener for UnixListener { | ||
async fn accept<'a>(&'a self) -> Result<Box<dyn GenericSocket>> { | ||
let (socket, _) = self.accept().await?; | ||
Ok((Box::new(socket))) | ||
} | ||
} | ||
|
||
impl GenericSocket for TcpStream {} | ||
impl GenericSocket for UnixStream {} | ||
|
||
pub async fn get_client( | ||
unix_socket_path: Option<String>, | ||
port: Option<String>, | ||
) -> Result<Box<dyn GenericSocket>> { | ||
if let Some(socket_path) = unix_socket_path { | ||
let stream = UnixStream::connect(socket_path).await?; | ||
return Ok(Box::new(stream)); | ||
} | ||
|
||
// Don't allow anything else than loopback until we have proper crypto | ||
// let address = format!("{}:{}", address, port); | ||
let address = format!("127.0.0.1:{}", port.unwrap()); | ||
|
||
// Connect to socket | ||
let socket = TcpStream::connect(&address) | ||
.await | ||
.context("Failed to connect to the daemon. Did you start it?")?; | ||
|
||
Ok(Box::new(socket)) | ||
} | ||
|
||
pub async fn get_listener( | ||
unix_socket_path: Option<String>, | ||
port: Option<String>, | ||
) -> Result<Box<dyn GenericListener>> { | ||
if let Some(socket_path) = unix_socket_path { | ||
return Ok(Box::new(UnixListener::bind(socket_path).await?)); | ||
} | ||
|
||
let port = port.unwrap(); | ||
let address = format!("127.0.0.1:{}", port); | ||
Ok(Box::new(TcpListener::bind(address).await?)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod directories; | ||
pub mod socket; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use anyhow::Result; | ||
use async_std::io::{Read, Write}; | ||
use async_std::net::{TcpListener, TcpStream}; | ||
use async_trait::async_trait; | ||
|
||
pub trait GenericSocket: Read + Write + Unpin + Send + Sync {} | ||
pub type SocketBox = Box<dyn GenericSocket>; | ||
|
||
#[async_trait] | ||
pub trait GenericListener: Sync + Send { | ||
async fn accept<'a>(&'a self) -> Result<Box<dyn GenericSocket>>; | ||
} | ||
|
||
#[async_trait] | ||
impl GenericListener for TcpListener { | ||
async fn accept<'a>(&'a self) -> Result<Box<dyn GenericSocket>> { | ||
let (socket, _) = self.accept().await?; | ||
Ok((Box::new(socket))) | ||
} | ||
} | ||
|
||
impl GenericSocket for TcpStream {} | ||
|
||
pub async fn get_listener( | ||
unix_socket_path: Option<String>, | ||
port: Option<String>, | ||
) -> Result<Box<dyn GenericListener>> { | ||
let port = port.unwrap(); | ||
let address = format!("127.0.0.1:{}", port); | ||
Ok(Box::new(TcpListener::bind(address).await?)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters