Created
October 1, 2014 08:37
-
-
Save zmack/08024aa6d147356cadbf to your computer and use it in GitHub Desktop.
Slow server, for some reason
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::fs; | |
use std::io::fs::PathExtensions; | |
use std::io::net::pipe::UnixListener; | |
use std::io::{Acceptor,Listener}; | |
use std::string::String; | |
fn main() { | |
let socket = Path::new("server.sock"); | |
let mut buffer = String::with_capacity(1024); | |
// Delete old socket if necessary | |
if socket.exists() { | |
fs::unlink(&socket).unwrap(); | |
} | |
// Bind to socket | |
let stream = match UnixListener::bind(&socket) { | |
Err(_) => fail!("failed to bind socket"), | |
Ok(stream) => stream, | |
}; | |
println!("Server started, waiting for clients"); | |
// Iterate over clients, blocks if no client available | |
for mut client in stream.listen().incoming() { | |
println!("Client connected"); | |
loop { | |
match client.read_byte() { | |
Ok(10) => { | |
client.write_line("1"); | |
buffer.clear(); | |
}, | |
Ok(b) => unsafe { buffer.push_byte(b) }, | |
_ => break | |
} | |
} | |
// println!("Client said: {}", client.read_to_string().unwrap()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment