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
pub trait StreamTransfer {
fn connect(&mut self) -> Result<(), String>;
fn recv(&mut self) -> Result<Option<Vec<u8>>, String>;
fn peek(&mut self) -> Result<Option<Vec<u8>>, String>;
fn send(&mut self, data: Vec<u8>) -> Result<(), String>;
}
pub trait PacketTransfer {
fn connect(&mut self) -> Result<(), String>;
fn recv(&mut self, tag: &str) -> Result<Option<Vec<u8>>, String> {
let ret = self.peek(tag)?;
self.remove(tag)?;
Ok(ret)
}
fn peek(&mut self, tag: &str) -> Result<Option<Vec<u8>>, String>;
fn send(&mut self, tag: &str, data: Vec<u8>) -> Result<(), String>;
fn remove(&mut self, tag: &str) -> Result<(), String>;
}
pub trait DataStorage {
fn connect(&mut self) -> Result<(), String>;
fn peek(&mut self, tag: &str) -> Result<Option<Vec<u8>>, String>;
fn send(&mut self, tag: &str, data: Vec<u8>) -> Result<(), String>;
fn remove(&mut self, tag: &str) -> Result<(), String>;
}
pub mod extends {
use super::*;
pub struct StreamTransferOverlay<T: PacketTransfer> {
packet_transfer: T,
recv: u128,
send: u128,
}
impl<T: PacketTransfer> StreamTransferOverlay<T> {
pub fn new(packet_transfer: T) -> Self {
Self {
packet_transfer,
recv: 0,
send: 0,
}
}
}
impl<T: PacketTransfer> StreamTransfer for StreamTransferOverlay<T> {
fn connect(&mut self) -> Result<(), String> {
self.packet_transfer.connect()
}
fn recv(&mut self) -> Result<Option<Vec<u8>>, String> {
self.recv += 1;
self.packet_transfer.recv(&(self.recv - 1).to_string())
}
fn peek(&mut self) -> Result<Option<Vec<u8>>, String> {
self.packet_transfer.peek(&self.recv.to_string())
}
fn send(&mut self, data: Vec<u8>) -> Result<(), String> {
self.send += 1;
self.packet_transfer
.send(&(self.send - 1).to_string(), data)
}
}
impl<T: DataStorage> PacketTransfer for T {
fn connect(&mut self) -> Result<(), String> {
self.connect()
}
fn peek(&mut self, tag: &str) -> Result<Option<Vec<u8>>, String> {
self.peek(tag)
}
fn send(&mut self, tag: &str, data: Vec<u8>) -> Result<(), String> {
self.send(tag, data)
}
fn remove(&mut self, tag: &str) -> Result<(), String> {
self.remove(tag)
}
}
}