OpenTTD Source
12.0-beta2
|
Go to the documentation of this file.
12 #ifndef NETWORK_CORE_PACKET_H
13 #define NETWORK_CORE_PACKET_H
18 #include "../../string_type.h"
75 size_t Send_bytes (
const byte *begin,
const byte *end);
114 if (amount == 0)
return 0;
116 assert(this->pos < this->
buffer.size());
117 assert(this->pos + amount <= this->
buffer.size());
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;
140 template <
typename A = size_t,
typename F,
typename D,
typename ... Args>
141 ssize_t
TransferOut(F transfer_function, D destination, Args&& ... args)
143 return TransferOutWithLimit<A>(transfer_function, std::numeric_limits<size_t>::max(), destination, std::forward<Args>(args)...);
175 template <
typename A = size_t,
typename F,
typename S,
typename ... Args>
176 ssize_t
TransferIn(F transfer_function, S source, Args&& ... args)
179 if (amount == 0)
return 0;
181 assert(this->pos < this->
buffer.size());
182 assert(this->pos + amount <= this->
buffer.size());
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;
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
size_t Size() const
Get the number of bytes in the packet.
SocketHandler for all network sockets in OpenTTD.
size_t Send_bytes(const byte *begin, const byte *end)
Send as many of the bytes as possible in the packet.
bool HasPacketSizeData() const
Check whether the packet, given the position of the "write" pointer, has read enough of the packet to...
ssize_t TransferOut(F transfer_function, D destination, Args &&... args)
Transfer data from the packet to the given function.
bool ParsePacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
uint8 PacketType
Identifier for the packet.
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
static void AddToQueue(Packet **queue, Packet *packet)
Add the given Packet to the end of the queue of packets.
bool CanReadFromPacket(size_t bytes_to_read, bool close_connection=false)
Is it safe to read from the packet, i.e.
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...
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...
ssize_t TransferOutWithLimit(F transfer_function, size_t limit, D destination, Args &&... args)
Transfer data from the packet to the given function.
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.
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
static const uint16 COMPAT_MTU
Number of bytes we can pack in a single packet for backward compatibility.
void PrepareToRead()
Prepares the packet so it can be read.
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
fluid_settings_t * settings
FluidSynth settings handle.
Packet * next
The next packet.
ssize_t TransferIn(F transfer_function, S source, Args &&... args)
Transfer data from the given function into the packet.
Internal entity of a packet.
StringValidationSettings
Settings for the string validation.
void Send_bool(bool data)
Package a boolean in the packet.
size_t limit
The limit for the packet size.
PacketType GetPacketType() const
Get the PacketType from this packet.
uint16 PacketSize
Size of the whole packet.
bool CanWriteToPacket(size_t bytes_to_write)
Is it safe to write to the packet, i.e.
bool Recv_bool()
Read a boolean from the packet.
void Send_uint64(uint64 data)
Package a 64 bits integer in the packet.
NetworkSocketHandler * cs
Socket we're associated with.
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
void Send_string(const std::string_view data)
Sends a string over the network.
uint64 Recv_uint64()
Read a 64 bits integer from the packet.
std::vector< byte > buffer
The buffer of this packet.
size_t RemainingBytesToTransfer() const
Get the amount of bytes that are still available for the Transfer functions.
@ SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
uint16 Recv_uint16()
Read a 16 bits integer from the packet.
PacketSize pos
The current read/write position in the packet.