-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcustom_middleware.rs
74 lines (60 loc) · 1.74 KB
/
custom_middleware.rs
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
// Custom middleware example.
// All bytes that go through the protocol are rotated by an offset of 13.
use std::num::Wrapping;
/// A rot-n middleware.
/// Rotates each byte by a specific offset.
#[derive(Clone, Debug)]
pub struct RotateMiddleware
{
pub offset: u8,
}
impl RotateMiddleware
{
pub fn rot13() -> Self {
RotateMiddleware { offset: 13 }
}
}
impl protocol::wire::Middleware for RotateMiddleware
{
fn decode_data(&mut self, data: Vec<u8>) -> Result<Vec<u8>, protocol::Error> {
Ok(data.into_iter().map(|byte| (Wrapping(byte) - Wrapping(self.offset)).0).collect())
}
fn encode_data(&mut self, data: Vec<u8>) -> Result<Vec<u8>, protocol::Error> {
Ok(data.into_iter().map(|byte| (Wrapping(byte) + Wrapping(self.offset)).0).collect())
}
}
protocol::define_middleware_pipeline!(Pipeline {
rot: RotateMiddleware
});
impl Pipeline
{
pub fn new() -> Self {
Pipeline {
rot: RotateMiddleware::rot13(),
}
}
}
#[derive(protocol::Protocol, Clone, Debug, PartialEq)]
pub struct Ping {
id: i64,
data: Vec<u8>
}
#[derive(protocol::Protocol, Clone, Debug, PartialEq)]
#[protocol(discriminant = "integer")]
#[protocol(discriminator(u8))]
pub enum Packet {
#[protocol(discriminator(0))]
Ping(Ping),
}
fn main() {
use std::net::TcpStream;
let stream = TcpStream::connect("127.0.0.1:34254").unwrap();
let mut connection = protocol::wire::stream::Connection::new(stream, Pipeline::new(), protocol::Settings::default());
connection.send_packet(&Packet::Ping(Ping { id: 0, data: vec![ 55 ]})).unwrap();
loop {
if let Some(response) = connection.receive_packet().unwrap() {
println!("{:?}", response);
break;
}
}
}