Added TCP/IP support

Closes #2
This commit is contained in:
techmetx11 2024-07-21 16:28:45 +01:00
parent 115c690416
commit 369cfb335f
No known key found for this signature in database
GPG Key ID: E60B63635FF4E062
2 changed files with 48 additions and 47 deletions

View File

@ -2,6 +2,8 @@ use lazy_regex::{regex, Lazy};
use regex::Regex; use regex::Regex;
pub static DEFAULT_SOCK_PATH: &str = "/tmp/inv_sig_helper.sock"; pub static DEFAULT_SOCK_PATH: &str = "/tmp/inv_sig_helper.sock";
pub static DEFAULT_TCP_URL: &str = "0.0.0.0:12999";
pub static TEST_YOUTUBE_VIDEO: &str = "https://www.youtube.com/watch?v=jNQXAC9IVRw"; pub static TEST_YOUTUBE_VIDEO: &str = "https://www.youtube.com/watch?v=jNQXAC9IVRw";
pub static REGEX_PLAYER_ID: &Lazy<Regex> = regex!("\\/s\\/player\\/([0-9a-f]{8})"); pub static REGEX_PLAYER_ID: &Lazy<Regex> = regex!("\\/s\\/player\\/([0-9a-f]{8})");

View File

@ -4,14 +4,15 @@ mod opcode;
mod player; mod player;
use ::futures::StreamExt; use ::futures::StreamExt;
use consts::DEFAULT_SOCK_PATH; use consts::{DEFAULT_SOCK_PATH, DEFAULT_TCP_URL};
use jobs::{process_decrypt_n_signature, process_fetch_update, GlobalState, JobOpcode}; use jobs::{process_decrypt_n_signature, process_fetch_update, GlobalState, JobOpcode};
use opcode::OpcodeDecoder; use opcode::OpcodeDecoder;
use player::fetch_update; use player::fetch_update;
use std::{env::args, sync::Arc}; use std::{env::args, sync::Arc};
use tokio::{ use tokio::{
fs::remove_file, fs::remove_file,
net::{UnixListener, UnixStream}, io::{AsyncReadExt, AsyncWrite},
net::{TcpListener, UnixListener},
sync::Mutex, sync::Mutex,
}; };
use tokio_util::codec::Framed; use tokio_util::codec::Framed;
@ -21,30 +22,25 @@ use crate::jobs::{
process_player_update_timestamp, process_player_update_timestamp,
}; };
macro_rules! break_fail { macro_rules! loop_main {
($res:expr) => { ($i:ident, $s:ident) => {
match $res { println!("Fetching player");
Ok(value) => value, match fetch_update($s.clone()).await {
Err(e) => { Ok(()) => println!("Successfully fetched player"),
println!("An error occurred while parsing the current request: {}", e); Err(x) => {
break; println!("Error occured while trying to fetch the player: {:?}", x);
} }
} }
loop {
let (socket, _addr) = $i.accept().await.unwrap();
let cloned_state = $s.clone();
tokio::spawn(async move {
process_socket(cloned_state, socket).await;
});
}
}; };
} }
macro_rules! eof_fail {
($res:expr, $stream:ident) => {
match $res {
Ok(value) => value,
Err(e) => {
println!("An error occurred while parsing the current request: {}", e);
break;
}
}
};
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let args: Vec<String> = args().collect(); let args: Vec<String> = args().collect();
@ -56,37 +52,40 @@ async fn main() {
// have to please rust // have to please rust
let state: Arc<GlobalState> = Arc::new(GlobalState::new()); let state: Arc<GlobalState> = Arc::new(GlobalState::new());
let socket: UnixListener = match UnixListener::bind(socket_url) { if socket_url == "--tcp" {
Ok(x) => x, let socket_tcp_url: &str = match args.get(2) {
Err(x) => { Some(stringref) => stringref,
if x.kind() == std::io::ErrorKind::AddrInUse { None => DEFAULT_TCP_URL,
remove_file(socket_url).await; };
UnixListener::bind(socket_url).unwrap() let tcp_socket = match TcpListener::bind(socket_tcp_url).await {
} else { Ok(x) => x,
Err(x) => {
println!("Error occurred while trying to bind: {}", x); println!("Error occurred while trying to bind: {}", x);
return; return;
} }
} };
}; loop_main!(tcp_socket, state);
} else {
println!("Fetching player"); let unix_socket = match UnixListener::bind(socket_url) {
match fetch_update(state.clone()).await { Ok(x) => x,
Ok(()) => println!("Successfully fetched player"), Err(x) => {
Err(x) => { if x.kind() == std::io::ErrorKind::AddrInUse {
println!("Error occured while trying to fetch the player: {:?}", x); remove_file(socket_url).await;
} UnixListener::bind(socket_url).unwrap()
} } else {
loop { println!("Error occurred while trying to bind: {}", x);
let (socket, _addr) = socket.accept().await.unwrap(); return;
}
let cloned_state = state.clone(); }
tokio::spawn(async move { };
process_socket(cloned_state, socket).await; loop_main!(unix_socket, state);
});
} }
} }
async fn process_socket(state: Arc<GlobalState>, socket: UnixStream) { async fn process_socket<W>(state: Arc<GlobalState>, socket: W)
where
W: AsyncReadExt + Send + AsyncWrite + 'static,
{
let decoder = OpcodeDecoder {}; let decoder = OpcodeDecoder {};
let str = Framed::new(socket, decoder); let str = Framed::new(socket, decoder);