moved sockets from main to sockethandler

This commit is contained in:
Nickiel12 2021-12-17 17:17:42 -08:00
parent edea79e99b
commit 31dc567e29
4 changed files with 94 additions and 30 deletions

View file

@ -1,19 +1,17 @@
use modules::socket_handler::Socket;
mod tests; mod tests;
mod modules; mod modules;
use std::net::{TcpListener, TcpStream, Shutdown};
const SERVER_ADDRESS: &str = "localhost:5000"; const SERVER_ADDRESS: &str = "localhost:5000";
fn main() { fn main() {
let listener = make_listener(SERVER_ADDRESS); let listener = Socket::make_listener(SERVER_ADDRESS);
drop(listener); drop(listener);
} }
fn make_listener(address: &str) -> TcpListener {
TcpListener::bind(address).unwrap()
}

View file

@ -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);
}
}

View file

@ -1,4 +1,3 @@
use crate::{make_listener, SERVER_ADDRESS};
#[test] #[test]
@ -7,26 +6,4 @@ fn it_works() {
assert_eq!(result, 4); 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();
}

View file

@ -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();
}