OpenTTD Source  13.2.1
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 #include <functional>
20 #include <limits>
21 
22 typedef uint16 PacketSize;
23 typedef uint8 PacketType;
24 
44 struct Packet {
45 private:
51  std::vector<byte> buffer;
53  size_t limit;
54 
57 
58 public:
59  Packet(NetworkSocketHandler *cs, size_t limit, size_t initial_read_size = sizeof(PacketSize));
60  Packet(PacketType type, size_t limit = COMPAT_MTU);
61 
62  static void AddToQueue(Packet **queue, Packet *packet);
63  static Packet *PopFromQueue(Packet **queue);
64 
65  /* Sending/writing of packets */
66  void PrepareToSend();
67 
68  bool CanWriteToPacket(size_t bytes_to_write);
69  void Send_bool (bool data);
70  void Send_uint8 (uint8 data);
71  void Send_uint16(uint16 data);
72  void Send_uint32(uint32 data);
73  void Send_uint64(uint64 data);
74  void Send_string(const std::string_view data);
75  void Send_buffer(const std::vector<byte> &data);
76  size_t Send_bytes (const byte *begin, const byte *end);
77 
78  /* Reading/receiving of packets */
79  bool HasPacketSizeData() const;
80  bool ParsePacketSize();
81  size_t Size() const;
82  void PrepareToRead();
83  PacketType GetPacketType() const;
84 
85  bool CanReadFromPacket(size_t bytes_to_read, bool close_connection = false);
86  bool Recv_bool ();
87  uint8 Recv_uint8 ();
88  uint16 Recv_uint16();
89  uint32 Recv_uint32();
90  uint64 Recv_uint64();
91  std::vector<byte> Recv_buffer();
93 
94  size_t RemainingBytesToTransfer() const;
95 
108  template <
109  typename A = size_t,
110  typename F,
111  typename D,
112  typename ... Args>
113  ssize_t TransferOutWithLimit(F transfer_function, size_t limit, D destination, Args&& ... args)
114  {
115  size_t amount = std::min(this->RemainingBytesToTransfer(), limit);
116  if (amount == 0) return 0;
117 
118  assert(this->pos < this->buffer.size());
119  assert(this->pos + amount <= this->buffer.size());
120  /* Making buffer a char means casting a lot in the Recv/Send functions. */
121  const char *output_buffer = reinterpret_cast<const char*>(this->buffer.data() + this->pos);
122  ssize_t bytes = transfer_function(destination, output_buffer, static_cast<A>(amount), std::forward<Args>(args)...);
123  if (bytes > 0) this->pos += bytes;
124  return bytes;
125  }
126 
142  template <typename A = size_t, typename F, typename D, typename ... Args>
143  ssize_t TransferOut(F transfer_function, D destination, Args&& ... args)
144  {
145  return TransferOutWithLimit<A>(transfer_function, std::numeric_limits<size_t>::max(), destination, std::forward<Args>(args)...);
146  }
147 
177  template <typename A = size_t, typename F, typename S, typename ... Args>
178  ssize_t TransferIn(F transfer_function, S source, Args&& ... args)
179  {
180  size_t amount = this->RemainingBytesToTransfer();
181  if (amount == 0) return 0;
182 
183  assert(this->pos < this->buffer.size());
184  assert(this->pos + amount <= this->buffer.size());
185  /* Making buffer a char means casting a lot in the Recv/Send functions. */
186  char *input_buffer = reinterpret_cast<char*>(this->buffer.data() + this->pos);
187  ssize_t bytes = transfer_function(source, input_buffer, static_cast<A>(amount), std::forward<Args>(args)...);
188  if (bytes > 0) this->pos += bytes;
189  return bytes;
190  }
191 };
192 
193 #endif /* NETWORK_CORE_PACKET_H */
Packet::PrepareToSend
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
Definition: packet.cpp:83
Packet::Size
size_t Size() const
Get the number of bytes in the packet.
Definition: packet.cpp:260
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:207
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:248
Packet::TransferOut
ssize_t TransferOut(F transfer_function, D destination, Args &&... args)
Transfer data from the packet to the given function.
Definition: packet.h:143
Packet::ParsePacketSize
bool ParsePacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
Definition: packet.cpp:269
Packet::Send_uint8
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:129
PacketType
uint8 PacketType
Identifier for the packet.
Definition: packet.h:23
Packet::Send_uint32
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:150
Packet::AddToQueue
static void AddToQueue(Packet **queue, Packet *packet)
Add the given Packet to the end of the queue of packets.
Definition: packet.cpp:59
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:229
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:408
Packet::PopFromQueue
static Packet * PopFromQueue(Packet **queue)
Pop the packet from the begin of the queue and set the begin of the queue to the second element in th...
Definition: packet.cpp:71
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:113
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::Recv_uint32
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:346
COMPAT_MTU
static const uint16 COMPAT_MTU
Number of bytes we can pack in a single packet for backward compatibility.
Definition: config.h:48
Packet::PrepareToRead
void PrepareToRead()
Prepares the packet so it can be read.
Definition: packet.cpp:288
Packet::Send_buffer
void Send_buffer(const std::vector< byte > &data)
Copy a sized byte buffer into the packet.
Definition: packet.cpp:192
Packet::Send_uint16
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
Definition: packet.cpp:139
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
Packet::next
Packet * next
The next packet.
Definition: packet.h:47
Packet::Recv_buffer
std::vector< byte > Recv_buffer()
Extract a sized byte buffer from the packet.
Definition: packet.cpp:384
Packet::TransferIn
ssize_t TransferIn(F transfer_function, S source, Args &&... args)
Transfer data from the given function into the packet.
Definition: packet.h:178
Packet
Internal entity of a packet.
Definition: packet.h:44
StringValidationSettings
StringValidationSettings
Settings for the string validation.
Definition: string_type.h:49
Packet::Send_bool
void Send_bool(bool data)
Package a boolean in the packet.
Definition: packet.cpp:120
Packet::limit
size_t limit
The limit for the packet size.
Definition: packet.h:53
Packet::GetPacketType
PacketType GetPacketType() const
Get the PacketType from this packet.
Definition: packet.cpp:298
PacketSize
uint16 PacketSize
Size of the whole packet.
Definition: packet.h:22
Packet::CanWriteToPacket
bool CanWriteToPacket(size_t bytes_to_write)
Is it safe to write to the packet, i.e.
Definition: packet.cpp:99
Packet::Recv_bool
bool Recv_bool()
Read a boolean from the packet.
Definition: packet.cpp:308
Packet::Send_uint64
void Send_uint64(uint64 data)
Package a 64 bits integer in the packet.
Definition: packet.cpp:163
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:317
core.h
Packet::Send_string
void Send_string(const std::string_view data)
Sends a string over the network.
Definition: packet.cpp:181
os_abstraction.h
Packet::Recv_uint64
uint64 Recv_uint64()
Read a 64 bits integer from the packet.
Definition: packet.cpp:363
Packet::buffer
std::vector< byte > buffer
The buffer of this packet.
Definition: packet.h:51
config.h
Packet::RemainingBytesToTransfer
size_t RemainingBytesToTransfer() const
Get the amount of bytes that are still available for the Transfer functions.
Definition: packet.cpp:430
SVS_REPLACE_WITH_QUESTION_MARK
@ SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
Definition: string_type.h:51
Packet::Recv_uint16
uint16 Recv_uint16()
Read a 16 bits integer from the packet.
Definition: packet.cpp:331
Packet::pos
PacketSize pos
The current read/write position in the packet.
Definition: packet.h:49