OpenTTD Source
20240919-master-gdf0233f4c2
|
Go to the documentation of this file.
12 #include "../../stdafx.h"
13 #include "../../timer/timer_game_calendar.h"
14 #include "../../debug.h"
15 #include "network_game_info.h"
18 #include "../../safeguards.h"
26 if (
bind !=
nullptr) {
28 this->bind.push_back(addr);
34 this->bind.emplace_back(
"", 0, AF_INET);
35 this->bind.emplace_back(
"", 0, AF_INET6);
50 addr.Listen(SOCK_DGRAM, &this->
sockets);
64 this->sockets.clear();
84 if (!send.
IsFamily(s.second.GetAddress()->ss_family))
continue;
90 unsigned long val = 1;
91 if (setsockopt(s.first, SOL_SOCKET, SO_BROADCAST, (
char *) &val,
sizeof(val)) < 0) {
112 for (
auto &s : this->
sockets) {
113 for (
int i = 0; i < 1000; i++) {
114 struct sockaddr_storage client_addr;
115 memset(&client_addr, 0,
sizeof(client_addr));
119 socklen_t client_len =
sizeof(client_addr);
123 ssize_t nbytes = p.
TransferIn<
int>(recvfrom, s.first, 0, (
struct sockaddr *)&client_addr, &client_len);
126 if (nbytes <= 0)
break;
127 if (nbytes <= 2)
continue;
128 #ifdef __EMSCRIPTEN__
129 client_len = FixAddrLenForEmscripten(client_addr);
141 Debug(net, 1,
"Invalid packet received (too small / decryption error)");
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
bool PrepareToRead()
Prepares the packet so it can be read.
size_t Size() const
Get the number of bytes in the packet.
void SendPacket(Packet &p, NetworkAddress &recv, bool all=false, bool broadcast=false)
Send a packet over UDP.
const sockaddr_storage * GetAddress()
Get the address in its internal representation.
bool SetNonBlocking([[maybe_unused]] SOCKET d)
Try to set the socket into non-blocking mode.
virtual void Receive_CLIENT_FIND_SERVER(Packet &p, NetworkAddress &client_addr)
Queries to the server for information about the game.
int GetAddressLength()
Get the (valid) length of the address.
ssize_t TransferOut(F transfer_function, D destination, Args &&... args)
Transfer data from the packet to the given function.
void CloseSocket()
Close the actual UDP socket.
NetworkUDPSocketHandler(NetworkAddressList *bind=nullptr)
Create an UDP socket but don't listen yet.
bool ParsePacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
virtual void Receive_SERVER_RESPONSE(Packet &p, NetworkAddress &client_addr)
Response to a query letting the client know we are here.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
@ PACKET_UDP_SERVER_RESPONSE
Reply of the game server with game information.
void Reopen()
Reopen the socket so we can send/receive stuff again.
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
SocketList sockets
The opened sockets.
static const size_t UDP_MTU
Number of bytes we can pack in a single UDP packet.
static NetworkError GetLast()
Get the last network error.
NetworkAddressList bind
The address to bind to.
std::string GetAddressAsString(bool with_family=true)
Get the address as a string, e.g.
ssize_t TransferIn(F transfer_function, S source, Args &&... args)
Transfer data from the given function into the packet.
void HandleUDPPacket(Packet &p, NetworkAddress &client_addr)
Handle an incoming packets by sending it to the correct function.
Internal entity of a packet.
@ PACKET_UDP_CLIENT_FIND_SERVER
Queries a game server for game information.
bool HasClientQuit() const
Whether the current client connected to the socket has quit.
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
PacketUDPType
Enum with all types of UDP packets.
const std::string & AsString() const
Get the string representation of the error message.
bool Listen()
Start listening on the given host and port.
@ PACKET_UDP_END
Must ALWAYS be on the end of this list!! (period)
bool IsFamily(int family)
Checks of this address is of the given family.
void ReceivePackets()
Receive a packet at UDP level.
void ReceiveInvalidPacket(PacketUDPType, NetworkAddress &client_addr)
Helper for logging receiving invalid packets.
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.