10#ifndef NETWORK_CORE_PACKET_H
11#define NETWORK_CORE_PACKET_H
23template <
typename enum_type>
25 static constexpr bool value =
false;
74 template <typename E, typename = std::enable_if_t<IsEnumPacketType<E>::value>>
90 std::span<const uint8_t>
Send_bytes(
const std::span<const uint8_t> span);
121 template <
typename F>
125 if (amount == 0)
return 0;
128 assert(this->pos + amount <= this->
buffer.size());
129 auto output_buffer = std::span<const uint8_t>(this->buffer.data() + this->pos, amount);
130 ssize_t bytes = transfer_function(output_buffer);
131 if (bytes > 0) this->pos += bytes;
144 template <
typename F>
173 template <
typename F>
177 if (amount == 0)
return 0;
180 assert(this->pos + amount <= this->
buffer.size());
181 auto input_buffer = std::span<uint8_t>(this->buffer.data() + this->pos, amount);
182 ssize_t bytes = transfer_function(input_buffer);
183 if (bytes > 0) this->pos += bytes;
SocketHandler for all network sockets in OpenTTD.
A type is considered 'convertible through base()' when it has a 'base()' function that returns someth...
Configuration options of the network stuff.
static const size_t COMPAT_MTU
Number of bytes we can pack in a single packet for backward compatibility.
Concept for unifying the convert through 'base()' behaviour of several 'strong' types.
Base for all network types (UDP and TCP).
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
fluid_settings_t * settings
FluidSynth settings handle.
Includes and/or implementations for the network stuff.
uint8_t PacketType
Identifier for the packet.
uint16_t PacketSize
Size of the whole packet.
@ ReplaceWithQuestionMark
Replace the unknown/bad bits with question marks.
Trait to mark an enumeration as a PacketType.
static constexpr bool value
True iff a PacketType.
size_t Size() const
Get the number of bytes in the packet.
static constexpr size_t ENCODED_LENGTH_OF_PACKET_TYPE
The length of the packet type in the byte stream once it's encoded.
uint16_t Recv_uint16()
Read a 16 bits integer from the packet.
size_t Recv_bytes(std::span< uint8_t > span)
Extract at most the length of the span bytes from the packet into the span.
uint64_t Recv_uint64()
Read a 64 bits integer from the packet.
bool Recv_bool()
Read a boolean from the packet.
uint32_t Recv_uint32()
Read a 32 bits integer from the packet.
NetworkSocketHandler * cs
Socket we're associated with.
ssize_t TransferOutWithLimit(F transfer_function, size_t limit)
Transfer data from the packet to the given function.
void Send_string(std::string_view data)
Sends a string over the network.
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...
void Send_bool(bool data)
Package a boolean in the packet.
bool PrepareToRead()
Prepares the packet so it can be read.
PacketType GetPacketType() const
Get the PacketType from this packet.
ssize_t TransferIn(F transfer_function)
Transfer data from the given function into the packet.
PacketSize pos
The current read/write position in the packet.
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.
size_t limit
The limit for the packet size.
bool HasPacketSizeData() const
Check whether the packet, given the position of the "write" pointer, has read enough of the packet to...
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.
bool ParsePacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
std::vector< uint8_t > buffer
The buffer of this packet.
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
Packet(NetworkSocketHandler *cs, size_t limit, size_t initial_read_size=Packet::ENCODED_LENGTH_OF_PACKET_SIZE)
Create a packet that is used to read from a network socket.
void Send_uint8(uint8_t data)
Package a 8 bits integer in the packet.
size_t RemainingBytesToTransfer() const
Get the amount of bytes that are still available for the Transfer functions.
void Send_uint32(uint32_t data)
Package a 32 bits integer in the packet.
void Send_buffer(const std::vector< uint8_t > &data)
Copy a sized byte buffer into the packet.
void Send_uint8(const ConvertibleThroughBase auto &data)
Package a 8 bits integer in the packet.
std::vector< uint8_t > Recv_buffer()
Extract a sized byte buffer from the packet.
Packet(NetworkSocketHandler *cs, E type, size_t limit=COMPAT_MTU)
Creates a packet to send.
void Send_uint16(uint16_t data)
Package a 16 bits integer in the packet.
bool CanReadFromPacket(size_t bytes_to_read, bool close_connection=false)
Is it safe to read from the packet, i.e.
bool CanWriteToPacket(size_t bytes_to_write)
Is it safe to write to the packet, i.e.
static constexpr size_t ENCODED_LENGTH_OF_PACKET_SIZE
The length of the packet size in the byte stream once it's encoded.
void Send_uint64(uint64_t data)
Package a 64 bits integer in the packet.
ssize_t TransferOut(F transfer_function)
Transfer data from the packet to the given function.