feat: improve get_pixel performance
This commit is contained in:
parent
0978664188
commit
f0fca8182f
3 changed files with 49 additions and 43 deletions
|
|
@ -11,6 +11,7 @@ const SET_PX_RGB_BIN: u8 = 128;
|
|||
const SET_PX_RGBA_BIN: u8 = 129;
|
||||
const SET_PX_W_BIN: u8 = 130;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct BinaryParser {}
|
||||
|
||||
impl BinaryParser {
|
||||
|
|
@ -97,21 +98,27 @@ impl IOProtocol for BinaryParser {
|
|||
|
||||
impl<W: AsyncWriteExt + std::marker::Unpin> Responder<W> for BinaryParser {
|
||||
async fn unparse(&self, response: Response, writer: &mut W) -> io::Result<()> {
|
||||
let help_text = format!(
|
||||
"
|
||||
match response {
|
||||
Response::Help => {
|
||||
let help_text = format!(
|
||||
"
|
||||
You found the binary protocol help text
|
||||
you can get this by sending ({HELP_BIN:02X}) to the server
|
||||
To get the size of a canvas, send ({SIZE_BIN:02X}) (u8 canvas) to the server
|
||||
To set a pixel using RGB, use ({SET_PX_RGB_BIN:02X}) (u8 canvas) (x as u16_le) (y as u16_le) (u8 r) (u8 g) (u8 b)
|
||||
",
|
||||
);
|
||||
match response {
|
||||
Response::Help => writer.write_all(help_text.as_bytes()).await,
|
||||
);
|
||||
writer.write_all(help_text.as_bytes()).await
|
||||
}
|
||||
Response::Size(x, y) => {
|
||||
writer.write_u16_le(x).await?;
|
||||
writer.write_u16_le(y).await
|
||||
}
|
||||
Response::GetPixel(_, _, c) => writer.write_all(&c).await,
|
||||
Response::GetPixel(_, _, c) => {
|
||||
writer.write_u8(c[0]).await?;
|
||||
writer.write_u8(c[1]).await?;
|
||||
writer.write_u8(c[2]).await
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
72
src/main.rs
72
src/main.rs
|
|
@ -127,6 +127,7 @@ async fn listen_handle() -> io::Result<()> {
|
|||
macro_rules! build_parser_type_enum {
|
||||
($($name:ident: $t:ty,)*) => {
|
||||
|
||||
#[derive(Clone)]
|
||||
enum ParserTypes {
|
||||
$($name($t),)*
|
||||
}
|
||||
|
|
@ -192,9 +193,9 @@ where
|
|||
None => return Err(Error::from(ErrorKind::InvalidInput)),
|
||||
Some(color) => color.to_be_bytes(),
|
||||
};
|
||||
match_parser!(parser: self.parser => parser.unparse(Response::GetPixel(x,y,[color[0], color[1], color[2]]), &mut self.writer).await?);
|
||||
|
||||
self.writer.flush().await?;
|
||||
match_parser!(parser: self.parser => parser.unparse(
|
||||
Response::GetPixel(x,y,[color[0], color[1], color[2]]), &mut self.writer).await?
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -231,43 +232,40 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
async fn parse_command(&mut self, command: Result<Command, std::io::Error>) -> io::Result<()> {
|
||||
match command {
|
||||
Ok(Command::Help) => self.help_command().await,
|
||||
Ok(Command::Size(canvas)) => self.size_command(canvas).await,
|
||||
Ok(Command::GetPixel(canvas, x, y)) => self.get_pixel_command(canvas, x, y).await,
|
||||
Ok(Command::SetPixel(canvas, x, y, color)) => {
|
||||
self.set_pixel_command(canvas, x, y, &color);
|
||||
Ok(())
|
||||
}
|
||||
Ok(Command::ChangeCanvas(canvas)) => self.change_canvas_command(canvas),
|
||||
Ok(Command::ChangeProtocol(protocol)) => {
|
||||
self.change_protocol(&protocol);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Err(err) if err.kind() == ErrorKind::UnexpectedEof => {
|
||||
return Ok(());
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn process_socket(&mut self) -> io::Result<()> {
|
||||
loop {
|
||||
for _ in 0..1000 {
|
||||
let parsed = match &self.parser {
|
||||
ParserTypes::TextParser(parser) => parser.parse(&mut self.reader).await,
|
||||
ParserTypes::BinaryParser(parser) => parser.parse(&mut self.reader).await,
|
||||
};
|
||||
match_parser!(parser: &self.parser.clone() => 'outer: loop {
|
||||
for _ in 0..1000 {
|
||||
let parsed = parser.parse(&mut self.reader).await;
|
||||
match parsed {
|
||||
Ok(Command::Help) => self.help_command().await?,
|
||||
Ok(Command::Size(canvas)) => self.size_command(canvas).await?,
|
||||
Ok(Command::GetPixel(canvas, x, y)) => {
|
||||
self.get_pixel_command(canvas, x, y).await?
|
||||
}
|
||||
Ok(Command::SetPixel(canvas, x, y, color)) => {
|
||||
self.set_pixel_command(canvas, x, y, &color);
|
||||
}
|
||||
Ok(Command::ChangeCanvas(canvas)) => {
|
||||
self.change_canvas_command(canvas)?;
|
||||
break 'outer;
|
||||
}
|
||||
Ok(Command::ChangeProtocol(protocol)) => {
|
||||
self.change_protocol(&protocol);
|
||||
break 'outer;
|
||||
}
|
||||
|
||||
self.parse_command(parsed).await?;
|
||||
}
|
||||
|
||||
increment_counter(self.counter);
|
||||
self.counter = 0;
|
||||
Err(err) if err.kind() == ErrorKind::UnexpectedEof => {
|
||||
return Ok(());
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
increment_counter(self.counter);
|
||||
self.counter = 0;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ use crate::{
|
|||
GRID_LENGTH, HELP_TEXT,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TextParser {
|
||||
canvas: Canvas,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue