File size: 4,329 Bytes
b98ffbb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
use std::{
io::{Read, Write},
net::{SocketAddr, TcpListener, TcpStream},
};
use crate::{ListenConnection, RequestReplyConnection, RequestReplyLayer};
pub type TcpRequestReplyConnection =
dyn RequestReplyConnection<RequestData = Vec<u8>, ReplyData = Vec<u8>, Error = std::io::Error>;
pub struct TcpLayer {}
impl TcpLayer {
pub fn new() -> Self {
Self {}
}
}
impl Default for TcpLayer {
fn default() -> Self {
Self::new()
}
}
impl RequestReplyLayer for TcpLayer {
type Address = SocketAddr;
type RequestData = Vec<u8>;
type ReplyData = Vec<u8>;
type Error = std::io::Error;
fn listen(
&mut self,
addr: Self::Address,
) -> Result<
Box<
dyn Iterator<
Item = Result<
Box<
dyn crate::ListenConnection<
RequestData = Self::RequestData,
ReplyData = Self::ReplyData,
Error = Self::Error,
>,
>,
Self::Error,
>,
>,
>,
Self::Error,
> {
let incoming: Box<dyn Iterator<Item = Result<_, _>>> = Box::new(
IntoIncoming {
listener: TcpListener::bind(addr)?,
}
.map(|r| {
r.map(|stream| {
let connection: Box<
dyn ListenConnection<
RequestData = Self::RequestData,
ReplyData = Self::ReplyData,
Error = Self::Error,
>,
> = Box::new(TcpConnection { stream });
connection
})
}),
);
Ok(incoming)
}
fn connect(
&mut self,
addr: Self::Address,
) -> Result<
Box<
dyn crate::RequestReplyConnection<
RequestData = Self::RequestData,
ReplyData = Self::ReplyData,
Error = Self::Error,
>,
>,
Self::Error,
> {
TcpStream::connect(addr).map(|s| {
let connection: Box<
dyn RequestReplyConnection<
RequestData = Self::RequestData,
ReplyData = Self::ReplyData,
Error = Self::Error,
>,
> = Box::new(TcpConnection { stream: s });
connection
})
}
}
struct TcpConnection {
stream: TcpStream,
}
impl ListenConnection for TcpConnection {
type RequestData = Vec<u8>;
type ReplyData = Vec<u8>;
type Error = std::io::Error;
fn handle_next(
&mut self,
handler: Box<dyn FnOnce(Self::RequestData) -> Result<Self::ReplyData, Self::Error>>,
) -> Result<(), Self::Error> {
let request = self.receive()?;
let reply = handler(request)?;
self.send(&reply)?;
Ok(())
}
}
impl RequestReplyConnection for TcpConnection {
type RequestData = Vec<u8>;
type ReplyData = Vec<u8>;
type Error = std::io::Error;
fn request(&mut self, request: &Self::RequestData) -> Result<Self::ReplyData, Self::Error> {
self.send(request)?;
let reply = self.receive()?;
Ok(reply)
}
}
impl TcpConnection {
fn send(&mut self, request: &[u8]) -> std::io::Result<()> {
let len_raw = (request.len() as u64).to_le_bytes();
self.stream.write_all(&len_raw)?;
self.stream.write_all(request)?;
Ok(())
}
fn receive(&mut self) -> std::io::Result<Vec<u8>> {
let reply_len = {
let mut raw = [0; 8];
self.stream.read_exact(&mut raw)?;
u64::from_le_bytes(raw) as usize
};
let mut reply = vec![0; reply_len];
self.stream.read_exact(&mut reply)?;
Ok(reply)
}
}
// taken from `std::net::tcp` module (still unstable)
pub struct IntoIncoming {
listener: TcpListener,
}
impl Iterator for IntoIncoming {
type Item = std::io::Result<TcpStream>;
fn next(&mut self) -> Option<std::io::Result<TcpStream>> {
Some(self.listener.accept().map(|p| p.0))
}
}
impl std::iter::FusedIterator for IntoIncoming {}
|