1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! This crate defines codec of AMQP protocol.
//!
//! # Byte architecture of general AMQP message frame
//!
//! position   0      1         3      7         size+7      size+8
//!            +------+---------+------+---------+-----------+
//!            | type | channel | size | payload | frame_end |
//!            +------+---------+------+---------+-----------+
//! length        1        2        4     size         1
//!
//! "payload" is defined for each frame type.
//!
extern crate tokio_io;

extern crate bytes;

#[macro_use]
extern crate log;

pub mod frame;
pub mod args;

pub use args::{FieldArgument, AmqpString};
pub use frame::{Frame, FrameHeader, FramePayload};
pub use frame::method;
pub use frame::content_header;
pub use frame::content_body;


use bytes::BytesMut;
use std::io::Error as IoError;

pub struct Codec;

impl tokio_io::codec::Decoder for Codec {
    type Item = Frame;
    type Error = IoError;

    fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, IoError> {
        Ok(frame::decoder::decode_frame(src))
    }
}

impl tokio_io::codec::Encoder for Codec {
    type Item = Frame;
    type Error = IoError;

    fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), IoError> {
        Ok(frame::encoder::encode_frame(item, dst))
    }
}