Skip to content

Commit

Permalink
Reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
fabi321 committed May 30, 2024
1 parent ddf9e16 commit 3ca731b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 31 deletions.
4 changes: 2 additions & 2 deletions breakwater-parser/benches/parsing.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{sync::Arc, time::Duration};
use std::sync::mpsc::channel;
use std::{sync::Arc, time::Duration};

use breakwater_core::{framebuffer::FrameBuffer};
use breakwater_core::framebuffer::FrameBuffer;
#[cfg(target_arch = "x86_64")]
use breakwater_parser::assembler::AssemblerParser;
use breakwater_parser::{
Expand Down
6 changes: 4 additions & 2 deletions breakwater-parser/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Needed for simple implementation
#![feature(portable_simd)]

use std::sync::mpsc::Sender;
use enum_dispatch::enum_dispatch;
use snafu::Snafu;
use std::sync::mpsc::Sender;

#[cfg(target_arch = "x86_64")]
pub mod assembler;
Expand All @@ -14,7 +14,9 @@ pub mod refactored;
#[derive(Debug, Snafu)]
pub enum ParserError {
#[snafu(display("Failed to write to TCP socket"))]
WriteToTcpSocket { source: std::sync::mpsc::SendError<Box<[u8]>> },
WriteToTcpSocket {
source: std::sync::mpsc::SendError<Box<[u8]>>,
},
}

#[enum_dispatch(ParserImplementation)]
Expand Down
2 changes: 1 addition & 1 deletion breakwater-parser/src/memchr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::sync::Arc;
use std::sync::mpsc::Sender;
use std::sync::Arc;

use breakwater_core::framebuffer::FrameBuffer;

Expand Down
33 changes: 20 additions & 13 deletions breakwater-parser/src/original.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::sync::mpsc::Sender;
use std::{
simd::{num::SimdUint, u32x8, Simd},
sync::Arc,
};
use std::sync::mpsc::Sender;

use breakwater_core::{framebuffer::FrameBuffer, HELP_TEXT};

Expand Down Expand Up @@ -130,15 +130,19 @@ impl Parser for OriginalParser {
last_byte_parsed = i;
i += 1;
if let Some(rgb) = self.fb.get(x, y) {
message_sender.send(
format!(
"PX {} {} {:06x}\n",
// We don't want to return the actual (absolute) coordinates, the client should also get the result offseted
x - self.connection_x_offset,
y - self.connection_y_offset,
rgb.to_be() >> 8
).into_boxed_str().into_boxed_bytes()
).expect("Failed to write message");
message_sender
.send(
format!(
"PX {} {} {:06x}\n",
// We don't want to return the actual (absolute) coordinates, the client should also get the result offseted
x - self.connection_x_offset,
y - self.connection_y_offset,
rgb.to_be() >> 8
)
.into_boxed_str()
.into_boxed_bytes(),
)
.expect("Failed to write message");
}
continue;
}
Expand All @@ -159,17 +163,20 @@ impl Parser for OriginalParser {
i += 4;
last_byte_parsed = i - 1;

message_sender.send(
message_sender
.send(
format!("SIZE {} {}\n", self.fb.get_width(), self.fb.get_height())
.into_boxed_str().into_boxed_bytes()
.into_boxed_str()
.into_boxed_bytes(),
)
.expect("Failed to write message");
continue;
} else if current_command & 0xffff_ffff == HELP_PATTERN {
i += 4;
last_byte_parsed = i - 1;

message_sender.send(HELP_TEXT.into())
message_sender
.send(HELP_TEXT.into())
.expect("Failed to write message");
continue;
}
Expand Down
17 changes: 7 additions & 10 deletions breakwater-parser/src/refactored.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,19 @@ impl RefactoredParser {
}

#[inline(always)]
fn handle_size(
&self,
message_sender: &Sender<Box<[u8]>>,
) -> Result<(), ParserError> {
fn handle_size(&self, message_sender: &Sender<Box<[u8]>>) -> Result<(), ParserError> {
message_sender
.send(
format!("SIZE {} {}\n", self.fb.get_width(), self.fb.get_height()).into_boxed_str().into_boxed_bytes(),
format!("SIZE {} {}\n", self.fb.get_width(), self.fb.get_height())
.into_boxed_str()
.into_boxed_bytes(),
)
.context(crate::WriteToTcpSocketSnafu)?;
Ok(())
}

#[inline(always)]
fn handle_help(
&self,
message_sender: &Sender<Box<[u8]>>,
) -> Result<(), ParserError> {
fn handle_help(&self, message_sender: &Sender<Box<[u8]>>) -> Result<(), ParserError> {
message_sender
.send(HELP_TEXT.into())
.context(crate::WriteToTcpSocketSnafu)?;
Expand Down Expand Up @@ -187,7 +183,8 @@ impl RefactoredParser {
y - self.connection_y_offset,
rgb.to_be() >> 8
)
.into_boxed_str().into_boxed_bytes(),
.into_boxed_str()
.into_boxed_bytes(),
)
.context(crate::WriteToTcpSocketSnafu)?;
}
Expand Down
9 changes: 6 additions & 3 deletions breakwater/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::{cmp::min, net::IpAddr, sync::Arc, time::Duration};
use std::sync::mpsc::channel;
use std::{cmp::min, net::IpAddr, sync::Arc, time::Duration};

use breakwater_core::framebuffer::FrameBuffer;
use breakwater_parser::{original::OriginalParser, Parser, ParserError};
Expand Down Expand Up @@ -156,7 +156,7 @@ pub async fn handle_connection(
// Instead we bulk the statistics and send them pre-aggregated.
let mut last_statistics = Instant::now();
let mut statistics_bytes_read: u64 = 0;

let (message_sender, message_receiver) = channel::<Box<[u8]>>();

loop {
Expand Down Expand Up @@ -227,7 +227,10 @@ pub async fn handle_connection(
}

while let Ok(message) = message_receiver.try_recv() {
stream.write_all(message.as_ref()).await.expect("Failed to write to tcp stream")
stream
.write_all(message.as_ref())
.await
.expect("Failed to write to tcp stream")
}
}
}
Expand Down

0 comments on commit 3ca731b

Please sign in to comment.