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
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use std::io::{IoResult, IoError, OtherIoError};
use std::io::BufReader;
use std::num::FromPrimitive;

///Headers for various different built-in message types
#[derive(FromPrimitive, Clone, Show, PartialEq, Copy)]
pub enum PacketType {
    Connect = 0,
    Accept,
    Reject,
    Disconnect,
    Message
}

///The underlying shape for transferring data.
#[derive(Clone)]
pub struct Packet {
    pub protocol_id: u32,
    ///The current id of the packet
    pub sequence_id: u16,
    pub packet_type: PacketType,
    ///Serialized user data goes in here
    pub packet_content: Option<Vec<u8>>
}

///Commands to send to subprocesses
pub enum TaskCommand {
    Disconnect,
}

impl Packet {

    pub fn connect(protocol_id: u32, sequence_id: u16) -> Packet {
        Packet {
            protocol_id: protocol_id,
            sequence_id: sequence_id,
            packet_type: PacketType::Connect,
            packet_content: None
        }
    }

    pub fn disconnect(protocol_id: u32, sequence_id: u16) -> Packet {
        Packet {
            protocol_id: protocol_id,
            sequence_id: sequence_id,
            packet_type: PacketType::Disconnect,
            packet_content: None
        }
    }
    
    pub fn accept(protocol_id: u32, sequence_id: u16) -> Packet {
        Packet {
            protocol_id: protocol_id,
            sequence_id: sequence_id,
            packet_type: PacketType::Accept,
            packet_content: None
        }
    }

    pub fn reject(protocol_id: u32, sequence_id: u16) -> Packet {
        Packet {
            protocol_id: protocol_id,
            sequence_id: sequence_id,
            packet_type: PacketType::Reject,
            packet_content: None
        }
    }

    pub fn message(protocol_id: u32, sequence_id: u16, message: Vec<u8>) -> Packet {
        Packet {
            protocol_id: protocol_id,
            sequence_id: sequence_id,
            packet_type: PacketType::Message,
            packet_content: Some(message)
        }
    }

    pub fn deserialize(raw: &[u8]) -> IoResult<Packet> {
        let mut r = BufReader::new(raw);
        let protocol_id = try!(r.read_be_u32());
        let sequence_id = try!(r.read_be_u16());
        let packet_type = try!(r.read_byte());
        let content = try!(r.read_to_end());

        match FromPrimitive::from_u8(packet_type) {
            Some(packet_type) => {
                Ok(Packet {
                    protocol_id: protocol_id,
                    sequence_id: sequence_id,
                    packet_type: packet_type,
                    packet_content: if content.len() > 0 { Some(content) } else { None }
                })
            },
            None => Err(IoError {
                kind: OtherIoError,
                desc: "Invalid packet type",
                detail: None
            })
        }

    }

    pub fn serialize(&self) -> IoResult<Vec<u8>> {
        let mut w = vec![];
        try!(w.write_be_u32(self.protocol_id));
        try!(w.write_be_u16(self.sequence_id));
        try!(w.write_u8(self.packet_type as u8));
        match self.packet_content {
            Some(ref content) => {
                try!(w.write(content.as_slice()))
            },
            None => ()
        }
        Ok(w)
    }
}