author | celestar |
Thu, 11 Jan 2007 13:16:26 +0000 | |
branch | custombridgeheads |
changeset 5648 | 1608018c5ff2 |
parent 5643 | 3778051e8095 |
permissions | -rw-r--r-- |
5642 | 1 |
/* $Id$ */ |
2 |
||
3 |
#ifndef NETWORK_CORE_UDP_H |
|
4 |
#define NETWORK_CORE_UDP_H |
|
5 |
||
6 |
#ifdef ENABLE_NETWORK |
|
7 |
||
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
8 |
#include "os_abstraction.h" |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
9 |
#include "game.h" |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
10 |
#include "packet.h" |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
11 |
#include "../../newgrf_config.h" |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
12 |
|
5642 | 13 |
/** |
14 |
* @file udp.h Basic functions to receive and send UDP packets. |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
15 |
* |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
16 |
* |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
17 |
* *** Requesting game information from a server *** |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
18 |
* |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
19 |
* This describes the on-the-wire structure of the request and reply |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
20 |
* packet of the NetworkGameInfo (see game.h) data. |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
21 |
* |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
22 |
* --- Points of attention --- |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
23 |
* - all > 1 byte integral values are written in little endian, |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
24 |
* unless specified otherwise. |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
25 |
* Thus, 0x01234567 would be sent as {0x67, 0x45, 0x23, 0x01}. |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
26 |
* - all sent strings are of variable length and terminated by a '\0'. |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
27 |
* Thus, the length of the strings is not sent. |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
28 |
* - years that are leap years in the 'days since X' to 'date' calculations: |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
29 |
* (year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0)) |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
30 |
* |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
31 |
* --- Request --- |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
32 |
* Bytes: Description: |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
33 |
* 2 size of the whole packet, in this case 3 |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
34 |
* 1 type of packet, in this case PACKET_UDP_CLIENT_FIND_SERVER (0) |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
35 |
* This packet would look like: { 0x03, 0x00, 0x00 } |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
36 |
* |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
37 |
* --- Reply --- |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
38 |
* Version: Bytes: Description: |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
39 |
* all 2 size of the whole packet |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
40 |
* all 1 type of packet, in this case PACKET_UDP_SERVER_RESPONSE (1) |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
41 |
* all 1 the version of this packet's structure |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
42 |
* |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
43 |
* 4+ 1 number of GRFs attached (n) |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
44 |
* 4+ n * 20 unique identifier for GRF files. Constists of: |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
45 |
* - one 4 byte variable with the GRF ID |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
46 |
* - 16 bytes (sent sequentially) for the MD5 checksum |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
47 |
* of the GRF |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
48 |
* |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
49 |
* 3+ 4 current game date in days since 1-1-0 (DMY) |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
50 |
* 3+ 4 game introduction date in days since 1-1-0 (DMY) |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
51 |
* |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
52 |
* 2+ 1 maximum number of companies allowed on the server |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
53 |
* 2+ 1 number of companies on the server |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
54 |
* 2+ 1 maximum number of spectators allowed on the server |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
55 |
* |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
56 |
* 1+ var string with the name of the server |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
57 |
* 1+ var string with the revision of the server |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
58 |
* 1+ 1 the language run on the server |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
59 |
* (0 = any, 1 = English, 2 = German, 3 = French) |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
60 |
* 1+ 1 whether the server uses a password (0 = no, 1 = yes) |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
61 |
* 1+ 1 maximum number of clients allowed on the server |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
62 |
* 1+ 1 number of clients on the server |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
63 |
* 1+ 1 number of spectators on the server |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
64 |
* 1 & 2 2 current game date in days since 1-1-1920 (DMY) |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
65 |
* 1 & 2 2 game introduction date in days since 1-1-1920 (DMY) |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
66 |
* 1+ var string with the name of the map |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
67 |
* 1+ 2 width of the map in tiles |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
68 |
* 1+ 2 height of the map in tiles |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
69 |
* 1+ 1 type of map: |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
70 |
* (0 = temperate, 1 = arctic, 2 = desert, 3 = toyland) |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
71 |
* 1+ 1 whether the server is dedicated (0 = no, 1 = yes) |
5642 | 72 |
*/ |
73 |
||
74 |
///** Sending/receiving of UDP packets **//// |
|
75 |
||
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
76 |
bool NetworkUDPListen(SOCKET *udp, const uint32 host, const uint16 port, const bool broadcast); |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
77 |
void NetworkUDPClose(SOCKET *udp); |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
78 |
|
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
79 |
void NetworkSendUDP_Packet(const SOCKET udp, Packet *p, const struct sockaddr_in *recv); |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
80 |
void NetworkUDPReceive(const SOCKET udp); |
5642 | 81 |
|
82 |
/** |
|
83 |
* Function that is called for every received UDP packet. |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
84 |
* @param udp the socket the packet is received on |
5642 | 85 |
* @param packet the received packet |
86 |
* @param client_addr the address of the sender of the packet |
|
87 |
*/ |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
88 |
void NetworkHandleUDPPacket(const SOCKET udp, Packet *p, const struct sockaddr_in *client_addr); |
5642 | 89 |
|
90 |
||
91 |
///** Sending/receiving of (large) chuncks of UDP packets **//// |
|
92 |
||
93 |
||
94 |
/** Enum with all types of UDP packets. The order MUST not be changed **/ |
|
95 |
enum { |
|
96 |
PACKET_UDP_CLIENT_FIND_SERVER, ///< Queries a game server for game information |
|
97 |
PACKET_UDP_SERVER_RESPONSE, ///< Reply of the game server with game information |
|
98 |
PACKET_UDP_CLIENT_DETAIL_INFO, ///< Queries a game server about details of the game, such as companies |
|
99 |
PACKET_UDP_SERVER_DETAIL_INFO, ///< Reply of the game server about details of the game, such as companies |
|
100 |
PACKET_UDP_SERVER_REGISTER, ///< Packet to register itself to the master server |
|
101 |
PACKET_UDP_MASTER_ACK_REGISTER, ///< Packet indicating registration has succedeed |
|
102 |
PACKET_UDP_CLIENT_GET_LIST, ///< Request for serverlist from master server |
|
103 |
PACKET_UDP_MASTER_RESPONSE_LIST, ///< Response from master server with server ip's + port's |
|
104 |
PACKET_UDP_SERVER_UNREGISTER, ///< Request to be removed from the server-list |
|
105 |
PACKET_UDP_CLIENT_GET_NEWGRFS, ///< Requests the name for a list of GRFs (GRF_ID and MD5) |
|
106 |
PACKET_UDP_SERVER_NEWGRFS, ///< Sends the list of NewGRF's requested. |
|
107 |
PACKET_UDP_END ///< Must ALWAYS be on the end of this list!! (period) |
|
108 |
}; |
|
109 |
||
110 |
void NetworkSend_GRFIdentifier(Packet *p, const GRFConfig *c); |
|
111 |
void NetworkSend_NetworkGameInfo(Packet *p, const NetworkGameInfo *info); |
|
112 |
||
113 |
void NetworkRecv_GRFIdentifier(NetworkClientState *cs, Packet *p, GRFConfig *c); |
|
114 |
void NetworkRecv_NetworkGameInfo(NetworkClientState *cs, Packet *p, NetworkGameInfo *info); |
|
115 |
||
116 |
/** |
|
117 |
* Function that is called for every GRFConfig that is read when receiving |
|
118 |
* a NetworkGameInfo. Only grfid and md5sum are set, the rest is zero. This |
|
119 |
* function must set all appropriate fields. This GRF is later appended to |
|
120 |
* the grfconfig list of the NetworkGameInfo. |
|
121 |
* @param config the GRF to handle |
|
122 |
*/ |
|
123 |
void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config); |
|
124 |
||
125 |
#endif /* ENABLE_NETWORK */ |
|
126 |
||
127 |
#endif /* NETWORK_CORE_UDP_H */ |