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 // This is the network info/connection protocol. It is used to find Quake
43 // servers, get info about them, and connect to them. Once connected, the
44 // Quake game protocol (documented elsewhere) is used.
48 // game_name is currently always "QUAKE", but is there so this same protocol
49 // can be used for future games as well; can you say Quake2?
52 // string game_name "QUAKE"
53 // byte net_protocol_version NET_PROTOCOL_VERSION
56 // string game_name "QUAKE"
57 // byte net_protocol_version NET_PROTOCOL_VERSION
74 // string server_address
77 // byte current_players
79 // byte protocol_version NET_PROTOCOL_VERSION
94 // There are two address forms used above. The short form is just a
95 // port number. The address that goes along with the port is defined as
96 // "whatever address you receive this reponse from". This lets us use
97 // the host OS to solve the problem of multiple host addresses (possibly
98 // with no routing between them); the host will use the right address
99 // when we reply to the inbound connection request. The long from is
100 // a full address and port in a string. It is used for returning the
101 // address of a server that is not running locally.
103 #define CCREQ_CONNECT 0x01
104 #define CCREQ_SERVER_INFO 0x02
105 #define CCREQ_PLAYER_INFO 0x03
106 #define CCREQ_RULE_INFO 0x04
108 #define CCREP_ACCEPT 0x81
109 #define CCREP_REJECT 0x82
110 #define CCREP_SERVER_INFO 0x83
111 #define CCREP_PLAYER_INFO 0x84
112 #define CCREP_RULE_INFO 0x85
114 typedef struct netconn_s
116 struct netconn_s *next;
118 lhnetsocket_t *mysocket;
119 lhnetaddress_t peeraddress;
121 // this is mostly identical to qsocket_t from quake
123 // if this time is reached, kick off peer
126 double lastMessageTime;
129 // writing buffer to send to peer as the next reliable message
130 // can be added to at any time, copied into sendMessage buffer when it is
131 // possible to send a reliable message and then cleared
133 unsigned char messagedata[NET_MAXMESSAGE];
135 // reliable message that is currently sending
136 // (for building fragments)
137 int sendMessageLength;
138 unsigned char sendMessage[NET_MAXMESSAGE];
140 // reliable message that is currently being received
141 // (for putting together fragments)
142 int receiveMessageLength;
143 unsigned char receiveMessage[NET_MAXMESSAGE];
147 unsigned int ackSequence;
148 unsigned int sendSequence;
149 unsigned int unreliableSendSequence;
151 unsigned int receiveSequence;
152 unsigned int unreliableReceiveSequence;
158 qboolean fatal_error;
160 float last_received; // for timeouts
162 // the statistics are cleared at each client begin, because
163 // the server connecting process gives a bogus picture of the data
164 float frame_latency; // rolling average
167 int drop_count; // dropped packets, cleared each level
168 int good_count; // cleared each level
172 // sequencing variables
173 int incoming_sequence;
174 int incoming_acknowledged;
175 int incoming_reliable_acknowledged; // single bit
177 int incoming_reliable_sequence; // single bit, maintained local
179 int outgoing_sequence;
180 int reliable_sequence; // single bit
181 int last_reliable_sequence; // sequence number of last send
185 // bandwidth estimator
186 double cleartime; // if realtime > nc->cleartime, free to go
188 // this tracks packet loss and packet sizes on the most recent packets
189 // used by shownetgraph feature
190 #define NETGRAPH_PACKETS 100
191 #define NETGRAPH_NOPACKET 0
192 #define NETGRAPH_LOSTPACKET -1
193 #define NETGRAPH_CHOKEDPACKET -2
194 int incoming_packetcounter;
195 int incoming_reliablesize[NETGRAPH_PACKETS];
196 int incoming_unreliablesize[NETGRAPH_PACKETS];
197 int incoming_acksize[NETGRAPH_PACKETS];
198 int outgoing_packetcounter;
199 int outgoing_reliablesize[NETGRAPH_PACKETS];
200 int outgoing_unreliablesize[NETGRAPH_PACKETS];
201 int outgoing_acksize[NETGRAPH_PACKETS];
206 extern netconn_t *netconn_list;
207 extern mempool_t *netconn_mempool;
209 extern cvar_t hostname;
210 extern cvar_t developer_networking;
212 #define SERVERLIST_TOTALSIZE 2048
213 #define SERVERLIST_VIEWLISTSIZE SERVERLIST_TOTALSIZE
214 #define SERVERLIST_ANDMASKCOUNT 5
215 #define SERVERLIST_ORMASKCOUNT 5
217 typedef enum serverlist_maskop_e
219 // SLMO_CONTAINS is the default for strings
220 // SLMO_GREATEREQUAL is the default for numbers (also used when OP == CONTAINS or NOTCONTAINS
230 } serverlist_maskop_t;
232 // struct with all fields that you can search for or sort by
233 typedef struct serverlist_info_s
235 // address for connecting
237 // ping time for sorting servers
245 // name of the session
249 // number of currently connected players (including bots)
251 // number of currently connected players that are bots
253 // number of currently connected players that are not bots
258 // (an integer that is used for filtering incompatible servers,
259 // not filterable by QC)
277 } serverlist_infofield_t;
285 } serverlist_query_state;
287 typedef struct serverlist_entry_s
289 // used to determine whether this entry should be included into the final view
290 serverlist_query_state query;
291 // used to count the number of times the host has tried to query this server already
292 unsigned querycounter;
293 // used to calculate ping when update comes in
295 // query protocol to use on this server
296 int protocol; // may be PROTOCOL_QUAKEWORLD or PROTOCOL_DARKPLACES7
298 serverlist_info_t info;
303 } serverlist_entry_t;
305 typedef struct serverlist_mask_s
308 serverlist_maskop_t tests[SLIF_COUNT];
309 serverlist_info_t info;
312 extern serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
313 extern serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
315 extern serverlist_infofield_t serverlist_sortbyfield;
316 extern qboolean serverlist_sortdescending;
318 extern int serverlist_viewcount;
319 extern serverlist_entry_t *serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
321 extern int serverlist_cachecount;
323 extern qboolean serverlist_consoleoutput;
325 //============================================================================
327 // public network functions
329 //============================================================================
331 extern char net_extresponse[NET_EXTRESPONSE_MAX][1400];
332 extern int net_extresponse_count;
333 extern int net_extresponse_last;
335 extern double masterquerytime;
336 extern int masterquerycount;
337 extern int masterreplycount;
338 extern int serverquerycount;
339 extern int serverreplycount;
341 extern sizebuf_t net_message;
343 extern cvar_t sv_public;
345 extern cvar_t cl_netlocalping;
347 extern cvar_t cl_netport;
348 extern cvar_t sv_netport;
349 extern cvar_t net_address;
350 //extern cvar_t net_netaddress_ipv6;
352 qboolean NetConn_CanSend(netconn_t *conn);
353 int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol, int rate);
354 void NetConn_CloseClientPorts(void);
355 void NetConn_OpenClientPorts(void);
356 void NetConn_CloseServerPorts(void);
357 void NetConn_OpenServerPorts(int opennetports);
358 void NetConn_UpdateSockets(void);
359 lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address);
360 lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address);
361 void NetConn_Init(void);
362 void NetConn_Shutdown(void);
363 netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress);
364 void NetConn_Close(netconn_t *conn);
365 void NetConn_Listen(qboolean state);
366 int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress);
367 int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const lhnetaddress_t *peeraddress);
368 int NetConn_WriteString(lhnetsocket_t *mysocket, const char *string, const lhnetaddress_t *peeraddress);
369 int NetConn_IsLocalGame(void);
370 void NetConn_ClientFrame(void);
371 void NetConn_ServerFrame(void);
372 void NetConn_SleepMicroseconds(int microseconds);
373 void NetConn_QueryMasters(qboolean querydp, qboolean queryqw);
374 void NetConn_Heartbeat(int priority);
375 void NetConn_QueryQueueFrame(void);
376 void Net_Stats_f(void);
377 void Net_Slist_f(void);
378 void Net_SlistQW_f(void);
380 // ServerList interface (public)
381 // manually refresh the view set, do this after having changed the mask or any other flag
382 void ServerList_RebuildViewList(void);
383 void ServerList_ResetMasks(void);
384 void ServerList_QueryList(qboolean querydp, qboolean queryqw);