OpenTTD Source
1.11.0-beta2
|
Go to the documentation of this file.
12 #include "../../stdafx.h"
13 #include "../../date_func.h"
14 #include "../../debug.h"
17 #include "../../safeguards.h"
25 if (
bind !=
nullptr) {
27 this->bind.push_back(addr);
33 this->bind.emplace_back(
nullptr, 0, AF_INET);
34 this->bind.emplace_back(
nullptr, 0, AF_INET6);
49 addr.Listen(SOCK_DGRAM, &this->
sockets);
52 return this->
sockets.size() != 0;
61 closesocket(s.second);
63 this->sockets.clear();
89 if (!send.
IsFamily(s.first.GetAddress()->ss_family))
continue;
95 unsigned long val = 1;
96 if (setsockopt(s.second, SOL_SOCKET, SO_BROADCAST, (
char *) &val,
sizeof(val)) < 0) {
97 DEBUG(net, 1,
"[udp] setting broadcast failed with: %i", GET_LAST_ERROR());
106 if (res == -1)
DEBUG(net, 1,
"[udp] sendto(%s) failed with: %i", send.
GetAddressAsString().c_str(), GET_LAST_ERROR());
117 for (
auto &s : this->
sockets) {
118 for (
int i = 0; i < 1000; i++) {
119 struct sockaddr_storage client_addr;
120 memset(&client_addr, 0,
sizeof(client_addr));
123 socklen_t client_len =
sizeof(client_addr);
127 int nbytes = recvfrom(s.second, (
char*)p.
buffer,
SEND_MTU, 0, (
struct sockaddr *)&client_addr, &client_len);
130 if (nbytes <= 0)
break;
131 if (nbytes <= 2)
continue;
132 #ifdef __EMSCRIPTEN__
133 client_len = FixAddrLenForEmscripten(client_addr);
141 if (nbytes != p.
size) {
243 for (i = 0; i < num_grfs; i++) {
319 DEBUG(net, 0,
"[udp] received invalid packet type %d from %s", type, client_addr->
GetAddressAsString().c_str());
334 DEBUG(net, 0,
"[udp] received packet type %d on wrong port from %s", type, client_addr->
GetAddressAsString().c_str());
The game information that is sent from the server to the clients.
@ 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.
void Recv_string(char *buffer, size_t size, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads a string till it finds a '\0' in the stream.
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)
byte companies_max
Max companies allowed on server.
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.
static const uint NETWORK_NUM_LANDSCAPES
The number of landscapes in OpenTTD.
@ PACKET_UDP_CLIENT_FIND_SERVER
Queries a game server for game information.
static const uint NETWORK_NUM_LANGUAGES
Number of known languages (to the network protocol) + 1 for 'any'.
@ PACKET_UDP_SERVER_UNREGISTER
Request to be removed from the server-list.
char map_name[NETWORK_NAME_LENGTH]
Map which is played ["random" for a randomized map].
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
@ 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 ...
bool use_password
Is this server passworded?
void Close() override
Close the given UDP socket.
byte game_info_version
Version of the game info.
GRFIdentifier ident
grfid and md5sum to uniquely identify newgrfs
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.
uint16 map_width
Map width.
virtual NetworkRecvStatus CloseConnection(bool error=true)
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
virtual void Receive_CLIENT_FIND_SERVER(Packet *p, NetworkAddress *client_addr)
Queries to the server for information about the game.
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
@ PACKET_UDP_MASTER_SESSION_KEY
Sends a fresh session key to the client.
virtual void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config)
Function that is called for every GRFConfig that is read when receiving a NetworkGameInfo.
static bool SetNonBlocking(SOCKET d)
Try to set the socket into non-blocking mode.
static const uint NETWORK_MAX_GRF_COUNT
Maximum number of GRFs that can be sent.
Date start_date
When the game started.
virtual void Receive_SERVER_DETAIL_INFO(Packet *p, NetworkAddress *client_addr)
Reply with detailed company information.
byte server_lang
Language of the server (we should make a nice table for this)
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
void Reopen()
Reopen the socket so we can send/receive stuff again.
Information about GRF, used in the game and (part of it) in savegames.
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
byte companies_on
How many started companies do we have.
#define DEBUG(name, level,...)
Output a line of debugging information.
void Send_string(const char *data)
Sends a string over the network.
virtual void Receive_CLIENT_GET_NEWGRFS(Packet *p, NetworkAddress *client_addr)
The client requests information about some NewGRFs.
uint8 flags
NOSAVE: GCF_Flags, bitset.
void PrepareToRead()
Prepares the packet so it can be read.
Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
SocketList sockets
The opened sockets.
void SendGRFIdentifier(Packet *p, const GRFIdentifier *grf)
Serializes the GRFIdentifier (GRF ID and MD5 checksum) to the packet.
PacketSize size
The size of the whole packet for received packets.
@ PACKET_UDP_END
Must ALWAYS be on the end of this list!! (period)
int32 Date
The type to store our dates in.
static const uint16 SEND_MTU
Number of bytes we can pack in a single packet.
NetworkAddressList bind
The address to bind to.
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
@ 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.
void SendNetworkGameInfo(Packet *p, const NetworkGameInfo *info)
Serializes the NetworkGameInfo struct to the packet.
void ReceiveNetworkGameInfo(Packet *p, NetworkGameInfo *info)
Deserializes the NetworkGameInfo struct from the packet.
Internal entity of a packet.
char server_name[NETWORK_NAME_LENGTH]
Server name.
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.
void Send_bool(bool data)
Package a boolean in the packet.
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...
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
virtual void Receive_MASTER_ACK_REGISTER(Packet *p, NetworkAddress *client_addr)
The master server acknowledges the registration.
byte clients_max
Max clients allowed on server.
bool Recv_bool()
Read a boolean from the packet.
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.
struct GRFConfig * next
NOSAVE: Next item in the linked list.
bool Listen()
Start listening on the given host and port.
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Date game_date
Current date.
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
void ReceiveGRFIdentifier(Packet *p, GRFIdentifier *grf)
Deserializes the GRFIdentifier (GRF ID and MD5 checksum) from the packet.
virtual void Receive_CLIENT_GET_LIST(Packet *p, NetworkAddress *client_addr)
The client requests a list of servers.
char server_revision[NETWORK_REVISION_LENGTH]
The version number the server is using (e.g.: 'r304' or 0.5.0)
@ 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.
byte spectators_on
How many spectators do we have?
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.
static const byte NETWORK_GAME_INFO_VERSION
What version of game-info do we use?
uint16 map_height
Map height.
byte clients_on
Current count of clients on server.
GRFConfig * grfconfig
List of NewGRF files used.
PacketUDPType
Enum with all types of UDP packets.
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.
@ GCF_STATIC
GRF file is used statically (can be used in any MP game)
bool dedicated
Is this a dedicated server?
uint16 Recv_uint16()
Read a 16 bits integer from the packet.
#define DAYS_TILL_ORIGINAL_BASE_YEAR
The offset in days from the '_date == 0' till 'ConvertYMDToDate(ORIGINAL_BASE_YEAR,...
byte spectators_max
Max spectators allowed on server.
static const Year MAX_YEAR
MAX_YEAR, nicely rounded value of the number of years that can be encoded in a single 32 bits date,...
byte map_set
Graphical set.