use serde_derive::{Deserialize, Serialize};
use std::error::Error;
use std::fmt::Display;
use std::fmt::Formatter;
use std::io;
use std::time::Duration;
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct CommandPacket {
pub protocol_id: u8,
pub protocol_version: u8,
pub id: u32,
pub command: Vec<u8>,
pub session_key: Duration,
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct StatePacket {
pub protocol_id: u8,
pub protocol_version: u8,
pub id: u32,
pub state: Vec<u8>,
pub session_key: Duration,
pub last_received: u32,
pub sequence: u32
}
#[derive(Debug)]
pub enum Exception {
IoError(io::Error),
BadProtocolVersion,
BincodeError(bincode::Error),
NotOrderedPacketError,
NotValidIdError,
}
impl Error for Exception {}
impl Display for Exception {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
match self {
Exception::BadProtocolVersion => write!(f, "Different lib version on client and server. You must update client and server."),
Exception::NotOrderedPacketError => write!(f, "Not ordered command or state received by this reason it was skipped. Maybe it is duplicated. Retry again."),
Exception::NotValidIdError => write!(f, "Packet not from this lib. Lib ignoring it. Retry again."),
_ => write!(f, "{:#?}", self),
}
}
}
impl std::convert::From<std::io::Error> for Exception {
fn from(err: std::io::Error) -> Self {
Exception::IoError(err)
}
}
impl std::convert::From<bincode::Error> for Exception {
fn from(err: bincode::Error) -> Self {
Exception::BincodeError(err)
}
}