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