From e35719f745615f7be274e4812df128343d37ea3f Mon Sep 17 00:00:00 2001 From: Titouan Rigoudy Date: Sat, 18 May 2019 20:20:28 +0000 Subject: [PATCH] Remove src/proto/transport.rs. --- src/proto/mod.rs | 1 - src/proto/transport.rs | 83 ------------------------------------------ 2 files changed, 84 deletions(-) delete mode 100644 src/proto/transport.rs diff --git a/src/proto/mod.rs b/src/proto/mod.rs index f93fb13..23340b8 100644 --- a/src/proto/mod.rs +++ b/src/proto/mod.rs @@ -6,7 +6,6 @@ mod packet; pub mod peer; pub mod server; mod stream; -mod transport; mod user; pub use self::base_codec::{Decode, ProtoDecode, ProtoDecoder, ProtoEncode, ProtoEncoder}; diff --git a/src/proto/transport.rs b/src/proto/transport.rs deleted file mode 100644 index 262eb1e..0000000 --- a/src/proto/transport.rs +++ /dev/null @@ -1,83 +0,0 @@ -use std::fmt; -use std::io; - -use bytes::BytesMut; -use tokio_io::codec::length_delimited::{Builder, Framed}; -use tokio_io::{AsyncRead, AsyncWrite}; - -use crate::proto::peer; -use crate::proto::{ProtoDecode, ProtoDecoder, ProtoEncode, ProtoEncoder, ServerRequest, ServerResponse}; - -fn decode_frame<'a, T>(frame_type: &str, bytes: &'a mut BytesMut) -> io::Result -where - T: ProtoDecode + fmt::Debug, -{ - let mut decoder = ProtoDecoder::new(&*bytes); - let frame = decoder.decode()?; - if decoder.has_remaining() { - warn!( - "Received {} with trailing bytes. Frame:\n{:?}Bytes:{:?}", - frame_type, - frame, - decoder.bytes() - ); - } - Ok(frame) -} - -fn encode_frame(frame: &T) -> io::Result { - let mut bytes = BytesMut::new(); - frame.encode(&mut ProtoEncoder::new(&mut bytes))?; - Ok(bytes) -} - -/// Wraps a raw byte async I/O object, providing it with the ability to read and -/// write entire frames at once. -/// The returned stream and sink of frames is intended to be combined (using -/// `Stream::and_then` and `Sink::with`) with the following decoding and -/// encoding functions to create a stream/sink of decoded messages. -pub fn new_framed(io: T) -> Framed { - Builder::new() - .length_field_length(4) - .little_endian() - .new_framed(io) -} - -/// Decodes a server response from the given byte buffer, which should contain -/// exactly the bytes encoding the returned response. -/// Intended to be used on the result of `new_framed` using `Stream::and_then`. -pub fn decode_server_response(bytes: &mut BytesMut) -> io::Result { - decode_frame("server response", bytes) -} - -/// Encodes the given server response into a byte buffer, then returns it. -/// Intended to be used on a sink of BytesMut objects, using `Sink::with`. -pub fn encode_server_response(response: &ServerResponse) -> io::Result { - encode_frame(response) -} - -/// Decodes a server request from the given byte buffer, which should contain -/// exactly the bytes encoding the returned response. -/// Intended to be used on the result of `new_framed` using `Stream::and_then`. -pub fn decode_server_request(bytes: &mut BytesMut) -> io::Result { - decode_frame("server request", bytes) -} - -/// Encodes the given server request into a byte buffer, then returns it. -/// Intended to be used on a sink of BytesMut objects, using `Sink::with`. -pub fn encode_server_request(request: &ServerRequest) -> io::Result { - encode_frame(request) -} - -/// Decodes a peer message from the given byte buffer, which should contain -/// exactly the bytes encoding the returned response. -/// Intended to be used on the result of `new_framed` using `Stream::and_then`. -pub fn decode_peer_message(bytes: &mut BytesMut) -> io::Result { - decode_frame("peer message", bytes) -} - -/// Encodes the given peer message into a byte buffer, then returns it. -/// Intended to be used on a sink of BytesMut objects, using `Sink::with`. -pub fn encode_peer_message(message: &peer::Message) -> io::Result { - encode_frame(message) -}