]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - netconn.h
Revert "Revert "Build in subdirs"", as the issues seem to be ironed out now.
[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         double cleartime;
132 }
133 netgraphitem_t;
134
135 typedef struct netconn_s
136 {
137         struct netconn_s *next;
138
139         lhnetsocket_t *mysocket;
140         lhnetaddress_t peeraddress;
141
142         // this is mostly identical to qsocket_t from quake
143
144         /// if this time is reached, kick off peer
145         double connecttime;
146         double timeout;
147         double lastMessageTime;
148         double lastSendTime;
149
150         /// writing buffer to send to peer as the next reliable message
151         /// can be added to at any time, copied into sendMessage buffer when it is
152         /// possible to send a reliable message and then cleared
153         /// @{
154         sizebuf_t message;
155         unsigned char messagedata[NET_MAXMESSAGE];
156         /// @}
157
158         /// reliable message that is currently sending
159         /// (for building fragments)
160         int sendMessageLength;
161         unsigned char sendMessage[NET_MAXMESSAGE];
162
163         /// reliable message that is currently being received
164         /// (for putting together fragments)
165         int receiveMessageLength;
166         unsigned char receiveMessage[NET_MAXMESSAGE];
167
168         /// used by both NQ and QW protocols
169         unsigned int outgoing_unreliable_sequence;
170
171         struct netconn_nq_s
172         {
173                 unsigned int ackSequence;
174                 unsigned int sendSequence;
175
176                 unsigned int receiveSequence;
177                 unsigned int unreliableReceiveSequence;
178         }
179         nq;
180         struct netconn_qw_s
181         {
182                 // QW protocol
183                 qboolean        fatal_error;
184
185                 float           last_received;          // for timeouts
186
187         // the statistics are cleared at each client begin, because
188         // the server connecting process gives a bogus picture of the data
189                 float           frame_latency;          // rolling average
190                 float           frame_rate;
191
192                 int                     drop_count;                     ///< dropped packets, cleared each level
193                 int                     good_count;                     ///< cleared each level
194
195                 int                     qport;
196
197         // sequencing variables
198                 int                     incoming_sequence;
199                 int                     incoming_acknowledged;
200                 int                     incoming_reliable_acknowledged; ///< single bit
201
202                 int                     incoming_reliable_sequence;             ///< single bit, maintained local
203
204                 int                     reliable_sequence;                      ///< single bit
205                 int                     last_reliable_sequence;         ///< sequence number of last send
206         }
207         qw;
208
209         // bandwidth estimator
210         double          cleartime;                      // if realtime > nc->cleartime, free to go
211         double          incoming_cleartime;             // if realtime > nc->cleartime, free to go (netgraph cleartime simulation only)
212
213         // this tracks packet loss and packet sizes on the most recent packets
214         // used by shownetgraph feature
215 #define NETGRAPH_PACKETS 256
216 #define NETGRAPH_NOPACKET 0
217 #define NETGRAPH_LOSTPACKET -1
218 #define NETGRAPH_CHOKEDPACKET -2
219         int incoming_packetcounter;
220         netgraphitem_t incoming_netgraph[NETGRAPH_PACKETS];
221         int outgoing_packetcounter;
222         netgraphitem_t outgoing_netgraph[NETGRAPH_PACKETS];
223
224         char address[128];
225         crypto_t crypto;
226
227         // statistic counters
228         int packetsSent;
229         int packetsReSent;
230         int packetsReceived;
231         int receivedDuplicateCount;
232         int droppedDatagrams;
233         int unreliableMessagesSent;
234         int unreliableMessagesReceived;
235         int reliableMessagesSent;
236         int reliableMessagesReceived;
237 } netconn_t;
238
239 extern netconn_t *netconn_list;
240 extern mempool_t *netconn_mempool;
241
242 extern cvar_t hostname;
243 extern cvar_t developer_networking;
244
245 #define SERVERLIST_VIEWLISTSIZE         SERVERLIST_TOTALSIZE
246
247 typedef enum serverlist_maskop_e
248 {
249         // SLMO_CONTAINS is the default for strings
250         // SLMO_GREATEREQUAL is the default for numbers (also used when OP == CONTAINS or NOTCONTAINS
251         SLMO_CONTAINS,
252         SLMO_NOTCONTAIN,
253
254         SLMO_LESSEQUAL,
255         SLMO_LESS,
256         SLMO_EQUAL,
257         SLMO_GREATER,
258         SLMO_GREATEREQUAL,
259         SLMO_NOTEQUAL,
260         SLMO_STARTSWITH,
261         SLMO_NOTSTARTSWITH
262 } serverlist_maskop_t;
263
264 /// struct with all fields that you can search for or sort by
265 typedef struct serverlist_info_s
266 {
267         /// address for connecting
268         char cname[128];
269         /// ping time for sorting servers
270         int ping;
271         /// name of the game
272         char game[32];
273         /// name of the mod
274         char mod[32];
275         /// name of the map
276         char map[32];
277         /// name of the session
278         char name[128];
279         /// qc-defined short status string
280         char qcstatus[128];
281         /// frags/ping/name list (if they fit in the packet)
282         char players[1400];
283         /// max client number
284         int maxplayers;
285         /// number of currently connected players (including bots)
286         int numplayers;
287         /// number of currently connected players that are bots
288         int numbots;
289         /// number of currently connected players that are not bots
290         int numhumans;
291         /// number of free slots
292         int freeslots;
293         /// protocol version
294         int protocol;
295         /// game data version
296         /// (an integer that is used for filtering incompatible servers,
297         ///  not filterable by QC)
298         int gameversion;
299         /// favorite server flag
300         qboolean isfavorite;
301 } serverlist_info_t;
302
303 typedef enum
304 {
305         SLIF_CNAME,
306         SLIF_PING,
307         SLIF_GAME,
308         SLIF_MOD,
309         SLIF_MAP,
310         SLIF_NAME,
311         SLIF_MAXPLAYERS,
312         SLIF_NUMPLAYERS,
313         SLIF_PROTOCOL,
314         SLIF_NUMBOTS,
315         SLIF_NUMHUMANS,
316         SLIF_FREESLOTS,
317         SLIF_QCSTATUS,
318         SLIF_PLAYERS,
319         SLIF_ISFAVORITE,
320         SLIF_COUNT
321 } serverlist_infofield_t;
322
323 typedef enum
324 {
325         SLSF_DESCENDING = 1,
326         SLSF_FAVORITESFIRST = 2
327 } serverlist_sortflags_t;
328
329 typedef enum
330 {
331         SQS_NONE = 0,
332         SQS_QUERYING,
333         SQS_QUERIED,
334         SQS_TIMEDOUT,
335         SQS_REFRESHING
336 } serverlist_query_state;
337
338 typedef struct serverlist_entry_s
339 {
340         /// used to determine whether this entry should be included into the final view
341         serverlist_query_state query;
342         /// used to count the number of times the host has tried to query this server already
343         unsigned querycounter;
344         /// used to calculate ping when update comes in
345         double querytime;
346         /// query protocol to use on this server, may be PROTOCOL_QUAKEWORLD or PROTOCOL_DARKPLACES7
347         int protocol;
348
349         serverlist_info_t info;
350
351         // legacy stuff
352         char line1[128];
353         char line2[128];
354 } serverlist_entry_t;
355
356 typedef struct serverlist_mask_s
357 {
358         qboolean                        active;
359         serverlist_maskop_t  tests[SLIF_COUNT];
360         serverlist_info_t info;
361 } serverlist_mask_t;
362
363 #define ServerList_GetCacheEntry(x) (&serverlist_cache[(x)])
364 #define ServerList_GetViewEntry(x) (ServerList_GetCacheEntry(serverlist_viewlist[(x)]))
365
366 extern serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
367 extern serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
368
369 extern serverlist_infofield_t serverlist_sortbyfield;
370 extern int serverlist_sortflags; // not using the enum, as it is a bitmask
371
372 #if SERVERLIST_TOTALSIZE > 65536
373 #error too many servers, change type of index array
374 #endif
375 extern int serverlist_viewcount;
376 extern unsigned short serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
377
378 extern int serverlist_cachecount;
379 extern serverlist_entry_t *serverlist_cache;
380
381 extern qboolean serverlist_consoleoutput;
382
383 void ServerList_GetPlayerStatistics(int *numplayerspointer, int *maxplayerspointer);
384
385 //============================================================================
386 //
387 // public network functions
388 //
389 //============================================================================
390
391 extern char cl_net_extresponse[NET_EXTRESPONSE_MAX][1400];
392 extern int cl_net_extresponse_count;
393 extern int cl_net_extresponse_last;
394
395 extern char sv_net_extresponse[NET_EXTRESPONSE_MAX][1400];
396 extern int sv_net_extresponse_count;
397 extern int sv_net_extresponse_last;
398
399 extern double masterquerytime;
400 extern int masterquerycount;
401 extern int masterreplycount;
402 extern int serverquerycount;
403 extern int serverreplycount;
404
405 extern sizebuf_t cl_message;
406 extern sizebuf_t sv_message;
407 extern char cl_readstring[MAX_INPUTLINE];
408 extern char sv_readstring[MAX_INPUTLINE];
409
410 extern cvar_t sv_public;
411
412 extern cvar_t cl_netlocalping;
413
414 extern cvar_t cl_netport;
415 extern cvar_t sv_netport;
416 extern cvar_t net_address;
417 extern cvar_t net_address_ipv6;
418 extern cvar_t net_usesizelimit;
419 extern cvar_t net_burstreserve;
420
421 qboolean NetConn_CanSend(netconn_t *conn);
422 int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol, int rate, int burstsize, qboolean quakesignon_suppressreliables);
423 qboolean NetConn_HaveClientPorts(void);
424 qboolean NetConn_HaveServerPorts(void);
425 void NetConn_CloseClientPorts(void);
426 void NetConn_OpenClientPorts(void);
427 void NetConn_CloseServerPorts(void);
428 void NetConn_OpenServerPorts(int opennetports);
429 void NetConn_UpdateSockets(void);
430 lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address);
431 lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address);
432 void NetConn_Init(void);
433 void NetConn_Shutdown(void);
434 netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress);
435 void NetConn_Close(netconn_t *conn);
436 void NetConn_Listen(qboolean state);
437 int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress);
438 int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const lhnetaddress_t *peeraddress);
439 int NetConn_WriteString(lhnetsocket_t *mysocket, const char *string, const lhnetaddress_t *peeraddress);
440 int NetConn_IsLocalGame(void);
441 void NetConn_ClientFrame(void);
442 void NetConn_ServerFrame(void);
443 void NetConn_SleepMicroseconds(int microseconds);
444 void NetConn_QueryMasters(qboolean querydp, qboolean queryqw);
445 void NetConn_Heartbeat(int priority);
446 void NetConn_QueryQueueFrame(void);
447 void Net_Stats_f(void);
448 void Net_Slist_f(void);
449 void Net_SlistQW_f(void);
450 void Net_Refresh_f(void);
451
452 /// ServerList interface (public)
453 /// manually refresh the view set, do this after having changed the mask or any other flag
454 void ServerList_RebuildViewList(void);
455 void ServerList_ResetMasks(void);
456 void ServerList_QueryList(qboolean resetcache, qboolean querydp, qboolean queryqw, qboolean consoleoutput);
457
458 /// called whenever net_slist_favorites changes
459 void NetConn_UpdateFavorites(void);
460
461 #define MAX_CHALLENGES 128
462 typedef struct challenge_s
463 {
464         lhnetaddress_t address;
465         double time;
466         char string[12];
467 }
468 challenge_t;
469
470 extern challenge_t challenge[MAX_CHALLENGES];
471
472 #endif
473