diff options
author | Rick Richardson <rick.richardson@gmail.com> | 2016-11-22 08:29:02 -0800 |
---|---|---|
committer | Rick Richardson <rick.richardson@gmail.com> | 2016-11-22 08:29:02 -0800 |
commit | 2cb600bd19fb655c69ded7ac3cb2e86172c6285b (patch) | |
tree | 44e7b178b46bd04c1816344c39c0cee35c23e2e0 /examples/udp-codec.rs | |
parent | b12d32ce1cfc361255e3b938ed660a3e3d437f6d (diff) |
changed CodecUdp::decode to return Self::In instead of Option<Self::In>
Diffstat (limited to 'examples/udp-codec.rs')
-rw-r--r-- | examples/udp-codec.rs | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/examples/udp-codec.rs b/examples/udp-codec.rs index f899180a..afc70d75 100644 --- a/examples/udp-codec.rs +++ b/examples/udp-codec.rs @@ -25,20 +25,27 @@ use std::str; /// so that it can respond back. /// In the real world, one would probably /// want an associative of remote peers to their state +/// +/// Note that this takes a pretty draconian stance by returning +/// an error if it can't find a newline in the datagram it received pub struct LineCodec { addr : Option<SocketAddr> } impl CodecUdp for LineCodec { - type In = Vec<u8>; + type In = Vec<Vec<u8>>; type Out = Vec<u8>; - fn decode(&mut self, addr : &SocketAddr, buf: &[u8]) -> Result<Option<Self::In>, io::Error> { + fn decode(&mut self, addr : &SocketAddr, buf: &[u8]) -> Result<Self::In, io::Error> { trace!("decoding {} - {}", str::from_utf8(buf).unwrap(), addr); self.addr = Some(*addr); - match buf.iter().position(|&b| b == b'\n') { - Some(i) => Ok(Some(buf[.. i].into())), - None => Ok(None), + let res : Vec<Vec<u8>> = buf.split(|c| *c == b'\n').map(|s| s.into()).collect(); + if res.len() > 0 { + Ok(res) + } + else { + Err(io::Error::new(io::ErrorKind::Other, + "failed to find newline in datagram")) } } @@ -83,8 +90,8 @@ fn main() { //Note that we pass srvsink into fold, so that it can be //supplied to every iteration. The reason for this is //sink.send moves itself into `send` and then returns itself - let srvloop = srvstream.fold(srvsink, move |sink, buf| { - println!("{}", str::from_utf8(buf.as_slice()).unwrap()); + let srvloop = srvstream.fold(srvsink, move |sink, lines| { + println!("{}", str::from_utf8(lines[0].as_slice()).unwrap()); sink.send(b"PONG".to_vec()) }).map(|_| ()); @@ -92,8 +99,8 @@ fn main() { let (clistream, clisink) = client.framed(clicodec).split(); //And another infinite iteration - let cliloop = clistream.fold(clisink, move |sink, buf| { - println!("{}", str::from_utf8(buf.as_slice()).unwrap()); + let cliloop = clistream.fold(clisink, move |sink, lines| { + println!("{}", str::from_utf8(lines[0].as_slice()).unwrap()); sink.send(b"PING".to_vec()) }).map(|_| ()); |