[][src]Struct facio::raw_packet::RawPacket

pub struct RawPacket {
    pub pid: i32,
    pub ptype: i32,
    pub pbody: String,
    // some fields omitted
}

Gives the underlying structure of a packet of any type. There are serialization and deserialization functions defined. No application level consistency is checked, besides that the body cannot be larger then 4086 (which is 4096-10).

Within the spec, only ASCII is allowed where in this implementation the body is of type String so generally UTF-8 is possible which the server-side might not support when sticking to the protocol.

Entities of this type are introduced through the new functions; they calculate a suitable psize according to the spec. The field psize is kept private; this library treats it as an implementation detail provided by the spec.

Example

use facio::raw_packet::*;

// create an auth packet with id `0` and `"mypass"` as password.
let auth_request_packet =
   RawPacket::new_auth(0, "mypass").unwrap();

assert_eq!(auth_request_packet.pbody, "mypass");
assert_eq!(auth_request_packet.ptype, PacketType::RequestAuth.as_i32());

Fields

pid: i32ptype: i32pbody: String

Methods

impl RawPacket[src]

pub fn new<T: Into<String>>(
    id: i32,
    raw_type: i32,
    body: T
) -> Result<RawPacket, RawPacketCreationError>
[src]

Creates a new raw packet, which is consistent with the spec, expect for the type, which is allowed to be any number. In other words: this creates a RawPacket with a suitable psize.

pub fn serialize<T: Write>(&self, w: &mut T) -> Result<()>[src]

Serialization according to the spec. This means:

  • Write psize as little endian i32.
  • Write pid as little endian i32.
  • Write ptype as little endian i32.
  • Write the body as null-terminated string.
  • Add another null to end the packet.

Since String isn't null-terminated, this null is added manually after writing the body.

pub fn deserialize<T: Read>(r: &mut T) -> Result<RawPacket>[src]

Deserialization according to the spec. See serialize.

pub fn new_from_type<T: Into<String>>(
    id: i32,
    raw_body: T,
    ptype: &PacketType
) -> Result<RawPacket, RawPacketCreationError>
[src]

Provides the base line for all convenience functions to create packets of a specific type using PacketType.

Example

use facio::raw_packet::*;

// create with id as stated in spec
let exec_packet_generic =
     RawPacket::new(0, 2, "/version").unwrap();
// create using the `PacketType` enum
let exec_packet_ftype =
     RawPacket::new_from_type(0, "/version", &PacketType::RequestExecCommand).unwrap();
// create using the convenience function
let exec_packet_conv =
     RawPacket::new_exec(0, "/version").unwrap();

assert_eq!(exec_packet_generic, exec_packet_ftype);
assert_eq!(exec_packet_ftype, exec_packet_conv);
// and hence by transitivity ... 

pub fn new_auth<T: Into<String>>(
    id: i32,
    pass: T
) -> Result<RawPacket, RawPacketCreationError>
[src]

pub fn new_exec<T: Into<String>>(
    id: i32,
    command: T
) -> Result<RawPacket, RawPacketCreationError>
[src]

pub fn new_response_auth<T: Into<String>>(
    id: i32,
    value: T
) -> Result<RawPacket, RawPacketCreationError>
[src]

pub fn new_response_value<T: Into<String>>(
    id: i32,
    value: T
) -> Result<RawPacket, RawPacketCreationError>
[src]

pub fn response_type(&self) -> Option<PacketType>[src]

Retrieves the ptype as a PacketType, where the packet is seen as a response. (See PacketType for more information.)

pub fn request_type(&self) -> Option<PacketType>[src]

Retrieves the ptype as a PacketType, where the packet is seen as a request.

Trait Implementations

impl Eq for RawPacket[src]

impl PartialEq<RawPacket> for RawPacket[src]

impl Debug for RawPacket[src]

Auto Trait Implementations

impl Send for RawPacket

impl Sync for RawPacket

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]