-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket_example.rs
119 lines (103 loc) · 3.44 KB
/
socket_example.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
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
use std::net::{TcpListener, TcpStream};
use std::io::prelude::*;
// use std::io::BufReader;
use jppe_derive::{BorrowByteDecode, BorrowByteEncode};
#[derive(Debug, PartialEq, Eq, BorrowByteDecode, BorrowByteEncode)]
pub struct Message<'a> {
pub version: u8,
#[jppe(byte_count=1)]
pub body: MessageBody<'a>
}
#[derive(Debug, PartialEq, Eq, BorrowByteDecode, BorrowByteEncode)]
#[repr(u8)]
pub enum MessageBody<'a> {
ReadReq {
address: u8,
length: u8,
} = 1,
ReadRsp {
address: u8,
length: u8,
#[jppe(length="length")]
data: &'a [u8],
},
WriteReq {
address: u8,
length: u8,
#[jppe(length="length")]
data: &'a [u8],
},
WriteRsp {
address: u8,
length: u8,
},
Stop,
}
fn handle_connection(mut stream: TcpStream) {
let mut input = [0; 1024];
stream.read(&mut input).unwrap();
let (_, value) = jppe::decode_borrow::<Message>(&input).unwrap();
println!("receive req, {value:?}");
assert_eq!(value.version, 1);
match value.body {
MessageBody::ReadReq { address, length } => {
assert_eq!(address, 0x0002);
assert_eq!(length, 3);
// Send Read Response Command
let value = Message {
version: 1,
body: MessageBody::ReadRsp { address: 0x0002, length: 3, data: b"\x00\x01\x02" },
};
stream.write_all(&jppe::encode_borrow(value)).unwrap();
},
MessageBody::WriteReq { address, length, data } => {
assert_eq!(address, 0x0002);
assert_eq!(length, 3);
assert_eq!(data, b"\x00\01\x02");
// Send Write Response Command
let value = Message {
version: 1,
body: MessageBody::WriteRsp { address: 0x0002, length: 3 },
};
stream.write_all(&jppe::encode_borrow(value)).unwrap();
},
_ => {
let value = Message {
version: 1,
body: MessageBody::Stop,
};
stream.write_all(&jppe::encode_borrow(value)).unwrap();
},
}
}
fn main() -> std::io::Result<()> {
// Since the test case is directly used here, unwrap is used.
std::thread::scope(|s| {
s.spawn(|| {
let listener = TcpListener::bind("127.0.0.1:6789").unwrap();
// accept connections and process them serially
for stream in listener.incoming() {
println!("{stream:?}");
handle_connection(stream.unwrap());
}
});
s.spawn(|| {
std::thread::sleep(std::time::Duration::from_secs(1));
let mut stream = TcpStream::connect("127.0.0.1:6789").unwrap();
// Send Read Command
let value = Message {
version: 1,
body: MessageBody::ReadReq { address: 0x0002, length: 3 },
};
let encode_value = jppe::encode_borrow(value);
stream.write_all(&encode_value).unwrap();
let mut buf = [0; 10];
stream.read(&mut buf).unwrap();
let (_, value) = jppe::decode_borrow::<Message>(&buf).unwrap();
println!("receive rsp, {value:?}");
assert_eq!(value.version, 1);
assert_eq!(value.body, MessageBody::ReadRsp { address: 0x0002, length: 3, data: b"\x00\x01\x02" });
});
});
Ok(())
}