24 if (
bind !=
nullptr) {
26 this->bind.push_back(addr);
32 this->bind.emplace_back(
"", 0, AF_INET);
33 this->bind.emplace_back(
"", 0, AF_INET6);
48 addr.Listen(SOCK_DGRAM, &this->
sockets);
62 this->sockets.clear();
82 if (!send.
IsFamily(s.second.GetAddress()->ss_family))
continue;
88 unsigned long val = 1;
89 if (setsockopt(s.first, SOL_SOCKET, SO_BROADCAST, (
char *) &val,
sizeof(val)) < 0) {
95 ssize_t res = p.
TransferOut([&](std::span<const uint8_t> buffer) {
96 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());
112 for (
auto &s : this->
sockets) {
113 for (
int i = 0; i < 1000; i++) {
114 struct sockaddr_storage client_addr{};
118 socklen_t client_len =
sizeof(client_addr);
122 ssize_t nbytes = p.
TransferIn([&](std::span<uint8_t> buffer) {
123 return recvfrom(s.first,
reinterpret_cast<char *
>(buffer.data()),
static_cast<int>(buffer.size()), 0,
reinterpret_cast<struct sockaddr *
>(&client_addr), &client_len);
127 if (nbytes <= 0)
break;
128 if (nbytes <= 2)
continue;
130 client_len = FixAddrLenForEmscripten(client_addr);
142 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.
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.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Convert NetworkGameInfo to Packet and back.
bool SetNonBlocking(SOCKET d)
Try to set the socket into non-blocking mode.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
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.
Definition of the game-calendar-timer.
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).