diff options
| author | Xavier ASUS <xavi92psx@gmail.com> | 2018-12-29 02:23:56 +0100 |
|---|---|---|
| committer | Xavier ASUS <xavi92psx@gmail.com> | 2018-12-29 02:23:56 +0100 |
| commit | 72e712189ba8d65b9cce1da1fae6aa8b8727c3e2 (patch) | |
| tree | 0b334dafaf50c5a6a190a80b407f8f6942512775 /src/app.rs | |
| parent | ab8eea8c95a63d659783969b3e367f0726764af4 (diff) | |
| download | rspsxserial-72e712189ba8d65b9cce1da1fae6aa8b8727c3e2.tar.gz | |
Program logic has been distributed from main to app and cmdline.
Diffstat (limited to 'src/app.rs')
| -rw-r--r-- | src/app.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/app.rs b/src/app.rs new file mode 100644 index 0000000..95327fc --- /dev/null +++ b/src/app.rs @@ -0,0 +1,42 @@ +use std::{string::String, io, collections::HashMap}; + +/// This function is called once all command line a +/// rguments have been successfully parsed, and tries +/// to establish a TCP connection against a front-end +/// if configured by command line parameters. +pub fn app(arg_hash: HashMap<String, String>) -> io::Result<()> { + + let tcp = arg_hash.get(&String::from("--tcp")); + + match tcp { + None => println!("No TCP address specified"), + Some(addr) => setup_tcp(addr)? + }; + + Ok(()) +} + +fn setup_tcp(tcp_addr : &String) -> io::Result<()> { + + use std::net::{TcpListener}; + + let listener = TcpListener::bind(tcp_addr)?; + + println!("Awaiting for connection on address {}", tcp_addr); + + for stream in listener.incoming() { + match stream { + Ok(s) => { + // do something with the TcpStream + } + Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { + // wait until network socket is ready, typically implemented + // via platform-specific APIs such as epoll or IOCP + continue; + } + Err(e) => panic!("encountered IO error: {}", e), + } + } + + Ok(()) +} |
