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