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);
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) {
97 ssize_t res = p.
TransferOut([&](std::span<const uint8_t> buffer) {
98 return sendto(s.first,
reinterpret_cast<const char *
>(buffer.data()),
static_cast<int>(buffer.size()), 0,
reinterpret_cast<const struct sockaddr *
>(send.
GetAddress()), send.
GetAddressLength());
114 for (
auto &s : this->
sockets) {
115 for (
int i = 0; i < 1000; i++) {
116 struct sockaddr_storage client_addr{};
120 socklen_t client_len =
sizeof(client_addr);
124 ssize_t nbytes = p.
TransferIn([&](std::span<uint8_t> buffer) {
125 return recvfrom(s.first,
reinterpret_cast<char *
>(buffer.data()),
static_cast<int>(buffer.size()), 0,
reinterpret_cast<struct sockaddr *
>(&client_addr), &client_len);
129 if (nbytes <= 0)
break;
130 if (nbytes <= 2)
continue;
132 client_len = FixAddrLenForEmscripten(client_addr);
144 Debug(net, 1,
"Invalid packet received (too small / decryption error)");
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
int GetAddressLength()
Get the (valid) length of the address.
bool IsFamily(int family)
Checks of this address is of the given family.
const sockaddr_storage * GetAddress()
Get the address in its internal representation.
std::string GetAddressAsString(bool with_family=true)
Get the address as a string, e.g.
std::string_view AsString() const
Get the string representation of the error message.
static NetworkError GetLast()
Get the last network error.
bool HasClientQuit() const
Whether the current client connected to the socket has quit.
void Reopen()
Reopen the socket so we can send/receive stuff again.
void CloseSocket()
Close the actual UDP socket.
bool Listen()
Start listening on the given host and port.
SocketList sockets
The opened sockets.
void SendPacket(Packet &p, NetworkAddress &recv, bool all=false, bool broadcast=false)
Send a packet over UDP.
void ReceivePackets()
Receive a packet at UDP level.
NetworkUDPSocketHandler(NetworkAddressList *bind=nullptr)
Create an UDP socket but don't listen yet.
virtual void Receive_CLIENT_FIND_SERVER(Packet &p, NetworkAddress &client_addr)
Queries to the server for information about the game.
void ReceiveInvalidPacket(PacketUDPType, NetworkAddress &client_addr)
Helper for logging receiving invalid packets.
NetworkAddressList bind
The address to bind to.
virtual void Receive_SERVER_RESPONSE(Packet &p, NetworkAddress &client_addr)
Response to a query letting the client know we are here.
void HandleUDPPacket(Packet &p, NetworkAddress &client_addr)
Handle an incoming packets by sending it to the correct function.
static const size_t UDP_MTU
Number of bytes we can pack in a single UDP packet.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
bool SetNonBlocking(SOCKET d)
Try to set the socket into non-blocking mode.
Internal entity of a packet.
size_t Size() const
Get the number of bytes in the packet.
bool PrepareToRead()
Prepares the packet so it can be read.
ssize_t TransferIn(F transfer_function)
Transfer data from the given function into the packet.
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.
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
ssize_t TransferOut(F transfer_function)
Transfer data from the packet to the given function.
Basic functions to receive and send UDP packets.
PacketUDPType
Enum with all types of UDP packets.
@ PACKET_UDP_CLIENT_FIND_SERVER
Queries a game server for game information.
@ PACKET_UDP_SERVER_RESPONSE
Reply of the game server with game information.
@ PACKET_UDP_END
Must ALWAYS be on the end of this list!! (period)