pub const LISTENSOCK_FILENO: u8 = 0;
#[derive(Debug)]
pub struct Header
{
pub version: u8,
pub type_: u8,
pub request_id: u16,
pub content_length: u16,
pub padding_length: u8,
pub reserved: [u8; 1],
}
pub const MAX_LENGTH: usize = 0xffff;
pub const HEADER_LEN: usize = 8;
pub const VERSION_1: u8 = 1;
pub const BEGIN_REQUEST: u8 = 1;
pub const ABORT_REQUEST: u8 = 2;
pub const END_REQUEST: u8 = 3;
pub const PARAMS: u8 = 4;
pub const STDIN: u8 = 5;
pub const STDOUT: u8 = 6;
pub const STDERR: u8 = 7;
pub const DATA: u8 = 8;
pub const GET_VALUES: u8 = 9;
pub const GET_VALUES_RESULT: u8 = 10;
pub const UNKNOWN_TYPE: u8 = 11;
pub const NULL_REQUEST_ID: u16 = 0;
pub struct BeginRequestBody
{
pub role: u16,
pub flags: u8,
pub reserved: [u8; 5],
}
pub const KEEP_CONN: u8 = 1;
pub const RESPONDER: u8 = 1;
pub const AUTHORIZER: u8 = 2;
pub const FILTER: u8 = 3;
pub struct EndRequestBody
{
pub app_status: u32,
pub protocol_status: u8,
pub reserved: [u8; 3],
}
pub const REQUEST_COMPLETE: u8 = 0;
pub const CANT_MPX_CONN: u8 = 1;
pub const OVERLOADED: u8 = 2;
pub const UNKNOWN_ROLE: u8 = 3;
pub const MAX_CONNS: &'static str = "MAX_CONNS";
pub const MAX_REQS: &'static str = "MAX_REQS";
pub const MPXS_CONNS: &'static str = "MPXS_CONNS";
struct UnknownTypeBody
{
pub type_: u8,
pub reserved: [u8; 7],
}
extern crate byteorder;
use self::byteorder::{ByteOrder, BigEndian};
pub trait Readable {
fn read(data: &[u8]) -> Self;
}
pub trait Writable {
fn write(&self) -> Vec<u8>;
}
impl Readable for Header
{
fn read(data: &[u8]) -> Header
{
Header {
version: data[0],
type_: data[1],
request_id: BigEndian::read_u16(&data[2..4]),
content_length: BigEndian::read_u16(&data[4..6]),
padding_length: data[6],
reserved: [0; 1],
}
}
}
impl Writable for Header
{
fn write(&self) -> Vec<u8>
{
let mut data: Vec<u8> = Vec::with_capacity(self::HEADER_LEN);
data.push(self.version);
data.push(self.type_);
let mut buf: [u8; 2] = [0; 2];
BigEndian::write_u16(&mut buf, self.request_id);
data.extend_from_slice(&buf);
let mut buf: [u8; 2] = [0; 2];
BigEndian::write_u16(&mut buf, self.content_length);
data.extend_from_slice(&buf);
data.push(self.padding_length);
data.extend_from_slice(&self.reserved);
data
}
}
impl Readable for BeginRequestBody
{
fn read(data: &[u8]) -> BeginRequestBody
{
BeginRequestBody {
role: BigEndian::read_u16(&data[0..2]),
flags: data[2],
reserved: [0; 5],
}
}
}
impl Writable for BeginRequestBody
{
fn write(&self) -> Vec<u8>
{
let mut data: Vec<u8> = Vec::with_capacity(8);
let mut buf: [u8; 2] = [0; 2];
BigEndian::write_u16(&mut buf, self.role);
data.extend_from_slice(&buf);
data.push(self.flags);
data.extend_from_slice(&self.reserved);
data
}
}
impl Readable for EndRequestBody
{
fn read(data: &[u8]) -> EndRequestBody
{
EndRequestBody {
app_status: BigEndian::read_u32(&data[0..4]),
protocol_status: data[4],
reserved: [0; 3],
}
}
}
impl Writable for EndRequestBody
{
fn write(&self) -> Vec<u8>
{
let mut data: Vec<u8> = Vec::with_capacity(8);
let mut buf: [u8; 4] = [0; 4];
BigEndian::write_u32(&mut buf, self.app_status);
data.extend_from_slice(&buf);
data.push(self.protocol_status);
data.extend_from_slice(&self.reserved);
data
}
}
impl Readable for UnknownTypeBody
{
fn read(data: &[u8]) -> Self
{
UnknownTypeBody {
type_: data[0],
reserved: [0; 7],
}
}
}
impl Writable for UnknownTypeBody
{
fn write(&self) -> Vec<u8>
{
let mut data: Vec<u8> = Vec::with_capacity(8);
data.push(self.type_);
data.extend_from_slice(&self.reserved);
data
}
}