1 /* 2 Copyright (C) 1997-2001 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 // 21 // protocol.h 22 // 23 24 /* 25 ============================================================================= 26 27 PROTOCOL 28 29 ============================================================================= 30 */ 31 32 #define UPDATE_BACKUP 16 // copies of entityState_t to keep buffered must be power of two 33 #define UPDATE_MASK (UPDATE_BACKUP-1) 34 35 // 36 // client to server 37 // 38 enum { 39 CLC_BAD, 40 CLC_NOP, 41 CLC_MOVE, // [[userCmd_t] 42 CLC_USERINFO, // [[userinfo string] 43 CLC_STRINGCMD, // [string] message 44 CLC_SETTING // [setting][value] R1Q2 settings support. 45 }; 46 47 enum { 48 CLSET_NOGUN, 49 CLSET_NOBLEND, 50 CLSET_RECORDING, 51 CLSET_MAX 52 }; 53 54 // playerState_t communication 55 enum { 56 PS_M_TYPE = 1 << 0, 57 PS_M_ORIGIN = 1 << 1, 58 PS_M_VELOCITY = 1 << 2, 59 PS_M_TIME = 1 << 3, 60 PS_M_FLAGS = 1 << 4, 61 PS_M_GRAVITY = 1 << 5, 62 PS_M_DELTA_ANGLES = 1 << 6, 63 64 PS_VIEWOFFSET = 1 << 7, 65 PS_VIEWANGLES = 1 << 8, 66 PS_KICKANGLES = 1 << 9, 67 PS_BLEND = 1 << 10, 68 PS_FOV = 1 << 11, 69 PS_WEAPONINDEX = 1 << 12, 70 PS_WEAPONFRAME = 1 << 13, 71 PS_RDFLAGS = 1 << 14, 72 73 PS_BBOX = 1 << 15 // for new ENHANCED_PROTOCOL_VERSION 74 }; 75 76 // enhanced protocol 77 enum { 78 EPS_GUNOFFSET = 1 << 0, 79 EPS_GUNANGLES = 1 << 1, 80 EPS_PMOVE_VELOCITY2 = 1 << 2, 81 EPS_PMOVE_ORIGIN2 = 1 << 3, 82 EPS_VIEWANGLE2 = 1 << 4, 83 EPS_STATS = 1 << 5 84 }; 85 86 87 // userCmd_t communication 88 // ms and light always sent, the others are optional 89 enum { 90 CM_ANGLE1 = 1 << 0, 91 CM_ANGLE2 = 1 << 1, 92 CM_ANGLE3 = 1 << 2, 93 CM_FORWARD = 1 << 3, 94 CM_SIDE = 1 << 4, 95 CM_UP = 1 << 5, 96 CM_BUTTONS = 1 << 6, 97 CM_IMPULSE = 1 << 7 98 }; 99 100 101 // sound communication 102 // a sound without an ent or pos will be a local only sound 103 enum { 104 SND_VOLUME = 1 << 0, // a byte 105 SND_ATTENUATION = 1 << 1, // a byte 106 SND_POS = 1 << 2, // three coordinates 107 SND_ENT = 1 << 3, // a short 0-2: channel, 3-12: entity 108 SND_OFFSET = 1 << 4 // a byte, msec offset from frame start 109 }; 110 111 #define DEFAULT_SOUND_PACKET_VOLUME 1.0 112 #define DEFAULT_SOUND_PACKET_ATTENUATION 1.0 113 114 // entityState_t communication 115 enum { 116 // try to pack the common update flags into the first byte 117 U_ORIGIN1 = 1 << 0, 118 U_ORIGIN2 = 1 << 1, 119 U_ANGLE2 = 1 << 2, 120 U_ANGLE3 = 1 << 3, 121 U_FRAME8 = 1 << 4, // frame is a byte 122 U_EVENT = 1 << 5, 123 U_REMOVE = 1 << 6, // REMOVE this entity, don't add it 124 U_MOREBITS1 = 1 << 7, // read one additional byte 125 126 // second byte 127 U_NUMBER16 = 1 << 8, // NUMBER8 is implicit if not set 128 U_ORIGIN3 = 1 << 9, 129 U_ANGLE1 = 1 << 10, 130 U_MODEL = 1 << 11, 131 U_RENDERFX8 = 1 << 12, // fullbright, etc 132 U_EFFECTS8 = 1 << 14, // autorotate, trails, etc 133 U_MOREBITS2 = 1 << 15, // read one additional byte 134 135 // third byte 136 U_SKIN8 = 1 << 16, 137 U_FRAME16 = 1 << 17, // frame is a short 138 U_RENDERFX16 = 1 << 18, // 8 + 16 = 32 139 U_EFFECTS16 = 1 << 19, // 8 + 16 = 32 140 U_MODEL2 = 1 << 20, // weapons, flags, etc 141 U_MODEL3 = 1 << 21, 142 U_MODEL4 = 1 << 22, 143 U_MOREBITS3 = 1 << 23, // read one additional byte 144 145 // fourth byte 146 U_OLDORIGIN = 1 << 24, // FIXME: get rid of this 147 U_SKIN16 = 1 << 25, 148 U_SOUND = 1 << 26, 149 U_SOLID = 1 << 27, 150 151 U_VELOCITY = 1 << 28 // new for ENHANCED_PROTOCOL_VERSION 152 }; 153 154 /* 155 ============================================================================= 156 157 MESSAGE FUNCTIONS 158 159 ============================================================================= 160 */ 161 162 typedef struct netMsg_s { 163 qBool allowOverflow; // if false, do a Com_Error 164 qBool overFlowed; // set to true if the buffer size failed 165 byte *data; 166 int maxSize; 167 int curSize; 168 int readCount; 169 int bufferSize; 170 } netMsg_t; 171 172 // supporting functions 173 void MSG_Init (netMsg_t *dest, byte *data, int length); 174 void MSG_Clear (netMsg_t *dest); 175 176 // writing 177 #define MSG_WriteAngle(dest,f) (MSG_WriteByte((dest),ANGLE2BYTE ((f)))) 178 #define MSG_WriteAngle16(dest,f) (MSG_WriteShort((dest),ANGLE2SHORT ((f)))) 179 #define MSG_WriteCoord(dest,f) (MSG_WriteShort((dest),(int)((f)*8))) 180 #define MSG_WritePos(dest,pos) (MSG_WriteCoord((dest),(pos)[0]), MSG_WriteCoord((dest),(pos)[1]), MSG_WriteCoord((dest),(pos)[2])) 181 182 void MSG_WriteByte (netMsg_t *dest, int c); 183 void MSG_WriteChar (netMsg_t *dest, int c); 184 void MSG_WriteDeltaUsercmd (netMsg_t *dest, struct userCmd_s *from, struct userCmd_s *cmd); 185 void MSG_WriteDeltaEntity (netMsg_t *dest, entityStateOld_t *from, entityStateOld_t *to, qBool force, qBool newEntity); 186 void MSG_WriteDir (netMsg_t *dest, vec3_t vector); 187 void MSG_WriteFloat (netMsg_t *dest, float f); 188 void MSG_WriteInt3 (netMsg_t *dest, int c); 189 void MSG_WriteLong (netMsg_t *dest, int c); 190 void MSG_WriteRaw (netMsg_t *dest, void *data, int length); 191 void MSG_WriteShort (netMsg_t *dest, int c); 192 void MSG_WriteString (netMsg_t *dest, char *s); 193 void MSG_WriteStringCat (netMsg_t *dest, char *data); 194 195 // reading 196 #define MSG_ReadAngle(src) (BYTE2ANGLE (MSG_ReadChar ((src)))) 197 #define MSG_ReadAngle16(src) (SHORT2ANGLE (MSG_ReadShort ((src)))) 198 #define MSG_ReadCoord(src) (MSG_ReadShort((src)) * (1.0f/8.0f)) 199 #define MSG_ReadPos(src,pos) ((pos)[0]=MSG_ReadCoord((src)), (pos)[1]=MSG_ReadCoord((src)), (pos)[2]=MSG_ReadCoord((src))) 200 201 void MSG_BeginReading (netMsg_t *src); 202 int MSG_ReadByte (netMsg_t *src); 203 int MSG_ReadChar (netMsg_t *src); 204 void MSG_ReadData (netMsg_t *src, void *buffer, int size); 205 void MSG_ReadDeltaUsercmd (netMsg_t *src, struct userCmd_s *from, struct userCmd_s *cmd); 206 void MSG_ReadDir (netMsg_t *src, vec3_t vector); 207 float MSG_ReadFloat (netMsg_t *src); 208 int MSG_ReadInt3 (netMsg_t *src); 209 int MSG_ReadLong (netMsg_t *src); 210 int MSG_ReadShort (netMsg_t *src); 211 char *MSG_ReadString (netMsg_t *src); 212 char *MSG_ReadStringLine (netMsg_t *src); 213 214 /* 215 ============================================================================= 216 217 NETCHAN 218 219 ============================================================================= 220 */ 221 222 // Default ports 223 #define PORT_ANY -1 224 #define PORT_MASTER 27900 225 #define PORT_SERVER 27910 226 227 // Maximum message lengths 228 #define PACKET_HEADER 10 // two ints and a short 229 230 #define MAX_SV_MSGLEN 1400 231 #define MAX_SV_USABLEMSG (MAX_SV_MSGLEN - PACKET_HEADER) 232 233 #define MAX_CL_MSGLEN 4096 234 #define MAX_CL_USABLEMSG (MAX_CL_MSGLEN - PACKET_HEADER) 235 236 typedef struct netStats_s { 237 qBool initialized; 238 uint32 initTime; 239 240 uint32 sizeIn; 241 uint32 sizeOut; 242 243 uint32 packetsIn; 244 uint32 packetsOut; 245 } netStats_t; 246 247 extern netStats_t netStats; 248 249 typedef enum netAdrType_s { 250 NA_LOOPBACK, 251 NA_BROADCAST, 252 NA_IP, 253 254 NA_MAX 255 } netAdrType_t; 256 257 typedef enum netSrc_s { 258 NS_CLIENT, 259 NS_SERVER, 260 261 NS_MAX 262 } netSrc_t; 263 264 typedef enum netConfig_s { 265 NET_NONE, 266 267 NET_CLIENT = 1 << 0, 268 NET_SERVER = 1 << 1, 269 } netConfig_t; 270 271 typedef struct netAdr_s { 272 netAdrType_t naType; 273 274 byte ip[4]; 275 276 uint16 port; 277 } netAdr_t; 278 279 // Compares with the port 280 #define NET_CompareAdr(a,b) \ 281 (a.naType != b.naType) ? qFalse : \ 282 (a.naType == NA_LOOPBACK) ? qTrue : \ 283 (a.naType == NA_IP) ? \ 284 ((a.ip[0] == b.ip[0]) && \ 285 (a.ip[1] == b.ip[1]) && \ 286 (a.ip[2] == b.ip[2]) && \ 287 (a.ip[3] == b.ip[3]) && \ 288 (a.port == b.port)) ? qTrue : qFalse \ 289 : qFalse 290 291 // Compares without the port 292 #define NET_CompareBaseAdr(a,b) \ 293 (a.naType != b.naType) ? qFalse : \ 294 (a.naType == NA_LOOPBACK) ? qTrue : \ 295 (a.naType == NA_IP) ? \ 296 ((a.ip[0] == b.ip[0]) && \ 297 (a.ip[1] == b.ip[1]) && \ 298 (a.ip[2] == b.ip[2]) && \ 299 (a.ip[3] == b.ip[3])) ? qTrue : qFalse \ 300 : qFalse 301 302 // Converts from sockaddr_in to netAdr_t 303 #define NET_SockAdrToNetAdr(s,a) \ 304 a->naType = NA_IP; \ 305 *(int *)&a->ip = ((struct sockaddr_in *)s)->sin_addr.s_addr; \ 306 a->port = ((struct sockaddr_in *)s)->sin_port; 307 308 // Checks if an address is a loopback address 309 #ifdef WIN32 310 # define NET_IsLocalAddress(adr) ((adr.naType == NA_LOOPBACK) ? qTrue : qFalse) 311 #else 312 qBool NET_IsLocalAddress (netAdr_t adr); 313 #endif 314 315 void NET_Init (void); 316 void NET_Shutdown (void); 317 318 netConfig_t NET_Config (netConfig_t openFlags); 319 320 qBool NET_GetPacket (netSrc_t sock, netAdr_t *fromAddr, netMsg_t *message); 321 int NET_SendPacket (netSrc_t sock, int length, void *data, netAdr_t *to); 322 323 char *NET_AdrToString (netAdr_t *a); 324 qBool NET_StringToAdr (char *s, netAdr_t *a); 325 void NET_Sleep (int msec); 326 327 typedef struct netChan_s { 328 qBool fatalError; 329 330 netSrc_t sock; 331 332 int dropped; // between last packet and previous 333 334 int lastReceived; // for timeouts 335 int lastSent; // for retransmits 336 337 netAdr_t remoteAddress; 338 uint16 qPort; // qport value to write when transmitting 339 uint16 protocol; 340 341 // Sequencing variables 342 int incomingSequence; 343 int incomingAcknowledged; 344 int incomingReliableAcknowledged; // single bit 345 int incomingReliableSequence; // single bit, maintained local 346 qBool gotReliable; 347 348 int outgoingSequence; 349 int reliableSequence; // single bit 350 int lastReliableSequence; // sequence number of last send 351 352 // Reliable staging and holding areas 353 netMsg_t message; // writing buffer to send to server 354 byte messageBuff[MAX_CL_USABLEMSG]; // leave space for header 355 356 // Message is copied to this buffer when it is first transfered 357 int reliableLength; 358 byte reliableBuff[MAX_CL_USABLEMSG]; // unacked reliable message 359 } netChan_t; 360 361 void Netchan_Init (void); 362 void Netchan_Setup (netSrc_t sock, netChan_t *chan, netAdr_t *adr, int protocol, int qPort, uint32 msgLen); 363 364 int Netchan_Transmit (netChan_t *chan, int length, byte *data); 365 void Netchan_OutOfBand (netSrc_t netSocket, netAdr_t *adr, int length, byte *data); 366 void Netchan_OutOfBandPrint (netSrc_t netSocket, netAdr_t *adr, char *format, ...); 367 qBool Netchan_Process (netChan_t *chan, netMsg_t *msg); 368