|
Odamex
Setting the Standard in Multiplayer Doom
|
00001 // Emacs style mode select -*- C++ -*- 00002 //----------------------------------------------------------------------------- 00003 // 00004 // $Id: i_net.h 1788 2010-08-24 04:42:57Z russellrice $ 00005 // 00006 // Copyright (C) 2000-2006 by Sergey Makovkin (CSDoom .62) 00007 // Copyright (C) 2006-2010 by The Odamex Team 00008 // 00009 // This program is free software; you can redistribute it and/or 00010 // modify it under the terms of the GNU General Public License 00011 // as published by the Free Software Foundation; either version 2 00012 // of the License, or (at your option) any later version. 00013 // 00014 // This program is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // DESCRIPTION: 00020 // Master Server communicator 00021 // 00022 //----------------------------------------------------------------------------- 00023 00024 00025 #ifndef __I_NET_H__ 00026 #define __I_NET_H__ 00027 00028 #define MAX_UDP_PACKET 8192 00029 #define MASTERPORT 15000 00030 #define LAUNCHERPORT 12999 00031 00032 typedef unsigned char byte; 00033 00034 #define CHALLENGE 5560020 // challenge 00035 #define SERVER_CHALLENGE 5560020 // doomsv challenge 00036 #define LAUNCHER_CHALLENGE 777123 // csdl challenge 00037 00038 extern int localport; 00039 extern int msg_badread; 00040 00041 typedef struct 00042 { 00043 byte ip[4]; 00044 unsigned short port; 00045 unsigned short pad; 00046 } netadr_t; 00047 00048 extern netadr_t net_from; // address of who sent the packet 00049 00050 class buf_t 00051 { 00052 public: 00053 byte *data; 00054 size_t allocsize, cursize, readpos; 00055 bool overflowed; // set to true if the buffer size failed 00056 00057 public: 00058 00059 void WriteByte(byte b) 00060 { 00061 byte *buf = SZ_GetSpace(sizeof(b)); 00062 00063 if(!overflowed) 00064 { 00065 *buf = b; 00066 } 00067 } 00068 00069 void WriteShort(short s) 00070 { 00071 byte *buf = SZ_GetSpace(sizeof(s)); 00072 00073 if(!overflowed) 00074 { 00075 buf[0] = s&0xff; 00076 buf[1] = s>>8; 00077 } 00078 } 00079 00080 void WriteLong(int l) 00081 { 00082 byte *buf = SZ_GetSpace(sizeof(l)); 00083 00084 if(!overflowed) 00085 { 00086 buf[0] = l&0xff; 00087 buf[1] = (l>>8)&0xff; 00088 buf[2] = (l>>16)&0xff; 00089 buf[3] = l>>24; 00090 } 00091 } 00092 00093 void WriteString(const char *c) 00094 { 00095 if(c && *c) 00096 { 00097 size_t l = strlen(c); 00098 byte *buf = SZ_GetSpace(l + 1); 00099 00100 if(!overflowed) 00101 { 00102 memcpy(buf, c, l + 1); 00103 } 00104 } 00105 else 00106 WriteByte(0); 00107 } 00108 00109 void WriteChunk(const char *c, unsigned l, int startpos = 0) 00110 { 00111 byte *buf = SZ_GetSpace(l); 00112 00113 if(!overflowed) 00114 { 00115 memcpy(buf, c + startpos, l); 00116 } 00117 } 00118 00119 int ReadByte() 00120 { 00121 if(readpos+1 > cursize) 00122 { 00123 overflowed = true; 00124 return -1; 00125 } 00126 return (unsigned char)data[readpos++]; 00127 } 00128 00129 int NextByte() 00130 { 00131 if(readpos+1 > cursize) 00132 { 00133 overflowed = true; 00134 return -1; 00135 } 00136 return (unsigned char)data[readpos]; 00137 } 00138 00139 byte *ReadChunk(size_t size) 00140 { 00141 if(readpos+size > cursize) 00142 { 00143 overflowed = true; 00144 return NULL; 00145 } 00146 size_t oldpos = readpos; 00147 readpos += size; 00148 return data+oldpos; 00149 } 00150 00151 int ReadShort() 00152 { 00153 if(readpos+2 > cursize) 00154 { 00155 overflowed = true; 00156 return -1; 00157 } 00158 size_t oldpos = readpos; 00159 readpos += 2; 00160 return (short)(data[oldpos] + (data[oldpos+1]<<8)); 00161 } 00162 00163 int ReadLong() 00164 { 00165 if(readpos+4 > cursize) 00166 { 00167 overflowed = true; 00168 return -1; 00169 } 00170 size_t oldpos = readpos; 00171 readpos += 4; 00172 return data[oldpos] + 00173 (data[oldpos+1]<<8) + 00174 (data[oldpos+2]<<16)+ 00175 (data[oldpos+3]<<24); 00176 } 00177 00178 const char *ReadString() 00179 { 00180 byte *begin = data + readpos; 00181 00182 while(ReadByte() > 0); 00183 00184 if(overflowed) 00185 { 00186 return ""; 00187 } 00188 00189 return (const char *)begin; 00190 } 00191 00192 size_t BytesLeftToRead() 00193 { 00194 return overflowed || cursize < readpos ? 0 : cursize - readpos; 00195 } 00196 00197 size_t BytesRead() 00198 { 00199 return readpos; 00200 } 00201 00202 byte *ptr() 00203 { 00204 return data; 00205 } 00206 00207 size_t size() 00208 { 00209 return cursize; 00210 } 00211 00212 size_t maxsize() 00213 { 00214 return allocsize; 00215 } 00216 00217 void setcursize(size_t len) 00218 { 00219 cursize = len > allocsize ? allocsize : len; 00220 } 00221 00222 void clear() 00223 { 00224 cursize = 0; 00225 readpos = 0; 00226 overflowed = false; 00227 } 00228 00229 void resize(size_t len) 00230 { 00231 if(data) 00232 delete[] data; 00233 00234 data = new byte[len]; 00235 allocsize = len; 00236 cursize = 0; 00237 readpos = 0; 00238 overflowed = false; 00239 } 00240 00241 byte *SZ_GetSpace(size_t length) 00242 { 00243 if (cursize + length >= allocsize) 00244 { 00245 clear(); 00246 overflowed = true; 00247 } 00248 00249 byte *ret = data + cursize; 00250 cursize += length; 00251 00252 return ret; 00253 } 00254 00255 buf_t &operator =(const buf_t &other) 00256 { 00257 if(data) 00258 delete[] data; 00259 00260 data = new byte[other.allocsize]; 00261 allocsize = other.allocsize; 00262 cursize = other.cursize; 00263 overflowed = other.overflowed; 00264 readpos = other.readpos; 00265 00266 if(!overflowed) 00267 for(size_t i = 0; i < cursize; i++) 00268 data[i] = other.data[i]; 00269 00270 return *this; 00271 } 00272 00273 buf_t() 00274 : data(0), allocsize(0), cursize(0), readpos(0), overflowed(false) 00275 { 00276 } 00277 buf_t(size_t len) 00278 : data(new byte[len]), allocsize(len), cursize(0), readpos(0), overflowed(false) 00279 { 00280 } 00281 buf_t(const buf_t &other) 00282 : data(new byte[other.allocsize]), allocsize(other.allocsize), cursize(other.cursize), readpos(other.readpos), overflowed(other.overflowed) 00283 00284 { 00285 if(!overflowed) 00286 for(size_t i = 0; i < cursize; i++) 00287 data[i] = other.data[i]; 00288 } 00289 ~buf_t() 00290 { 00291 if(data) 00292 delete[] data; 00293 } 00294 }; 00295 00296 extern buf_t net_message; 00297 00298 void CloseNetwork(void); 00299 void InitNetCommon(void); 00300 void I_SetPort(netadr_t &addr, int port); 00301 void I_DoSelect(void); 00302 00303 char *NET_AdrToString(netadr_t a, bool displayport = true); 00304 bool NET_StringToAdr(char *s, netadr_t *a); 00305 bool NET_CompareAdr(netadr_t a, netadr_t b); 00306 int NET_GetPacket(void); 00307 void NET_SendPacket(int length, byte *data, netadr_t to); 00308 00309 #endif