mesh_portal_tcp_common/
lib.rs1#[macro_use]
2extern crate anyhow;
3
4
5use std::convert::{TryFrom, TryInto};
6
7use anyhow::Error;
8use tokio::io::{AsyncReadExt, AsyncWriteExt, AsyncWrite};
9
10use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf};
11use std::marker::PhantomData;
12use std::time::Duration;
13use mesh_portal::version::latest::frame::{PrimitiveFrame, CloseReason};
14use mesh_portal::version::latest::portal::{outlet, inlet, initin, initout};
15
16#[cfg(test)]
17mod tests {
18 #[test]
19 fn it_works() {
20 let result = 2 + 2;
21 assert_eq!(result, 4);
22 }
23}
24
25pub struct FrameWriter<FRAME> where FRAME: TryInto<PrimitiveFrame> {
26 stream: PrimitiveFrameWriter,
27 phantom: PhantomData<FRAME>
28}
29
30impl <FRAME> FrameWriter<FRAME> where FRAME: TryInto<PrimitiveFrame> {
31 pub fn new(stream: PrimitiveFrameWriter) -> Self {
32 Self {
33 stream,
34 phantom: PhantomData
35 }
36 }
37
38 pub fn done(self) -> PrimitiveFrameWriter {
39 self.stream
40 }
41}
42
43impl FrameWriter<outlet::Frame> {
44
45 pub async fn write( &mut self, frame: outlet::Frame ) -> Result<(),Error> {
46 let frame = frame.try_into()?;
47 self.stream.write(frame).await
48 }
49
50 pub async fn close( &mut self, reason: CloseReason ) {
51 self.write(outlet::Frame::Close(reason) ).await.unwrap_or_default();
52 }
53
54}
55
56impl FrameWriter<inlet::Frame> {
57
58 pub async fn write( &mut self, frame: inlet::Frame ) -> Result<(),Error> {
59 let frame = frame.try_into()?;
60 self.stream.write(frame).await
61 }
62
63 pub async fn close( &mut self, reason: CloseReason ) {
64 self.write(inlet::Frame::Close(reason) ).await.unwrap_or_default();
65 }
66}
67
68impl FrameWriter<initin::Frame> {
69
70 pub async fn write( &mut self, frame: initin::Frame ) -> Result<(),Error> {
71 let frame = frame.try_into()?;
72 self.stream.write(frame).await
73 }
74
75 pub async fn close( &mut self, reason: CloseReason ) {
76 }
77}
78
79impl FrameWriter<initout::Frame> {
80
81 pub async fn write( &mut self, frame: initout::Frame ) -> Result<(),Error> {
82 let frame = frame.try_into()?;
83 self.stream.write(frame).await
84 }
85
86 pub async fn close( &mut self, reason: CloseReason ) {
87 }
88}
89pub struct FrameReader<FRAME> {
90 stream: PrimitiveFrameReader,
91 phantom: PhantomData<FRAME>
92}
93
94impl <FRAME> FrameReader<FRAME> {
95 pub fn new(stream: PrimitiveFrameReader) -> Self {
96 Self {
97 stream,
98 phantom: PhantomData
99 }
100 }
101
102 pub fn done(self) -> PrimitiveFrameReader {
103 self.stream
104 }
105}
106
107impl FrameReader<initin::Frame> {
108 pub async fn read( &mut self ) -> Result<initin::Frame,Error> {
109 let frame = self.stream.read().await?;
110 Ok(initin::Frame::try_from(frame)?)
111 }
112}
113
114impl FrameReader<initout::Frame> {
115 pub async fn read( &mut self ) -> Result<initout::Frame,Error> {
116 let frame = self.stream.read().await?;
117 Ok(initout::Frame::try_from(frame)?)
118 }
119}
120
121impl FrameReader<outlet::Frame> {
122 pub async fn read( &mut self ) -> Result<outlet::Frame,Error> {
123 let frame = self.stream.read().await?;
124 Ok(outlet::Frame::try_from(frame)?)
125 }
126}
127
128impl FrameReader<inlet::Frame> {
129 pub async fn read( &mut self ) -> Result<inlet::Frame,Error> {
130 let frame = self.stream.read().await?;
131 Ok(inlet::Frame::try_from(frame)?)
132 }
133}
134
135pub struct PrimitiveFrameReader {
136 read: OwnedReadHalf
137}
138
139impl PrimitiveFrameReader {
140
141 pub fn new(read: OwnedReadHalf ) -> Self {
142 Self {
143 read
144 }
145 }
146
147 pub async fn read(&mut self) -> Result<PrimitiveFrame,Error> {
148 let size = self.read.read_u32().await? as usize;
149
150 let mut vec= vec![0 as u8; size];
151 let buf = vec.as_mut_slice();
152 self.read.read_exact(buf).await?;
153 Result::Ok(PrimitiveFrame {
154 data: vec
155 })
156 }
157
158 pub async fn read_string(&mut self) -> Result<String,Error> {
159 let frame = self.read().await?;
160 Ok(frame.try_into()?)
161 }
162
163
164}
165
166pub struct PrimitiveFrameWriter {
167 write: OwnedWriteHalf,
168}
169
170impl PrimitiveFrameWriter {
171
172 pub fn new(write: OwnedWriteHalf) -> Self {
173 Self {
174 write,
175 }
176 }
177
178
179 pub async fn write( &mut self, frame: PrimitiveFrame ) -> Result<(),Error> {
180 self.write.write_u32(frame.size() ).await?;
181 self.write.write_all(frame.data.as_slice() ).await?;
182 Ok(())
183 }
184
185
186 pub async fn write_string(&mut self, string: String) -> Result<(),Error> {
187 let frame = PrimitiveFrame::from(string);
188 self.write(frame).await
189 }
190}
191
192
193