Change from Frame to ClientFrames and ServerFrames

This commit is contained in:
Bastian Gruber 2023-05-08 12:25:09 +02:00
parent 2902d9d19c
commit 0233e3196d
No known key found for this signature in database
GPG key ID: BE9F8C772B188CBF
4 changed files with 48 additions and 34 deletions

View file

@ -1,4 +1,4 @@
use crate::frame::{self, Frame};
use crate::frame::{self, ClientFrames, ServerFrames};
use bytes::{Buf, BytesMut};
use std::io::Cursor;
@ -20,7 +20,7 @@ impl Connection {
}
}
pub async fn read_frame(&mut self) -> crate::Result<Option<Frame>> {
pub async fn read_frame(&mut self) -> crate::Result<Option<ClientFrames>> {
loop {
info!("Loop read_frame");
if let Some(frame) = self.parse_frame()? {
@ -38,20 +38,20 @@ impl Connection {
}
}
fn parse_frame(&mut self) -> crate::Result<Option<Frame>> {
fn parse_frame(&mut self) -> crate::Result<Option<ClientFrames>> {
use frame::Error::Incomplete;
let mut buf = Cursor::new(&self.buffer[..]);
debug!(?buf);
match Frame::check(&mut buf) {
match ClientFrames::check(&mut buf) {
Ok(_) => {
info!("Frame::check succesful");
let len = buf.position() as usize;
debug!(?len);
buf.set_position(0);
let frame = Frame::parse(&mut buf)?;
let frame = ClientFrames::parse(&mut buf)?;
self.buffer.advance(len);
Ok(Some(frame))
@ -61,7 +61,7 @@ impl Connection {
}
}
pub async fn write_frame(&mut self, frame: &Frame) -> tokio::io::Result<()> {
pub async fn write_frame(&mut self, frame: &ServerFrames) -> tokio::io::Result<()> {
unimplemented!()
}
}

View file

@ -6,28 +6,44 @@ use std::string::FromUtf8Error;
use tracing::{debug, error};
#[derive(Clone, Debug)]
pub enum Frame {
Error { msg: String },
pub enum ClientFrames {
Plate { plate: String, timestamp: u32 },
WantHeartbeat { interval: u32 },
IAmCamera { road: u16, mile: u16, limit: u16 },
IAmDispatcher { roads: Vec<u16> },
}
#[derive(Clone, Debug)]
pub enum ServerFrames {
Error {
msg: String,
},
Ticket {
plate: String,
road: u16,
mile1: u16,
timestamp1: u16,
mile2: u16,
timestamp2: u16,
speed: u16,
},
Heartbeat,
}
#[derive(Debug)]
pub enum Error {
Incomplete,
Other(crate::Error),
}
impl Frame {
impl ClientFrames {
pub fn check(src: &mut Cursor<&[u8]>) -> Result<(), Error> {
match get_u8(src)? {
// Error: msg: str
0x10 => {
let n = get_length(src)?;
skip(src, n as usize)
}
// Error: msg: str (Server -> Client)
// 0x10 => {
// let n = get_length(src)?;
// skip(src, n as usize)
// }
// Plate: plate: str, timestamp: u32
0x20 => {
// Read length character of the plate string
@ -73,14 +89,14 @@ impl Frame {
}
}
pub fn parse(src: &mut Cursor<&[u8]>) -> Result<Frame, Error> {
pub fn parse(src: &mut Cursor<&[u8]>) -> Result<ClientFrames, Error> {
match get_u8(src)? {
// Error: msg: str
0x10 => {
let n = get_length(src)?;
let msg = get_str(src, n)?.to_string();
Ok(Frame::Error { msg })
}
// Error: msg: str (Server -> Client)
// 0x10 => {
// let n = get_length(src)?;
// let msg = get_str(src, n)?.to_string();
// Ok(Frame::Error { msg })
// }
// Plate: plate: str, timestamp: u32
0x20 => {
// Read length character of the plate string
@ -89,7 +105,7 @@ impl Frame {
let plate = get_str(src, n)?.to_string();
// check if valid timestamp
let timestamp = get_u32(src)?;
Ok(Frame::Plate { plate, timestamp })
Ok(ClientFrames::Plate { plate, timestamp })
}
// Ticket (just Server -> Client)
// 0x21 => {
@ -98,7 +114,7 @@ impl Frame {
// Want Heartbeat: interval: u32
0x40 => {
let interval = get_u32(src)?;
Ok(Frame::WantHeartbeat { interval })
Ok(ClientFrames::WantHeartbeat { interval })
}
// Heartbeat (just Server -> Client)
// 0x41 => {
@ -112,7 +128,7 @@ impl Frame {
let mile = get_u16(src)?;
// limit
let limit = get_u16(src)?;
Ok(Frame::IAmCamera { road, mile, limit })
Ok(ClientFrames::IAmCamera { road, mile, limit })
}
// IAmDispatcher: numroads: u8, roads: [u16]
0x81 => {
@ -121,7 +137,7 @@ impl Frame {
// roads
let roads = get_u16_vec(src, numroads as usize)?;
Ok(Frame::IAmDispatcher { roads })
Ok(ClientFrames::IAmDispatcher { roads })
}
actual => Err(format!("protocol error; invalid frame type byte `{}`", actual).into()),
}

View file

@ -2,7 +2,7 @@ mod connection;
pub use connection::Connection;
pub mod frame;
pub use frame::Frame;
pub use frame::ClientFrames;
pub mod server;

View file

@ -1,4 +1,4 @@
use crate::{frame::Frame, Connection, Shutdown};
use crate::{frame::ClientFrames, Connection, Shutdown};
use std::future::Future;
use std::sync::Arc;
@ -8,6 +8,7 @@ use tokio::time::{self, Duration};
use tracing::{debug, error, info};
struct Listener {
db_holder: DbDropGuard,
listener: TcpListener,
limit_connections: Arc<Semaphore>,
notify_shutdown: broadcast::Sender<()>,
@ -129,19 +130,16 @@ impl Handler {
};
match frame {
Frame::Error { msg } => {
info!("Error message: {msg}")
}
Frame::Plate { plate, timestamp } => {
ClientFrames::Plate { plate, timestamp } => {
info!("Plate: {plate}, timestamp: {timestamp}");
}
Frame::WantHeartbeat { interval } => {
ClientFrames::WantHeartbeat { interval } => {
info!("Want heartbeat: {interval}");
}
Frame::IAmCamera { road, mile, limit } => {
ClientFrames::IAmCamera { road, mile, limit } => {
info!("Road: {road}, mile: {mile}, limit: {limit}");
}
Frame::IAmDispatcher { roads } => {
ClientFrames::IAmDispatcher { roads } => {
info!("roads: {roads:?}");
}
}