1mod arq;
42mod datatype;
43mod error;
44mod fragment;
45mod log;
46mod packet;
47mod server;
48mod socket;
49mod utils;
50
51pub use crate::arq::Reliability;
52pub use crate::log::enable_raknet_log;
53pub use crate::server::*;
54pub use crate::socket::*;
55
56#[tokio::test]
93async fn test_connect() {
94 let mut server = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
95 .await
96 .unwrap();
97 let local_addr = server.local_addr().unwrap();
98 server.listen().await;
99
100 let notify = std::sync::Arc::new(tokio::sync::Notify::new());
101 let notify2 = notify.clone();
102
103 tokio::spawn(async move {
104 let client1 = server.accept().await.unwrap();
105 assert!(client1.local_addr().unwrap() == local_addr);
106 client1
107 .send(&[0xfe, 2, 3], Reliability::Reliable)
108 .await
109 .unwrap();
110 notify2.notified().await;
111 });
112 let client2 = RaknetSocket::connect(&local_addr).await.unwrap();
113 assert!(client2.peer_addr().unwrap() == local_addr);
114 let buf = client2.recv().await.unwrap();
115 assert!(buf == vec![0xfe, 2, 3]);
116
117 notify.notify_one();
118}
119
120#[tokio::test]
121async fn test_send_recv_fragment_data() {
122 let mut server = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
123 .await
124 .unwrap();
125 let local_addr = server.local_addr().unwrap();
126 server.listen().await;
127
128 let notify = std::sync::Arc::new(tokio::sync::Notify::new());
129 let notify2 = notify.clone();
130
131 tokio::spawn(async move {
132 let client1 = server.accept().await.unwrap();
133 assert!(client1.local_addr().unwrap() == local_addr);
134
135 let mut a = vec![3u8; 1000];
136 let mut b = vec![2u8; 1000];
137 let mut c = vec![0xfe; 1000];
138 b.append(&mut a);
139 c.append(&mut b);
140
141 client1
142 .send(&c, Reliability::ReliableOrdered)
143 .await
144 .unwrap();
145
146 notify2.notified().await;
147 });
148 let client2 = RaknetSocket::connect(&local_addr).await.unwrap();
149 assert!(client2.peer_addr().unwrap() == local_addr);
150 let buf = client2.recv().await.unwrap();
151 assert!(buf.len() == 3000);
152 assert!(buf[0..1000] == vec![0xfe; 1000]);
153 assert!(buf[1000..2000] == vec![2u8; 1000]);
154 assert!(buf[2000..3000] == vec![3u8; 1000]);
155
156 notify.notify_one();
157}
158
159#[tokio::test]
160async fn test_send_recv_more_reliability_type_packet() {
161 let mut server = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
162 .await
163 .unwrap();
164 let local_addr = server.local_addr().unwrap();
165 server.listen().await;
166
167 let notify = std::sync::Arc::new(tokio::sync::Notify::new());
168 let notify2 = notify.clone();
169
170 tokio::spawn(async move {
171 let client1 = server.accept().await.unwrap();
172 assert!(client1.local_addr().unwrap() == local_addr);
173
174 client1
175 .send(&[0xfe, 1, 2, 3], Reliability::Unreliable)
176 .await
177 .unwrap();
178 let data = client1.recv().await.unwrap();
179 assert!(data == [0xfe, 4, 5, 6].to_vec());
180
181 client1
182 .send(&[0xfe, 7, 8, 9], Reliability::UnreliableSequenced)
183 .await
184 .unwrap();
185 let data = client1.recv().await.unwrap();
186 assert!(data == [0xfe, 10, 11, 12].to_vec());
187
188 client1
189 .send(&[0xfe, 13, 14, 15], Reliability::Reliable)
190 .await
191 .unwrap();
192 let data = client1.recv().await.unwrap();
193 assert!(data == [0xfe, 16, 17, 18].to_vec());
194
195 let mut a = vec![3u8; 1000];
196 let mut b = vec![2u8; 1000];
197 let mut c = vec![0xfe; 1000];
198 b.append(&mut a);
199 c.append(&mut b);
200
201 client1
202 .send(&c, Reliability::ReliableOrdered)
203 .await
204 .unwrap();
205
206 let buf = client1.recv().await.unwrap();
207 assert!(buf.len() == 3000);
208 assert!(buf[0..1000] == vec![0xfe; 1000]);
209 assert!(buf[1000..2000] == vec![2u8; 1000]);
210 assert!(buf[2000..3000] == vec![3u8; 1000]);
211
212 client1
213 .send(&[0xfe, 19, 20, 21], Reliability::ReliableSequenced)
214 .await
215 .unwrap();
216 let data = client1.recv().await.unwrap();
217 assert!(data == [0xfe, 22, 23, 24].to_vec());
218
219 notify2.notified().await;
220 });
221 let client2 = RaknetSocket::connect(&local_addr).await.unwrap();
222 assert!(client2.peer_addr().unwrap() == local_addr);
223
224 let buf = client2.recv().await.unwrap();
225 assert!(buf == [0xfe, 1, 2, 3]);
226
227 client2
228 .send(&[0xfe, 4, 5, 6], Reliability::Unreliable)
229 .await
230 .unwrap();
231
232 let buf = client2.recv().await.unwrap();
233 assert!(buf == [0xfe, 7, 8, 9]);
234
235 client2
236 .send(&[0xfe, 10, 11, 12], Reliability::UnreliableSequenced)
237 .await
238 .unwrap();
239
240 let buf = client2.recv().await.unwrap();
241 assert!(buf == [0xfe, 13, 14, 15]);
242
243 client2
244 .send(&[0xfe, 16, 17, 18], Reliability::Reliable)
245 .await
246 .unwrap();
247
248 let buf = client2.recv().await.unwrap();
249 assert!(buf.len() == 3000);
250 assert!(buf[0..1000] == vec![0xfe; 1000]);
251 assert!(buf[1000..2000] == vec![2u8; 1000]);
252 assert!(buf[2000..3000] == vec![3u8; 1000]);
253
254 let mut a = vec![3u8; 1000];
255 let mut b = vec![2u8; 1000];
256 let mut c = vec![0xfe; 1000];
257 b.append(&mut a);
258 c.append(&mut b);
259
260 client2
261 .send(&c, Reliability::ReliableOrdered)
262 .await
263 .unwrap();
264
265 let buf = client2.recv().await.unwrap();
266 assert!(buf == [0xfe, 19, 20, 21]);
267
268 client2
269 .send(&[0xfe, 22, 23, 24], Reliability::ReliableSequenced)
270 .await
271 .unwrap();
272
273 notify.notify_one();
274}
275
276#[tokio::test]
277async fn test_loss_packet1() {
278 let notify = std::sync::Arc::new(tokio::sync::Notify::new());
279 let notify2 = notify.clone();
280 let mut server = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
281 .await
282 .unwrap();
283 let local_addr = server.local_addr().unwrap();
284 server.listen().await;
285 tokio::spawn(async move {
286 let mut client1 = server.accept().await.unwrap();
287 client1.set_loss_rate(8);
289
290 for i in 0..10 {
291 let mut flag = vec![0xfe_u8];
292 let mut data = vec![i as u8; 2000];
293 flag.append(&mut data);
294 client1
295 .send(&flag, Reliability::ReliableOrdered)
296 .await
297 .unwrap();
298
299 let data = client1.recv().await.unwrap();
300 assert!(data == flag);
301 }
302
303 notify2.notified().await;
304 });
305 let mut client2 = RaknetSocket::connect(&local_addr).await.unwrap();
306 client2.set_loss_rate(8);
308
309 for i in 0..10 {
310 let mut flag = vec![0xfe_u8];
311 let mut data = vec![i as u8; 2000];
312 flag.append(&mut data);
313 client2
314 .send(&flag, Reliability::ReliableOrdered)
315 .await
316 .unwrap();
317
318 let data = client2.recv().await.unwrap();
319 assert!(data == flag);
320 }
321 notify.notify_one();
322}
323
324#[tokio::test]
325async fn test_loss_packet2() {
326 let notify = std::sync::Arc::new(tokio::sync::Notify::new());
327 let notify2 = notify.clone();
328 let mut server = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
329 .await
330 .unwrap();
331 let local_addr = server.local_addr().unwrap();
332 server.listen().await;
333 tokio::spawn(async move {
334 let mut client1 = server.accept().await.unwrap();
335 client1.set_loss_rate(8);
337
338 for i in 0..10 {
339 let mut flag = vec![0xfe_u8];
340 let mut data = vec![i as u8; 2000];
341 flag.append(&mut data);
342 client1
343 .send(&flag, Reliability::ReliableOrdered)
344 .await
345 .unwrap();
346 }
347
348 for i in 0..10 {
349 let mut flag = vec![0xfe_u8];
350 let mut data = vec![i as u8; 2000];
351 flag.append(&mut data);
352 let data = client1.recv().await.unwrap();
353 assert!(data == flag);
354 }
355 notify2.notified().await;
356 });
357 let mut client2 = RaknetSocket::connect(&local_addr).await.unwrap();
358 client2.set_loss_rate(8);
360
361 for i in 0..10 {
362 let mut flag = vec![0xfe_u8];
363 let mut data = vec![i as u8; 2000];
364 flag.append(&mut data);
365 client2
366 .send(&flag, Reliability::ReliableOrdered)
367 .await
368 .unwrap();
369 }
370
371 for i in 0..10 {
372 let mut flag = vec![0xfe_u8];
373 let mut data = vec![i as u8; 2000];
374 flag.append(&mut data);
375 let data = client2.recv().await.unwrap();
376 assert!(data == flag);
377 }
378 notify.notify_one();
379}
380
381#[tokio::test]
382async fn test_loss_packet_with_sequenced() {
383 let notify = std::sync::Arc::new(tokio::sync::Notify::new());
384 let notify2 = notify.clone();
385 let mut server = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
386 .await
387 .unwrap();
388 let local_addr = server.local_addr().unwrap();
389 server.listen().await;
390 tokio::spawn(async move {
391 let mut client1 = server.accept().await.unwrap();
392 client1.set_loss_rate(8);
394
395 for i in 0..100 {
396 let mut flag = vec![0xfe_u8];
397 let mut data = vec![i as u8; 20];
398 flag.append(&mut data);
399 client1
400 .send(&flag, Reliability::ReliableSequenced)
401 .await
402 .unwrap();
403 }
404
405 let mut last = 0;
406 for i in 0..50 {
407 let mut flag = vec![0xfe_u8];
408 let mut data = vec![i as u8; 20];
409 flag.append(&mut data);
410 let data = client1.recv().await.unwrap();
411 assert!(data[1] >= last);
412 last = data[1];
413 }
414 notify2.notified().await;
415 });
416 let mut client2 = RaknetSocket::connect(&local_addr).await.unwrap();
417 client2.set_loss_rate(8);
419
420 for i in 0..100 {
421 let mut flag = vec![0xfe_u8];
422 let mut data = vec![i as u8; 20];
423 flag.append(&mut data);
424 client2
425 .send(&flag, Reliability::ReliableSequenced)
426 .await
427 .unwrap();
428 }
429
430 let mut last = 0;
431 for i in 0..50 {
432 let mut flag = vec![0xfe_u8];
433 let mut data = vec![i as u8; 20];
434 flag.append(&mut data);
435 let data = client2.recv().await.unwrap();
436 assert!(data[1] >= last);
437 last = data[1];
438 }
439 notify.notify_one();
440}
441
442#[tokio::test]
443async fn test_raknet_server_close() {
444 for _ in 0..10 {
445 let mut server = RaknetListener::bind(&"127.0.0.1:19132".parse().unwrap())
446 .await
447 .unwrap();
448 server.listen().await;
449 let client = RaknetSocket::connect(&"127.0.0.1:19132".parse().unwrap())
450 .await
451 .unwrap();
452 let mut a = vec![3u8; 1000];
453 let mut b = vec![2u8; 1000];
454 let mut c = vec![0xfe; 1000];
455 b.append(&mut a);
456 c.append(&mut b);
457 client.send(&c, Reliability::ReliableOrdered).await.unwrap();
458
459 let client2 = server.accept().await.unwrap();
460 let buf = client2.recv().await.unwrap();
461 assert!(buf.len() == 3000);
462 assert!(buf[0..1000] == vec![0xfe; 1000]);
463 assert!(buf[1000..2000] == vec![2u8; 1000]);
464 assert!(buf[2000..3000] == vec![3u8; 1000]);
465
466 server.close().await.unwrap();
467 client.close().await.unwrap();
468 }
469
470 let mut server1 = RaknetListener::bind(&"127.0.0.1:19132".parse().unwrap())
471 .await
472 .unwrap();
473 server1.listen().await;
474 server1.close().await.unwrap();
475 let mut server2 = RaknetListener::bind(&"127.0.0.1:19132".parse().unwrap())
476 .await
477 .unwrap();
478 server2.listen().await;
479}
480
481#[tokio::test]
482async fn test_send_recv_full_packet() {
483 let mut server = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
484 .await
485 .unwrap();
486 let remote_addr = format!("127.0.0.1:{}", server.local_addr().unwrap().port());
487 tokio::spawn(async move {
488 server.listen().await;
489 let client = server.accept().await.unwrap();
490
491 for _ in 0..50 {
492 client
493 .send(&vec![0xfe; 1000], Reliability::ReliableSequenced)
494 .await
495 .unwrap();
496 }
497 client.flush().await.unwrap();
498
499 client.close().await.unwrap();
500 server.close().await.unwrap();
501 });
502
503 let client = RaknetSocket::connect(&remote_addr.parse().unwrap())
504 .await
505 .unwrap();
506
507 for _ in 0..50 {
508 let buf = client.recv().await.unwrap();
509 assert!(buf == [0xfe; 1000]);
510 }
511}
512
513#[tokio::test]
514async fn test_send_recv_with_flush() {
515 let mut server = RaknetListener::bind(&"127.0.0.1:0".parse().unwrap())
516 .await
517 .unwrap();
518 let remote_addr = format!("127.0.0.1:{}", server.local_addr().unwrap().port());
519 let s1 = std::sync::Arc::new(tokio::sync::Semaphore::new(1));
520 let s2 = s1.clone();
521 tokio::spawn(async move {
522 server.listen().await;
523 let client = server.accept().await.unwrap();
524
525 for _ in 0..50 {
526 #[allow(unused_must_use)]
527 {
528 s1.acquire().await.unwrap();
529 }
530 client
531 .send(&vec![0xfe; 1000], Reliability::ReliableSequenced)
532 .await
533 .unwrap();
534 client.flush().await.unwrap();
535 }
536
537 client.close().await.unwrap();
538 for _ in 0..5 {
539 client
540 .send(&vec![0xfe; 1000], Reliability::ReliableSequenced)
541 .await
542 .unwrap();
543 match match client.flush().await {
544 Ok(_) => panic!("incorrect return"),
545 Err(e) => e,
546 } {
547 error::RaknetError::ConnectionClosed => {}
548 _ => panic!("incorrect return"),
549 };
550 }
551 server.close().await.unwrap();
552 });
553
554 let client = RaknetSocket::connect(&remote_addr.parse().unwrap())
555 .await
556 .unwrap();
557
558 for _ in 0..50 {
559 let buf = client.recv().await.unwrap();
560 assert!(buf == [0xfe; 1000]);
561 s2.add_permits(1);
562 }
563}
564
565