Skip to content

Commit 00206e3

Browse files
committed
Refactoring
1 parent 3bab457 commit 00206e3

File tree

5 files changed

+23
-29
lines changed

5 files changed

+23
-29
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/unpfs/Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/serialize.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,8 @@ impl Decodable for u64 {
389389
impl Decodable for String {
390390
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
391391
let len: u16 = Decodable::decode(r)?;
392-
let buf = read_exact(r, len as usize)?;
393-
String::from_utf8(buf).or(res!(io_err!(Other, "Invalid UTF-8 sequence")))
392+
String::from_utf8(read_exact(r, len as usize)?)
393+
.or(res!(io_err!(Other, "Invalid UTF-8 sequence")))
394394
}
395395
}
396396

@@ -485,8 +485,7 @@ impl Decodable for DirEntryData {
485485
impl Decodable for Data {
486486
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
487487
let len: u32 = Decodable::decode(r)?;
488-
let buf = read_exact(r, len as usize)?;
489-
Ok(Data(buf))
488+
Ok(Data(read_exact(r, len as usize)?))
490489
}
491490
}
492491

src/srv.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,7 @@ impl<Fs, RwExt> ServerInstance<Fs, RwExt>
153153
let msg = serialize::read_msg(&mut self.stream)?;
154154

155155
debug!("\t→ {:?}", msg);
156-
let (fcall, tag) = dispatch_once(
157-
msg,
158-
&mut self.fs,
159-
&mut self.fids)?;
156+
let (fcall, tag) = dispatch_once(msg, &mut self.fs, &mut self.fids)?;
160157

161158
utils::respond(&mut self.stream, tag, fcall)?;
162159
}
@@ -183,14 +180,12 @@ impl<Fs, RwExt> SpawnServerInstance<Fs, RwExt>
183180

184181
fn dispatch(&mut self) -> Result<()> {
185182
loop {
183+
//let msg = serialize::read_msg(&mut self.stream)?;
186184
let msg = serialize::read_msg(&mut self.stream)?;
187185

188186
debug!("\t→ {:?}", msg);
189-
let (fcall, tag) = dispatch_once(
190-
msg,
191-
&mut *self.fs.lock().unwrap(),
192-
&mut self.fids
193-
)?;
187+
let (fcall, tag) =
188+
dispatch_once(msg, &mut *self.fs.lock().unwrap(), &mut self.fids)?;
194189

195190
utils::respond(&mut self.stream, tag, fcall)?;
196191
}
@@ -302,9 +297,8 @@ pub fn srv<Fs: Filesystem>(filesystem: Fs, addr: &str) -> Result<()> {
302297
/// This function spawns a new thread to handle its 9P messages
303298
/// when a client connects to the server.
304299
pub fn srv_spawn<Fs: Filesystem + Send + 'static>(filesystem: Fs, addr: &str) -> Result<()> {
305-
let (proto, sockaddr) = utils::parse_proto(addr).ok_or(
306-
io_err!(InvalidInput, "Invalid protocol or address")
307-
)?;
300+
let (proto, sockaddr) = utils::parse_proto(addr)
301+
.ok_or(io_err!(InvalidInput, "Invalid protocol or address"))?;
308302

309303
if proto != "tcp" {
310304
return res!(io_err!(InvalidInput, format!("Unsupported protocol: {}", proto)));
@@ -315,14 +309,15 @@ pub fn srv_spawn<Fs: Filesystem + Send + 'static>(filesystem: Fs, addr: &str) ->
315309

316310
loop {
317311
let (stream, remote) = listener.accept()?;
318-
let (fs, thread_name) = (arc_fs.clone(), format!("{}", remote));
312+
let (fs, thread_name) = (arc_fs.clone(), remote.to_string());
319313

320314
let _ = thread::Builder::new().name(thread_name.clone()).spawn(move || {
321315
info!("ServerThread={:?} started", thread_name);
322-
let result = {|| {
323-
utils::setup_tcp_stream(&stream)?;
324-
SpawnServerInstance::new(fs, stream)?.dispatch()
325-
}}();
316+
317+
let result = utils::setup_tcp_stream(&stream)
318+
.map_err(From::from)
319+
.and_then(|_| SpawnServerInstance::new(fs, stream)?.dispatch());
320+
326321
info!("ServerThread={:?} finished: {:?}", thread_name, result);
327322
});
328323
}

src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ extern crate net2;
33
extern crate byteorder;
44

55
use std::net::TcpStream;
6+
//use std::time::Duration;
67
use self::byteorder::WriteBytesExt;
78
use self::net2::TcpStreamExt;
89

@@ -33,8 +34,7 @@ pub fn parse_proto(arg: &str) -> Option<(&str, String)>{
3334

3435
// See also: diod/libdiod/diod_sock.c
3536
pub fn setup_tcp_stream(stream: &TcpStream) -> ::std::io::Result<()> {
36-
//try!(TcpStreamExt::set_nodelay(stream, true));
37-
//TcpStreamExt::set_keepalive(stream, Some(Duration::from_secs(120)))
37+
//TcpStreamExt::set_keepalive(stream, Some(Duration::from_secs(120)))?;
3838
TcpStreamExt::set_nodelay(stream, true)
3939
}
4040

0 commit comments

Comments
 (0)