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