forked from editso/fuso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
181 lines (147 loc) · 5.32 KB
/
lib.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
mod buffer;
mod cipher;
mod core;
mod error;
mod stream;
mod udp;
pub use crate::buffer::*;
pub use crate::cipher::*;
pub use crate::core::*;
pub use crate::error::*;
pub use crate::stream::*;
pub use crate::udp::*;
pub use async_trait::*;
#[cfg(test)]
mod tests {
use std::{
sync::{Arc, Mutex},
time::Duration,
};
use futures::AsyncReadExt;
use smol::{future::FutureExt, io::AsyncWriteExt, net::UdpSocket};
use crate::{core::Packet, now_mills, UdpListener, UdpStreamEx};
fn init_logger() {
env_logger::builder()
.filter_level(log::LevelFilter::Debug)
.init();
}
#[test]
fn test_copy() {
let mut buf = [1;1024];
let data = [1,2,3];
unsafe{
std::ptr::copy(data.as_ptr(), buf.as_mut_ptr(), 3);
}
println!("{:?}", buf)
}
#[test]
fn test_time() {
// let time = std::time::SystemTime::now()
// .duration_since(std::time::SystemTime::UNIX_EPOCH).unwrap();
println!("{:?}", now_mills())
}
#[test]
fn test_packet() {
init_logger();
let packet = Packet::new(1, "Hello".into());
log::debug!("{:?}", Packet::size());
log::debug!("{:?}", packet);
assert_eq!(5, packet.get_len());
let data = packet.encode();
log::debug!("len: {}, raw: {:?}", data.len(), data);
let packet = Packet::decode_data(&data).unwrap();
log::debug!("data len: {}, {:?}", packet.get_len(), packet.get_data());
}
#[test]
fn test_waker() {
init_logger();
smol::block_on(async move {
let (sender, receiver) = std::sync::mpsc::channel();
let receiver = Arc::new(Mutex::new(receiver));
{
smol::spawn(async move {
loop {
let receiver = receiver.clone();
let msg = smol::unblock(move || receiver.lock().unwrap().recv()).await;
println!("msg = {}", msg.unwrap())
}
})
.detach();
}
println!("test");
let mut io = smol::Unblock::new(std::io::stdin());
loop {
let mut buf = Vec::new();
buf.resize(1024, 0);
match io.read(&mut buf).await {
Ok(n) => {
buf.truncate(n);
sender.send(String::from_utf8(buf).unwrap()).unwrap();
}
Err(e) => {
log::error!("{}", e);
}
}
}
});
}
#[test]
fn test_udp_stream() {
init_logger();
smol::block_on(async move {
let listen_future = async move {
match UdpListener::bind("0.0.0.0:9999").await {
Ok(udp) => match udp.accept().await {
Ok(mut stream) => {
log::debug!("new udp");
let mut buf = Vec::new();
buf.resize(1024, 0);
match stream.read(&mut buf).await {
Ok(n) => {
buf.truncate(n);
log::debug!("recv {}", String::from_utf8_lossy(&buf));
stream.write_all(b"Hello!").await.unwrap();
smol::Timer::after(Duration::from_millis(2000)).await;
}
Err(e) => {
log::error!("recv {}", e);
}
}
}
Err(e) => {
log::error!("accept {}", e);
}
},
Err(e) => {
log::error!("bind {}", e);
}
};
};
let client_future = async move {
smol::Timer::after(Duration::from_millis(2000)).await;
match UdpSocket::bind("0.0.0.0:0").await {
Ok(udp) => {
udp.connect("127.0.0.1:9999").await.unwrap();
let mut udp = udp.as_udp_stream();
udp.write(b"Hello World").await.unwrap();
let mut buf = Vec::new();
buf.resize(1024, 0);
match udp.read(&mut buf).await {
Ok(n) => {
buf.truncate(n);
log::debug!("read {}", String::from_utf8_lossy(&buf));
}
Err(e) => {
log::error!("read {}", e);
}
}
}
Err(e) => {
log::error!("connect {}", e);
}
}
};
client_future.race(listen_future).await
});
}
}