celestar@5642: /* $Id$ */ celestar@5642: celestar@5642: #ifndef NETWORK_CORE_UDP_H celestar@5642: #define NETWORK_CORE_UDP_H celestar@5642: celestar@5642: #ifdef ENABLE_NETWORK celestar@5642: celestar@5648: #include "os_abstraction.h" celestar@5648: #include "game.h" celestar@5648: #include "packet.h" celestar@5648: #include "../../newgrf_config.h" celestar@5648: celestar@5642: /** celestar@5642: * @file udp.h Basic functions to receive and send UDP packets. celestar@5648: * celestar@5648: * celestar@5648: * *** Requesting game information from a server *** celestar@5648: * celestar@5648: * This describes the on-the-wire structure of the request and reply celestar@5648: * packet of the NetworkGameInfo (see game.h) data. celestar@5648: * celestar@5648: * --- Points of attention --- celestar@5648: * - all > 1 byte integral values are written in little endian, celestar@5648: * unless specified otherwise. celestar@5648: * Thus, 0x01234567 would be sent as {0x67, 0x45, 0x23, 0x01}. celestar@5648: * - all sent strings are of variable length and terminated by a '\0'. celestar@5648: * Thus, the length of the strings is not sent. celestar@5648: * - years that are leap years in the 'days since X' to 'date' calculations: celestar@5648: * (year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0)) celestar@5648: * celestar@5648: * --- Request --- celestar@5648: * Bytes: Description: celestar@5648: * 2 size of the whole packet, in this case 3 celestar@5648: * 1 type of packet, in this case PACKET_UDP_CLIENT_FIND_SERVER (0) celestar@5648: * This packet would look like: { 0x03, 0x00, 0x00 } celestar@5648: * celestar@5648: * --- Reply --- celestar@5648: * Version: Bytes: Description: celestar@5648: * all 2 size of the whole packet celestar@5648: * all 1 type of packet, in this case PACKET_UDP_SERVER_RESPONSE (1) celestar@5648: * all 1 the version of this packet's structure celestar@5648: * celestar@5648: * 4+ 1 number of GRFs attached (n) celestar@5648: * 4+ n * 20 unique identifier for GRF files. Constists of: celestar@5648: * - one 4 byte variable with the GRF ID celestar@5648: * - 16 bytes (sent sequentially) for the MD5 checksum celestar@5648: * of the GRF celestar@5648: * celestar@5648: * 3+ 4 current game date in days since 1-1-0 (DMY) celestar@5648: * 3+ 4 game introduction date in days since 1-1-0 (DMY) celestar@5648: * celestar@5648: * 2+ 1 maximum number of companies allowed on the server celestar@5648: * 2+ 1 number of companies on the server celestar@5648: * 2+ 1 maximum number of spectators allowed on the server celestar@5648: * celestar@5648: * 1+ var string with the name of the server celestar@5648: * 1+ var string with the revision of the server celestar@5648: * 1+ 1 the language run on the server celestar@5648: * (0 = any, 1 = English, 2 = German, 3 = French) celestar@5648: * 1+ 1 whether the server uses a password (0 = no, 1 = yes) celestar@5648: * 1+ 1 maximum number of clients allowed on the server celestar@5648: * 1+ 1 number of clients on the server celestar@5648: * 1+ 1 number of spectators on the server celestar@5648: * 1 & 2 2 current game date in days since 1-1-1920 (DMY) celestar@5648: * 1 & 2 2 game introduction date in days since 1-1-1920 (DMY) celestar@5648: * 1+ var string with the name of the map celestar@5648: * 1+ 2 width of the map in tiles celestar@5648: * 1+ 2 height of the map in tiles celestar@5648: * 1+ 1 type of map: celestar@5648: * (0 = temperate, 1 = arctic, 2 = desert, 3 = toyland) celestar@5648: * 1+ 1 whether the server is dedicated (0 = no, 1 = yes) celestar@5642: */ celestar@5642: celestar@5642: ///** Sending/receiving of UDP packets **//// celestar@5642: celestar@5648: bool NetworkUDPListen(SOCKET *udp, const uint32 host, const uint16 port, const bool broadcast); celestar@5648: void NetworkUDPClose(SOCKET *udp); celestar@5648: celestar@5648: void NetworkSendUDP_Packet(const SOCKET udp, Packet *p, const struct sockaddr_in *recv); celestar@5648: void NetworkUDPReceive(const SOCKET udp); celestar@5642: celestar@5642: /** celestar@5642: * Function that is called for every received UDP packet. celestar@5648: * @param udp the socket the packet is received on celestar@5642: * @param packet the received packet celestar@5642: * @param client_addr the address of the sender of the packet celestar@5642: */ celestar@5648: void NetworkHandleUDPPacket(const SOCKET udp, Packet *p, const struct sockaddr_in *client_addr); celestar@5642: celestar@5642: celestar@5642: ///** Sending/receiving of (large) chuncks of UDP packets **//// celestar@5642: celestar@5642: celestar@5642: /** Enum with all types of UDP packets. The order MUST not be changed **/ celestar@5642: enum { celestar@5642: PACKET_UDP_CLIENT_FIND_SERVER, ///< Queries a game server for game information celestar@5642: PACKET_UDP_SERVER_RESPONSE, ///< Reply of the game server with game information celestar@5642: PACKET_UDP_CLIENT_DETAIL_INFO, ///< Queries a game server about details of the game, such as companies celestar@5642: PACKET_UDP_SERVER_DETAIL_INFO, ///< Reply of the game server about details of the game, such as companies celestar@5642: PACKET_UDP_SERVER_REGISTER, ///< Packet to register itself to the master server celestar@5642: PACKET_UDP_MASTER_ACK_REGISTER, ///< Packet indicating registration has succedeed celestar@5642: PACKET_UDP_CLIENT_GET_LIST, ///< Request for serverlist from master server celestar@5642: PACKET_UDP_MASTER_RESPONSE_LIST, ///< Response from master server with server ip's + port's celestar@5642: PACKET_UDP_SERVER_UNREGISTER, ///< Request to be removed from the server-list celestar@5642: PACKET_UDP_CLIENT_GET_NEWGRFS, ///< Requests the name for a list of GRFs (GRF_ID and MD5) celestar@5642: PACKET_UDP_SERVER_NEWGRFS, ///< Sends the list of NewGRF's requested. celestar@5642: PACKET_UDP_END ///< Must ALWAYS be on the end of this list!! (period) celestar@5642: }; celestar@5642: celestar@5642: void NetworkSend_GRFIdentifier(Packet *p, const GRFConfig *c); celestar@5642: void NetworkSend_NetworkGameInfo(Packet *p, const NetworkGameInfo *info); celestar@5642: celestar@5642: void NetworkRecv_GRFIdentifier(NetworkClientState *cs, Packet *p, GRFConfig *c); celestar@5642: void NetworkRecv_NetworkGameInfo(NetworkClientState *cs, Packet *p, NetworkGameInfo *info); celestar@5642: celestar@5642: /** celestar@5642: * Function that is called for every GRFConfig that is read when receiving celestar@5642: * a NetworkGameInfo. Only grfid and md5sum are set, the rest is zero. This celestar@5642: * function must set all appropriate fields. This GRF is later appended to celestar@5642: * the grfconfig list of the NetworkGameInfo. celestar@5642: * @param config the GRF to handle celestar@5642: */ celestar@5642: void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config); celestar@5642: celestar@5642: #endif /* ENABLE_NETWORK */ celestar@5642: celestar@5642: #endif /* NETWORK_CORE_UDP_H */