]> icculus.org git repositories - divverent/darkplaces.git/blob - netconn.h
changed entity networking prioritization code to use the center of the model rather...
[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 // 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.
45 //
46 //
47 // General notes:
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?
50 //
51 // CCREQ_CONNECT
52 //              string  game_name                               "QUAKE"
53 //              byte    net_protocol_version    NET_PROTOCOL_VERSION
54 //
55 // CCREQ_SERVER_INFO
56 //              string  game_name                               "QUAKE"
57 //              byte    net_protocol_version    NET_PROTOCOL_VERSION
58 //
59 // CCREQ_PLAYER_INFO
60 //              byte    player_number
61 //
62 // CCREQ_RULE_INFO
63 //              string  rule
64 //
65 //
66 //
67 // CCREP_ACCEPT
68 //              long    port
69 //
70 // CCREP_REJECT
71 //              string  reason
72 //
73 // CCREP_SERVER_INFO
74 //              string  server_address
75 //              string  host_name
76 //              string  level_name
77 //              byte    current_players
78 //              byte    max_players
79 //              byte    protocol_version        NET_PROTOCOL_VERSION
80 //
81 // CCREP_PLAYER_INFO
82 //              byte    player_number
83 //              string  name
84 //              long    colors
85 //              long    frags
86 //              long    connect_time
87 //              string  address
88 //
89 // CCREP_RULE_INFO
90 //              string  rule
91 //              string  value
92
93 //      note:
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.
102
103 #define CCREQ_CONNECT           0x01
104 #define CCREQ_SERVER_INFO       0x02
105 #define CCREQ_PLAYER_INFO       0x03
106 #define CCREQ_RULE_INFO         0x04
107
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
113
114 typedef struct netconn_s
115 {
116         struct netconn_s *next;
117
118         lhnetsocket_t *mysocket;
119         lhnetaddress_t peeraddress;
120
121         // this is mostly identical to qsocket_t from quake
122
123         // if this time is reached, kick off peer
124         double connecttime;
125         double timeout;
126         double lastMessageTime;
127         double lastSendTime;
128
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
132         sizebuf_t message;
133         unsigned char messagedata[NET_MAXMESSAGE];
134
135         // reliable message that is currently sending
136         // (for building fragments)
137         int sendMessageLength;
138         unsigned char sendMessage[NET_MAXMESSAGE];
139
140         // reliable message that is currently being received
141         // (for putting together fragments)
142         int receiveMessageLength;
143         unsigned char receiveMessage[NET_MAXMESSAGE];
144
145         struct netconn_nq_s
146         {
147                 unsigned int ackSequence;
148                 unsigned int sendSequence;
149                 unsigned int unreliableSendSequence;
150
151                 unsigned int receiveSequence;
152                 unsigned int unreliableReceiveSequence;
153         }
154         nq;
155         struct netconn_qw_s
156         {
157                 // QW protocol
158                 qboolean        fatal_error;
159
160                 float           last_received;          // for timeouts
161
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
165                 float           frame_rate;
166
167                 int                     drop_count;                     // dropped packets, cleared each level
168                 int                     good_count;                     // cleared each level
169
170                 int                     qport;
171
172         // bandwidth estimator
173                 double          cleartime;                      // if realtime > nc->cleartime, free to go
174                 double          rate;                           // seconds / byte
175
176         // sequencing variables
177                 int                     incoming_sequence;
178                 int                     incoming_acknowledged;
179                 int                     incoming_reliable_acknowledged; // single bit
180
181                 int                     incoming_reliable_sequence;             // single bit, maintained local
182
183                 int                     outgoing_sequence;
184                 int                     reliable_sequence;                      // single bit
185                 int                     last_reliable_sequence;         // sequence number of last send
186         }
187         qw;
188
189         // this tracks which of the last 100 received packet sequence numbers were lost
190         int packetlostcounter;
191         unsigned char packetlost[100];
192
193         char address[128];
194 } netconn_t;
195
196 extern netconn_t *netconn_list;
197 extern mempool_t *netconn_mempool;
198
199 extern cvar_t hostname;
200 extern cvar_t developer_networking;
201
202 #define SERVERLIST_TOTALSIZE            2048
203 #define SERVERLIST_VIEWLISTSIZE         SERVERLIST_TOTALSIZE
204 #define SERVERLIST_ANDMASKCOUNT         5
205 #define SERVERLIST_ORMASKCOUNT          5
206
207 typedef enum serverlist_maskop_e
208 {
209         // SLMO_CONTAINS is the default for strings
210         // SLMO_GREATEREQUAL is the default for numbers (also used when OP == CONTAINS or NOTCONTAINS
211         SLMO_CONTAINS,
212         SLMO_NOTCONTAIN,
213
214         SLMO_LESSEQUAL,
215         SLMO_LESS,
216         SLMO_EQUAL,
217         SLMO_GREATER,
218         SLMO_GREATEREQUAL,
219         SLMO_NOTEQUAL
220 } serverlist_maskop_t;
221
222 // struct with all fields that you can search for or sort by
223 typedef struct serverlist_info_s
224 {
225         // address for connecting
226         char cname[128];
227         // ping time for sorting servers
228         int ping;
229         // name of the game
230         char game[32];
231         // name of the mod
232         char mod[32];
233         // name of the map
234         char map[32];
235         // name of the session
236         char name[128];
237         // max client number
238         int maxplayers;
239         // number of currently connected players
240         int numplayers;
241         // protocol version
242         int protocol;
243         // game data version
244         // (an integer that is used for filtering incompatible servers,
245         //  not filterable by QC)
246         int gameversion;
247 } serverlist_info_t;
248
249 typedef enum
250 {
251         SLIF_CNAME,
252         SLIF_PING,
253         SLIF_GAME,
254         SLIF_MOD,
255         SLIF_MAP,
256         SLIF_NAME,
257         SLIF_MAXPLAYERS,
258         SLIF_NUMPLAYERS,
259         SLIF_PROTOCOL,
260         SLIF_COUNT
261 } serverlist_infofield_t;
262
263 typedef enum
264 {
265         SQS_NONE = 0,
266         SQS_QUERYING,
267         SQS_QUERIED,
268         SQS_TIMEDOUT
269 } serverlist_query_state;
270
271 typedef struct serverlist_entry_s
272 {
273         // used to determine whether this entry should be included into the final view
274         serverlist_query_state query;
275         // used to count the number of times the host has tried to query this server already
276         unsigned querycounter;
277         // used to calculate ping when update comes in
278         double querytime;
279         // query protocol to use on this server
280         int protocol; // may be PROTOCOL_QUAKEWORLD or PROTOCOL_DARKPLACES7
281
282         serverlist_info_t info;
283
284         // legacy stuff
285         char line1[128];
286         char line2[128];
287 } serverlist_entry_t;
288
289 typedef struct serverlist_mask_s
290 {
291         qboolean                        active;
292         serverlist_maskop_t  tests[SLIF_COUNT];
293         serverlist_info_t info;
294 } serverlist_mask_t;
295
296 extern serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
297 extern serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
298
299 extern serverlist_infofield_t serverlist_sortbyfield;
300 extern qboolean serverlist_sortdescending;
301
302 extern int serverlist_viewcount;
303 extern serverlist_entry_t *serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
304
305 extern int serverlist_cachecount;
306
307 extern qboolean serverlist_consoleoutput;
308
309 //============================================================================
310 //
311 // public network functions
312 //
313 //============================================================================
314
315 extern char net_extresponse[NET_EXTRESPONSE_MAX][1400];
316 extern int net_extresponse_count;
317 extern int net_extresponse_last;
318
319 extern double masterquerytime;
320 extern int masterquerycount;
321 extern int masterreplycount;
322 extern int serverquerycount;
323 extern int serverreplycount;
324
325 extern sizebuf_t net_message;
326
327 extern cvar_t cl_netlocalping;
328
329 extern cvar_t cl_netport;
330 extern cvar_t sv_netport;
331 extern cvar_t net_address;
332 //extern cvar_t net_netaddress_ipv6;
333
334 int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol);
335 void NetConn_CloseClientPorts(void);
336 void NetConn_OpenClientPorts(void);
337 void NetConn_CloseServerPorts(void);
338 void NetConn_OpenServerPorts(int opennetports);
339 void NetConn_UpdateSockets(void);
340 lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address);
341 lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address);
342 void NetConn_Init(void);
343 void NetConn_Shutdown(void);
344 netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress);
345 void NetConn_Close(netconn_t *conn);
346 void NetConn_Listen(qboolean state);
347 int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress);
348 int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const lhnetaddress_t *peeraddress);
349 int NetConn_WriteString(lhnetsocket_t *mysocket, const char *string, const lhnetaddress_t *peeraddress);
350 int NetConn_IsLocalGame(void);
351 void NetConn_ClientFrame(void);
352 void NetConn_ServerFrame(void);
353 void NetConn_QueryMasters(qboolean querydp, qboolean queryqw);
354 void NetConn_Heartbeat(int priority);
355 void NetConn_QueryQueueFrame(void);
356 void Net_Stats_f(void);
357 void Net_Slist_f(void);
358 void Net_SlistQW_f(void);
359
360 // ServerList interface (public)
361 // manually refresh the view set, do this after having changed the mask or any other flag
362 void ServerList_RebuildViewList(void);
363 void ServerList_ResetMasks(void);
364 void ServerList_QueryList(qboolean querydp, qboolean queryqw);
365
366 #endif
367