victorem/entities.rs
1use serde_derive::{Deserialize, Serialize};
2use std::error::Error;
3use std::fmt::Display;
4use std::fmt::Formatter;
5use std::io;
6use std::time::Duration;
7
8#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
9pub struct CommandPacket {
10 pub protocol_id: u8,
11 pub protocol_version: u8,
12 pub id: u32,
13 pub command: Vec<u8>,
14 pub session_key: Duration,
15}
16
17#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
18pub struct StatePacket {
19 pub protocol_id: u8,
20 pub protocol_version: u8,
21 pub id: u32,
22 pub state: Vec<u8>,
23 pub session_key: Duration,
24 pub last_received: u32,
25 pub sequence: u32
26}
27
28#[derive(Debug)]
29///Error in framework
30pub enum Exception {
31 ///Error on send or recv from UDP
32 /// If it kind is [`std::io::ErrorKind::WouldBlock`] then retry again.
33 IoError(io::Error),
34 /// Different lib version on client and server.
35 /// You must update client and server.
36 BadProtocolVersion,
37 ///Error on serialize or deserialize
38 BincodeError(bincode::Error),
39 /// Not ordered command or state received by this reason it was skipped.
40 /// Maybe it is duplicated.
41 /// Retry again.
42 NotOrderedPacketError,
43 ///Packet not from this lib.
44 /// Lib ignoring it.
45 /// Retry again.
46 NotValidIdError,
47}
48
49impl Error for Exception {}
50
51impl Display for Exception {
52 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
53 match self {
54 Exception::BadProtocolVersion => write!(f, "Different lib version on client and server. You must update client and server."),
55 Exception::NotOrderedPacketError => write!(f, "Not ordered command or state received by this reason it was skipped. Maybe it is duplicated. Retry again."),
56 Exception::NotValidIdError => write!(f, "Packet not from this lib. Lib ignoring it. Retry again."),
57 _ => write!(f, "{:#?}", self),
58 }
59 }
60}
61
62impl std::convert::From<std::io::Error> for Exception {
63 fn from(err: std::io::Error) -> Self {
64 Exception::IoError(err)
65 }
66}
67
68impl std::convert::From<bincode::Error> for Exception {
69 fn from(err: bincode::Error) -> Self {
70 Exception::BincodeError(err)
71 }
72}
73//#[derive(Debug)]
74//pub struct LoggerMonad<T>(Result<T, Exception>);
75//
76//impl<T> LoggerMonad<T> {
77// pub fn new(value: Result<T, Exception>) -> LoggerMonad<T> {
78// LoggerMonad(value)
79// }
80//
81// pub fn and_then<U, F: FnOnce(T) -> LoggerMonad<U>>(self, f: F) -> LoggerMonad<U> {
82// match self.0 {
83// Ok(x) => {
84// let monad = f(x);
85// match &monad.0 {
86// Ok(_) => monad,
87// Err(e) => {
88// eprintln!("{:#?}", e);
89// monad
90// }
91// }
92// }
93// Err(e) => LoggerMonad::new(Err(e)),
94// }
95// }
96//
97// pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> LoggerMonad<U> {
98// self.and_then(|x| LoggerMonad::new(Ok(f(x))))
99// }
100//
101// pub fn and<U>(self, data: LoggerMonad<U>) -> LoggerMonad<U> {
102// self.and_then(|_| data)
103// }
104//
105// pub fn unwrap(self) -> T {
106// self.0.unwrap()
107// }
108//
109// pub fn unwrap_or(self, def: T) -> T {
110// self.0.unwrap_or(def)
111// }
112//}