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)");
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.
const std::string & 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,...)
Ouptut a line of debugging information.
bool SetNonBlocking([[maybe_unused]] 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.
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.
ssize_t TransferIn(F transfer_function, S source, Args &&... args)
Transfer data from the given function into the packet.
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
ssize_t TransferOut(F transfer_function, D destination, Args &&... args)
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)