OpenTTD Source  12.0-beta2
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  size_t Send_bytes (const byte *begin, const byte *end);
76 
77  /* Reading/receiving of packets */
78  bool HasPacketSizeData() const;
79  bool ParsePacketSize();
80  size_t Size() const;
81  void PrepareToRead();
82  PacketType GetPacketType() const;
83 
84  bool CanReadFromPacket(size_t bytes_to_read, bool close_connection = false);
85  bool Recv_bool ();
86  uint8 Recv_uint8 ();
87  uint16 Recv_uint16();
88  uint32 Recv_uint32();
89  uint64 Recv_uint64();
91 
92  size_t RemainingBytesToTransfer() const;
93 
106  template <
107  typename A = size_t,
108  typename F,
109  typename D,
110  typename ... Args>
111  ssize_t TransferOutWithLimit(F transfer_function, size_t limit, D destination, Args&& ... args)
112  {
113  size_t amount = std::min(this->RemainingBytesToTransfer(), limit);
114  if (amount == 0) return 0;
115 
116  assert(this->pos < this->buffer.size());
117  assert(this->pos + amount <= this->buffer.size());
118  /* Making buffer a char means casting a lot in the Recv/Send functions. */
119  const char *output_buffer = reinterpret_cast<const char*>(this->buffer.data() + this->pos);
120  ssize_t bytes = transfer_function(destination, output_buffer, static_cast<A>(amount), std::forward<Args>(args)...);
121  if (bytes > 0) this->pos += bytes;
122  return bytes;
123  }
124 
140  template <typename A = size_t, typename F, typename D, typename ... Args>
141  ssize_t TransferOut(F transfer_function, D destination, Args&& ... args)
142  {
143  return TransferOutWithLimit<A>(transfer_function, std::numeric_limits<size_t>::max(), destination, std::forward<Args>(args)...);
144  }
145 
175  template <typename A = size_t, typename F, typename S, typename ... Args>
176  ssize_t TransferIn(F transfer_function, S source, Args&& ... args)
177  {
178  size_t amount = this->RemainingBytesToTransfer();
179  if (amount == 0) return 0;
180 
181  assert(this->pos < this->buffer.size());
182  assert(this->pos + amount <= this->buffer.size());
183  /* Making buffer a char means casting a lot in the Recv/Send functions. */
184  char *input_buffer = reinterpret_cast<char*>(this->buffer.data() + this->pos);
185  ssize_t bytes = transfer_function(source, input_buffer, static_cast<A>(amount), std::forward<Args>(args)...);
186  if (bytes > 0) this->pos += bytes;
187  return bytes;
188  }
189 };
190 
191 #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:249
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:196
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:237
Packet::TransferOut
ssize_t TransferOut(F transfer_function, D destination, Args &&... args)
Transfer data from the packet to the given function.
Definition: packet.h:141
Packet::ParsePacketSize
bool ParsePacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
Definition: packet.cpp:258
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:218
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:380
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:111
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:335
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:277
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::TransferIn
ssize_t TransferIn(F transfer_function, S source, Args &&... args)
Transfer data from the given function into the packet.
Definition: packet.h:176
Packet
Internal entity of a packet.
Definition: packet.h:44
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: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:287
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:297
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:306
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:352
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:402
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:320
Packet::pos
PacketSize pos
The current read/write position in the packet.
Definition: packet.h:49