16 #include "../../stdafx.h" 17 #include "../../date_func.h" 18 #include "../../debug.h" 21 #include "../../safeguards.h" 31 *this->bind.
Append() = *addr;
53 addr->Listen(SOCK_DGRAM, &this->
sockets);
65 closesocket(s->second);
93 if (!send.
IsFamily(s->first.GetAddress()->ss_family))
continue;
97 #ifndef BEOS_NET_SERVER 100 unsigned long val = 1;
101 if (setsockopt(s->second, SOL_SOCKET, SO_BROADCAST, (
char *) &val,
sizeof(val)) < 0) {
102 DEBUG(net, 1,
"[udp] setting broadcast failed with: %i", GET_LAST_ERROR());
124 for (
int i = 0; i < 1000; i++) {
125 struct sockaddr_storage client_addr;
126 memset(&client_addr, 0,
sizeof(client_addr));
129 socklen_t client_len =
sizeof(client_addr);
133 int nbytes = recvfrom(s->second, (
char*)p.
buffer,
SEND_MTU, 0, (
struct sockaddr *)&client_addr, &client_len);
136 if (nbytes <= 0)
break;
137 if (nbytes <= 2)
continue;
144 if (nbytes != p.
size) {
246 for (i = 0; i < num_grfs; i++) {
Date start_date
When the game started.
byte spectators_max
Max spectators allowed on server.
uint16 map_height
Map height.
bool HasClientQuit() const
Whether the current client connected to the socket has quit.
virtual void Receive_CLIENT_GET_NEWGRFS(Packet *p, NetworkAddress *client_addr)
The client requests information about some NewGRFs.
Internal entity of a packet.
Sends a fresh session key to the client.
NetworkUDPSocketHandler(NetworkAddressList *bind=NULL)
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.
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Queries a game server for game information.
#define DAYS_TILL_ORIGINAL_BASE_YEAR
The offset in days from the '_date == 0' till 'ConvertYMDToDate(ORIGINAL_BASE_YEAR, 0, 1)'.
byte game_info_version
Version of the game info.
Requests the name for a list of GRFs (GRF_ID and MD5)
void ReceivePackets()
Receive a packet at UDP level.
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
byte server_lang
Language of the server (we should make a nice table for this)
void Send_string(const char *data)
Sends a string over the network.
virtual void Receive_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr)
Return of server information to the client.
Request for serverlist from master server.
void SendNetworkGameInfo(Packet *p, const NetworkGameInfo *info)
Serializes the NetworkGameInfo struct to the packet.
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
virtual void Receive_SERVER_UNREGISTER(Packet *p, NetworkAddress *client_addr)
A server unregisters itself at the master server.
SocketList sockets
The opened sockets.
void Clear()
Remove all items from the list.
virtual void Receive_MASTER_SESSION_KEY(Packet *p, NetworkAddress *client_addr)
The master server sends us a session key.
GRF file is used statically (can be used in any MP game)
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
const T * Begin() const
Get the pointer to the first item (const)
PacketUDPType
Enum with all types of UDP packets.
uint16 map_width
Map width.
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
Request to be removed from the server-list.
GRFIdentifier ident
grfid and md5sum to uniquely identify newgrfs
void Reopen()
Reopen the socket so we can send/receive stuff again.
const T * End() const
Get the pointer behind the last valid item (const)
struct GRFConfig * next
NOSAVE: Next item in the linked list.
char server_name[NETWORK_NAME_LENGTH]
Server name.
T * Append(uint to_add=1)
Append an item and return it.
void ReceiveInvalidPacket(PacketUDPType, NetworkAddress *client_addr)
Helper for logging receiving invalid packets.
bool IsFamily(int family)
Checks of this address is of the given family.
The game information that is sent from the server to the clients.
byte companies_max
Max companies allowed on server.
void PrepareToRead()
Prepares the packet so it can be read.
virtual void Receive_CLIENT_FIND_SERVER(Packet *p, NetworkAddress *client_addr)
Queries to the server for information about the game.
virtual void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config)
Function that is called for every GRFConfig that is read when receiving a NetworkGameInfo.
byte companies_on
How many started companies do we have.
uint Length() const
Get the number of items in the list.
byte * buffer
The buffer of this packet, of basically variable length up to SEND_MTU.
Must ALWAYS be on the end of this list!! (period)
Information about GRF, used in the game and (part of it) in savegames.
int GetAddressLength()
Get the (valid) length of the address.
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
byte clients_max
Max clients allowed on server.
virtual void Receive_CLIENT_GET_LIST(Packet *p, NetworkAddress *client_addr)
The client requests a list of servers.
Packet to register itself to the master server.
Packet indicating registration has succeeded.
bool use_password
Is this server passworded?
virtual void Receive_SERVER_NEWGRFS(Packet *p, NetworkAddress *client_addr)
The server returns information about some NewGRFs.
Reply of the game server about details of the game, such as companies.
uint8 flags
NOSAVE: GCF_Flags, bitset.
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
static const byte NETWORK_GAME_INFO_VERSION
What version of game-info do we use?
byte clients_on
Current count of clients on server.
char server_revision[NETWORK_REVISION_LENGTH]
The version number the server is using (e.g.: 'r304' or 0.5.0)
PacketSize size
The size of the whole packet for received packets.
virtual void Receive_CLIENT_DETAIL_INFO(Packet *p, NetworkAddress *client_addr)
Query for detailed information about companies.
virtual void Receive_MASTER_ACK_REGISTER(Packet *p, NetworkAddress *client_addr)
The master server acknowledges the registration.
bool Recv_bool()
Read a boolean from the packet.
bool Listen()
Start listening on the given host and port.
bool dedicated
Is this a dedicated server?
void SendPacket(Packet *p, NetworkAddress *recv, bool all=false, bool broadcast=false)
Send a packet over UDP.
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
#define DEBUG(name, level,...)
Output a line of debugging information.
Sends the list of NewGRF's requested.
char map_name[NETWORK_NAME_LENGTH]
Map which is played ["random" for a randomized map].
byte map_set
Graphical set.
Date game_date
Current date.
void HandleUDPPacket(Packet *p, NetworkAddress *client_addr)
Handle an incoming packets by sending it to the correct function.
void Close()
Close the given UDP socket.
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
virtual void Receive_SERVER_DETAIL_INFO(Packet *p, NetworkAddress *client_addr)
Reply with detailed company information.
static bool SetNonBlocking(SOCKET d)
Try to set the socket into non-blocking mode.
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 CDECL error(const char *s,...)
Error handling for fatal non-user errors.
static const uint16 SEND_MTU
Number of bytes we can pack in a single packet.
Response from master server with server ip's + port's.
NetworkAddressList bind
The address to bind to.
void ReceiveNetworkGameInfo(Packet *p, NetworkGameInfo *info)
Deserializes the NetworkGameInfo struct from the packet.
static const uint NETWORK_NUM_LANGUAGES
Number of known languages (to the network protocol) + 1 for 'any'.
NetworkRecvStatus CloseConnection(bool error=true)
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
Queries a game server about details of the game, such as companies.
int32 Date
The type to store our dates in.
Reply of the game server with game information.
virtual void Receive_MASTER_RESPONSE_LIST(Packet *p, NetworkAddress *client_addr)
The server sends a list of servers.
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
void ReceiveGRFIdentifier(Packet *p, GRFIdentifier *grf)
Deserializes the GRFIdentifier (GRF ID and MD5 checksum) from the packet.
Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
uint16 Recv_uint16()
Read a 16 bits integer from the packet.
static const uint NETWORK_NUM_LANDSCAPES
The number of landscapes in OpenTTD.
void Send_bool(bool data)
Package a boolean in the packet.
GRFConfig * grfconfig
List of NewGRF files used.
void SendGRFIdentifier(Packet *p, const GRFIdentifier *grf)
Serializes the GRFIdentifier (GRF ID and MD5 checksum) to the packet.
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...
Basic functions to receive and send UDP packets.
const sockaddr_storage * GetAddress()
Get the address in its internal representation.
void GetAddressAsString(char *buffer, const char *last, bool with_family=true)
Get the address as a string, e.g.
byte spectators_on
How many spectators do we have?
static const uint NETWORK_MAX_GRF_COUNT
Maximum number of GRFs that can be sent.
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.