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