Class OcsfmlNetwork.packet

class packet : object .. end
Utility class to build blocks of data to transfer over the network.

Packets provide a safe and easy way to serialize data, in order to send it over the network using sockets (sf::TcpSocket, sf::UdpSocket).

Packets solve 2 fundamental problems that arise when transfering data over the network:

The sf::Packet class provides both input and output modes. It is designed to follow the behaviour of standard C++ streams, using operators >> and << to extract and insert data.

It is recommended to use only fixed-size types (like Int32, etc.), to avoid possible differences between the sender and the receiver. Indeed, the native C++ types may have different sizes on two platforms and your data may be corrupted if that happens.

Usage example:

    let x = 24;
    let s = "hello";
    let d = 5.89;
    
    (* Group the variables to send into a packet *)
    let packet = new packet in
    packet << (`Int32 x) << (`String s) << (`Float d) ;
    
    (* Send it over the network (socket is a valid tcp_socket) *)
    socket#send packet
    
    -----------------------------------------------------------------

    (* Receive the packet at the other end *)
    let packet = new packet in
    socket#receive packet ;
    
    (* Extract the variables contained in the packet *)
    let x = ref 0 in
    let s = ref "" in
    ler d = ref 0. in
    if (packet >> (`Int32 x) >> (`String s) >> (`Float d))#is_valid
    then begin
    (* Data extracted successfully... *)
    end
    


method append : OcsfmlSystem.raw_data_type -> unit
Append data to the end of the packet.
method clear : unit
Clear the packet.

After calling clear, the packet is empty.

method destroy : unit
method end_of_packet : bool
Tell if the reading position has reached the end of the packet.

This function is useful to know if there is some data left to be read, without actually reading it.
Returns True if all data was read, false otherwise

method get_data_size : int
Get the size of the data contained in the packet.

This function returns the number of bytes pointed to by what getData returns.
Returns Data size, in bytes

method get_data : OcsfmlSystem.raw_data_type
Get a pointer to the data contained in the packet.

Warning: the returned pointer may become invalid after you append data to the packet, therefore it should never be stored. The return pointer is NULL if the packet is empty.
Returns Bigarray of the data

method is_valid : bool
Test the validity of the packet, for reading.

A packet will be in an invalid state if it has no more data to read.
Returns True if last data extraction from packet was successful

method read_bool : bool
Read a boolean from the packet
method read_float : float
Read a floating point number from the packet
method read_int16 : int
Read a 16 bits signed integer from the packet
method read_int32 : int
Read a 32 bits signed integer from the packet
method read_int8 : int
Read a 8 bits signed integer from the packet
method read_string : string
Read a string from the packet
method read_uint16 : int
Read a 16 bits unsigned integer from the packet
method read_uint32 : int
Read a 16 bits unsigned integer from the packet
method read_uint8 : int
Read a 8 bits unsigned integer from the packet
method write_bool : bool -> unit
Write a boolean into the packet
method write_float : float -> unit
Write a floating point number into the packet
method write_int16 : int -> unit
Write a 16 bits signed integer into the packet
method write_int32 : int -> unit
Write a 32 bits signed integer into the packet
method write_int8 : int -> unit
Write a 8 bits signed integer into the packet
method write_string : string -> unit
Write a string into the packet
method write_uint16 : int -> unit
Write a 16 bits unsigned integer into the packet
method write_uint32 : int -> unit
Write a 32 bits unsigned integer into the packet
method write_uint8 : int -> unit
Write a 8 bits unsigned integer into the packet