(svn r27729) -Codechange: Do not count static NewGRF when checking for the maximum...
[openttd.git] / src / network / core / packet.h
blob7f344d0179f2f213770b811016a9c97fc8f3c504
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /**
11 * @file packet.h Basic functions to create, fill and read packets.
14 #ifndef NETWORK_CORE_PACKET_H
15 #define NETWORK_CORE_PACKET_H
17 #include "config.h"
18 #include "core.h"
19 #include "../../string_type.h"
21 #ifdef ENABLE_NETWORK
23 typedef uint16 PacketSize; ///< Size of the whole packet.
24 typedef uint8 PacketType; ///< Identifier for the packet
26 /**
27 * Internal entity of a packet. As everything is sent as a packet,
28 * all network communication will need to call the functions that
29 * populate the packet.
30 * Every packet can be at most SEND_MTU bytes. Overflowing this
31 * limit will give an assertion when sending (i.e. writing) the
32 * packet. Reading past the size of the packet when receiving
33 * will return all 0 values and "" in case of the string.
35 * --- Points of attention ---
36 * - all > 1 byte integral values are written in little endian,
37 * unless specified otherwise.
38 * Thus, 0x01234567 would be sent as {0x67, 0x45, 0x23, 0x01}.
39 * - all sent strings are of variable length and terminated by a '\0'.
40 * Thus, the length of the strings is not sent.
41 * - years that are leap years in the 'days since X' to 'date' calculations:
42 * (year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0))
44 struct Packet {
45 /** The next packet. Used for queueing packets before sending. */
46 Packet *next;
47 /**
48 * The size of the whole packet for received packets. For packets
49 * that will be sent, the value is filled in just before the
50 * actual transmission.
52 PacketSize size;
53 /** The current read/write position in the packet */
54 PacketSize pos;
55 /** The buffer of this packet, of basically variable length up to SEND_MTU. */
56 byte *buffer;
58 private:
59 /** Socket we're associated with. */
60 NetworkSocketHandler *cs;
62 public:
63 Packet(NetworkSocketHandler *cs);
64 Packet(PacketType type);
65 ~Packet();
67 /* Sending/writing of packets */
68 void PrepareToSend();
70 void Send_bool (bool data);
71 void Send_uint8 (uint8 data);
72 void Send_uint16(uint16 data);
73 void Send_uint32(uint32 data);
74 void Send_uint64(uint64 data);
75 void Send_string(const char *data);
77 /* Reading/receiving of packets */
78 void ReadRawPacketSize();
79 void PrepareToRead();
81 bool CanReadFromPacket (uint bytes_to_read);
82 bool Recv_bool ();
83 uint8 Recv_uint8 ();
84 uint16 Recv_uint16();
85 uint32 Recv_uint32();
86 uint64 Recv_uint64();
87 void Recv_string(char *buffer, size_t size, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
90 #endif /* ENABLE_NETWORK */
92 #endif /* NETWORK_CORE_PACKET_H */