-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbasic.rs
More file actions
55 lines (46 loc) · 1.49 KB
/
Copy pathbasic.rs
File metadata and controls
55 lines (46 loc) · 1.49 KB
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
#[derive(protocol::Protocol, Clone, Debug, PartialEq)]
pub struct Handshake;
#[derive(protocol::Protocol, Clone, Debug, PartialEq)]
pub struct Hello {
id: i64,
data: Vec<u8>,
}
#[derive(protocol::Protocol, Clone, Debug, PartialEq)]
pub struct Goodbye {
id: i64,
reason: String,
}
#[derive(protocol::Protocol, Clone, Debug, PartialEq)]
pub struct Node {
name: String,
enabled: bool
}
// Defines a packet kind enum.
#[derive(protocol::Protocol, Clone, Debug, PartialEq)]
#[protocol(discriminant = "integer")]
pub enum Packet {
#[protocol(discriminator(0x00))]
Handshake(Handshake),
#[protocol(discriminator(0x01))]
Hello(Hello),
#[protocol(discriminator(0x02))]
Goodbye(Goodbye),
}
fn main() {
use std::net::TcpStream;
let stream = TcpStream::connect("127.0.0.1:34254").unwrap();
let settings = protocol::Settings {
byte_order: protocol::ByteOrder::LittleEndian,
..Default::default()
};
let mut connection = protocol::wire::stream::Connection::new(stream, protocol::wire::middleware::pipeline::default(), settings);
connection.send_packet(&Packet::Handshake(Handshake)).unwrap();
connection.send_packet(&Packet::Hello(Hello { id: 0, data: vec![ 55 ]})).unwrap();
connection.send_packet(&Packet::Goodbye(Goodbye { id: 0, reason: "leaving".to_string() })).unwrap();
loop {
if let Some(response) = connection.receive_packet().unwrap() {
println!("{:?}", response);
break;
}
}
}