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/>.
11 * @file packet.h Basic functions to create, fill and read packets.
14 #ifndef NETWORK_CORE_PACKET_H
15 #define NETWORK_CORE_PACKET_H
19 #include "../../string_type.h"
23 typedef uint16 PacketSize
; ///< Size of the whole packet.
24 typedef uint8 PacketType
; ///< Identifier for the packet
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))
45 /** The next packet. Used for queueing packets before sending. */
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.
53 /** The current read/write position in the packet */
55 /** The buffer of this packet, of basically variable length up to SEND_MTU. */
59 /** Socket we're associated with. */
60 NetworkSocketHandler
*cs
;
63 Packet(NetworkSocketHandler
*cs
);
64 Packet(PacketType type
);
67 /* Sending/writing of packets */
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();
81 bool CanReadFromPacket (uint bytes_to_read
);
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 */