Go to the documentation of this file.
12 #include "../../stdafx.h"
13 #include "../../date_func.h"
14 #include "../../debug.h"
18 #include "../../safeguards.h"
26 if (
bind !=
nullptr) {
28 this->bind.push_back(addr);
34 this->bind.emplace_back(
nullptr, 0, AF_INET);
35 this->bind.emplace_back(
nullptr, 0, AF_INET6);
50 addr.Listen(SOCK_DGRAM, &this->
sockets);
53 return this->
sockets.size() != 0;
62 closesocket(s.second);
64 this->sockets.clear();
90 if (!send.
IsFamily(s.first.GetAddress()->ss_family))
continue;
96 unsigned long val = 1;
97 if (setsockopt(s.second, SOL_SOCKET, SO_BROADCAST, (
char *) &val,
sizeof(val)) < 0) {
118 for (
auto &s : this->
sockets) {
119 for (
int i = 0; i < 1000; i++) {
120 struct sockaddr_storage client_addr;
121 memset(&client_addr, 0,
sizeof(client_addr));
124 socklen_t client_len =
sizeof(client_addr);
128 int nbytes = recvfrom(s.second, (
char*)p.
buffer,
SEND_MTU, 0, (
struct sockaddr *)&client_addr, &client_len);
131 if (nbytes <= 0)
break;
132 if (nbytes <= 2)
continue;
133 #ifdef __EMSCRIPTEN__
134 client_len = FixAddrLenForEmscripten(client_addr);
142 if (nbytes != p.
size) {
183 DEBUG(net, 0,
"[udp] received invalid packet type %d from %s", type, client_addr->
GetAddressAsString().c_str());
198 DEBUG(net, 0,
"[udp] received packet type %d on wrong port from %s", type, client_addr->
GetAddressAsString().c_str());
@ PACKET_UDP_CLIENT_GET_LIST
Request for serverlist from master server.
virtual void Receive_SERVER_UNREGISTER(Packet *p, NetworkAddress *client_addr)
A server unregisters itself at the master server.
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
@ PACKET_UDP_CLIENT_DETAIL_INFO
Queries a game server about details of the game, such as companies.
virtual void Receive_MASTER_SESSION_KEY(Packet *p, NetworkAddress *client_addr)
The master server sends us a session key.
const sockaddr_storage * GetAddress()
Get the address in its internal representation.
@ PACKET_UDP_CLIENT_GET_NEWGRFS
Requests the name for a list of GRFs (GRF_ID and MD5)
virtual void Receive_MASTER_RESPONSE_LIST(Packet *p, NetworkAddress *client_addr)
The server sends a list of servers.
byte * buffer
The buffer of this packet, of basically variable length up to SEND_MTU.
int GetAddressLength()
Get the (valid) length of the address.
@ PACKET_UDP_CLIENT_FIND_SERVER
Queries a game server for game information.
@ PACKET_UDP_SERVER_UNREGISTER
Request to be removed from the server-list.
@ PACKET_UDP_SERVER_REGISTER
Packet to register itself to the master server.
NetworkRecvStatus CloseConnection(bool error=true) override
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
void Close() override
Close the given UDP socket.
virtual void Receive_SERVER_NEWGRFS(Packet *p, NetworkAddress *client_addr)
The server returns information about some NewGRFs.
NetworkUDPSocketHandler(NetworkAddressList *bind=nullptr)
Create an UDP socket but don't listen yet.
virtual void Receive_SERVER_REGISTER(Packet *p, NetworkAddress *client_addr)
Registers the server to the master server.
virtual NetworkRecvStatus CloseConnection(bool error=true)
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
virtual void Receive_CLIENT_FIND_SERVER(Packet *p, NetworkAddress *client_addr)
Queries to the server for information about the game.
@ PACKET_UDP_MASTER_SESSION_KEY
Sends a fresh session key to the client.
virtual void Receive_SERVER_DETAIL_INFO(Packet *p, NetworkAddress *client_addr)
Reply with detailed company information.
void Reopen()
Reopen the socket so we can send/receive stuff again.
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
#define DEBUG(name, level,...)
Output a line of debugging information.
virtual void Receive_CLIENT_GET_NEWGRFS(Packet *p, NetworkAddress *client_addr)
The client requests information about some NewGRFs.
void PrepareToRead()
Prepares the packet so it can be read.
SocketList sockets
The opened sockets.
PacketSize size
The size of the whole packet for received packets.
static NetworkError GetLast()
Get the last network error.
@ PACKET_UDP_END
Must ALWAYS be on the end of this list!! (period)
static const uint16 SEND_MTU
Number of bytes we can pack in a single packet.
NetworkAddressList bind
The address to bind to.
@ PACKET_UDP_MASTER_ACK_REGISTER
Packet indicating registration has succeeded.
@ PACKET_UDP_SERVER_DETAIL_INFO
Reply of the game server about details of the game, such as companies.
Internal entity of a packet.
void ReceiveInvalidPacket(PacketUDPType, NetworkAddress *client_addr)
Helper for logging receiving invalid packets.
@ PACKET_UDP_SERVER_NEWGRFS
Sends the list of NewGRF's requested.
@ PACKET_UDP_SERVER_RESPONSE
Reply of the game server with 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...
virtual void Receive_MASTER_ACK_REGISTER(Packet *p, NetworkAddress *client_addr)
The master server acknowledges the registration.
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
void HandleUDPPacket(Packet *p, NetworkAddress *client_addr)
Handle an incoming packets by sending it to the correct function.
bool Listen()
Start listening on the given host and port.
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
virtual void Receive_CLIENT_GET_LIST(Packet *p, NetworkAddress *client_addr)
The client requests a list of servers.
bool SetNonBlocking(SOCKET d)
Try to set the socket into non-blocking mode.
@ PACKET_UDP_MASTER_RESPONSE_LIST
Response from master server with server ip's + port's.
bool IsFamily(int family)
Checks of this address is of the given family.
virtual void Receive_CLIENT_DETAIL_INFO(Packet *p, NetworkAddress *client_addr)
Query for detailed information about companies.
void ReceivePackets()
Receive a packet at UDP level.
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
virtual void Receive_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr)
Return of server information to the client.
PacketUDPType
Enum with all types of UDP packets.
const char * AsString() const
Get the string representation of the error message.
void GetAddressAsString(char *buffer, const char *last, bool with_family=true)
Get the address as a string, e.g.
void SendPacket(Packet *p, NetworkAddress *recv, bool all=false, bool broadcast=false)
Send a packet over UDP.