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

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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 "../../core/convertible_through_base.hpp"
19#include "../../string_type.h"
20
21 typedef uint16_t PacketSize;
22 typedef uint8_t PacketType;
23
43 struct Packet {
44 static constexpr size_t EncodedLengthOfPacketSize() { return sizeof(PacketSize); }
45 static constexpr size_t EncodedLengthOfPacketType() { return sizeof(PacketType); }
46private:
48 PacketSize pos;
50 std::vector<uint8_t> buffer;
52 size_t limit;
53
55 NetworkSocketHandler *cs;
56
57public:
58 Packet(NetworkSocketHandler *cs, size_t limit, size_t initial_read_size = EncodedLengthOfPacketSize());
59 Packet(NetworkSocketHandler *cs, PacketType type, size_t limit = COMPAT_MTU);
60
61 /* Sending/writing of packets */
62 void PrepareToSend();
63
64 bool CanWriteToPacket(size_t bytes_to_write);
65 void Send_bool (bool data);
66 void Send_uint8 (uint8_t data);
67 void Send_uint8 (const ConvertibleThroughBase auto &data) { this->Send_uint8(data.base()); }
68 void Send_uint16(uint16_t data);
69 void Send_uint32(uint32_t data);
70 void Send_uint64(uint64_t data);
71 void Send_string(std::string_view data);
72 void Send_buffer(const std::vector<uint8_t> &data);
73 std::span<const uint8_t> Send_bytes(const std::span<const uint8_t> span);
74
75 /* Reading/receiving of packets */
76 bool HasPacketSizeData() const;
77 bool ParsePacketSize();
78 size_t Size() const;
79 [[nodiscard]] bool PrepareToRead();
80 PacketType GetPacketType() const;
81
82 bool CanReadFromPacket(size_t bytes_to_read, bool close_connection = false);
83 bool Recv_bool ();
84 uint8_t Recv_uint8 ();
85 uint16_t Recv_uint16();
86 uint32_t Recv_uint32();
87 uint64_t Recv_uint64();
88 std::vector<uint8_t> Recv_buffer();
89 size_t Recv_bytes(std::span<uint8_t> span);
90 std::string Recv_string(size_t length, StringValidationSettings settings = StringValidationSetting::ReplaceWithQuestionMark);
91
92 size_t RemainingBytesToTransfer() const;
93
104 template <typename F>
105 ssize_t TransferOutWithLimit(F transfer_function, size_t limit)
106 {
107 size_t amount = std::min(this->RemainingBytesToTransfer(), limit);
108 if (amount == 0) return 0;
109
110 assert(this->pos < this->buffer.size());
111 assert(this->pos + amount <= this->buffer.size());
112 auto output_buffer = std::span<const uint8_t>(this->buffer.data() + this->pos, amount);
113 ssize_t bytes = transfer_function(output_buffer);
114 if (bytes > 0) this->pos += bytes;
115 return bytes;
116 }
117
127 template <typename F>
128 ssize_t TransferOut(F transfer_function)
129 {
130 return TransferOutWithLimit(transfer_function, std::numeric_limits<size_t>::max());
131 }
132
156 template <typename F>
157 ssize_t TransferIn(F transfer_function)
158 {
159 size_t amount = this->RemainingBytesToTransfer();
160 if (amount == 0) return 0;
161
162 assert(this->pos < this->buffer.size());
163 assert(this->pos + amount <= this->buffer.size());
164 auto input_buffer = std::span<uint8_t>(this->buffer.data() + this->pos, amount);
165 ssize_t bytes = transfer_function(input_buffer);
166 if (bytes > 0) this->pos += bytes;
167 return bytes;
168 }
169};
170
171#endif /* NETWORK_CORE_PACKET_H */
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
NetworkSocketHandler
SocketHandler for all network sockets in OpenTTD.
Definition core.h:43
ConvertibleThroughBase
A type is considered 'convertible through base()' when it has a 'base()' function that returns someth...
Definition convertible_through_base.hpp:19
config.h
Configuration options of the network stuff.
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
Base for all network types (UDP and TCP)
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition fluidsynth.cpp:25
os_abstraction.h
Network stuff has many things that needs to be included and/or implemented by default.
PacketType
uint8_t PacketType
Identifier for the packet.
Definition packet.h:22
PacketSize
uint16_t PacketSize
Size of the whole packet.
Definition packet.h:21
StringValidationSetting::ReplaceWithQuestionMark
@ ReplaceWithQuestionMark
Replace the unknown/bad bits with question marks.
Packet
Internal entity of a packet.
Definition packet.h:43
Packet::Size
size_t Size() const
Get the number of bytes in the packet.
Definition packet.cpp:250
Packet::Recv_uint16
uint16_t Recv_uint16()
Read a 16 bits integer from the packet.
Definition packet.cpp:332
Packet::Recv_bytes
size_t Recv_bytes(std::span< uint8_t > span)
Extract at most the length of the span bytes from the packet into the span.
Definition packet.cpp:403
Packet::Recv_uint64
uint64_t Recv_uint64()
Read a 64 bits integer from the packet.
Definition packet.cpp:364
Packet::Recv_bool
bool Recv_bool()
Read a boolean from the packet.
Definition packet.cpp:309
Packet::Recv_uint32
uint32_t Recv_uint32()
Read a 32 bits integer from the packet.
Definition packet.cpp:347
Packet::cs
NetworkSocketHandler * cs
Socket we're associated with.
Definition packet.h:55
Packet::TransferOutWithLimit
ssize_t TransferOutWithLimit(F transfer_function, size_t limit)
Transfer data from the packet to the given function.
Definition packet.h:105
Packet::Send_string
void Send_string(std::string_view data)
Sends a string over the network.
Definition packet.cpp:172
Packet::Recv_string
std::string Recv_string(size_t length, StringValidationSettings settings=StringValidationSetting::ReplaceWithQuestionMark)
Reads characters (bytes) from the packet until it finds a '\0', or reaches a maximum of length charac...
Definition packet.cpp:425
Packet::Send_bool
void Send_bool(bool data)
Package a boolean in the packet.
Definition packet.cpp:111
Packet::PrepareToRead
bool PrepareToRead()
Prepares the packet so it can be read.
Definition packet.cpp:278
Packet::GetPacketType
PacketType GetPacketType() const
Get the PacketType from this packet.
Definition packet.cpp:297
Packet::TransferIn
ssize_t TransferIn(F transfer_function)
Transfer data from the given function into the packet.
Definition packet.h:157
Packet::pos
PacketSize pos
The current read/write position in the packet.
Definition packet.h:48
Packet::Send_bytes
std::span< const uint8_t > Send_bytes(const std::span< const uint8_t > span)
Send as many of the bytes as possible in the packet.
Definition packet.cpp:197
Packet::limit
size_t limit
The limit for the packet size.
Definition packet.h:52
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:238
Packet::Recv_uint8
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.
Definition packet.cpp:318
Packet::ParsePacketSize
bool ParsePacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
Definition packet.cpp:259
Packet::buffer
std::vector< uint8_t > buffer
The buffer of this packet.
Definition packet.h:50
Packet::PrepareToSend
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
Definition packet.cpp:66
Packet::Send_uint8
void Send_uint8(uint8_t data)
Package a 8 bits integer in the packet.
Definition packet.cpp:120
Packet::RemainingBytesToTransfer
size_t RemainingBytesToTransfer() const
Get the amount of bytes that are still available for the Transfer functions.
Definition packet.cpp:447
Packet::Send_uint32
void Send_uint32(uint32_t data)
Package a 32 bits integer in the packet.
Definition packet.cpp:141
Packet::Send_buffer
void Send_buffer(const std::vector< uint8_t > &data)
Copy a sized byte buffer into the packet.
Definition packet.cpp:183
Packet::Recv_buffer
std::vector< uint8_t > Recv_buffer()
Extract a sized byte buffer from the packet.
Definition packet.cpp:385
Packet::Send_uint16
void Send_uint16(uint16_t data)
Package a 16 bits integer in the packet.
Definition packet.cpp:130
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:219
Packet::CanWriteToPacket
bool CanWriteToPacket(size_t bytes_to_write)
Is it safe to write to the packet, i.e.
Definition packet.cpp:90
Packet::Send_uint64
void Send_uint64(uint64_t data)
Package a 64 bits integer in the packet.
Definition packet.cpp:154
Packet::TransferOut
ssize_t TransferOut(F transfer_function)
Transfer data from the packet to the given function.
Definition packet.h:128