From 31dc567e294310bb93b7b87ed04ac82a5c2c1d7d Mon Sep 17 00:00:00 2001 From: Nickiel12 <35903114+Nickiel12@users.noreply.github.com> Date: Fri, 17 Dec 2021 17:17:42 -0800 Subject: [PATCH] moved sockets from main to sockethandler --- src/main.rs | 12 +++----- src/modules/socket_handler.rs | 51 +++++++++++++++++++++++++++++++ src/tests/main_tests.rs | 23 -------------- src/tests/socket_handler_tests.rs | 38 +++++++++++++++++++++++ 4 files changed, 94 insertions(+), 30 deletions(-) diff --git a/src/main.rs b/src/main.rs index d4bbdf3..7659ce5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,17 @@ +use modules::socket_handler::Socket; + mod tests; mod modules; -use std::net::{TcpListener, TcpStream, Shutdown}; + const SERVER_ADDRESS: &str = "localhost:5000"; fn main() { - let listener = make_listener(SERVER_ADDRESS); - + let listener = Socket::make_listener(SERVER_ADDRESS); + drop(listener); } - -fn make_listener(address: &str) -> TcpListener { - TcpListener::bind(address).unwrap() -} diff --git a/src/modules/socket_handler.rs b/src/modules/socket_handler.rs index e69de29..2e43e1b 100644 --- a/src/modules/socket_handler.rs +++ b/src/modules/socket_handler.rs @@ -0,0 +1,51 @@ +use workctl::sync_flag; +use std::net::{TcpListener, TcpStream, Shutdown}; +use std::io::{Read, Write}; +use std::thread::{self, JoinHandle}; +use std::time::Duration; + + + +trait SocketCallback { + fn handle_message(message: String); +} + +pub struct Socket{ + +} + +impl Socket { + + pub fn make_listener(address: &str) -> TcpListener { + TcpListener::bind(address).unwrap() + } + + pub fn handle_connections(listener: TcpListener) -> (sync_flag::SyncFlagTx, JoinHandle<()>){ + let (tx, thread_stop_flag) = sync_flag::new_syncflag(true); + let handle = thread::spawn(move || { + while thread_stop_flag.get() { + for (stream, addr) in listener.accept() { + + } + thread::sleep(Duration::from_millis(100)); + } + drop(listener); + }); + (tx, handle) + } + + pub fn handle_client(mut stream: TcpStream) { + + let mut buffer = [0; 1024]; + + let read_size = stream.read(&mut buffer).unwrap(); + + if read_size == 0 { + stream.shutdown(Shutdown::Both).unwrap(); + return + } + + let output = String::from_utf8_lossy(&buffer[..]); + println!("recieved: {}", output); + } +} \ No newline at end of file diff --git a/src/tests/main_tests.rs b/src/tests/main_tests.rs index f31afb9..c1ebb92 100644 --- a/src/tests/main_tests.rs +++ b/src/tests/main_tests.rs @@ -1,4 +1,3 @@ -use crate::{make_listener, SERVER_ADDRESS}; #[test] @@ -7,26 +6,4 @@ fn it_works() { assert_eq!(result, 4); } -#[test] -fn can_make_socket_listener(){ - let listener = make_listener(SERVER_ADDRESS); - drop(listener); -} - -#[test] -fn create_and_connect_to_listener() { - let listener = make_listener(SERVER_ADDRESS); - - let join_handle = std::thread::spawn(move || { - let _outgoing = std::net::TcpStream::connect(SERVER_ADDRESS).unwrap(); - }); - join_handle.join().unwrap(); - drop(listener); -} - -#[test] -#[should_panic] -fn panic_no_listener() { - let _outgoing = std::net::TcpStream::connect(SERVER_ADDRESS).unwrap(); -} diff --git a/src/tests/socket_handler_tests.rs b/src/tests/socket_handler_tests.rs index 139597f..3f03830 100644 --- a/src/tests/socket_handler_tests.rs +++ b/src/tests/socket_handler_tests.rs @@ -1,2 +1,40 @@ +use crate::{SERVER_ADDRESS, modules::socket_handler::Socket}; + +#[test] +fn can_make_socket_listener(){ + let listener = Socket::make_listener(SERVER_ADDRESS); + drop(listener); +} + +#[test] +fn create_and_connect_to_listener() { + let listener = Socket::make_listener(SERVER_ADDRESS); + + let join_handle = std::thread::spawn(move || { + let _outgoing = std::net::TcpStream::connect(SERVER_ADDRESS).unwrap(); + }); + join_handle.join().unwrap(); + drop(listener); +} + +#[test] +#[should_panic] +fn panic_no_listener() { + let _outgoing = std::net::TcpStream::connect("localhost").unwrap(); +} + +#[test] +fn can_handle_messages() { + let listener = Socket::make_listener(SERVER_ADDRESS); + + let (mut flag, connection_handle) = Socket::handle_connections(listener); + + let join_handle = std::thread::spawn(move || { + let _outgoing = std::net::TcpStream::connect(SERVER_ADDRESS).unwrap(); + }); + join_handle.join().unwrap(); + flag.set(false); + connection_handle.join().unwrap(); +}