network/core/packet.h
branchcustombridgeheads
changeset 5642 bfa6074e2833
equal deleted inserted replaced
5641:d4d00a16ef26 5642:bfa6074e2833
       
     1 /* $Id$ */
       
     2 
       
     3 #ifndef NETWORK_CORE_PACKET_H
       
     4 #define NETWORK_CORE_PACKET_H
       
     5 
       
     6 #ifdef ENABLE_NETWORK
       
     7 
       
     8 /**
       
     9  * @file packet.h Basic functions to create, fill and read packets.
       
    10  */
       
    11 
       
    12 typedef struct NetworkClientState NetworkClientState;
       
    13 
       
    14 /**
       
    15  * Queries the network client state struct to determine whether
       
    16  * the client has quit. It indirectly also queries whether the
       
    17  * packet is corrupt as the connection will be closed if it is
       
    18  * reading beyond the boundary of the received packet.
       
    19  * @param cs the state to query
       
    20  * @param true if the connection should be considered dropped
       
    21  */
       
    22 bool HasClientQuit(NetworkClientState *cs);
       
    23 
       
    24 typedef uint16 PacketSize; ///< Size of the whole packet.
       
    25 typedef uint8  PacketType; ///< Identifier for the packet
       
    26 
       
    27 /**
       
    28  * Internal entity of a packet. As everything is sent as a packet,
       
    29  * all network communication will need to call the functions that
       
    30  * populate the packet.
       
    31  * Every packet can be at most SEND_MTU bytes. Overflowing this
       
    32  * limit will give an assertion when sending (i.e. writing) the
       
    33  * packet. Reading past the size of the packet when receiving
       
    34  * will return all 0 values and "" in case of the string.
       
    35  */
       
    36 typedef struct Packet {
       
    37 	/** The next packet. Used for queueing packets before sending. */
       
    38 	struct Packet *next;
       
    39 	/** The size of the whole packet for received packets. For packets
       
    40 	 * that will be sent, the value is filled in just before the
       
    41 	 * actual transmission. */
       
    42 	PacketSize size;
       
    43 	/** The current read/write position in the packet */
       
    44 	PacketSize pos;
       
    45 	/** The buffer of this packet */
       
    46 	byte buffer[SEND_MTU];
       
    47 } Packet;
       
    48 
       
    49 
       
    50 Packet *NetworkSend_Init(PacketType type);
       
    51 void NetworkSend_FillPacketSize(Packet *packet);
       
    52 void NetworkSend_uint8 (Packet *packet, uint8 data);
       
    53 void NetworkSend_uint16(Packet *packet, uint16 data);
       
    54 void NetworkSend_uint32(Packet *packet, uint32 data);
       
    55 void NetworkSend_uint64(Packet *packet, uint64 data);
       
    56 void NetworkSend_string(Packet *packet, const char* data);
       
    57 
       
    58 void NetworkRecv_ReadPacketSize(Packet *packet);
       
    59 uint8  NetworkRecv_uint8 (NetworkClientState *cs, Packet *packet);
       
    60 uint16 NetworkRecv_uint16(NetworkClientState *cs, Packet *packet);
       
    61 uint32 NetworkRecv_uint32(NetworkClientState *cs, Packet *packet);
       
    62 uint64 NetworkRecv_uint64(NetworkClientState *cs, Packet *packet);
       
    63 void   NetworkRecv_string(NetworkClientState *cs, Packet *packet, char* buffer, size_t size);
       
    64 
       
    65 #endif /* ENABLE_NETWORK */
       
    66 
       
    67 #endif /* NETWORK_CORE_PACKET_H */