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

OpenTTD Source 14.0-RC3

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 "os_abstraction.h"
16 #include "config.h"
17 #include "core.h"
18 #include "../../string_type.h"
19
20 typedef uint16_t PacketSize;
21 typedef uint8_t PacketType;
22
42 struct Packet {
43 private:
45 PacketSize pos;
47 std::vector<byte> buffer;
49 size_t limit;
50
52 NetworkSocketHandler *cs;
53
54 public:
55 Packet(NetworkSocketHandler *cs, size_t limit, size_t initial_read_size = sizeof(PacketSize));
56 Packet(PacketType type, size_t limit = COMPAT_MTU);
57
58 /* Sending/writing of packets */
59 void PrepareToSend();
60
61 bool CanWriteToPacket(size_t bytes_to_write);
62 void Send_bool (bool data);
63 void Send_uint8 (uint8_t data);
64 void Send_uint16(uint16_t data);
65 void Send_uint32(uint32_t data);
66 void Send_uint64(uint64_t data);
67 void Send_string(const std::string_view data);
68 void Send_buffer(const std::vector<byte> &data);
69 size_t Send_bytes (const byte *begin, const byte *end);
70
71 /* Reading/receiving of packets */
72 bool HasPacketSizeData() const;
73 bool ParsePacketSize();
74 size_t Size() const;
75 void PrepareToRead();
76 PacketType GetPacketType() const;
77
78 bool CanReadFromPacket(size_t bytes_to_read, bool close_connection = false);
79 bool Recv_bool ();
80 uint8_t Recv_uint8 ();
81 uint16_t Recv_uint16();
82 uint32_t Recv_uint32();
83 uint64_t Recv_uint64();
84 std::vector<byte> Recv_buffer();
85 std::string Recv_string(size_t length, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
86
87 size_t RemainingBytesToTransfer() const;
88
101 template <
102 typename A = size_t,
103 typename F,
104 typename D,
105 typename ... Args>
106 ssize_t TransferOutWithLimit(F transfer_function, size_t limit, D destination, Args&& ... args)
107 {
108 size_t amount = std::min(this->RemainingBytesToTransfer(), limit);
109 if (amount == 0) return 0;
110
111 assert(this->pos < this->buffer.size());
112 assert(this->pos + amount <= this->buffer.size());
113 /* Making buffer a char means casting a lot in the Recv/Send functions. */
114 const char *output_buffer = reinterpret_cast<const char*>(this->buffer.data() + this->pos);
115 ssize_t bytes = transfer_function(destination, output_buffer, static_cast<A>(amount), std::forward<Args>(args)...);
116 if (bytes > 0) this->pos += bytes;
117 return bytes;
118 }
119
135 template <typename A = size_t, typename F, typename D, typename ... Args>
136 ssize_t TransferOut(F transfer_function, D destination, Args&& ... args)
137 {
138 return TransferOutWithLimit<A>(transfer_function, std::numeric_limits<size_t>::max(), destination, std::forward<Args>(args)...);
139 }
140
170 template <typename A = size_t, typename F, typename S, typename ... Args>
171 ssize_t TransferIn(F transfer_function, S source, Args&& ... args)
172 {
173 size_t amount = this->RemainingBytesToTransfer();
174 if (amount == 0) return 0;
175
176 assert(this->pos < this->buffer.size());
177 assert(this->pos + amount <= this->buffer.size());
178 /* Making buffer a char means casting a lot in the Recv/Send functions. */
179 char *input_buffer = reinterpret_cast<char*>(this->buffer.data() + this->pos);
180 ssize_t bytes = transfer_function(source, input_buffer, static_cast<A>(amount), std::forward<Args>(args)...);
181 if (bytes > 0) this->pos += bytes;
182 return bytes;
183 }
184 };
185
186 #endif /* NETWORK_CORE_PACKET_H */
Packet::Recv_uint64
uint64_t Recv_uint64()
Read a 64 bits integer from the packet.
Definition: packet.cpp:338
Packet::Send_uint64
void Send_uint64(uint64_t data)
Package a 64 bits integer in the packet.
Definition: packet.cpp:138
Packet::PrepareToSend
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
Definition: packet.cpp:58
Packet::Size
size_t Size() const
Get the number of bytes in the packet.
Definition: packet.cpp:235
NetworkSocketHandler
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:42
Packet::Send_bytes
size_t Send_bytes(const byte *begin, const byte *end)
Send as many of the bytes as possible in the packet.
Definition: packet.cpp:182
Packet::HasPacketSizeData
bool HasPacketSizeData() const
Check whether the packet, given the position of the "write" pointer, has read enough of the packet to...
Definition: packet.cpp:223
PacketType
uint8_t PacketType
Identifier for the packet.
Definition: packet.h:21
Packet::TransferOut
ssize_t TransferOut(F transfer_function, D destination, Args &&... args)
Transfer data from the packet to the given function.
Definition: packet.h:136
Packet::ParsePacketSize
bool ParsePacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
Definition: packet.cpp:244
Packet::CanReadFromPacket
bool CanReadFromPacket(size_t bytes_to_read, bool close_connection=false)
Is it safe to read from the packet, i.e.
Definition: packet.cpp:204
Packet::Recv_string
std::string Recv_string(size_t length, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads characters (bytes) from the packet until it finds a '\0', or reaches a maximum of length charac...
Definition: packet.cpp:383
Packet::TransferOutWithLimit
ssize_t TransferOutWithLimit(F transfer_function, size_t limit, D destination, Args &&... args)
Transfer data from the packet to the given function.
Definition: packet.h:106
Packet::Packet
Packet(NetworkSocketHandler *cs, size_t limit, size_t initial_read_size=sizeof(PacketSize))
Create a packet that is used to read from a network socket.
Definition: packet.cpp:31
Packet::Send_uint32
void Send_uint32(uint32_t data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:125
Packet::Send_uint8
void Send_uint8(uint8_t data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:104
Packet::Recv_uint32
uint32_t Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:321
Packet::PrepareToRead
void PrepareToRead()
Prepares the packet so it can be read.
Definition: packet.cpp:263
Packet::Send_buffer
void Send_buffer(const std::vector< byte > &data)
Copy a sized byte buffer into the packet.
Definition: packet.cpp:167
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
Packet::Recv_buffer
std::vector< byte > Recv_buffer()
Extract a sized byte buffer from the packet.
Definition: packet.cpp:359
Packet::TransferIn
ssize_t TransferIn(F transfer_function, S source, Args &&... args)
Transfer data from the given function into the packet.
Definition: packet.h:171
Packet
Internal entity of a packet.
Definition: packet.h:42
PacketSize
uint16_t PacketSize
Size of the whole packet.
Definition: packet.h:20
StringValidationSettings
StringValidationSettings
Settings for the string validation.
Definition: string_type.h:44
Packet::Send_bool
void Send_bool(bool data)
Package a boolean in the packet.
Definition: packet.cpp:95
Packet::limit
size_t limit
The limit for the packet size.
Definition: packet.h:49
Packet::GetPacketType
PacketType GetPacketType() const
Get the PacketType from this packet.
Definition: packet.cpp:273
Packet::CanWriteToPacket
bool CanWriteToPacket(size_t bytes_to_write)
Is it safe to write to the packet, i.e.
Definition: packet.cpp:74
Packet::Recv_bool
bool Recv_bool()
Read a boolean from the packet.
Definition: packet.cpp:283
Packet::cs
NetworkSocketHandler * cs
Socket we're associated with.
Definition: packet.h:52
COMPAT_MTU
static const size_t COMPAT_MTU
Number of bytes we can pack in a single packet for backward compatibility.
Definition: config.h:46
core.h
Packet::Send_string
void Send_string(const std::string_view data)
Sends a string over the network.
Definition: packet.cpp:156
os_abstraction.h
Packet::buffer
std::vector< byte > buffer
The buffer of this packet.
Definition: packet.h:47
config.h
Packet::Recv_uint16
uint16_t Recv_uint16()
Read a 16 bits integer from the packet.
Definition: packet.cpp:306
Packet::RemainingBytesToTransfer
size_t RemainingBytesToTransfer() const
Get the amount of bytes that are still available for the Transfer functions.
Definition: packet.cpp:405
SVS_REPLACE_WITH_QUESTION_MARK
@ SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
Definition: string_type.h:46
Packet::Send_uint16
void Send_uint16(uint16_t data)
Package a 16 bits integer in the packet.
Definition: packet.cpp:114
Packet::Recv_uint8
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:292
Packet::pos
PacketSize pos
The current read/write position in the packet.
Definition: packet.h:45