]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - netconn.h
use checkdisk flag on model loading after ingame download, this should
[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 #define NETFLAG_CRYPTO          0x40000000
38
39
40 #define NET_PROTOCOL_VERSION    3
41 #define NET_EXTRESPONSE_MAX 16
42
43 /// \page netconn The network info/connection protocol.
44 /// It is used to find Quake
45 /// servers, get info about them, and connect to them.  Once connected, the
46 /// Quake game protocol (documented elsewhere) is used.
47 ///
48 ///
49 /// General notes:\code
50 ///     game_name is currently always "QUAKE", but is there so this same protocol
51 ///             can be used for future games as well; can you say Quake2?
52 ///
53 /// CCREQ_CONNECT
54 ///             string  game_name                               "QUAKE"
55 ///             byte    net_protocol_version    NET_PROTOCOL_VERSION
56 ///
57 /// CCREQ_SERVER_INFO
58 ///             string  game_name                               "QUAKE"
59 ///             byte    net_protocol_version    NET_PROTOCOL_VERSION
60 ///
61 /// CCREQ_PLAYER_INFO
62 ///             byte    player_number
63 ///
64 /// CCREQ_RULE_INFO
65 ///             string  rule
66 ///
67 /// CCREQ_RCON
68 ///             string  password
69 ///             string  command
70 ///
71 ///
72 ///
73 /// CCREP_ACCEPT
74 ///             long    port
75 ///
76 /// CCREP_REJECT
77 ///             string  reason
78 ///
79 /// CCREP_SERVER_INFO
80 ///             string  server_address
81 ///             string  host_name
82 ///             string  level_name
83 ///             byte    current_players
84 ///             byte    max_players
85 ///             byte    protocol_version        NET_PROTOCOL_VERSION
86 ///
87 /// CCREP_PLAYER_INFO
88 ///             byte    player_number
89 ///             string  name
90 ///             long    colors
91 ///             long    frags
92 ///             long    connect_time
93 ///             string  address
94 ///
95 /// CCREP_RULE_INFO
96 ///             string  rule
97 ///             string  value
98 ///
99 /// CCREP_RCON
100 ///             string  reply
101 /// \endcode
102 ///     \note
103 ///             There are two address forms used above.  The short form is just a
104 ///             port number.  The address that goes along with the port is defined as
105 ///             "whatever address you receive this reponse from".  This lets us use
106 ///             the host OS to solve the problem of multiple host addresses (possibly
107 ///             with no routing between them); the host will use the right address
108 ///             when we reply to the inbound connection request.  The long from is
109 ///             a full address and port in a string.  It is used for returning the
110 ///             address of a server that is not running locally.
111
112 #define CCREQ_CONNECT           0x01
113 #define CCREQ_SERVER_INFO       0x02
114 #define CCREQ_PLAYER_INFO       0x03
115 #define CCREQ_RULE_INFO         0x04
116 #define CCREQ_RCON              0x05 // RocketGuy: ProQuake rcon support
117
118 #define CCREP_ACCEPT            0x81
119 #define CCREP_REJECT            0x82
120 #define CCREP_SERVER_INFO       0x83
121 #define CCREP_PLAYER_INFO       0x84
122 #define CCREP_RULE_INFO         0x85
123 #define CCREP_RCON              0x86 // RocketGuy: ProQuake rcon support
124
125 typedef struct netgraphitem_s
126 {
127         double time;
128         int reliablebytes;
129         int unreliablebytes;
130         int ackbytes;
131 }
132 netgraphitem_t;
133
134 typedef struct netconn_s
135 {
136         struct netconn_s *next;
137
138         lhnetsocket_t *mysocket;
139         lhnetaddress_t peeraddress;
140
141         // this is mostly identical to qsocket_t from quake
142
143         /// if this time is reached, kick off peer
144         double connecttime;
145         double timeout;
146         double lastMessageTime;
147         double lastSendTime;
148
149         /// writing buffer to send to peer as the next reliable message
150         /// can be added to at any time, copied into sendMessage buffer when it is
151         /// possible to send a reliable message and then cleared
152         /// @{
153         sizebuf_t message;
154         unsigned char messagedata[NET_MAXMESSAGE];
155         /// @}
156
157         /// reliable message that is currently sending
158         /// (for building fragments)
159         int sendMessageLength;
160         unsigned char sendMessage[NET_MAXMESSAGE];
161
162         /// reliable message that is currently being received
163         /// (for putting together fragments)
164         int receiveMessageLength;
165         unsigned char receiveMessage[NET_MAXMESSAGE];
166
167         /// used by both NQ and QW protocols
168         unsigned int outgoing_unreliable_sequence;
169
170         struct netconn_nq_s
171         {
172                 unsigned int ackSequence;
173                 unsigned int sendSequence;
174
175                 unsigned int receiveSequence;
176                 unsigned int unreliableReceiveSequence;
177         }
178         nq;
179         struct netconn_qw_s
180         {
181                 // QW protocol
182                 qboolean        fatal_error;
183
184                 float           last_received;          // for timeouts
185
186         // the statistics are cleared at each client begin, because
187         // the server connecting process gives a bogus picture of the data
188                 float           frame_latency;          // rolling average
189                 float           frame_rate;
190
191                 int                     drop_count;                     ///< dropped packets, cleared each level
192                 int                     good_count;                     ///< cleared each level
193
194                 int                     qport;
195
196         // sequencing variables
197                 int                     incoming_sequence;
198                 int                     incoming_acknowledged;
199                 int                     incoming_reliable_acknowledged; ///< single bit
200
201                 int                     incoming_reliable_sequence;             ///< single bit, maintained local
202
203                 int                     reliable_sequence;                      ///< single bit
204                 int                     last_reliable_sequence;         ///< sequence number of last send
205         }
206         qw;
207
208         // bandwidth estimator
209         double          cleartime;                      // if realtime > nc->cleartime, free to go
210
211         // this tracks packet loss and packet sizes on the most recent packets
212         // used by shownetgraph feature
213 #define NETGRAPH_PACKETS 256
214 #define NETGRAPH_NOPACKET 0
215 #define NETGRAPH_LOSTPACKET -1
216 #define NETGRAPH_CHOKEDPACKET -2
217         int incoming_packetcounter;
218         netgraphitem_t incoming_netgraph[NETGRAPH_PACKETS];
219         int outgoing_packetcounter;
220         netgraphitem_t outgoing_netgraph[NETGRAPH_PACKETS];
221
222         char address[128];
223         crypto_t crypto;
224 } netconn_t;
225
226 extern netconn_t *netconn_list;
227 extern mempool_t *netconn_mempool;
228
229 extern cvar_t hostname;
230 extern cvar_t developer_networking;
231
232 #define SERVERLIST_VIEWLISTSIZE         SERVERLIST_TOTALSIZE
233
234 typedef enum serverlist_maskop_e
235 {
236         // SLMO_CONTAINS is the default for strings
237         // SLMO_GREATEREQUAL is the default for numbers (also used when OP == CONTAINS or NOTCONTAINS
238         SLMO_CONTAINS,
239         SLMO_NOTCONTAIN,
240
241         SLMO_LESSEQUAL,
242         SLMO_LESS,
243         SLMO_EQUAL,
244         SLMO_GREATER,
245         SLMO_GREATEREQUAL,
246         SLMO_NOTEQUAL,
247         SLMO_STARTSWITH,
248         SLMO_NOTSTARTSWITH
249 } serverlist_maskop_t;
250
251 /// struct with all fields that you can search for or sort by
252 typedef struct serverlist_info_s
253 {
254         /// address for connecting
255         char cname[128];
256         /// ping time for sorting servers
257         int ping;
258         /// name of the game
259         char game[32];
260         /// name of the mod
261         char mod[32];
262         /// name of the map
263         char map[32];
264         /// name of the session
265         char name[128];
266         /// qc-defined short status string
267         char qcstatus[128];
268         /// frags/ping/name list (if they fit in the packet)
269         char players[1400];
270         /// max client number
271         int maxplayers;
272         /// number of currently connected players (including bots)
273         int numplayers;
274         /// number of currently connected players that are bots
275         int numbots;
276         /// number of currently connected players that are not bots
277         int numhumans;
278         /// number of free slots
279         int freeslots;
280         /// protocol version
281         int protocol;
282         /// game data version
283         /// (an integer that is used for filtering incompatible servers,
284         ///  not filterable by QC)
285         int gameversion;
286         /// favorite server flag
287         qboolean isfavorite;
288 } serverlist_info_t;
289
290 typedef enum
291 {
292         SLIF_CNAME,
293         SLIF_PING,
294         SLIF_GAME,
295         SLIF_MOD,
296         SLIF_MAP,
297         SLIF_NAME,
298         SLIF_MAXPLAYERS,
299         SLIF_NUMPLAYERS,
300         SLIF_PROTOCOL,
301         SLIF_NUMBOTS,
302         SLIF_NUMHUMANS,
303         SLIF_FREESLOTS,
304         SLIF_QCSTATUS,
305         SLIF_PLAYERS,
306         SLIF_ISFAVORITE,
307         SLIF_COUNT
308 } serverlist_infofield_t;
309
310 typedef enum
311 {
312         SLSF_DESCENDING = 1,
313         SLSF_FAVORITESFIRST = 2
314 } serverlist_sortflags_t;
315
316 typedef enum
317 {
318         SQS_NONE = 0,
319         SQS_QUERYING,
320         SQS_QUERIED,
321         SQS_TIMEDOUT,
322         SQS_REFRESHING
323 } serverlist_query_state;
324
325 typedef struct serverlist_entry_s
326 {
327         /// used to determine whether this entry should be included into the final view
328         serverlist_query_state query;
329         /// used to count the number of times the host has tried to query this server already
330         unsigned querycounter;
331         /// used to calculate ping when update comes in
332         double querytime;
333         /// query protocol to use on this server, may be PROTOCOL_QUAKEWORLD or PROTOCOL_DARKPLACES7
334         int protocol;
335
336         serverlist_info_t info;
337
338         // legacy stuff
339         char line1[128];
340         char line2[128];
341 } serverlist_entry_t;
342
343 typedef struct serverlist_mask_s
344 {
345         qboolean                        active;
346         serverlist_maskop_t  tests[SLIF_COUNT];
347         serverlist_info_t info;
348 } serverlist_mask_t;
349
350 #define ServerList_GetCacheEntry(x) (&serverlist_cache[(x)])
351 #define ServerList_GetViewEntry(x) (ServerList_GetCacheEntry(serverlist_viewlist[(x)]))
352
353 extern serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
354 extern serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
355
356 extern serverlist_infofield_t serverlist_sortbyfield;
357 extern int serverlist_sortflags; // not using the enum, as it is a bitmask
358
359 #if SERVERLIST_TOTALSIZE > 65536
360 #error too many servers, change type of index array
361 #endif
362 extern int serverlist_viewcount;
363 extern unsigned short serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
364
365 extern int serverlist_cachecount;
366 extern serverlist_entry_t *serverlist_cache;
367
368 extern qboolean serverlist_consoleoutput;
369
370 void ServerList_GetPlayerStatistics(int *numplayerspointer, int *maxplayerspointer);
371
372 //============================================================================
373 //
374 // public network functions
375 //
376 //============================================================================
377
378 extern char cl_net_extresponse[NET_EXTRESPONSE_MAX][1400];
379 extern int cl_net_extresponse_count;
380 extern int cl_net_extresponse_last;
381
382 extern char sv_net_extresponse[NET_EXTRESPONSE_MAX][1400];
383 extern int sv_net_extresponse_count;
384 extern int sv_net_extresponse_last;
385
386 extern double masterquerytime;
387 extern int masterquerycount;
388 extern int masterreplycount;
389 extern int serverquerycount;
390 extern int serverreplycount;
391
392 extern sizebuf_t net_message;
393
394 extern cvar_t sv_public;
395
396 extern cvar_t cl_netlocalping;
397
398 extern cvar_t cl_netport;
399 extern cvar_t sv_netport;
400 extern cvar_t net_address;
401 extern cvar_t net_address_ipv6;
402
403 qboolean NetConn_CanSend(netconn_t *conn);
404 int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol, int rate, qboolean quakesignon_suppressreliables);
405 qboolean NetConn_HaveClientPorts(void);
406 qboolean NetConn_HaveServerPorts(void);
407 void NetConn_CloseClientPorts(void);
408 void NetConn_OpenClientPorts(void);
409 void NetConn_CloseServerPorts(void);
410 void NetConn_OpenServerPorts(int opennetports);
411 void NetConn_UpdateSockets(void);
412 lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address);
413 lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address);
414 void NetConn_Init(void);
415 void NetConn_Shutdown(void);
416 netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress);
417 void NetConn_Close(netconn_t *conn);
418 void NetConn_Listen(qboolean state);
419 int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress);
420 int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const lhnetaddress_t *peeraddress);
421 int NetConn_WriteString(lhnetsocket_t *mysocket, const char *string, const lhnetaddress_t *peeraddress);
422 int NetConn_IsLocalGame(void);
423 void NetConn_ClientFrame(void);
424 void NetConn_ServerFrame(void);
425 void NetConn_SleepMicroseconds(int microseconds);
426 void NetConn_QueryMasters(qboolean querydp, qboolean queryqw);
427 void NetConn_Heartbeat(int priority);
428 void NetConn_QueryQueueFrame(void);
429 void Net_Stats_f(void);
430 void Net_Slist_f(void);
431 void Net_SlistQW_f(void);
432 void Net_Refresh_f(void);
433
434 /// ServerList interface (public)
435 /// manually refresh the view set, do this after having changed the mask or any other flag
436 void ServerList_RebuildViewList(void);
437 void ServerList_ResetMasks(void);
438 void ServerList_QueryList(qboolean resetcache, qboolean querydp, qboolean queryqw, qboolean consoleoutput);
439
440 /// called whenever net_slist_favorites changes
441 void NetConn_UpdateFavorites(void);
442
443 #define MAX_CHALLENGES 128
444 typedef struct challenge_s
445 {
446         lhnetaddress_t address;
447         double time;
448         char string[12];
449 }
450 challenge_t;
451
452 extern challenge_t challenge[MAX_CHALLENGES];
453
454 #endif
455