OpenTTD Source: src/network/core/packet.h Source File

OpenTTD Source 1.11.0-RC1

packet.h
Go to the documentation of this file.
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 */
7
12 #ifndef NETWORK_CORE_PACKET_H
13 #define NETWORK_CORE_PACKET_H
14
15 #include "config.h"
16 #include "core.h"
17 #include "../../string_type.h"
18
19 typedef uint16 PacketSize;
20 typedef uint8 PacketType;
21
40 struct Packet {
42 Packet *next;
48 PacketSize size;
50 PacketSize pos;
52 byte *buffer;
53
54 private:
56 NetworkSocketHandler *cs;
57
58 public:
59 Packet(NetworkSocketHandler *cs);
60 Packet(PacketType type);
61 ~Packet();
62
63 /* Sending/writing of packets */
64 void PrepareToSend();
65
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);
72
73 /* Reading/receiving of packets */
74 void ReadRawPacketSize();
75 void PrepareToRead();
76
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);
84 };
85
86 #endif /* NETWORK_CORE_PACKET_H */
Packet::PrepareToSend
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
Definition: packet.cpp:61
Packet::Recv_string
void Recv_string(char *buffer, size_t size, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads a string till it finds a '\0' in the stream.
Definition: packet.cpp:286
Packet::buffer
byte * buffer
The buffer of this packet, of basically variable length up to SEND_MTU.
Definition: packet.h:52
NetworkSocketHandler
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:41
Packet::Send_uint8
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:96
PacketType
uint8 PacketType
Identifier for the packet.
Definition: packet.h:20
Packet::Send_uint32
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:117
Packet::Recv_uint32
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:246
Packet::Send_string
void Send_string(const char *data)
Sends a string over the network.
Definition: packet.cpp:148
Packet::PrepareToRead
void PrepareToRead()
Prepares the packet so it can be read.
Definition: packet.cpp:196
Packet::size
PacketSize size
The size of the whole packet for received packets.
Definition: packet.h:48
Packet::Send_uint16
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
Definition: packet.cpp:106
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
Packet::next
Packet * next
The next packet.
Definition: packet.h:42
Packet::CanReadFromPacket
bool CanReadFromPacket(uint bytes_to_read)
Is it safe to read from the packet, i.e.
Definition: packet.cpp:169
Packet::ReadRawPacketSize
void ReadRawPacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
Definition: packet.cpp:186
Packet
Internal entity of a packet.
Definition: packet.h:40
StringValidationSettings
StringValidationSettings
Settings for the string validation.
Definition: string_type.h:48
Packet::Send_bool
void Send_bool(bool data)
Package a boolean in the packet.
Definition: packet.cpp:87
Packet::~Packet
~Packet()
Free the buffer of this packet.
Definition: packet.cpp:53
PacketSize
uint16 PacketSize
Size of the whole packet.
Definition: packet.h:19
Packet::Recv_bool
bool Recv_bool()
Read a boolean from the packet.
Definition: packet.cpp:208
Packet::Send_uint64
void Send_uint64(uint64 data)
Package a 64 bits integer in the packet.
Definition: packet.cpp:130
Packet::cs
NetworkSocketHandler * cs
Socket we're associated with.
Definition: packet.h:56
Packet::Recv_uint8
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:217
core.h
Packet::Packet
Packet(NetworkSocketHandler *cs)
Create a packet that is used to read from a network socket.
Definition: packet.cpp:23
Packet::Recv_uint64
uint64 Recv_uint64()
Read a 64 bits integer from the packet.
Definition: packet.cpp:263
config.h
SVS_REPLACE_WITH_QUESTION_MARK
@ SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
Definition: string_type.h:50
Packet::Recv_uint16
uint16 Recv_uint16()
Read a 16 bits integer from the packet.
Definition: packet.cpp:231
Packet::pos
PacketSize pos
The current read/write position in the packet.
Definition: packet.h:50