feat(network): implement split read / write for NamedPipe bearer (#371)

This commit is contained in:
Clark Alesna 2024-01-03 19:34:16 +08:00 committed by GitHub
parent 57d84fe1e9
commit 1e7407867f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 43 deletions

View file

@ -255,33 +255,37 @@ impl NodeClient {
Ok(client)
}
// #[cfg(windows)]
// pub async fn connect(
// pipe_name: impl AsRef<std::ffi::OsStr>,
// magic: u64,
// ) -> Result<Self, Error> {
// let bearer = tokio::task::spawn_blocking(move ||
// Bearer::connect_named_pipe(pipe_name)) .await
// .expect("can't join tokio thread")
// .map_err(Error::ConnectFailure)?;
#[cfg(windows)]
pub async fn connect(
pipe_name: impl AsRef<std::ffi::OsStr>,
magic: u64,
) -> Result<Self, Error> {
let pipe_name = pipe_name.as_ref().to_os_string();
// let mut client = Self::new(bearer);
let bearer = tokio::task::spawn_blocking(move || {
Bearer::connect_named_pipe(pipe_name)
})
.await
.expect("can't join tokio thread")
.map_err(Error::ConnectFailure)?;
// let versions = handshake::n2c::VersionTable::v10_and_above(magic);
let mut client = Self::new(bearer);
// let handshake = client
// .handshake()
// .handshake(versions)
// .await
// .map_err(Error::HandshakeProtocol)?;
let versions = handshake::n2c::VersionTable::v10_and_above(magic);
// if let handshake::Confirmation::Rejected(reason) = handshake {
// error!(?reason, "handshake refused");
// return Err(Error::IncompatibleVersion);
// }
let handshake = client
.handshake()
.handshake(versions)
.await
.map_err(Error::HandshakeProtocol)?;
// Ok(client)
// }
if let handshake::Confirmation::Rejected(reason) = handshake {
error!(?reason, "handshake refused");
return Err(Error::IncompatibleVersion);
}
Ok(client)
}
#[cfg(unix)]
pub async fn handshake_query(

View file

@ -5,7 +5,7 @@ use std::collections::HashMap;
use byteorder::{ByteOrder, NetworkEndian};
use pallas_codec::{minicbor, Fragment};
use thiserror::Error;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::io::{AsyncReadExt, AsyncWriteExt, ReadHalf, WriteHalf};
use tokio::task::JoinHandle;
use tokio::time::Instant;
use tokio::{select, sync::mpsc::error::SendError};
@ -71,8 +71,9 @@ pub enum Bearer {
#[cfg(unix)]
Unix(unix::UnixStream),
// #[cfg(windows)]
// NamedPipe(NamedPipeClient),
#[cfg(windows)]
NamedPipe(NamedPipeClient),
}
impl Bearer {
@ -124,12 +125,12 @@ impl Bearer {
Ok((Self::Unix(stream), addr))
}
// #[cfg(windows)]
// pub fn connect_named_pipe(pipe_name: impl AsRef<std::ffi::OsStr>) ->
// IOResult<Self> { let client =
// tokio::net::windows::named_pipe::ClientOptions::new().open(&pipe_name)?;
// Ok(Self::NamedPipe(client))
// }
#[cfg(windows)]
pub fn connect_named_pipe(pipe_name: impl AsRef<std::ffi::OsStr>) ->
IOResult<Self> { let client =
tokio::net::windows::named_pipe::ClientOptions::new().open(&pipe_name)?;
Ok(Self::NamedPipe(client))
}
pub fn into_split(self) -> (BearerReadHalf, BearerWriteHalf) {
match self {
@ -143,6 +144,15 @@ impl Bearer {
let (r, w) = x.into_split();
(BearerReadHalf::Unix(r), BearerWriteHalf::Unix(w))
}
#[cfg(windows)]
Bearer::NamedPipe(x) => {
let (read, write) = tokio::io::split(x);
let reader = BearerReadHalf::NamedPipe(read);
let writer = BearerWriteHalf::NamedPipe(write);
(reader, writer)
}
}
}
}
@ -152,6 +162,9 @@ pub enum BearerReadHalf {
#[cfg(unix)]
Unix(unix::unix::OwnedReadHalf),
#[cfg(windows)]
NamedPipe(ReadHalf<NamedPipeClient>),
}
impl BearerReadHalf {
@ -161,6 +174,9 @@ impl BearerReadHalf {
#[cfg(unix)]
BearerReadHalf::Unix(x) => x.read_exact(buf).await,
#[cfg(windows)]
BearerReadHalf::NamedPipe(x) => x.read_exact(buf).await,
}
}
}
@ -170,6 +186,9 @@ pub enum BearerWriteHalf {
#[cfg(unix)]
Unix(unix::unix::OwnedWriteHalf),
#[cfg(windows)]
NamedPipe(WriteHalf<NamedPipeClient>),
}
impl BearerWriteHalf {
@ -179,6 +198,9 @@ impl BearerWriteHalf {
#[cfg(unix)]
Self::Unix(x) => x.write_all(buf).await,
#[cfg(windows)]
Self::NamedPipe(x) => x.write_all(buf).await,
}
}
@ -188,8 +210,9 @@ impl BearerWriteHalf {
#[cfg(unix)]
Self::Unix(x) => x.flush().await,
//#[cfg(windows)]
//Bearer::NamedPipe(x) => x.flush().await,
#[cfg(windows)]
Self::NamedPipe(x) => x.flush().await,
}
}
}