]> icculus.org git repositories - divverent/darkplaces.git/blob - netconn.h
experimental (not terribly useful) support for r_shadow_realtime_dlight 1 mode (_worl...
[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_NAMELEN                     128
28
29 #define NET_MAXMESSAGE          65536
30 #define NET_HEADERSIZE          (2 * sizeof(unsigned int))
31 #define NET_DATAGRAMSIZE        (MAX_DATAGRAM + NET_HEADERSIZE)
32
33 // NetHeader flags
34 #define NETFLAG_LENGTH_MASK     0x0000ffff
35 #define NETFLAG_DATA            0x00010000
36 #define NETFLAG_ACK                     0x00020000
37 #define NETFLAG_NAK                     0x00040000
38 #define NETFLAG_EOM                     0x00080000
39 #define NETFLAG_UNRELIABLE      0x00100000
40 #define NETFLAG_CTL                     0x80000000
41
42
43 #define NET_PROTOCOL_VERSION    3
44
45 // This is the network info/connection protocol.  It is used to find Quake
46 // servers, get info about them, and connect to them.  Once connected, the
47 // Quake game protocol (documented elsewhere) is used.
48 //
49 //
50 // General notes:
51 //      game_name is currently always "QUAKE", but is there so this same protocol
52 //              can be used for future games as well; can you say Quake2?
53 //
54 // CCREQ_CONNECT
55 //              string  game_name                               "QUAKE"
56 //              byte    net_protocol_version    NET_PROTOCOL_VERSION
57 //
58 // CCREQ_SERVER_INFO
59 //              string  game_name                               "QUAKE"
60 //              byte    net_protocol_version    NET_PROTOCOL_VERSION
61 //
62 // CCREQ_PLAYER_INFO
63 //              byte    player_number
64 //
65 // CCREQ_RULE_INFO
66 //              string  rule
67 //
68 //
69 //
70 // CCREP_ACCEPT
71 //              long    port
72 //
73 // CCREP_REJECT
74 //              string  reason
75 //
76 // CCREP_SERVER_INFO
77 //              string  server_address
78 //              string  host_name
79 //              string  level_name
80 //              byte    current_players
81 //              byte    max_players
82 //              byte    protocol_version        NET_PROTOCOL_VERSION
83 //
84 // CCREP_PLAYER_INFO
85 //              byte    player_number
86 //              string  name
87 //              long    colors
88 //              long    frags
89 //              long    connect_time
90 //              string  address
91 //
92 // CCREP_RULE_INFO
93 //              string  rule
94 //              string  value
95
96 //      note:
97 //              There are two address forms used above.  The short form is just a
98 //              port number.  The address that goes along with the port is defined as
99 //              "whatever address you receive this reponse from".  This lets us use
100 //              the host OS to solve the problem of multiple host addresses (possibly
101 //              with no routing between them); the host will use the right address
102 //              when we reply to the inbound connection request.  The long from is
103 //              a full address and port in a string.  It is used for returning the
104 //              address of a server that is not running locally.
105
106 #define CCREQ_CONNECT           0x01
107 #define CCREQ_SERVER_INFO       0x02
108 #define CCREQ_PLAYER_INFO       0x03
109 #define CCREQ_RULE_INFO         0x04
110
111 #define CCREP_ACCEPT            0x81
112 #define CCREP_REJECT            0x82
113 #define CCREP_SERVER_INFO       0x83
114 #define CCREP_PLAYER_INFO       0x84
115 #define CCREP_RULE_INFO         0x85
116
117 typedef struct netconn_s
118 {
119         struct netconn_s *next;
120
121         lhnetsocket_t *mysocket;
122         lhnetaddress_t peeraddress;
123
124         // this is mostly identical to qsocket_t from quake
125
126         // if this time is reached, kick off peer
127         double connecttime;
128         double timeout;
129         double lastMessageTime;
130         double lastSendTime;
131
132         qboolean canSend;
133         qboolean sendNext;
134
135         unsigned int ackSequence;
136         unsigned int sendSequence;
137         unsigned int unreliableSendSequence;
138         int sendMessageLength;
139         qbyte sendMessage[NET_MAXMESSAGE];
140
141         unsigned int receiveSequence;
142         unsigned int unreliableReceiveSequence;
143         int receiveMessageLength;
144         qbyte receiveMessage[NET_MAXMESSAGE];
145
146         char address[NET_NAMELEN];
147 } netconn_t;
148
149 extern netconn_t *netconn_list;
150 extern mempool_t *netconn_mempool;
151
152 extern cvar_t hostname;
153 extern cvar_t developer_networking;
154 extern char playername[];
155 extern int playercolor;
156
157 #define HOSTCACHESIZE   128
158
159 typedef struct
160 {
161         // ping time for sorting servers
162         double ping;
163         // address for connecting
164         char cname[128];
165         // description (seen by user)
166         char line1[128];
167         char line2[128];
168 } hostcache_t;
169
170 extern int hostCacheCount;
171 extern hostcache_t hostcache[HOSTCACHESIZE];
172
173 #if !defined(_WIN32 ) && !defined (__linux__) && !defined (__sun__)
174 #ifndef htonl
175 extern unsigned long htonl (unsigned long hostlong);
176 #endif
177 #ifndef htons
178 extern unsigned short htons (unsigned short hostshort);
179 #endif
180 #ifndef ntohl
181 extern unsigned long ntohl (unsigned long netlong);
182 #endif
183 #ifndef ntohs
184 extern unsigned short ntohs (unsigned short netshort);
185 #endif
186 #endif
187
188 //============================================================================
189 //
190 // public network functions
191 //
192 //============================================================================
193
194 extern sizebuf_t net_message;
195
196 extern cvar_t cl_fakelocalping_min;
197 extern cvar_t cl_fakelocalping_max;
198
199 int NetConn_SendReliableMessage(netconn_t *conn, sizebuf_t *data);
200 //void NetConn_SendMessageNext(netconn_t *conn);
201 //void NetConn_ReSendMessage(netconn_t *conn);
202 qboolean NetConn_CanSendMessage(netconn_t *conn);
203 int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data);
204 void NetConn_CloseClientPorts(void);
205 void NetConn_OpenClientPorts(void);
206 void NetConn_CloseServerPorts(void);
207 void NetConn_OpenServerPorts(int opennetports);
208 lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address);
209 lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address);
210 void NetConn_Init(void);
211 void NetConn_Shutdown(void);
212 netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress);
213 void NetConn_Close(netconn_t *conn);
214 void NetConn_Listen(qboolean state);
215 int NetConn_IsLocalGame(void);
216 //int NetConn_ReceivedMessage(netconn_t *conn, qbyte *data, int length);
217 //int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length, lhnetaddress_t *peeraddress);
218 //int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length, lhnetaddress_t *peeraddress);
219 void NetConn_ClientFrame(void);
220 void NetConn_ServerFrame(void);
221 void NetConn_QueryMasters(void);
222 void NetConn_Heartbeat(int priority);
223 int NetConn_SendToAll(sizebuf_t *data, double blocktime);
224 void Net_Stats_f(void);
225 void Net_Slist_f(void);
226
227 #endif
228