mirror of https://github.com/chipsenkbeil/distant
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.1 KiB
Rust
38 lines
1.1 KiB
Rust
3 years ago
|
use super::{Codec, DataStream, Transport};
|
||
3 years ago
|
use tokio::{
|
||
|
io,
|
||
|
net::{
|
||
|
unix::{OwnedReadHalf, OwnedWriteHalf, SocketAddr},
|
||
|
UnixStream,
|
||
|
},
|
||
|
};
|
||
|
|
||
|
impl DataStream for UnixStream {
|
||
|
type Read = OwnedReadHalf;
|
||
|
type Write = OwnedWriteHalf;
|
||
|
|
||
|
fn to_connection_tag(&self) -> String {
|
||
|
self.peer_addr()
|
||
|
.map(|addr| format!("{:?}", addr))
|
||
|
.unwrap_or_else(|_| String::from("--"))
|
||
|
}
|
||
|
|
||
|
fn into_split(self) -> (Self::Read, Self::Write) {
|
||
|
UnixStream::into_split(self)
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
impl<U: Codec> Transport<UnixStream, U> {
|
||
|
/// Establishes a connection to the socket at the specified path and uses the provided codec
|
||
|
/// for transportation
|
||
|
pub async fn connect(path: impl AsRef<std::path::Path>, codec: U) -> io::Result<Self> {
|
||
3 years ago
|
let stream = UnixStream::connect(path.as_ref()).await?;
|
||
3 years ago
|
Ok(Transport::new(stream, codec))
|
||
3 years ago
|
}
|
||
|
|
||
|
/// Returns the address of the peer the transport is connected to
|
||
|
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
||
3 years ago
|
self.0.get_ref().peer_addr()
|
||
3 years ago
|
}
|
||
|
}
|