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