From f1a452f2edf653fe8937d4feb9e30eb125159cd0 Mon Sep 17 00:00:00 2001 From: VerbenaIDK Date: Thu, 19 Dec 2024 15:12:08 -0300 Subject: [PATCH] Add networking things Simple networking things - Internal protocol for networking thread (code that determines what should be done with the recieved data) - Networking thread (the meat of the thing) - Functions to kill, debug and send data to the thread - That's all I think To use this: First start the network thread with the function network() from net.rs That should start the thread and return a variable "tx" which is the sender for the thread communication To use the networking thread, you must use one of the functions in net.rs related to communication and you must input the necessary values, the first value is ALWAYS the tx returned by the network() function and WILL NOT work without it, so the network() function must be called and within the same scope (where the tx variable containing the sender is) you must use one of the communication functions To do (for Dan): - Networking implementation for the GUI - Server - Further expand things that the network thread does (if necessary) To do (for me): - Once GUI implementation is finished, properly test the changes - Probably will have to do debugging idk... suffering... - VerbenaIDK, yasmina :3 --- src/main.rs | 1 + src/net.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/net.rs diff --git a/src/main.rs b/src/main.rs index 232b209..79f2e21 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use eframe::egui; use std::{sync::{Arc, Mutex}, thread, time::Duration}; +pub mod net; fn main() -> Result<(), eframe::Error> { let options = eframe::NativeOptions::default(); diff --git a/src/net.rs b/src/net.rs new file mode 100644 index 0000000..15b7a5f --- /dev/null +++ b/src/net.rs @@ -0,0 +1,75 @@ +use std::thread::sleep; +use std::thread; +use std::net::UdpSocket; +use std::time::Duration; +use std::sync::mpsc::{channel, Sender}; +use std::collections::VecDeque; + + +pub fn network() -> Sender> { + let (tx, rx) = channel(); + let tx: Sender> = tx.clone(); + + thread::spawn( move || { + let mut net_active = 1; + while net_active == 1 { + let server_socket = UdpSocket::bind("0.0.0.0:0").unwrap(); + let target = "100.127.105.90:4000"; + let dat = rx.recv().unwrap(); + + + + if dat[0] < 0 { + eprintln!("Invalid code (negative code): {}" ,dat[0]) + + } + + if dat[0] == 0 { // code 0, kills thread + net_active = 0; + + } + if dat[0] == 1 { + sleep(Duration::from_secs(1800)); // code 1, thread sleeps for 30 minutes + + } + + if dat[0] == 2 { // code 2, sends data to server + if dat[1] > 0 && dat[2] > 0 && dat[3] > 0 { + let mut aa = VecDeque::from(dat); + aa.pop_front(); + let serialized_data = format!("{:?}\n", aa); + + println!("Sending scores and upgrade data\n + data is: {}" + ,serialized_data); + + if let Err(e) = server_socket.send_to(serialized_data.as_bytes(), target) { + eprintln!("Failed to send data: {}" ,e) + } + } + } + } + }); + + tx +} + +pub fn network_disable(tx: Sender>) -> Sender> { + let kill = [0, 0].to_vec(); + let _ = tx.send(kill); + tx +} + +pub fn send_score(tx: Sender>, x: i32, upgrd_cost: i32, score: i32) -> Sender> { + let send: [i64; 4] = [2, x.into(), upgrd_cost.into(), score.into()]; + let _ = tx.send(send.to_vec()); + tx +} + +pub fn debug(tx: Sender>) -> Sender> { + let send: [i64; 5] = [2, 65565, 65565, 65565, 1337]; + let _ = tx.send(send.to_vec()); + tx +} + +