Fix: Data races on cursor state in OpenGL backends
[openttd-github.git] / src / network / core / packet.h
blobc9be4eeb53736da6cb4f7b92a03ef0f7e3d990d4
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /**
9 * @file packet.h Basic functions to create, fill and read packets.
12 #ifndef NETWORK_CORE_PACKET_H
13 #define NETWORK_CORE_PACKET_H
15 #include "config.h"
16 #include "core.h"
17 #include "../../string_type.h"
19 typedef uint16 PacketSize; ///< Size of the whole packet.
20 typedef uint8 PacketType; ///< Identifier for the packet
22 /**
23 * Internal entity of a packet. As everything is sent as a packet,
24 * all network communication will need to call the functions that
25 * populate the packet.
26 * Every packet can be at most SEND_MTU bytes. Overflowing this
27 * limit will give an assertion when sending (i.e. writing) the
28 * packet. Reading past the size of the packet when receiving
29 * will return all 0 values and "" in case of the string.
31 * --- Points of attention ---
32 * - all > 1 byte integral values are written in little endian,
33 * unless specified otherwise.
34 * Thus, 0x01234567 would be sent as {0x67, 0x45, 0x23, 0x01}.
35 * - all sent strings are of variable length and terminated by a '\0'.
36 * Thus, the length of the strings is not sent.
37 * - years that are leap years in the 'days since X' to 'date' calculations:
38 * (year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0))
40 struct Packet {
41 /** The next packet. Used for queueing packets before sending. */
42 Packet *next;
43 /**
44 * The size of the whole packet for received packets. For packets
45 * that will be sent, the value is filled in just before the
46 * actual transmission.
48 PacketSize size;
49 /** The current read/write position in the packet */
50 PacketSize pos;
51 /** The buffer of this packet, of basically variable length up to SEND_MTU. */
52 byte *buffer;
54 private:
55 /** Socket we're associated with. */
56 NetworkSocketHandler *cs;
58 public:
59 Packet(NetworkSocketHandler *cs);
60 Packet(PacketType type);
61 ~Packet();
63 /* Sending/writing of packets */
64 void PrepareToSend();
66 void Send_bool (bool data);
67 void Send_uint8 (uint8 data);
68 void Send_uint16(uint16 data);
69 void Send_uint32(uint32 data);
70 void Send_uint64(uint64 data);
71 void Send_string(const char *data);
73 /* Reading/receiving of packets */
74 void ReadRawPacketSize();
75 void PrepareToRead();
77 bool CanReadFromPacket (uint bytes_to_read);
78 bool Recv_bool ();
79 uint8 Recv_uint8 ();
80 uint16 Recv_uint16();
81 uint32 Recv_uint32();
82 uint64 Recv_uint64();
83 void Recv_string(char *buffer, size_t size, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
86 #endif /* NETWORK_CORE_PACKET_H */