From 9a2222d8023b67f0821ce18d50d7476e1a8052f7 Mon Sep 17 00:00:00 2001 From: Aaron Weiss Date: Fri, 26 May 2017 14:09:09 +0200 Subject: [PATCH] Updated Tokio protocol code. --- Cargo.toml | 2 ++ src/lib.rs | 2 ++ src/proto/irc.rs | 23 +++++++++++++++-------- src/proto/line.rs | 30 +++++++++++++++++------------- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 21c2c95..f235985 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,11 +17,13 @@ ctcp = [] nochanlists = [] [dependencies] +bytes = "0.4" encoding = "0.2" futures = "0.1" native-tls = "0.1" rustc-serialize = "0.3" time = "0.1" tokio-core = "0.1" +tokio-io = "0.1" tokio-service = "0.1" tokio-tls = "0.1" diff --git a/src/lib.rs b/src/lib.rs index 281a131..bbed42b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,11 +2,13 @@ #![warn(missing_docs)] +extern crate bytes; extern crate encoding; extern crate futures; extern crate native_tls; extern crate rustc_serialize; extern crate time; +extern crate tokio_io; extern crate tokio_core; extern crate tokio_service; extern crate tokio_tls; diff --git a/src/proto/irc.rs b/src/proto/irc.rs index ccef4f2..31eac1d 100644 --- a/src/proto/irc.rs +++ b/src/proto/irc.rs @@ -1,9 +1,10 @@ //! Implementation of IRC codec for Tokio. use std::io; +use bytes::BytesMut; +use tokio_io::codec::{Decoder, Encoder}; use proto::line::LineCodec; use proto::message::Message; -use tokio_core::io::{Codec, EasyBuf}; /// An IRC codec built around an inner codec. pub struct IrcCodec { @@ -17,19 +18,25 @@ impl IrcCodec { } } -impl Codec for IrcCodec { - type In = Message; - type Out = Message; +impl Decoder for IrcCodec { + type Item = Message; + type Error = io::Error; - fn decode(&mut self, buf: &mut EasyBuf) -> io::Result> { - self.inner.decode(buf).and_then(|res| res.map_or(Ok(None), |msg| { + fn decode(&mut self, src: &mut BytesMut) -> io::Result> { + self.inner.decode(src).and_then(|res| res.map_or(Ok(None), |msg| { msg.parse::().map(|msg| Some(msg)).map_err(|err| { io::Error::new(io::ErrorKind::InvalidInput, err) }) })) } +} - fn encode(&mut self, msg: Message, buf: &mut Vec) -> io::Result<()> { - self.inner.encode(msg.to_string(), buf) +impl Encoder for IrcCodec { + type Item = Message; + type Error = io::Error; + + + fn encode(&mut self, msg: Message, dst: &mut BytesMut) -> io::Result<()> { + self.inner.encode(msg.to_string(), dst) } } diff --git a/src/proto/line.rs b/src/proto/line.rs index 40e4076..a819378 100644 --- a/src/proto/line.rs +++ b/src/proto/line.rs @@ -1,10 +1,10 @@ //! Implementation of line-delimiting codec for Tokio. use std::io; -use std::io::prelude::*; +use bytes::{BufMut, BytesMut}; use encoding::{DecoderTrap, EncoderTrap, EncodingRef}; use encoding::label::encoding_from_whatwg_label; -use tokio_core::io::{Codec, EasyBuf}; +use tokio_io::codec::{Decoder, Encoder}; /// A line-based codec parameterized by an encoding. pub struct LineCodec { @@ -23,17 +23,17 @@ impl LineCodec { } } -impl Codec for LineCodec { - type In = String; - type Out = String; +impl Decoder for LineCodec { + type Item = String; + type Error = io::Error; - fn decode(&mut self, buf: &mut EasyBuf) -> io::Result> { - if let Some(n) = buf.as_ref().iter().position(|b| *b == b'\n') { + fn decode(&mut self, src: &mut BytesMut) -> io::Result> { + if let Some(n) = src.as_ref().iter().position(|b| *b == b'\n') { // Remove the next frame from the buffer. - let line = buf.drain_to(n); + let line = src.split_to(n + 1); // Remove the new-line from the buffer. - buf.drain_to(1); + src.split_to(1); // Decode the line using the codec's encoding. match self.encoding.decode(line.as_ref(), DecoderTrap::Replace) { @@ -47,8 +47,13 @@ impl Codec for LineCodec { Ok(None) } } +} - fn encode(&mut self, msg: String, buf: &mut Vec) -> io::Result<()> { +impl Encoder for LineCodec { + type Item = String; + type Error = io::Error; + + fn encode(&mut self, msg: String, dst: &mut BytesMut) -> io::Result<()> { // Encode the message using the codec's encoding. let data = try!(self.encoding.encode(&msg, EncoderTrap::Replace).map_err(|data| { io::Error::new( @@ -58,9 +63,8 @@ impl Codec for LineCodec { })); // Write the encoded message to the output buffer. - try!(buf.write_all(&data)); + dst.put(&data); - // Flush the output buffer. - buf.flush() + Ok(()) } }