added socket message parsing
This commit is contained in:
parent
4530cf45a8
commit
2278b4cd79
1 changed files with 34 additions and 6 deletions
|
@ -1,9 +1,8 @@
|
||||||
use std::{
|
use std::{
|
||||||
net::SocketAddr,
|
net::SocketAddr, sync::{
|
||||||
sync::{
|
|
||||||
atomic::{AtomicBool, Ordering},
|
atomic::{AtomicBool, Ordering},
|
||||||
Arc,
|
Arc,
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
use async_channel::Sender;
|
use async_channel::Sender;
|
||||||
|
@ -20,6 +19,9 @@ use tokio_tungstenite::{
|
||||||
|
|
||||||
use crate::coordinator::{ApplicationEvent, MoveEvent};
|
use crate::coordinator::{ApplicationEvent, MoveEvent};
|
||||||
|
|
||||||
|
const FRAME_WIDTH: u32 = 640;
|
||||||
|
const FRAME_HEIGHT: u32 = 480;
|
||||||
|
|
||||||
async fn accept_connection(peer: SocketAddr, stream: TcpStream, mec: Sender<ApplicationEvent>) {
|
async fn accept_connection(peer: SocketAddr, stream: TcpStream, mec: Sender<ApplicationEvent>) {
|
||||||
if let Err(e) = handle_connection(peer, stream, mec.clone()).await {
|
if let Err(e) = handle_connection(peer, stream, mec.clone()).await {
|
||||||
match e {
|
match e {
|
||||||
|
@ -41,9 +43,17 @@ async fn handle_connection(
|
||||||
while let Some(msg) = ws_stream.next().await {
|
while let Some(msg) = ws_stream.next().await {
|
||||||
let msg = msg?;
|
let msg = msg?;
|
||||||
if msg.is_text() {
|
if msg.is_text() {
|
||||||
|
let (x_off, y_off) = match process_incoming_string(msg.to_string()) {
|
||||||
|
Ok(val) => val,
|
||||||
|
Err(e) => {
|
||||||
|
error!("{e}");
|
||||||
|
(0, 0)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if let Err(e) = mec
|
if let Err(e) = mec
|
||||||
.send(ApplicationEvent::MoveEvent(
|
.send(ApplicationEvent::MoveEvent(
|
||||||
process_incoming_string(msg.to_string()),
|
MoveEvent {x: x_off, y: y_off},
|
||||||
5,
|
5,
|
||||||
))
|
))
|
||||||
.await
|
.await
|
||||||
|
@ -57,8 +67,26 @@ async fn handle_connection(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_incoming_string(message: String) -> MoveEvent {
|
fn process_incoming_string(message: String) -> core::result::Result<(i32, i32), String> {
|
||||||
return MoveEvent { x: 10, y: 30 };
|
let coords: Vec<&str> = message.split(',').collect();
|
||||||
|
if coords.len() != 4 {
|
||||||
|
return Err("Invalid socket input format".to_string());
|
||||||
|
}
|
||||||
|
let x1: u32 = coords[0].parse().map_err(|_| "Invalid x coordinate")?;
|
||||||
|
let y1: u32 = coords[1].parse().map_err(|_| "Invalid y coordinate")?;
|
||||||
|
|
||||||
|
let coords2: Vec<&str> = coords[2].split(':').collect();
|
||||||
|
if coords2.len() != 2 {
|
||||||
|
return Err("Invalid socket input format".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
let x2: u32 = coords2[0].parse().map_err(|_| "Invalid width")?;
|
||||||
|
let y2: u32= coords2[1].parse().map_err(|_| "Invalid height")?;
|
||||||
|
|
||||||
|
let x_adjust = (FRAME_WIDTH - x2) as i32 - x1 as i32;
|
||||||
|
let y_adjust = (FRAME_HEIGHT - y2) as i32 - y1 as i32;
|
||||||
|
|
||||||
|
return Ok((x_adjust, y_adjust));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn start_socketserver(
|
pub async fn start_socketserver(
|
||||||
|
|
Loading…
Reference in a new issue