use std::io::{self, Read, Write};
use std::time::Duration;
use crate::capabilities::NseCapabilityContext;
use crate::providers::{
broker_tcp_connect, broker_tcp_receive, broker_tcp_send, NseHostServices, NseResolvedEndpoint,
NseTcpConnection,
};
pub const BROKERED_STREAM_DEFAULT_TIMEOUT: Duration = Duration::from_secs(120);
pub struct BrokeredTcpStream {
handle: Box<dyn NseTcpConnection>,
ctx: NseCapabilityContext,
operation: &'static str,
}
impl BrokeredTcpStream {
pub fn connect(
ctx: &NseCapabilityContext,
services: &NseHostServices,
host: &str,
port: u16,
timeout: Duration,
operation: &'static str,
) -> Result<(Self, NseResolvedEndpoint), String> {
let (handle, endpoint) = broker_tcp_connect(ctx, services, host, port, timeout, operation)?;
Ok((
Self {
handle,
ctx: ctx.clone(),
operation,
},
endpoint,
))
}
pub fn endpoint(&self) -> &NseResolvedEndpoint {
self.handle.endpoint()
}
pub fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if buf.is_empty() {
return Ok(0);
}
let data = broker_tcp_receive(&self.ctx, self.handle.as_mut(), buf.len(), self.operation)
.map_err(io::Error::other)?;
let n = data.len().min(buf.len());
buf[..n].copy_from_slice(&data[..n]);
Ok(n)
}
pub fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let mut written = 0;
while written < buf.len() {
let n = broker_tcp_send(
&self.ctx,
self.handle.as_mut(),
&buf[written..],
self.operation,
)
.map_err(io::Error::other)?;
if n == 0 {
return Err(io::Error::other("brokered TCP send wrote zero bytes"));
}
written += n;
}
Ok(written)
}
pub fn set_read_timeout(&mut self, dur: Option<Duration>) -> io::Result<()> {
self.handle
.set_timeouts(dur.unwrap_or(BROKERED_STREAM_DEFAULT_TIMEOUT))
.map_err(|e| io::Error::other(e.to_string()))
}
pub fn set_write_timeout(&mut self, dur: Option<Duration>) -> io::Result<()> {
self.handle
.set_timeouts(dur.unwrap_or(BROKERED_STREAM_DEFAULT_TIMEOUT))
.map_err(|e| io::Error::other(e.to_string()))
}
pub fn is_alive(&self) -> bool {
self.handle.is_alive()
}
pub fn close(&mut self) {
self.handle.close();
}
}
impl Read for BrokeredTcpStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
BrokeredTcpStream::read(self, buf)
}
}
impl Write for BrokeredTcpStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
BrokeredTcpStream::write(self, buf)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
pub fn broker_send_all(
ctx: &NseCapabilityContext,
handle: &mut dyn NseTcpConnection,
data: &[u8],
operation: &'static str,
) -> Result<(), String> {
let mut written = 0;
while written < data.len() {
let n = broker_tcp_send(ctx, handle, &data[written..], operation)?;
if n == 0 {
return Err("brokered TCP send wrote zero bytes".to_string());
}
written += n;
}
Ok(())
}
pub fn broker_write_all(
ctx: &NseCapabilityContext,
handle: &mut dyn NseTcpConnection,
data: &[u8],
operation: &'static str,
) -> io::Result<()> {
broker_send_all(ctx, handle, data, operation).map_err(io::Error::other)
}
pub fn broker_read_into(
ctx: &NseCapabilityContext,
handle: &mut dyn NseTcpConnection,
buf: &mut [u8],
operation: &'static str,
) -> io::Result<usize> {
if buf.is_empty() {
return Ok(0);
}
let data = broker_tcp_receive(ctx, handle, buf.len(), operation).map_err(io::Error::other)?;
let n = data.len().min(buf.len());
buf[..n].copy_from_slice(&data[..n]);
Ok(n)
}