2 Copyright (C) 1996-1997 Id Software, Inc.
3 Copyright (C) 2003 Forest Hale
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.
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.
14 See the GNU General Public License for more details.
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.
27 #define NET_HEADERSIZE (2 * sizeof(unsigned int))
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
39 #define NET_PROTOCOL_VERSION 3
40 #define NET_EXTRESPONSE_MAX 16
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.
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?
53 /// string game_name "QUAKE"
54 /// byte net_protocol_version NET_PROTOCOL_VERSION
57 /// string game_name "QUAKE"
58 /// byte net_protocol_version NET_PROTOCOL_VERSION
61 /// byte player_number
79 /// string server_address
82 /// byte current_players
84 /// byte protocol_version NET_PROTOCOL_VERSION
87 /// byte player_number
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.
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
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
124 typedef struct netconn_s
126 struct netconn_s *next;
128 lhnetsocket_t *mysocket;
129 lhnetaddress_t peeraddress;
131 // this is mostly identical to qsocket_t from quake
133 /// if this time is reached, kick off peer
136 double lastMessageTime;
139 /// writing buffer to send to peer as the next reliable message
140 /// can be added to at any time, copied into sendMessage buffer when it is
141 /// possible to send a reliable message and then cleared
144 unsigned char messagedata[NET_MAXMESSAGE];
147 /// reliable message that is currently sending
148 /// (for building fragments)
149 int sendMessageLength;
150 unsigned char sendMessage[NET_MAXMESSAGE];
152 /// reliable message that is currently being received
153 /// (for putting together fragments)
154 int receiveMessageLength;
155 unsigned char receiveMessage[NET_MAXMESSAGE];
157 /// used by both NQ and QW protocols
158 unsigned int outgoing_unreliable_sequence;
162 unsigned int ackSequence;
163 unsigned int sendSequence;
165 unsigned int receiveSequence;
166 unsigned int unreliableReceiveSequence;
172 qboolean fatal_error;
174 float last_received; // for timeouts
176 // the statistics are cleared at each client begin, because
177 // the server connecting process gives a bogus picture of the data
178 float frame_latency; // rolling average
181 int drop_count; ///< dropped packets, cleared each level
182 int good_count; ///< cleared each level
186 // sequencing variables
187 int incoming_sequence;
188 int incoming_acknowledged;
189 int incoming_reliable_acknowledged; ///< single bit
191 int incoming_reliable_sequence; ///< single bit, maintained local
193 int reliable_sequence; ///< single bit
194 int last_reliable_sequence; ///< sequence number of last send
198 // bandwidth estimator
199 double cleartime; // if realtime > nc->cleartime, free to go
201 // this tracks packet loss and packet sizes on the most recent packets
202 // used by shownetgraph feature
203 #define NETGRAPH_PACKETS 100
204 #define NETGRAPH_NOPACKET 0
205 #define NETGRAPH_LOSTPACKET -1
206 #define NETGRAPH_CHOKEDPACKET -2
207 int incoming_packetcounter;
208 int incoming_reliablesize[NETGRAPH_PACKETS];
209 int incoming_unreliablesize[NETGRAPH_PACKETS];
210 int incoming_acksize[NETGRAPH_PACKETS];
211 int outgoing_packetcounter;
212 int outgoing_reliablesize[NETGRAPH_PACKETS];
213 int outgoing_unreliablesize[NETGRAPH_PACKETS];
214 int outgoing_acksize[NETGRAPH_PACKETS];
219 extern netconn_t *netconn_list;
220 extern mempool_t *netconn_mempool;
222 extern cvar_t hostname;
223 extern cvar_t developer_networking;
225 #define SERVERLIST_TOTALSIZE 2048
226 #define SERVERLIST_VIEWLISTSIZE SERVERLIST_TOTALSIZE
227 #define SERVERLIST_ANDMASKCOUNT 5
228 #define SERVERLIST_ORMASKCOUNT 5
230 typedef enum serverlist_maskop_e
232 // SLMO_CONTAINS is the default for strings
233 // SLMO_GREATEREQUAL is the default for numbers (also used when OP == CONTAINS or NOTCONTAINS
245 } serverlist_maskop_t;
247 /// struct with all fields that you can search for or sort by
248 typedef struct serverlist_info_s
250 /// address for connecting
252 /// ping time for sorting servers
260 /// name of the session
262 /// qc-defined short status string
264 /// frags/ping/name list (if they fit in the packet)
266 /// max client number
268 /// number of currently connected players (including bots)
270 /// number of currently connected players that are bots
272 /// number of currently connected players that are not bots
274 /// number of free slots
278 /// game data version
279 /// (an integer that is used for filtering incompatible servers,
280 /// not filterable by QC)
282 /// favorite server flag
304 } serverlist_infofield_t;
309 SLSF_FAVORITESFIRST = 2
310 } serverlist_sortflags_t;
319 } serverlist_query_state;
321 typedef struct serverlist_entry_s
323 /// used to determine whether this entry should be included into the final view
324 serverlist_query_state query;
325 /// used to count the number of times the host has tried to query this server already
326 unsigned querycounter;
327 /// used to calculate ping when update comes in
329 /// query protocol to use on this server, may be PROTOCOL_QUAKEWORLD or PROTOCOL_DARKPLACES7
332 serverlist_info_t info;
337 } serverlist_entry_t;
339 typedef struct serverlist_mask_s
342 serverlist_maskop_t tests[SLIF_COUNT];
343 serverlist_info_t info;
346 extern serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
347 extern serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
349 extern serverlist_infofield_t serverlist_sortbyfield;
350 extern int serverlist_sortflags; // not using the enum, as it is a bitmask
352 extern int serverlist_viewcount;
353 extern serverlist_entry_t *serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
355 extern int serverlist_cachecount;
357 extern qboolean serverlist_consoleoutput;
359 void ServerList_GetPlayerStatistics(int *numplayerspointer, int *maxplayerspointer);
361 //============================================================================
363 // public network functions
365 //============================================================================
367 extern char net_extresponse[NET_EXTRESPONSE_MAX][1400];
368 extern int net_extresponse_count;
369 extern int net_extresponse_last;
371 extern double masterquerytime;
372 extern int masterquerycount;
373 extern int masterreplycount;
374 extern int serverquerycount;
375 extern int serverreplycount;
377 extern sizebuf_t net_message;
379 extern cvar_t sv_public;
381 extern cvar_t cl_netlocalping;
383 extern cvar_t cl_netport;
384 extern cvar_t sv_netport;
385 extern cvar_t net_address;
386 extern cvar_t net_address_ipv6;
388 qboolean NetConn_CanSend(netconn_t *conn);
389 int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol, int rate, qboolean quakesignon_suppressreliables);
390 qboolean NetConn_HaveClientPorts(void);
391 qboolean NetConn_HaveServerPorts(void);
392 void NetConn_CloseClientPorts(void);
393 void NetConn_OpenClientPorts(void);
394 void NetConn_CloseServerPorts(void);
395 void NetConn_OpenServerPorts(int opennetports);
396 void NetConn_UpdateSockets(void);
397 lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address);
398 lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address);
399 void NetConn_Init(void);
400 void NetConn_Shutdown(void);
401 netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress);
402 void NetConn_Close(netconn_t *conn);
403 void NetConn_Listen(qboolean state);
404 int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress);
405 int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const lhnetaddress_t *peeraddress);
406 int NetConn_WriteString(lhnetsocket_t *mysocket, const char *string, const lhnetaddress_t *peeraddress);
407 int NetConn_IsLocalGame(void);
408 void NetConn_ClientFrame(void);
409 void NetConn_ServerFrame(void);
410 void NetConn_SleepMicroseconds(int microseconds);
411 void NetConn_QueryMasters(qboolean querydp, qboolean queryqw);
412 void NetConn_Heartbeat(int priority);
413 void NetConn_QueryQueueFrame(void);
414 void Net_Stats_f(void);
415 void Net_Slist_f(void);
416 void Net_SlistQW_f(void);
417 void Net_Refresh_f(void);
419 /// ServerList interface (public)
420 /// manually refresh the view set, do this after having changed the mask or any other flag
421 void ServerList_RebuildViewList(void);
422 void ServerList_ResetMasks(void);
423 void ServerList_QueryList(qboolean resetcache, qboolean querydp, qboolean queryqw, qboolean consoleoutput);
425 /// called whenever net_slist_favorites changes
426 void NetConn_UpdateFavorites(void);