mirror of https://github.com/Trivernis/bromine.git
commit
aec08da839
@ -0,0 +1,36 @@
|
||||
pub mod tcp;
|
||||
|
||||
#[cfg(unix)]
|
||||
pub mod unix_socket;
|
||||
|
||||
use crate::prelude::IPCResult;
|
||||
use async_trait::async_trait;
|
||||
use std::fmt::Debug;
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
|
||||
#[async_trait]
|
||||
pub trait AsyncStreamProtocolListener: Sized + Send + Sync {
|
||||
type AddressType: Clone + Debug + Send + Sync;
|
||||
type RemoteAddressType: Debug;
|
||||
type Stream: 'static + AsyncProtocolStream<AddressType = Self::AddressType>;
|
||||
|
||||
async fn protocol_bind(address: Self::AddressType) -> IPCResult<Self>;
|
||||
|
||||
async fn protocol_accept(&self) -> IPCResult<(Self::Stream, Self::RemoteAddressType)>;
|
||||
}
|
||||
|
||||
pub trait AsyncProtocolStreamSplit {
|
||||
type OwnedSplitReadHalf: AsyncRead + Send + Sync + Unpin;
|
||||
type OwnedSplitWriteHalf: AsyncWrite + Send + Sync + Unpin;
|
||||
|
||||
fn protocol_into_split(self) -> (Self::OwnedSplitReadHalf, Self::OwnedSplitWriteHalf);
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait AsyncProtocolStream:
|
||||
AsyncRead + AsyncWrite + Send + Sync + AsyncProtocolStreamSplit + Sized
|
||||
{
|
||||
type AddressType: Clone + Debug + Send + Sync;
|
||||
|
||||
async fn protocol_connect(address: Self::AddressType) -> IPCResult<Self>;
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
use crate::prelude::IPCResult;
|
||||
use crate::protocol::{AsyncProtocolStream, AsyncProtocolStreamSplit, AsyncStreamProtocolListener};
|
||||
use async_trait::async_trait;
|
||||
use std::net::SocketAddr;
|
||||
use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
|
||||
#[async_trait]
|
||||
impl AsyncStreamProtocolListener for TcpListener {
|
||||
type AddressType = SocketAddr;
|
||||
type RemoteAddressType = SocketAddr;
|
||||
type Stream = TcpStream;
|
||||
|
||||
async fn protocol_bind(address: Self::AddressType) -> IPCResult<Self> {
|
||||
let listener = TcpListener::bind(address).await?;
|
||||
|
||||
Ok(listener)
|
||||
}
|
||||
|
||||
async fn protocol_accept(&self) -> IPCResult<(Self::Stream, Self::RemoteAddressType)> {
|
||||
let connection = self.accept().await?;
|
||||
|
||||
Ok(connection)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncProtocolStreamSplit for TcpStream {
|
||||
type OwnedSplitReadHalf = OwnedReadHalf;
|
||||
type OwnedSplitWriteHalf = OwnedWriteHalf;
|
||||
|
||||
fn protocol_into_split(self) -> (Self::OwnedSplitReadHalf, Self::OwnedSplitWriteHalf) {
|
||||
self.into_split()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl AsyncProtocolStream for TcpStream {
|
||||
type AddressType = SocketAddr;
|
||||
|
||||
async fn protocol_connect(address: Self::AddressType) -> IPCResult<Self> {
|
||||
let stream = TcpStream::connect(address).await?;
|
||||
|
||||
Ok(stream)
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
use crate::error::Result;
|
||||
use crate::prelude::IPCResult;
|
||||
use crate::protocol::{AsyncProtocolStream, AsyncProtocolStreamSplit, AsyncStreamProtocolListener};
|
||||
use async_trait::async_trait;
|
||||
use std::path::PathBuf;
|
||||
use tokio::io::Interest;
|
||||
use tokio::net::unix::OwnedWriteHalf;
|
||||
use tokio::net::unix::{OwnedReadHalf, SocketAddr};
|
||||
use tokio::net::{UnixListener, UnixStream};
|
||||
|
||||
#[async_trait]
|
||||
impl AsyncStreamProtocolListener for UnixListener {
|
||||
type AddressType = PathBuf;
|
||||
type RemoteAddressType = SocketAddr;
|
||||
type Stream = UnixStream;
|
||||
|
||||
async fn protocol_bind(address: Self::AddressType) -> Result<Self> {
|
||||
let listener = UnixListener::bind(address)?;
|
||||
|
||||
Ok(listener)
|
||||
}
|
||||
|
||||
async fn protocol_accept(&self) -> Result<(Self::Stream, Self::RemoteAddressType)> {
|
||||
let connection = self.accept().await?;
|
||||
|
||||
Ok(connection)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncProtocolStreamSplit for UnixStream {
|
||||
type OwnedSplitReadHalf = OwnedReadHalf;
|
||||
type OwnedSplitWriteHalf = OwnedWriteHalf;
|
||||
|
||||
fn protocol_into_split(self) -> (Self::OwnedSplitReadHalf, Self::OwnedSplitWriteHalf) {
|
||||
self.into_split()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl AsyncProtocolStream for UnixStream {
|
||||
type AddressType = PathBuf;
|
||||
|
||||
async fn protocol_connect(address: Self::AddressType) -> IPCResult<Self> {
|
||||
let stream = UnixStream::connect(address).await?;
|
||||
stream
|
||||
.ready(Interest::READABLE | Interest::WRITABLE)
|
||||
.await?;
|
||||
|
||||
Ok(stream)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue