-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
49 lines (43 loc) · 1.31 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use lazy_static::lazy_static;
use resp::Decoder;
use std::collections::HashMap;
use std::env;
use std::io::{BufReader, Write};
use std::net::Shutdown;
use std::net::{TcpListener, TcpStream};
use std::sync::Mutex;
use std::thread;
type STORE = Mutex<HashMap<String, String>>;
lazy_static! {
static ref RUDIS_DB: STORE = Mutex::new(HashMap::new());
}
mod commands;
use crate::commands::process_client_request;
fn main() {
let addr = env::args()
.skip(1)
.next()
.unwrap_or("127.0.0.1:6378".to_owned());
let listener = TcpListener::bind(&addr).unwrap();
println!("rudis_sync listening on {} ...", addr);
for stream in listener.incoming() {
let stream = stream.unwrap();
println!("New connection from: {:?}", stream);
thread::spawn(|| handle_client(stream));
// thread::sleep(std::time::Duration::from_millis(3000));
}
}
fn handle_client(stream: TcpStream) {
let mut stream = BufReader::new(stream);
let decode = Decoder::new(&mut stream).decode();
match decode {
Ok(v) => {
let reply = process_client_request(v);
stream.get_mut().write_all(&reply).unwrap();
}
Err(e) => {
println!("Invalid command: {:?}", e);
let _ = stream.get_mut().shutdown(Shutdown::Both);
}
}
}