]> icculus.org git repositories - divverent/darkplaces.git/blob - net.h
whitespace cleanup
[divverent/darkplaces.git] / net.h
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // net.h -- quake's interface to the networking layer
21
22 #ifndef NET_H
23 #define NET_H
24
25 struct qsockaddr
26 {
27         short sa_family;
28         unsigned char sa_data[14];
29 };
30
31
32 #define NET_NAMELEN                     64
33
34 #define NET_MAXMESSAGE          16384
35 #define NET_HEADERSIZE          (2 * sizeof(unsigned int))
36 #define NET_DATAGRAMSIZE        (MAX_DATAGRAM + NET_HEADERSIZE)
37
38 // NetHeader flags
39 #define NETFLAG_LENGTH_MASK     0x0000ffff
40 #define NETFLAG_DATA            0x00010000
41 #define NETFLAG_ACK                     0x00020000
42 #define NETFLAG_NAK                     0x00040000
43 #define NETFLAG_EOM                     0x00080000
44 #define NETFLAG_UNRELIABLE      0x00100000
45 #define NETFLAG_CTL                     0x80000000
46
47
48 #define NET_PROTOCOL_VERSION    3
49
50 // This is the network info/connection protocol.  It is used to find Quake
51 // servers, get info about them, and connect to them.  Once connected, the
52 // Quake game protocol (documented elsewhere) is used.
53 //
54 //
55 // General notes:
56 //      game_name is currently always "QUAKE", but is there so this same protocol
57 //              can be used for future games as well; can you say Quake2?
58 //
59 // CCREQ_CONNECT
60 //              string  game_name                               "QUAKE"
61 //              byte    net_protocol_version    NET_PROTOCOL_VERSION
62 //
63 // CCREQ_SERVER_INFO
64 //              string  game_name                               "QUAKE"
65 //              byte    net_protocol_version    NET_PROTOCOL_VERSION
66 //
67 // CCREQ_PLAYER_INFO
68 //              byte    player_number
69 //
70 // CCREQ_RULE_INFO
71 //              string  rule
72 //
73 //
74 //
75 // CCREP_ACCEPT
76 //              long    port
77 //
78 // CCREP_REJECT
79 //              string  reason
80 //
81 // CCREP_SERVER_INFO
82 //              string  server_address
83 //              string  host_name
84 //              string  level_name
85 //              byte    current_players
86 //              byte    max_players
87 //              byte    protocol_version        NET_PROTOCOL_VERSION
88 //
89 // CCREP_PLAYER_INFO
90 //              byte    player_number
91 //              string  name
92 //              long    colors
93 //              long    frags
94 //              long    connect_time
95 //              string  address
96 //
97 // CCREP_RULE_INFO
98 //              string  rule
99 //              string  value
100
101 //      note:
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.
110
111 #define CCREQ_CONNECT           0x01
112 #define CCREQ_SERVER_INFO       0x02
113 #define CCREQ_PLAYER_INFO       0x03
114 #define CCREQ_RULE_INFO         0x04
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
122 typedef struct qsocket_s
123 {
124         struct qsocket_s        *next;
125         double                  connecttime;
126         double                  lastMessageTime;
127         double                  lastSendTime;
128
129         qboolean                disconnected;
130         qboolean                canSend;
131         qboolean                sendNext;
132
133         int                             driver;
134         int                             landriver;
135         int                             socket;
136         void                    *driverdata;
137
138         unsigned int    ackSequence;
139         unsigned int    sendSequence;
140         unsigned int    unreliableSendSequence;
141         int                             sendMessageLength;
142         qbyte                   sendMessage [NET_MAXMESSAGE];
143
144         unsigned int    receiveSequence;
145         unsigned int    unreliableReceiveSequence;
146         int                             receiveMessageLength;
147         qbyte                   receiveMessage [NET_MAXMESSAGE];
148
149         struct qsockaddr        addr;
150         char                            address[NET_NAMELEN];
151
152 } qsocket_t;
153
154 extern qsocket_t        *net_activeSockets;
155 extern mempool_t *net_mempool;
156
157 typedef struct
158 {
159         char            *name;
160         qboolean        initialized;
161         int                     controlSock;
162         int                     (*Init) (void);
163         void            (*Shutdown) (void);
164         void            (*Listen) (qboolean state);
165         int             (*OpenSocket) (int port);
166         int             (*CloseSocket) (int socket);
167         int             (*Connect) (int socket, struct qsockaddr *addr);
168         int             (*CheckNewConnections) (void);
169         int             (*Read) (int socket, qbyte *buf, int len, struct qsockaddr *addr);
170         int             (*Write) (int socket, qbyte *buf, int len, struct qsockaddr *addr);
171         int             (*Broadcast) (int socket, qbyte *buf, int len);
172         char *          (*AddrToString) (struct qsockaddr *addr);
173         int             (*StringToAddr) (char *string, struct qsockaddr *addr);
174         int             (*GetSocketAddr) (int socket, struct qsockaddr *addr);
175         int             (*GetNameFromAddr) (struct qsockaddr *addr, char *name);
176         int             (*GetAddrFromName) (char *name, struct qsockaddr *addr);
177         int                     (*AddrCompare) (struct qsockaddr *addr1, struct qsockaddr *addr2);
178         int                     (*GetSocketPort) (struct qsockaddr *addr);
179         int                     (*SetSocketPort) (struct qsockaddr *addr, int port);
180 } net_landriver_t;
181
182 #define MAX_NET_DRIVERS         8
183 extern int                              net_numlandrivers;
184 extern net_landriver_t  net_landrivers[MAX_NET_DRIVERS];
185
186 typedef struct
187 {
188         char            *name;
189         qboolean        initialized;
190         int                     (*Init) (void);
191         void            (*Listen) (qboolean state);
192         void            (*SearchForHosts) (qboolean xmit);
193         qsocket_t       *(*Connect) (char *host);
194         qsocket_t       *(*CheckNewConnections) (void);
195         int                     (*QGetMessage) (qsocket_t *sock);
196         int                     (*QSendMessage) (qsocket_t *sock, sizebuf_t *data);
197         int                     (*SendUnreliableMessage) (qsocket_t *sock, sizebuf_t *data);
198         qboolean        (*CanSendMessage) (qsocket_t *sock);
199         qboolean        (*CanSendUnreliableMessage) (qsocket_t *sock);
200         void            (*Close) (qsocket_t *sock);
201         void            (*Shutdown) (void);
202         int                     controlSock;
203 } net_driver_t;
204
205 extern int                      net_numdrivers;
206 extern net_driver_t     net_drivers[MAX_NET_DRIVERS];
207
208 extern int                      DEFAULTnet_hostport;
209 extern int                      net_hostport;
210
211 extern int net_driverlevel;
212 extern cvar_t           hostname;
213 extern char                     playername[];
214 extern int                      playercolor;
215
216 extern int              messagesSent;
217 extern int              messagesReceived;
218 extern int              unreliableMessagesSent;
219 extern int              unreliableMessagesReceived;
220
221 qsocket_t *NET_NewQSocket (void);
222 void NET_FreeQSocket(qsocket_t *);
223 double SetNetTime(void);
224
225
226 #define HOSTCACHESIZE   8
227
228 typedef struct
229 {
230         char    name[16];
231         char    map[16];
232         char    cname[32];
233         int             users;
234         int             maxusers;
235         int             driver;
236         int             ldriver;
237         struct qsockaddr addr;
238 } hostcache_t;
239
240 extern int hostCacheCount;
241 extern hostcache_t hostcache[HOSTCACHESIZE];
242
243 #if !defined(_WIN32 ) && !defined (__linux__) && !defined (__sun__)
244 #ifndef htonl
245 extern unsigned long htonl (unsigned long hostlong);
246 #endif
247 #ifndef htons
248 extern unsigned short htons (unsigned short hostshort);
249 #endif
250 #ifndef ntohl
251 extern unsigned long ntohl (unsigned long netlong);
252 #endif
253 #ifndef ntohs
254 extern unsigned short ntohs (unsigned short netshort);
255 #endif
256 #endif
257
258 //============================================================================
259 //
260 // public network functions
261 //
262 //============================================================================
263
264 extern  double          net_time;
265 extern  sizebuf_t       net_message;
266 extern  int                     net_activeconnections;
267
268 void            NET_Init (void);
269 void            NET_Shutdown (void);
270
271 struct qsocket_s        *NET_CheckNewConnections (void);
272 // returns a new connection number if there is one pending, else -1
273
274 struct qsocket_s        *NET_Connect (char *host);
275 // called by client to connect to a host.  Returns -1 if not able to
276
277 qboolean NET_CanSendMessage (qsocket_t *sock);
278 // Returns true or false if the given qsocket can currently accept a
279 // message to be transmitted.
280
281 int                     NET_GetMessage (struct qsocket_s *sock);
282 // returns data in net_message sizebuf
283 // returns 0 if no data is waiting
284 // returns 1 if a message was received
285 // returns 2 if an unreliable message was received
286 // returns -1 if the connection died
287
288 int                     NET_SendMessage (struct qsocket_s *sock, sizebuf_t *data);
289 int                     NET_SendUnreliableMessage (struct qsocket_s *sock, sizebuf_t *data);
290 // returns 0 if the message connot be delivered reliably, but the connection
291 //              is still considered valid
292 // returns 1 if the message was sent properly
293 // returns -1 if the connection died
294
295 int                     NET_SendToAll(sizebuf_t *data, int blocktime);
296 // This is a reliable *blocking* send to all attached clients.
297
298
299 void            NET_Close (struct qsocket_s *sock);
300 // if a dead connection is returned by a get or send function, this function
301 // should be called when it is convenient
302
303 // Server calls when a client is kicked off for a game related misbehavior
304 // like an illegal protocal conversation.  Client calls when disconnecting
305 // from a server.
306 // A netcon_t number will not be reused until this function is called for it
307
308 void NET_Poll(void);
309
310
311 typedef struct _PollProcedure
312 {
313         struct _PollProcedure   *next;
314         double                                  nextTime;
315         void                                    (*procedure)();
316         void                                    *arg;
317 } PollProcedure;
318
319 void SchedulePollProcedure(PollProcedure *pp, double timeOffset);
320
321 extern  qboolean        ipxAvailable;
322 extern  qboolean        tcpipAvailable;
323 extern  char            my_ipx_address[NET_NAMELEN];
324 extern  char            my_tcpip_address[NET_NAMELEN];
325
326 extern  qboolean        slistInProgress;
327 extern  qboolean        slistSilent;
328 extern  qboolean        slistLocal;
329
330 extern cvar_t hostname;
331
332 void NET_Slist_f (void);
333
334 #endif
335