]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - netconn.h
-Added a new hostcache interface:
[xonotic/darkplaces.git] / netconn.h
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3 Copyright (C) 2003 Forest Hale
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 */
21
22 #ifndef NET_H
23 #define NET_H
24
25 #include "lhnet.h"
26
27 #define NET_HEADERSIZE          (2 * sizeof(unsigned int))
28
29 // NetHeader flags
30 #define NETFLAG_LENGTH_MASK     0x0000ffff
31 #define NETFLAG_DATA            0x00010000
32 #define NETFLAG_ACK                     0x00020000
33 #define NETFLAG_NAK                     0x00040000
34 #define NETFLAG_EOM                     0x00080000
35 #define NETFLAG_UNRELIABLE      0x00100000
36 #define NETFLAG_CTL                     0x80000000
37
38
39 #define NET_PROTOCOL_VERSION    3
40
41 // This is the network info/connection protocol.  It is used to find Quake
42 // servers, get info about them, and connect to them.  Once connected, the
43 // Quake game protocol (documented elsewhere) is used.
44 //
45 //
46 // General notes:
47 //      game_name is currently always "QUAKE", but is there so this same protocol
48 //              can be used for future games as well; can you say Quake2?
49 //
50 // CCREQ_CONNECT
51 //              string  game_name                               "QUAKE"
52 //              byte    net_protocol_version    NET_PROTOCOL_VERSION
53 //
54 // CCREQ_SERVER_INFO
55 //              string  game_name                               "QUAKE"
56 //              byte    net_protocol_version    NET_PROTOCOL_VERSION
57 //
58 // CCREQ_PLAYER_INFO
59 //              byte    player_number
60 //
61 // CCREQ_RULE_INFO
62 //              string  rule
63 //
64 //
65 //
66 // CCREP_ACCEPT
67 //              long    port
68 //
69 // CCREP_REJECT
70 //              string  reason
71 //
72 // CCREP_SERVER_INFO
73 //              string  server_address
74 //              string  host_name
75 //              string  level_name
76 //              byte    current_players
77 //              byte    max_players
78 //              byte    protocol_version        NET_PROTOCOL_VERSION
79 //
80 // CCREP_PLAYER_INFO
81 //              byte    player_number
82 //              string  name
83 //              long    colors
84 //              long    frags
85 //              long    connect_time
86 //              string  address
87 //
88 // CCREP_RULE_INFO
89 //              string  rule
90 //              string  value
91
92 //      note:
93 //              There are two address forms used above.  The short form is just a
94 //              port number.  The address that goes along with the port is defined as
95 //              "whatever address you receive this reponse from".  This lets us use
96 //              the host OS to solve the problem of multiple host addresses (possibly
97 //              with no routing between them); the host will use the right address
98 //              when we reply to the inbound connection request.  The long from is
99 //              a full address and port in a string.  It is used for returning the
100 //              address of a server that is not running locally.
101
102 #define CCREQ_CONNECT           0x01
103 #define CCREQ_SERVER_INFO       0x02
104 #define CCREQ_PLAYER_INFO       0x03
105 #define CCREQ_RULE_INFO         0x04
106
107 #define CCREP_ACCEPT            0x81
108 #define CCREP_REJECT            0x82
109 #define CCREP_SERVER_INFO       0x83
110 #define CCREP_PLAYER_INFO       0x84
111 #define CCREP_RULE_INFO         0x85
112
113 typedef struct netconn_s
114 {
115         struct netconn_s *next;
116
117         lhnetsocket_t *mysocket;
118         lhnetaddress_t peeraddress;
119         
120         // this is mostly identical to qsocket_t from quake
121
122         // if this time is reached, kick off peer
123         double connecttime;
124         double timeout;
125         double lastMessageTime;
126         double lastSendTime;
127
128         qboolean canSend;
129         qboolean sendNext;
130
131         unsigned int ackSequence;
132         unsigned int sendSequence;
133         unsigned int unreliableSendSequence;
134         int sendMessageLength;
135         qbyte sendMessage[NET_MAXMESSAGE];
136
137         unsigned int receiveSequence;
138         unsigned int unreliableReceiveSequence;
139         int receiveMessageLength;
140         qbyte receiveMessage[NET_MAXMESSAGE];
141
142         char address[128];
143 } netconn_t;
144
145 extern netconn_t *netconn_list;
146 extern mempool_t *netconn_mempool;
147
148 extern cvar_t hostname;
149 extern cvar_t developer_networking;
150 extern char playername[];
151 extern int playercolor;
152
153 #define HOSTCACHE_TOTALSIZE                     2048
154 #define HOSTCACHE_VIEWCACHESIZE         128
155
156 typedef enum 
157 {
158         HCMO_GREATEREQUAL,
159         HCMO_GREATER,
160         HCMO_EQUAL,
161         HCMO_LESSEQUAL,
162         HCMO_LESS,
163 } hostcache_maskop_t;
164
165 // struct with all fields that you can search for or sort by
166 typedef struct
167 {
168         // address for connecting
169         char cname[128];
170         // ping time for sorting servers
171         int ping;
172         // name of the game
173         char game[32];
174         // name of the mod
175         char mod[32];
176         // name of the map
177         char map[32];
178         // name of the session
179         char name[128];
180         // max client number
181         int maxplayers;
182         // number of currently connected players
183         int numplayers;
184         // protocol version
185         int protocol;
186 } hostcache_info_t;
187
188 typedef enum 
189 {
190         HCIF_CNAME,
191         HCIF_PING,
192         HCIF_GAME,
193         HCIF_MOD,
194         HCIF_MAP,
195         HCIF_NAME,
196         HCIF_MAXPLAYERS,
197         HCIF_NUMPLAYERS,
198         HCIF_PROTOCOL,
199         HCIF_COUNT
200 } hostcache_infofield_t;
201
202 typedef struct
203 {
204         // used to determine whether this entry should be included into the final view
205         qboolean finished; 
206         // used to calculate ping when update comes in
207         double querytime;
208
209         hostcache_info_t info;
210         
211         // legacy stuff
212         char line1[128];
213         char line2[128];
214 } hostcache_t;
215
216 typedef struct
217 {
218         hostcache_maskop_t pingtest;
219         hostcache_maskop_t maxplayerstest;
220         hostcache_maskop_t numplayerstest;
221         hostcache_maskop_t protocoltest;
222         hostcache_info_t info;
223 } hostcache_mask_t;
224
225 extern hostcache_mask_t                 hostcache_currentmask;
226 extern hostcache_infofield_t    hostcache_sortbyfield;
227 extern qboolean                                 hostcache_sortdescending;
228
229 extern int                      hostcache_viewcount;
230 extern hostcache_t      *hostcache_viewset[HOSTCACHE_VIEWCACHESIZE];
231
232 extern qboolean         hostcache_consoleoutput;
233
234 #if !defined(_WIN32 ) && !defined (__linux__) && !defined (__sun__)
235 #ifndef htonl
236 extern unsigned long htonl (unsigned long hostlong);
237 #endif
238 #ifndef htons
239 extern unsigned short htons (unsigned short hostshort);
240 #endif
241 #ifndef ntohl
242 extern unsigned long ntohl (unsigned long netlong);
243 #endif
244 #ifndef ntohs
245 extern unsigned short ntohs (unsigned short netshort);
246 #endif
247 #endif
248
249 //============================================================================
250 //
251 // public network functions
252 //
253 //============================================================================
254
255 extern double masterquerytime;
256 extern int masterquerycount;
257 extern int masterreplycount;
258 extern int serverquerycount;
259 extern int serverreplycount;
260
261 extern sizebuf_t net_message;
262
263 extern cvar_t cl_netlocalping;
264
265 int NetConn_SendReliableMessage(netconn_t *conn, sizebuf_t *data);
266 //void NetConn_SendMessageNext(netconn_t *conn);
267 //void NetConn_ReSendMessage(netconn_t *conn);
268 qboolean NetConn_CanSendMessage(netconn_t *conn);
269 int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data);
270 void NetConn_CloseClientPorts(void);
271 void NetConn_OpenClientPorts(void);
272 void NetConn_CloseServerPorts(void);
273 void NetConn_OpenServerPorts(int opennetports);
274 lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address);
275 lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address);
276 void NetConn_Init(void);
277 void NetConn_Shutdown(void);
278 netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress);
279 void NetConn_Close(netconn_t *conn);
280 void NetConn_Listen(qboolean state);
281 int NetConn_IsLocalGame(void);
282 //int NetConn_ReceivedMessage(netconn_t *conn, qbyte *data, int length);
283 //int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length, lhnetaddress_t *peeraddress);
284 //int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length, lhnetaddress_t *peeraddress);
285 void NetConn_ClientFrame(void);
286 void NetConn_ServerFrame(void);
287 void NetConn_QueryMasters(void);
288 void NetConn_Heartbeat(int priority);
289 int NetConn_SendToAll(sizebuf_t *data, double blocktime);
290 void Net_Stats_f(void);
291 void Net_Slist_f(void);
292
293 // Hostcache interface
294 // manually refresh the view set, do this after having changed the mask or any other flag
295 void HostCache_RebuildViewSet(void);
296 void HostCache_ResetMask(void);
297 void HostCache_QueryList(void);
298
299 #endif
300