- Timestamp:
- Dec 30, 2005, 1:52:26 AM (19 years ago)
- Location:
- branches/network_merged/src
- Files:
-
- 43 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network_merged/src/defs/class_id.h
r6287 r6340 253 253 CL_CONNECTION_MONITOR = 0x00000b05, 254 254 CL_HANDSHAKE = 0x00000b06, 255 CL_ ENTITY_MANAGER= 0x00000b07,255 CL_NETWORK_GAME_MANAGER = 0x00000b07, 256 256 257 257 -
branches/network_merged/src/lib/coord/p_node.cc
r6307 r6340 27 27 #include "glincl.h" 28 28 #include "color.h" 29 30 #include "synchronizeable.h" 29 31 30 32 using namespace std; … … 1007 1009 return (PNODE_ROTATE_AND_MOVE); 1008 1010 } 1011 1012 /** 1013 * Writes data from network containing information about the state 1014 * @param data pointer to data 1015 * @param length length of data 1016 * @param sender hostID of sender 1017 */ 1018 int PNode::writeState( const byte * data, int length, int sender ) 1019 { 1020 SYNCHELP_READ_BEGIN(); 1021 1022 SYNCHELP_READ_FKT( BaseObject::writeState ); 1023 1024 PRINTF(0)("name = %s\n", this->getName()); 1025 1026 char * parentName = NULL; 1027 SYNCHELP_READ_STRINGM( parentName ); 1028 1029 if ( strcmp(parentName, "")==0 ) 1030 { 1031 setParent( (char*)NULL ); 1032 } 1033 else 1034 { 1035 setParent( parentName ); 1036 } 1037 1038 delete[] parentName; 1039 1040 int parentMode; 1041 SYNCHELP_READ_INT( parentMode ); 1042 this->setParentMode((PARENT_MODE)parentMode); 1043 1044 float f1, f2, f3, f4; 1045 1046 SYNCHELP_READ_FLOAT( f1 ); 1047 SYNCHELP_READ_FLOAT( f2 ); 1048 SYNCHELP_READ_FLOAT( f3 ); 1049 this->setRelCoor( f1, f2, f3 ); 1050 //this->setRelCoor( 10, 0, 0 ); 1051 1052 SYNCHELP_READ_FLOAT( f1 ); 1053 SYNCHELP_READ_FLOAT( f2 ); 1054 SYNCHELP_READ_FLOAT( f3 ); 1055 //this->setAbsCoor( f1, f2, f3 ); 1056 1057 SYNCHELP_READ_FLOAT( f1 ); 1058 SYNCHELP_READ_FLOAT( f2 ); 1059 SYNCHELP_READ_FLOAT( f3 ); 1060 SYNCHELP_READ_FLOAT( f4 ); 1061 this->setRelDir( Quaternion( Vector(f2, f3, f4), f1 ) ); 1062 1063 SYNCHELP_READ_FLOAT( f1 ); 1064 SYNCHELP_READ_FLOAT( f2 ); 1065 SYNCHELP_READ_FLOAT( f3 ); 1066 SYNCHELP_READ_FLOAT( f4 ); 1067 //this->setAbsDir( Quaternion( Vector(f2, f3, f4), f1 ) ); 1068 1069 int n; 1070 char * childName; 1071 1072 SYNCHELP_READ_INT( n ); 1073 1074 for (int i = 0; i<n; i++) 1075 { 1076 SYNCHELP_READ_STRINGM( childName ); 1077 PRINTF(0)("childname = %s\n", childName); 1078 addChild( childName ); 1079 delete childName; 1080 childName = NULL; 1081 } 1082 1083 return SYNCHELP_READ_N; 1084 } 1085 1086 /** 1087 * data copied in data will bee sent to another host 1088 * @param data pointer to data 1089 * @param maxLength max length of data 1090 * @return the number of bytes writen 1091 */ 1092 int PNode::readState( byte * data, int maxLength ) 1093 { 1094 SYNCHELP_WRITE_BEGIN(); 1095 1096 SYNCHELP_WRITE_FKT( BaseObject::readState ); 1097 1098 PRINTF(0)("name = %s\n", this->getName()); 1099 1100 if ( this->parent ) 1101 { 1102 SYNCHELP_WRITE_STRING( parent->getName() ); 1103 } 1104 else 1105 { 1106 SYNCHELP_WRITE_STRING( "" ); 1107 } 1108 1109 SYNCHELP_WRITE_INT( this->parentMode ); 1110 1111 SYNCHELP_WRITE_FLOAT( this->relCoordinate.x ); 1112 SYNCHELP_WRITE_FLOAT( this->relCoordinate.y ); 1113 SYNCHELP_WRITE_FLOAT( this->relCoordinate.z ); 1114 1115 PRINTF(0)("%s, %f, %f, %f\n", getClassName(), relCoordinate.x, relCoordinate.y, relCoordinate.z); 1116 1117 SYNCHELP_WRITE_FLOAT( this->absCoordinate.x ); 1118 SYNCHELP_WRITE_FLOAT( this->absCoordinate.y ); 1119 SYNCHELP_WRITE_FLOAT( this->absCoordinate.z ); 1120 1121 PRINTF(0)("%s, %f, %f, %f\n", getClassName(), absCoordinate.x, absCoordinate.y, absCoordinate.z); 1122 1123 SYNCHELP_WRITE_FLOAT( this->relDirection.w ); 1124 SYNCHELP_WRITE_FLOAT( this->relDirection.v.x ); 1125 SYNCHELP_WRITE_FLOAT( this->relDirection.v.y ); 1126 SYNCHELP_WRITE_FLOAT( this->relDirection.v.z ); 1127 1128 SYNCHELP_WRITE_FLOAT( this->absDirection.w ); 1129 SYNCHELP_WRITE_FLOAT( this->absDirection.v.x ); 1130 SYNCHELP_WRITE_FLOAT( this->absDirection.v.y ); 1131 SYNCHELP_WRITE_FLOAT( this->absDirection.v.z ); 1132 1133 int n = children.size(); 1134 SYNCHELP_WRITE_INT( n ); 1135 1136 for (std::list<PNode*>::const_iterator it = children.begin(); it!=children.end(); it++) 1137 { 1138 PRINTF(0)("childname = %s\n", (*it)->getName() ); 1139 SYNCHELP_WRITE_STRING( (*it)->getName() ); 1140 } 1141 return SYNCHELP_WRITE_N; 1142 } -
branches/network_merged/src/lib/coord/p_node.h
r6142 r6340 20 20 21 21 #include "base_object.h" 22 #include "stdincl.h" 22 23 23 24 #include "vector.h" … … 178 179 static PARENT_MODE charToParentingMode(const char* parentingMode); 179 180 181 int writeState(const byte* data, int length, int sender); 182 int readState(byte* data, int maxLength ); 180 183 181 184 private: -
branches/network_merged/src/lib/lang/base_object.cc
r6282 r6340 22 22 #include "compiler.h" 23 23 #include "class_list.h" 24 25 #include "synchronizeable.h" 24 26 25 27 using namespace std; … … 180 182 181 183 } 184 185 /** 186 * Writes data from network containing information about the state 187 * @param data pointer to data 188 * @param length length of data 189 * @param sender hostID of sender 190 */ 191 int BaseObject::writeState( const byte * data, int length, int sender ) 192 { 193 SYNCHELP_READ_BEGIN(); 194 195 if ( objectName ) 196 { 197 delete[] objectName; 198 objectName = NULL; 199 } 200 SYNCHELP_READ_STRINGM( this->objectName ); 201 202 return SYNCHELP_READ_N; 203 } 204 205 /** 206 * data copied in data will bee sent to another host 207 * @param data pointer to data 208 * @param maxLength max length of data 209 * @return the number of bytes writen 210 */ 211 int BaseObject::readState( byte * data, int maxLength ) 212 { 213 SYNCHELP_WRITE_BEGIN(); 214 215 SYNCHELP_WRITE_STRING( this->objectName ); 216 217 return SYNCHELP_WRITE_N; 218 } -
branches/network_merged/src/lib/lang/base_object.h
r6280 r6340 15 15 #define NULL 0 //!< NULL 16 16 #endif 17 18 #include "stdincl.h" 17 19 18 20 class TiXmlElement; … … 45 47 bool operator==(ClassID classID) { return this->isA(classID); }; 46 48 49 int writeState(const byte* data, int length, int sender); 50 int readState(byte* data, int maxLength ); 47 51 protected: 48 52 void setClassID(ClassID classID, const char* className); -
branches/network_merged/src/lib/network/converter.cc
r6139 r6340 54 54 { 55 55 const int mod = 256; // = 2^8 56 57 56 57 58 58 byte* result = new byte[INTSIZE]; 59 59 int sgn; … … 65 65 x = -x; 66 66 } 67 67 68 68 for (int i = 0; i < INTSIZE; i++) 69 69 { … … 71 71 x /= mod; 72 72 } 73 73 74 74 if (sgn == -1) 75 75 result[INTSIZE - 1] += sgnadd; 76 77 78 return result; 76 77 78 return result; 79 } 80 81 /*! 82 * Converts an int into a byte-array and stores the result into a given byte-array 83 * @remarks: The int is stored in big-endian 84 * @param x: The int which is to convert 85 * @return: A byte-array that accords the given int value 86 */ 87 int Converter::intToByteArray(int x, byte* a, int length) 88 { 89 if (length < INTSIZE) 90 { 91 PRINTF(1)("byte buffer to short to store an int. Needed length %i. Avaiable length %i", INTSIZE, length); 92 return -1; 93 } 94 95 const int mod = 256; // = 2^8 96 97 int sgn; 98 if (x >= 0) 99 sgn = 1; 100 else 101 { 102 sgn = -1; 103 x = -x; 104 } 105 106 for (int i = 0; i < INTSIZE; i++) 107 { 108 a[i] = x % mod; 109 x /= mod; 110 } 111 112 if (sgn == -1) 113 a[INTSIZE - 1] += sgnadd; 114 115 return INTSIZE; 79 116 } 80 117 … … 84 121 * @return: An int that accords the given byte-array 85 122 */ 86 int Converter::byteArrayToInt( byte* a)123 int Converter::byteArrayToInt(const byte* a, int* x) 87 124 { 88 125 int mult = 1; 89 126 const int step = 256; // = 2 ^ 8 90 int result= 0;127 *x = 0; 91 128 for (int i = 0; i < INTSIZE - 1; i++) 92 129 { 93 result+= a[i] * mult;130 *x += a[i] * mult; 94 131 mult *= step; 95 132 } 96 97 printf("tara: %i", result); 98 133 99 134 if (a[INTSIZE - 1] >= sgnadd) 100 135 { 101 result += (a[INTSIZE - 1] - sgnadd) * mult; 102 result *= -1; 103 } 104 else 105 result += a[INTSIZE - 1] * mult; 106 107 return result; 108 } 109 110 /*char* Converter::floatToBinString(float x) 136 *x += (a[INTSIZE - 1] - sgnadd) * mult; 137 *x *= -1; 138 } 139 else 140 *x += a[INTSIZE - 1] * mult; 141 142 return INTSIZE; 143 } 144 145 /*! 146 * Converts a float into a string containing its binary representation 147 */ 148 char* Converter::floatToBinString(float x) 111 149 { 112 150 char* result = new char[200]; 113 151 int pos = 0; 114 115 int h = (int)x; 116 if (h > x) 117 h--; 118 x -= h; 119 120 121 while (h > 0 && pos < 200) 122 { 123 //printf("%i ", pos); 124 125 if (h % 2 == 1) 126 { 127 result[pos] = '1'; 128 h -= 1; 129 } 130 else 131 result[pos] = '0'; 132 pos++; 133 h /= 2; 134 } 135 136 //printf("x = %f\n", x); 137 138 139 //invert 140 for (int i = 0; i < pos / 2; i++) 141 { 142 char temp = result[i]; 143 result[i] = result[pos - 1 - i]; 144 result[pos - 1 - i] = temp; 145 } 146 147 148 result[pos++] = '.'; 149 float sub = 0.5; 150 while (x > 0 && pos < 200) 151 { 152 //printf("%i ", pos); 153 152 153 if (x < 0) 154 { 155 result[pos++] = '-'; 156 x = -x; 157 } 158 159 float sub = 1; 160 while (sub < x) 161 sub *= 2; 162 163 while ((x > 0 || sub >= 1) && pos < 200) 164 { 154 165 if (x >= sub) 155 166 { … … 161 172 pos++; 162 173 sub /= 2; 163 } 164 165 166 return result; 167 }*/ 168 169 char* Converter::floatToBinString(float x) 170 { 171 char* result = new char[200]; 172 int pos = 0; 173 174 175 if (sub == 0.5f) 176 result[pos++] = '.'; 177 } 178 179 return result; 180 } 181 182 const int expmult = 8388608; //2^23 183 184 /*! 185 * Converts a float value into a byte-array 186 * @param x: The float which is to convert 187 * @return: A byte-array which accords the given float 188 */ 189 byte* Converter::floatToByteArray(float x) 190 { 191 int mantisse = 0; 192 int exponent = 128; 193 194 int sgn; 195 if (x < 0) 196 { 197 x = -x; 198 sgn = -1; 199 } 200 else 201 sgn = 1; 202 174 203 float sub = 1; 175 204 while (sub < x) 205 { 176 206 sub *= 2; 177 178 //printf("sub = %f\n", sub); 179 180 //while (sub >= 1 && pos < 200) 181 while ((x > 0 || sub >= 1) && pos < 200) 207 exponent++; 208 } 209 210 while (x > 0) 182 211 { 183 212 if (x >= sub) 184 213 { 185 result[pos] = '1';214 mantisse += 1; 186 215 x -= sub; 187 216 } 188 else 189 result[pos] = '0';190 pos++;217 218 mantisse *= 2; 219 exponent--; 191 220 sub /= 2; 192 193 if (sub == 0.5f) 194 result[pos++] = '.'; 195 } 196 197 /*result[pos++] = '.'; 198 sub = 0.5; 199 while (x > 0 && pos < 200) 221 } 222 exponent++; 223 mantisse /= 2; 224 while (mantisse < expmult) 225 { 226 mantisse *= 2; 227 exponent--; 228 } 229 230 //printf("mantisse = %i exponent = %i \n", mantisse, exponent); 231 232 mantisse -= expmult; 233 234 int hx = mantisse + expmult * exponent; 235 byte* result = intToByteArray(hx); 236 if (sgn == -1) 237 result[3] += sgnadd; 238 239 return result; 240 } 241 242 243 /*! 244 * Converts a byte-array into a float value 245 * @param a: The byte-array which is to convert 246 * @return: A float value which accords the given byte-array 247 */ 248 float Converter::byteArrayToFloat(byte* a) 249 { 250 int hmant = a[0] + a[1] * 256 + a[2] * 65536; 251 int mantisse = hmant % expmult; 252 mantisse += expmult; 253 254 int hexp = a[2] + a[3] * 256; 255 int exponent = (hexp / 128) % 256 - 128; 256 257 int sgn; 258 if (a[3] >= sgnadd) 259 sgn = -1; 260 else 261 sgn = 1; 262 263 //printf("mantisse = %i exponent = %i \n", mantisse, exponent); 264 265 float emult = 1; 266 if (exponent > 0) 267 for (int i = 0; i < exponent; i++) 268 emult *= 2; 269 else if (exponent < 0) 270 for (int i = 0; i > exponent; i--) 271 emult /= 2; 272 273 float result = mantisse * emult; 274 if (sgn == -1) 275 result = -result; 276 277 return result; 278 } 279 280 /*! 281 * Converts a float value into a byte-array 282 * @param x: The float which is to convert 283 * @return: A byte-array which accords the given float 284 */ 285 byte* Converter::_floatToByteArray(float x) 286 { 287 byte* p = (byte*)&x; 288 byte* result = new byte[4]; 289 for (int i = 0; i < 4; i++) 290 result[i] = p[i]; 291 return result; 292 } 293 294 295 /*! 296 * Converts a byte-array into a float value 297 * @param a: The byte-array which is to convert 298 * @return: A float value which accords the given byte-array 299 */ 300 float Converter::_byteArrayToFloat(byte* a) 301 { 302 float* p = (float*)a; 303 float result = *p; 304 return result; 305 } 306 307 /*! 308 * Converts a float value into a byte-array and stores the result into a given byte-array 309 * @param x: The float which is to convert 310 * @return: A byte-array which accords the given float 311 */ 312 int Converter::floatToByteArray(float x, byte* a, int length) 313 { 314 if (length < FLOATSIZE) 315 { 316 PRINTF(1)("byte buffer to short to store a float. Needed length %i. Avaiable length %i", FLOATSIZE, length); 317 return -1; 318 } 319 320 //handle 0 else function will loop for ever 321 if ( x == 0 ) 322 { 323 for ( int i = 0; i<FLOATSIZE; i++) 324 a[i] = 0; 325 return FLOATSIZE; 326 } 327 328 int mantisse = 0; 329 int exponent = 128; 330 331 int sgn; 332 if (x < 0) 333 { 334 x = -x; 335 sgn = -1; 336 } 337 else 338 sgn = 1; 339 340 float sub = 1; 341 while (sub < x) 342 { 343 sub *= 2; 344 exponent++; 345 } 346 347 while (x > 0) 200 348 { 201 349 if (x >= sub) 202 350 { 203 result[pos] = '1';351 mantisse += 1; 204 352 x -= sub; 205 353 } 206 else 207 result[pos] = '0';208 pos++;354 355 mantisse *= 2; 356 exponent--; 209 357 sub /= 2; 210 358 } 211 */ 212 213 return result; 214 } 359 exponent++; 360 mantisse /= 2; 361 while (mantisse < expmult) 362 { 363 mantisse *= 2; 364 exponent--; 365 } 366 367 //printf("mantisse = %i exponent = %i \n", mantisse, exponent); 368 369 mantisse -= expmult; 370 371 int hx = mantisse + expmult * exponent; 372 int result = intToByteArray(hx, a, length); 373 if (sgn == -1) 374 a[3] += sgnadd; 375 376 return result; 377 } 378 379 380 /*! 381 * Converts a byte-array into a float value 382 * @param a: The byte-array which is to convert 383 * @return: A float value which accords the given byte-array 384 */ 385 int Converter::byteArrayToFloat(const byte* a, float* x) 386 { 387 //handle 0 388 for (int i = 0; i<FLOATSIZE; i++) 389 { 390 if (a[i]!=0) 391 break; 392 if ( i==FLOATSIZE-1 ) 393 { 394 *x = 0.0f; 395 return FLOATSIZE; 396 } 397 } 398 399 400 int hmant = a[0] + a[1] * 256 + a[2] * 65536; 401 int mantisse = hmant % expmult; 402 mantisse += expmult; 403 404 int hexp = a[2] + a[3] * 256; 405 int exponent = (hexp / 128) % 256 - 128; 406 407 int sgn; 408 if (a[3] >= sgnadd) 409 sgn = -1; 410 else 411 sgn = 1; 412 413 //printf("mantisse = %i exponent = %i \n", mantisse, exponent); 414 415 float emult = 1; 416 if (exponent > 0) 417 for (int i = 0; i < exponent; i++) 418 emult *= 2; 419 else if (exponent < 0) 420 for (int i = 0; i > exponent; i--) 421 emult /= 2; 422 423 *x = mantisse * emult; 424 if (sgn == -1) 425 *x = - *x; 426 427 return FLOATSIZE; 428 } 429 430 /** 431 * copies a strint to a byte array 432 * @param s: string to copy 433 * @param a: byte array 434 * @param length: string length 435 * @return: the used number of bytes in byte array 436 */ 437 int Converter::stringToByteArray( const char * s, byte * a, int length, int maxLength ) 438 { 439 if ( length+INTSIZE > maxLength ) 440 { 441 PRINTF(1)("Byte array is too small (%d) to store %d bytes\n", maxLength, length+INTSIZE); 442 return -1; 443 } 444 445 int n = Converter::intToByteArray( length, a, maxLength ); 446 447 memcpy( a+INTSIZE, s, length ); 448 449 return length + INTSIZE; 450 } 451 452 /** 453 * reads a string out of a byte array 454 * @param a: byte array 455 * @param s: string 456 * @param maxLength: max bytes to copy 457 * @return: the number of read bytes in byte array 458 */ 459 int Converter::byteArrayToString( const byte * a, char * s, int maxLength ) 460 { 461 int length; 462 463 int n = Converter::byteArrayToInt( a, &length ); 464 465 466 if ( length+1 > maxLength ) 467 { 468 PRINTF(1)("There is not enough space in string (%d) to store %d bytes\n", maxLength, length+1 ); 469 strncpy(s,"",maxLength); 470 return -1; 471 } 472 473 memcpy( s, a+n, length ); 474 s[length] = '\0'; 475 476 return n+length; 477 } 478 479 /** 480 * reads a string out of a byte array and allocates memory for string 481 * @param a: byte array 482 * @param s: string 483 * @param maxLength: max bytes to copy 484 * @return: the number of read bytes in byte array 485 */ 486 int Converter::byteArrayToStringM( const byte * a, char*& s ) 487 { 488 int length; 489 490 int n = Converter::byteArrayToInt( a, &length ); 491 492 s = new char[length+1]; 493 494 if ( !s ) 495 { 496 PRINTF(1)("Could not allocate memory!\n" ); 497 return -1; 498 } 499 500 memcpy( s, a+n, length ); 501 s[length] = '\0'; 502 503 return n+length; 504 } 505 -
branches/network_merged/src/lib/network/converter.h
r6139 r6340 3 3 * Is able to convert int to byte-array and vice versa 4 4 */ 5 5 6 6 #ifndef _CONVERTER 7 7 #define _CONVERTER … … 15 15 /* The size of an int in byte */ 16 16 #define INTSIZE 4 17 /* The size of a float in byte */ 18 #define FLOATSIZE 4 17 19 18 20 /*! … … 23 25 public: 24 26 static byte* intToByteArray(int x); 25 static int byteArrayToInt(byte* a); 26 27 //byte* floatToByteArray(float x); 28 //float byteArrayToFloat(byte[] a); 29 27 static int byteArrayToInt(const byte* a); 28 29 static int intToByteArray(int x, byte* a, int length); 30 static int byteArrayToInt(const byte* a, int* x); 31 32 static int floatToByteArray(float x, byte* a, int length); 33 static int byteArrayToFloat(const byte* a, float* x); 34 35 static int stringToByteArray(const char* s, byte* a, int length, int maxLength); 36 static int byteArrayToString(const byte* a, char* s, int maxLength); 37 static int byteArrayToStringM(const byte* a, char*& s ); 38 30 39 //Test 31 40 static char* floatToBinString(float x); 32 41 42 static byte* floatToByteArray(float x); 43 static float byteArrayToFloat(byte* a); 44 45 static byte* _floatToByteArray(float x); 46 static float _byteArrayToFloat(byte* a); 33 47 private: 34 48 Converter(); -
branches/network_merged/src/lib/network/data_stream.h
r5822 r6340 13 13 #include "netdefs.h" 14 14 15 #define DATA_STREAM_BUFFER_SIZE 1024 15 #define DATA_STREAM_BUFFER_SIZE 10240 16 16 17 17 class DataStream : public BaseObject -
branches/network_merged/src/lib/network/handshake.cc
r6139 r6340 37 37 } 38 38 39 void Handshake::writeBytes( const byte * data, int length)39 int Handshake::writeBytes( const byte * data, int length, int sender) 40 40 { 41 41 PRINTF(5)("Handshake::writeBytes states = %d %d %d %d (%d)\n", hasState( HS_RECVD_INIT ), hasState( HS_RECVD_VER ), hasState( HS_RECVD_HID ), hasState( HS_COMPLETED ), state); 42 42 43 SYNCHELP_READ_BEGIN(); 44 43 45 if ( hasState( HS_COMPLETED ) ) 44 return ;46 return 0; 45 47 46 48 if ( !hasState( HS_RECVD_INIT ) ) … … 50 52 PRINTF(0)("initial packet has wrong size %d instead of %d\n", length, _INITIAL_DATA_LENGTH); 51 53 setState( HS_COMPLETED ); 52 return ;54 return 0; 53 55 } 54 56 … … 57 59 PRINTF(0)("initial packed does not match\n"); 58 60 setState( HS_COMPLETED ); 59 return ;61 return length; 60 62 } 61 63 62 64 setState( HS_RECVD_INIT ); 63 65 PRINTF(0)("got valid initial packet from client %d\n", clientId); 64 return ;66 return length; 65 67 } 66 68 … … 71 73 PRINTF(0)("version number packet has wrong size %d instead of %d\n", length, _ORXONOX_VERSION_LENGTH); 72 74 setState( HS_COMPLETED ); 73 return ;75 return 0; 74 76 } 75 77 … … 78 80 PRINTF(0)("versions do not match\n"); 79 81 setState( HS_COMPLETED ); 80 return ;82 return length; 81 83 } 82 84 … … 84 86 85 87 PRINTF(0)("client %d's version does match\n", clientId); 86 return ;88 return length; 87 89 } 88 90 89 91 if ( !isServer() && hasState( HS_RECVD_VER ) && !hasState( HS_RECVD_HID ) ) 90 92 { 91 if ( length != 2)93 if ( length != INTSIZE+INTSIZE ) 92 94 { 93 95 PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, 1); 94 96 setState( HS_COMPLETED ); 95 return ;97 return 0; 96 98 } 97 99 … … 99 101 setState( HS_RECVD_HID ); 100 102 this->isOk = true; 101 this->newHostId = data[0];102 this->newNetworkGameManagerId = data[1];103 SYNCHELP_READ_INT( this->newHostId ); 104 SYNCHELP_READ_INT( this->newNetworkGameManagerId ); 103 105 104 106 if ( newHostId == 0 ) … … 112 114 PRINTF(0)("got my hostID: %d and networkGameManagerId: %d\n", newHostId, newNetworkGameManagerId); 113 115 } 114 return ;116 return SYNCHELP_READ_N; 115 117 } 116 118 … … 120 122 { 121 123 PRINTF(5)("Handshake::readBytes states = %d %d %d %d (%d)\n", hasState( HS_SENT_INIT ), hasState( HS_SENT_VER ), hasState( HS_SENT_HID ), hasState( HS_COMPLETED ), state); 124 125 SYNCHELP_WRITE_BEGIN(); 122 126 123 127 if ( hasState( HS_COMPLETED ) ) … … 172 176 isOk = false; 173 177 //memcpy(data, (byte*)0, 4); 174 data[0] = 0; 178 SYNCHELP_WRITE_INT( 0 ); 179 SYNCHELP_WRITE_INT( 0 ); 175 180 } 176 181 else … … 178 183 isOk = true; 179 184 //memcpy(data, &clientId, 4); 180 data[0] = (byte)clientId;181 data[1] = (byte)networkGameManagerId;185 SYNCHELP_WRITE_INT( clientId ); 186 SYNCHELP_WRITE_INT( networkGameManagerId ); 182 187 } 183 188 *reciever = clientId; 184 return 2;189 return SYNCHELP_WRITE_N; 185 190 } 186 191 -
branches/network_merged/src/lib/network/handshake.h
r6139 r6340 42 42 inline void doReject(){ setState(HS_DO_REJECT); } 43 43 44 virtual void writeBytes(const byte* data, int length);44 virtual int writeBytes(const byte* data, int length, int sender); 45 45 virtual int readBytes(byte* data, int maxLength, int * reciever); 46 46 virtual void writeDebug() const; -
branches/network_merged/src/lib/network/network_game_manager.cc
r6139 r6340 20 20 #define DEBUG_MODULE_NETWORK 21 21 22 #include "factory.h" 23 #include "network_stream.h" 24 #include "converter.h" 25 22 26 /* include your own header */ 23 27 #include "network_game_manager.h" … … 27 31 using namespace std; 28 32 33 NetworkGameManager* NetworkGameManager::singletonRef = NULL; 34 29 35 /*! 30 36 * Standard constructor … … 32 38 NetworkGameManager::NetworkGameManager() 33 39 { 40 PRINTF(0)("START\n"); 41 34 42 /* set the class id for the base object */ 35 this->setClassID(CL_ENTITY_MANAGER, "EntityManager"); 43 this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager"); 44 45 allOutBuffer.length = 0; 46 47 allOutBuffer.maxLength = 10*1024; 48 49 allOutBuffer.buffer = new byte[10*1024]; 50 51 newUniqueID = MAX_CONNECTIONS + 2; 52 53 hasRequestedWorld = false; 36 54 } 37 55 … … 41 59 NetworkGameManager::~NetworkGameManager() 42 60 { 43 } 44 45 46 void NetworkGameManager::writeBytes(const byte* data, int length) 47 { 61 for ( int i = 0; i<outBuffer.size(); i++) 62 { 63 if ( outBuffer[i].buffer ) 64 delete outBuffer[i].buffer; 65 } 66 67 if ( allOutBuffer.buffer ) 68 delete allOutBuffer.buffer; 69 } 70 71 72 int NetworkGameManager::writeBytes(const byte* data, int length, int sender) 73 { 74 int i = 0; 75 byte b; 76 77 while ( i<length ) 78 { 79 b = data[i++]; 80 81 PRINTF(0)("WriteBytes: b = %d\n", b); 82 83 if ( isServer() ) 84 { 85 if ( b == REQUEST_CREATE ) 86 { 87 if ( !handleRequestCreate( i, data, length, sender ) ) 88 return i; 89 continue; 90 } 91 if ( b == REQUEST_REMOVE ) 92 { 93 if ( !handleRequestRemove( i, data, length, sender ) ) 94 return i; 95 continue; 96 } 97 } 98 else 99 { 100 if ( b == CREATE_ENTITY ) 101 { 102 if ( !handleCreateEntity( i, data, length, sender ) ) 103 return i; 104 continue; 105 } 106 if ( b == REMOVE_ENTITY ) 107 { 108 if ( !handleRemoveEntity( i, data, length, sender ) ) 109 return i; 110 continue; 111 } 112 if ( b == CREATE_ENTITY_LIST ) 113 { 114 if ( !handleCreateEntityList( i, data, length, sender ) ) 115 return i; 116 continue; 117 } 118 if ( b == REMOVE_ENTITY_LIST ) 119 { 120 if ( !handleRemoveEntityList( i, data, length, sender ) ) 121 return i; 122 continue; 123 } 124 if ( b == YOU_ARE_ENTITY ) 125 { 126 if ( !handleYouAreEntity( i, data, length, sender ) ) 127 return i; 128 continue; 129 } 130 } 131 132 if ( b == REQUEST_SYNC ) 133 { 134 if ( !handleRequestSync( i, data, length, sender ) ) 135 return i; 136 continue; 137 } 138 139 if ( b == REQUEST_ENTITY_LIST ) 140 { 141 PRINTF(0)("sending THE list\n"); 142 sendEntityList( sender ); 143 continue; 144 } 145 146 //if we get her something with data is wrong 147 PRINTF(1)("Data is not in the right format! i=%d\n", i); 148 return i; 149 } 150 151 return i; 48 152 } 49 153 50 154 int NetworkGameManager::readBytes(byte* data, int maxLength, int * reciever) 51 155 { 156 if ( !isServer() && !hasRequestedWorld ) 157 { 158 SYNCHELP_WRITE_BEGIN(); 159 byte b = REQUEST_ENTITY_LIST; 160 SYNCHELP_WRITE_BYTE( b ); 161 hasRequestedWorld = true; 162 PRINTF(0)("the world is enough! id=%d\n", this->getUniqueID()); 163 return SYNCHELP_WRITE_N; 164 } 165 for ( int i = 0; i<outBuffer.size(); i++ ) 166 { 167 *reciever = i; 168 if ( outBuffer[i].length>0 ) 169 { 170 int nbytes = outBuffer[i].length; 171 outBuffer[i].length = 0; 172 173 if ( nbytes > maxLength ) 174 { 175 PRINTF(1)("OutBuffer.length (%d) > (%d) networkStreamBuffer.maxLength\n", nbytes, maxLength); 176 return 0; 177 } 178 179 memcpy(data, outBuffer[i].buffer, nbytes); 180 return nbytes; 181 } 182 } 183 184 *reciever = 0; 185 int nbytes = allOutBuffer.length; 186 allOutBuffer.length = 0; 187 188 if ( nbytes <=0 ) 189 return 0; 190 191 if ( nbytes > maxLength ) 192 { 193 PRINTF(1)("OutBuffer.length (%d) > (%d) networkStreamBuffer.length\n", nbytes, maxLength); 194 return 0; 195 } 196 197 memcpy( data, allOutBuffer.buffer, nbytes ); 198 return nbytes; 52 199 } 53 200 … … 63 210 /*! 64 211 * Checks whether this is connected to a server or a client 65 * and afterwards creates the needed entity if possible212 * and afterwards creates the needed entity 66 213 * @param classID: The ID of the class of which an entity should be created 67 214 */ 68 void NetworkGameManager::createEntity(int classID) 69 { 70 } 215 void NetworkGameManager::createEntity( ClassID classID, int owner ) 216 { 217 if ( this->isServer() ) 218 { 219 if ( newUniqueID < 0 ) 220 { 221 PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n"); 222 return; 223 } 224 225 this->executeCreateEntity( classID, newUniqueID++, owner ); 226 } 227 else 228 { 229 this->requestCreateEntity( classID ); 230 } 231 } 232 233 234 /*! 235 * Checks whether this is connected to a server or a client 236 * and afterwards creates the needed entity 237 * @param classID: The ID of the class of which an entity should be created 238 */ 239 BaseObject* NetworkGameManager::createEntity( TiXmlElement* element) 240 { 241 if ( this->isServer() ) 242 { 243 if ( newUniqueID < 0 ) 244 { 245 PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n"); 246 return NULL; 247 } 248 newUniqueID++; 249 250 BaseObject * b = Factory::fabricate( element ); 251 252 if ( !b ) 253 { 254 PRINTF(1)("Could not fabricate Object with classID %x\n", element->Value() ); 255 return NULL; 256 } 257 258 259 if ( b->isA(CL_SYNCHRONIZEABLE) ) 260 { 261 Synchronizeable * s = dynamic_cast<Synchronizeable*>(b); 262 s->setUniqueID( newUniqueID ); 263 s->setOwner( 0 ); 264 this->networkStream->connectSynchronizeable( *s ); 265 return b; 266 } 267 else 268 { 269 PRINTF(1)("Class %s is not a synchronizeable!\n", b->getClassName() ); 270 delete b; 271 } 272 } 273 else 274 { 275 PRINTF(1)("This node is not a server and cannot create id %x\n", element->Value()); 276 } 277 return NULL; 278 } 279 71 280 72 281 /*! … … 77 286 void NetworkGameManager::removeEntity(int uniqueID) 78 287 { 288 if ( this->isServer() ) 289 { 290 this->executeRemoveEntity( uniqueID ); 291 } 292 else 293 { 294 this->requestRemoveEntity( uniqueID ); 295 } 79 296 } 80 297 … … 85 302 * @param classID: The ID of the class of which an entity should be created 86 303 */ 87 void NetworkGameManager::requestCreateEntity(int classID) 88 { 304 void NetworkGameManager::requestCreateEntity(ClassID classID) 305 { 306 if ( !writeToClientBuffer( allOutBuffer, (byte)REQUEST_CREATE ) ) 307 return; 308 if ( !writeToClientBuffer( allOutBuffer, (int)classID ) ) 309 return; 89 310 } 90 311 … … 95 316 void NetworkGameManager::requestRemoveEntity(int uniqueID) 96 317 { 318 if ( !writeToClientBuffer( allOutBuffer, (byte)REQUEST_REMOVE ) ) 319 return; 320 if ( !writeToClientBuffer( allOutBuffer, uniqueID ) ) 321 return; 97 322 } 98 323 … … 102 327 * @param classID: The ID of the class of which an entity should be created 103 328 */ 104 void NetworkGameManager::executeCreateEntity(int classID) 105 { 329 void NetworkGameManager::executeCreateEntity(ClassID classID, int uniqueID, int owner) 330 { 331 if ( !writeToClientBuffer( allOutBuffer, (byte)CREATE_ENTITY ) ) 332 return; 333 if ( !writeToClientBuffer( allOutBuffer, (int)classID ) ) 334 return; 335 if ( !writeToClientBuffer( allOutBuffer, uniqueID ) ) 336 return; 337 if ( !writeToClientBuffer( allOutBuffer, owner ) ) 338 return; 339 340 doCreateEntity( classID, uniqueID, owner ); 106 341 } 107 342 … … 113 348 void NetworkGameManager::executeRemoveEntity(int uniqueID) 114 349 { 350 if ( !writeToClientBuffer( allOutBuffer, (byte)REMOVE_ENTITY ) ) 351 return; 352 if ( !writeToClientBuffer( allOutBuffer, uniqueID ) ) 353 return; 354 355 doRemoveEntity(uniqueID); 115 356 } 116 357 … … 119 360 * @return: true if the entity can be created, false otherwise 120 361 */ 121 bool NetworkGameManager::canCreateEntity(int classID) 122 { 123 } 362 bool NetworkGameManager::canCreateEntity(ClassID classID) 363 { 364 return true; 365 } 366 367 /*! 368 * Sends the Entities to the new connected client 369 * @param userID: The ID of the user 370 */ 371 void NetworkGameManager::sendEntityList( int userID ) 372 { 373 if ( !isServer() ) 374 return; 375 376 if ( userID >= outBuffer.size() ) 377 resizeBufferVector( userID ); 378 379 SynchronizeableList::const_iterator it, e; 380 381 it = this->networkStream->getSyncBegin(); 382 e = this->networkStream->getSyncEnd(); 383 384 if ( !writeToClientBuffer( outBuffer[userID], (byte)CREATE_ENTITY_LIST ) ) 385 return; 386 387 // -2 because you must not send network_game_manager and handshake 388 if ( !writeToClientBuffer( outBuffer[userID], networkStream->getSyncCount() ) ) 389 return; 390 391 //PRINTF(0)("SendEntityList: n = %d\n", networkStream->getSyncCount()-2 ); 392 393 while ( it != e ) 394 { 395 396 if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getLeafClassID()) ) ) 397 return; 398 //PRINTF(0)("SendEntityList: ClassID = %x\n", (*it)->getRealClassID()); 399 400 if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getUniqueID()) ) ) 401 return; 402 403 if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getOwner()) ) ) 404 return; 405 406 it++; 407 } 408 409 410 } 411 412 /** 413 * Creates a buffer for user n 414 * @param n The ID of the user 415 */ 416 void NetworkGameManager::resizeBufferVector( int n ) 417 { 418 for ( int i = outBuffer.size(); i<=n; i++) 419 { 420 clientBuffer outBuf; 421 422 outBuf.length = 0; 423 424 outBuf.maxLength = 5*1024; 425 426 outBuf.buffer = new byte[5*1014]; 427 428 outBuffer.push_back(outBuf); 429 } 430 } 431 432 /** 433 * Creates the entity on this host 434 * @param classID: ClassID of the entity to create 435 * @param uniqueID: Unique ID to assign to the synchronizeable 436 * @param owner: owner of this synchronizealbe 437 */ 438 void NetworkGameManager::doCreateEntity( ClassID classID, int uniqueID, int owner ) 439 { 440 BaseObject * b = Factory::fabricate( classID ); 441 442 if ( !b ) 443 { 444 PRINTF(1)("Could not fabricate Object with classID %x\n", classID); 445 return; 446 } 447 448 if ( b->isA(CL_SYNCHRONIZEABLE) ) 449 { 450 Synchronizeable * s = dynamic_cast<Synchronizeable*>(b); 451 s->setUniqueID( uniqueID ); 452 s->setOwner( owner ); 453 this->networkStream->connectSynchronizeable( *s ); 454 if ( !isServer() ) 455 s->setIsOutOfSync( true ); 456 PRINTF(0)("Fabricated %s with id %d\n", s->getClassName(), s->getUniqueID()); 457 } 458 else 459 { 460 PRINTF(1)("Class with ID %x is not a synchronizeable!", (int)classID); 461 delete b; 462 } 463 } 464 465 /** 466 * Removes a entity on this host 467 * @param uniqueID: unique ID assigned with the entity to remove 468 */ 469 void NetworkGameManager::doRemoveEntity( int uniqueID ) 470 { 471 SynchronizeableList::const_iterator it,e; 472 it = this->networkStream->getSyncBegin(); 473 e = this->networkStream->getSyncEnd(); 474 475 while ( it != e ) 476 { 477 if ( (*it)->getUniqueID() == uniqueID ) 478 { 479 delete *it; 480 break; 481 } 482 it++; 483 } 484 } 485 486 /** 487 * Tell the synchronizeable that a user's synchronizeable is out of sync 488 * @param uniqueID: unique ID assigned with the entity which is out of sync 489 * @param userID: user ID who's synchronizeable is out of sync 490 */ 491 void NetworkGameManager::doRequestSync( int uniqueID, int userID ) 492 { 493 SynchronizeableList::const_iterator it,e; 494 it = this->networkStream->getSyncBegin(); 495 e = this->networkStream->getSyncEnd(); 496 497 while ( it != e ) 498 { 499 if ( (*it)->getUniqueID() == uniqueID ) 500 { 501 (*it)->requestSync( userID ); 502 break; 503 } 504 it++; 505 } 506 } 507 508 /** 509 * Copies length bytes to the clientBuffer with error checking 510 * @param clientBuffer: the clientBuffer to write to 511 * @param data: buffer to the data 512 * @param length: length of data 513 * @return false on error true else 514 */ 515 bool NetworkGameManager::writeToClientBuffer( clientBuffer &cb, byte * data, int length ) 516 { 517 if ( length > cb.maxLength-cb.length ) 518 { 519 PRINTF(1)("No space left in clientBuffer\n"); 520 return false; 521 } 522 523 memcpy( cb.buffer+cb.length, data, length ); 524 return true; 525 } 526 527 /** 528 * Reads data from clientBuffer with error checking 529 * @param clientBuffer: the clientBuffer to read from 530 * @param data: pointer to the buffer 531 * @param length: 532 * @return 533 */ 534 bool NetworkGameManager::readFromClientBuffer( clientBuffer &cb, byte * data, int length ) 535 { 536 if ( cb.length < length ) 537 { 538 PRINTF(0)("There is not enough data in clientBuffer\n"); 539 return 0; 540 } 541 542 memcpy( data, cb.buffer+cb.length-length, length ); 543 return true; 544 } 545 546 /** 547 * Tells this client that he has to control this entity 548 * @param uniqueID: the entity's uniqeID 549 */ 550 void NetworkGameManager::doYouAre( int uniqueID ) 551 { 552 //TODO: what has to be done 553 } 554 555 /** 556 * Tells a remote client that he has to control this entity 557 * @param uniqueID: the entity's uniqeID 558 * @param userID: the users ID 559 */ 560 void NetworkGameManager::sendYouAre( int uniqueID, int userID ) 561 { 562 if ( !isServer() ) 563 return; 564 565 if ( userID != 0 ) 566 { 567 if ( !writeToClientBuffer( outBuffer[userID], (byte)YOU_ARE_ENTITY ) ) 568 return; 569 570 if ( !writeToClientBuffer( outBuffer[userID], uniqueID ) ) 571 return; 572 } 573 else 574 { 575 doYouAre(uniqueID); 576 } 577 } 578 579 bool NetworkGameManager::handleRequestCreate( int & i, const byte * data, int length, int sender ) 580 { 581 if ( INTSIZE > length-i ) 582 { 583 PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); 584 return false; 585 } 586 int classID; 587 i += Converter::byteArrayToInt( &data[i], &classID ); 588 589 createEntity( (ClassID)classID ); 590 591 return true; 592 } 593 594 bool NetworkGameManager::handleRequestRemove( int & i, const byte * data, int length, int sender ) 595 { 596 if ( INTSIZE > length-i ) 597 { 598 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); 599 return false; 600 } 601 int uniqueID; 602 i += Converter::byteArrayToInt( &data[i], &uniqueID ); 603 604 removeEntity( uniqueID ); 605 606 return true; 607 } 608 609 bool NetworkGameManager::handleCreateEntity( int & i, const byte * data, int length, int sender ) 610 { 611 if ( INTSIZE > length-i ) 612 { 613 PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); 614 return false; 615 } 616 int classID; 617 i += Converter::byteArrayToInt( &data[i], &classID ); 618 619 if ( INTSIZE > length-i ) 620 { 621 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); 622 return false; 623 } 624 int uniqueID; 625 i += Converter::byteArrayToInt( &data[i], &uniqueID ); 626 627 if ( INTSIZE > length-i ) 628 { 629 PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n"); 630 return false; 631 } 632 int owner; 633 i += Converter::byteArrayToInt( &data[i], &owner ); 634 635 doCreateEntity( (ClassID)classID, uniqueID, owner ); 636 637 return true; 638 } 639 640 bool NetworkGameManager::handleRemoveEntity( int & i, const byte * data, int length, int sender ) 641 { 642 if ( INTSIZE > length-i ) 643 { 644 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); 645 return false; 646 } 647 int uniqueID; 648 i += Converter::byteArrayToInt( &data[i], &uniqueID ); 649 650 doRemoveEntity( uniqueID ); 651 652 return true; 653 } 654 655 bool NetworkGameManager::handleCreateEntityList( int & i, const byte * data, int length, int sender ) 656 { 657 if ( INTSIZE > length-i ) 658 { 659 PRINTF(1)("Cannot read n from buffer! Not enough data left!\n"); 660 return false; 661 } 662 663 PRINTF(0)("HandleCreateEntityList: data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]); 664 665 int n; 666 i += Converter::byteArrayToInt( &data[i], &n ); 667 668 669 PRINTF(0)("HandleCreateEntityList: n = %d\n", n); 670 671 int classID, uniqueID, owner; 672 673 for ( int j = 0; j<n; j++ ) 674 { 675 676 if ( INTSIZE > length-i ) 677 { 678 PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); 679 return false; 680 } 681 i += Converter::byteArrayToInt( &data[i], &classID ); 682 683 if ( INTSIZE > length-i ) 684 { 685 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); 686 return false; 687 } 688 i += Converter::byteArrayToInt( &data[i], &uniqueID ); 689 690 if ( INTSIZE > length-i ) 691 { 692 PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n"); 693 return false; 694 } 695 i += Converter::byteArrayToInt( &data[i], &owner ); 696 697 if ( classID != CL_NETWORK_GAME_MANAGER && classID != CL_HANDSHAKE ) 698 doCreateEntity( (ClassID)classID, uniqueID, owner ); 699 700 } 701 return true; 702 } 703 704 bool NetworkGameManager::handleRemoveEntityList( int & i, const byte * data, int length, int sender ) 705 { 706 if ( INTSIZE > length-i ) 707 { 708 PRINTF(1)("Cannot read n from buffer! Not enough data left!\n"); 709 return false; 710 } 711 int n; 712 i += Converter::byteArrayToInt( &data[i], &n ); 713 714 int uniqueID; 715 716 for ( int j = 0; j<n; j++ ) 717 { 718 719 if ( INTSIZE > length-i ) 720 { 721 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); 722 return false; 723 } 724 i += Converter::byteArrayToInt( &data[i], &uniqueID ); 725 726 doRemoveEntity( uniqueID ); 727 } 728 729 return true; 730 } 731 732 bool NetworkGameManager::handleYouAreEntity( int & i, const byte * data, int length, int sender ) 733 { 734 if ( INTSIZE > length-i ) 735 { 736 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); 737 return false; 738 } 739 740 int uniqueID; 741 i += Converter::byteArrayToInt( &data[i], &uniqueID ); 742 743 doYouAre( uniqueID ); 744 745 return true; 746 } 747 748 bool NetworkGameManager::handleRequestSync( int & i, const byte * data, int length, int sender ) 749 { 750 if ( INTSIZE > length-i ) 751 { 752 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); 753 return false; 754 } 755 int uniqueID; 756 i += Converter::byteArrayToInt( &data[i], &uniqueID ); 757 758 doRequestSync( uniqueID, sender ); 759 760 return true; 761 } 762 763 bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, byte b ) 764 { 765 if ( cb.maxLength-cb.length < 1 ) 766 { 767 PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 byte\n"); 768 return false; 769 } 770 771 cb.buffer[cb.length++] = b; 772 773 return true; 774 } 775 776 bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, int i ) 777 { 778 int n = Converter::intToByteArray( i, cb.buffer+cb.length, cb.maxLength-cb.length ); 779 cb.length += n; 780 781 if ( n <= 0 ) 782 { 783 PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 int\n"); 784 return false; 785 } 786 787 return true; 788 } 789 790 void NetworkGameManager::sync( int uniqueID, int owner ) 791 { 792 if ( owner==this->getHostID() ) 793 return; 794 795 if ( !isServer() ) 796 executeRequestSync( uniqueID, 0 ); 797 else 798 executeRequestSync( uniqueID, owner ); 799 } 800 801 void NetworkGameManager::executeRequestSync( int uniqueID, int user ) 802 { 803 if ( user >= outBuffer.size() ) 804 resizeBufferVector( user ); 805 806 if ( !writeToClientBuffer( outBuffer[user], (byte)REQUEST_SYNC ) ) 807 return; 808 if ( !writeToClientBuffer( outBuffer[user], uniqueID ) ) 809 return; 810 } 811 -
branches/network_merged/src/lib/network/network_game_manager.h
r6139 r6340 14 14 15 15 16 class TiXmlElement; 16 17 17 18 /** 18 19 * protocol definition 19 20 * 20 * CREATE_ENTITY: CLASS_ID, UNIQUE_ID, OWNER21 * REMOVE_ENTITY: UNIQUE_ID21 * CREATE_ENTITY: CLASS_ID, UNIQUE_ID, OWNER 22 * REMOVE_ENTITY: UNIQUE_ID 22 23 * 23 * CREATE_ENTITY_LIST: NUMBER, [CLASS_ID, UNIQUE_ID, OWNER][0..NUMBER]24 * REMOVE_ENTITY_LIST: NUMBER, [UNIQUE_ID][0..NUMBER]24 * CREATE_ENTITY_LIST: NUMBER, [CLASS_ID, UNIQUE_ID, OWNER][0..NUMBER] 25 * REMOVE_ENTITY_LIST: NUMBER, [UNIQUE_ID][0..NUMBER] 25 26 * 26 * REQUEST_CREATE: CLASS_ID27 * REQUEST_REMOVE: UNIQUE_ID27 * REQUEST_CREATE: CLASS_ID 28 * REQUEST_REMOVE: UNIQUE_ID 28 29 * 29 * REQUEST_SYNC: UNIQUE_ID30 * REQUEST_SYNC_LIST:NUMBER, [UNIQUE_ID][0..NUMBER]30 * //REQUEST_CREATE_LIST: NUMBER, [CLASS_ID][0..NUMBER] 31 * //REQUEST_CREATE_LIST: NUMBER, [UNIQUE_ID][0..NUMBER] 31 32 * 33 * REQUEST_ENTITY_LIST: //request the whole world :D 34 * REQUEST_SYNC: UNIQUE_ID 35 * //REQUEST_SYNC_LIST: NUMBER, [UNIQUE_ID][0..NUMBER] 36 * 37 * YOU_ARE_ENTITY: UNIQUE_ID 32 38 * 33 39 */ 34 40 41 typedef enum NetworkGameManagerProtocol{ 42 CREATE_ENTITY = 0, 43 REMOVE_ENTITY, 44 CREATE_ENTITY_LIST, 45 REMOVE_ENTITY_LIST, 46 REQUEST_CREATE, 47 REQUEST_REMOVE, 48 REQUEST_SYNC, 49 YOU_ARE_ENTITY, 50 REQUEST_ENTITY_LIST 51 }; 35 52 36 37 38 39 40 41 42 53 struct clientBuffer 54 { 55 int length; 56 int maxLength; 57 byte * buffer; 58 }; 43 59 44 60 /*! … … 48 64 { 49 65 public: 50 NetworkGameManager();51 66 ~NetworkGameManager(); 52 67 53 virtual void writeBytes(const byte* data, int length); 68 static NetworkGameManager* NetworkGameManager::getInstance() 69 { if (!NetworkGameManager::singletonRef) NetworkGameManager::singletonRef = new NetworkGameManager(); return NetworkGameManager::singletonRef; } 70 71 virtual int writeBytes(const byte* data, int length, int sender); 54 72 virtual int readBytes(byte* data, int maxLength, int * reciever); 55 73 virtual void writeDebug() const; 56 74 virtual void readDebug() const; 57 75 58 void createEntity( int classID);59 void createEntityList(int* classIDList);60 void removeEntity( int uniqueID);61 void removeEntityList(int* uniqueIDList);76 void createEntity( ClassID classID, int owner = 0 ); 77 BaseObject* createEntity( TiXmlElement* element); 78 void removeEntity( int uniqueID ); 79 void sendYouAre( int uniqueID, int userID ); 62 80 63 void sync(int uniqueID); 64 void syncList(int* uniqueIDList); 81 void sync(int uniqueID, int owner); 65 82 83 void sendEntityList(int userID); 66 84 67 85 private: 68 void requestCreateEntity(int classID);69 void executeCreateEntity(int classID); 70 void requestCreateEntity List(int* classIDList);71 void executeCreateEntity List(int* classIDList);86 NetworkGameManager(); 87 88 void requestCreateEntity(ClassID classID); 89 void executeCreateEntity(ClassID classID, int uniqueID = 0, int owner = 0); 72 90 73 91 void requestRemoveEntity(int uniqueID); 74 92 void executeRemoveEntity(int uniqueID); 75 void requestRemoveEntityList(int* uniqueIDList);76 void executeRemoveEntityList(int* uniqueIDList);77 93 94 void executeRequestSync( int uniqueID, int user ); 78 95 96 void doCreateEntity(ClassID classID, int uniqueID, int owner); 97 void doRemoveEntity(int uniqueID); 98 void doRequestSync(int uniqueID, int userID); 99 void doYouAre( int uniqueID ); 79 100 80 bool canCreateEntity( intclassID);101 bool canCreateEntity(ClassID classID); 81 102 103 void resizeBufferVector(int n); 82 104 105 inline bool writeToClientBuffer( clientBuffer &cb, byte*data, int length ); 106 inline bool writeToClientBuffer( clientBuffer &cb, byte b ); 107 inline bool writeToClientBuffer( clientBuffer &cb, int i ); 108 inline bool readFromClientBuffer( clientBuffer &cb, byte*data, int length ); 109 110 //helper functions for writeBytes 111 inline bool handleRequestCreate( int& i, const byte* data, int length, int sender ); 112 inline bool handleRequestRemove( int& i, const byte* data, int length, int sender ); 113 inline bool handleCreateEntity( int& i, const byte* data, int length, int sender ); 114 inline bool handleRemoveEntity( int& i, const byte* data, int length, int sender ); 115 inline bool handleCreateEntityList( int& i, const byte* data, int length, int sender ); 116 inline bool handleRemoveEntityList( int& i, const byte* data, int length, int sender ); 117 inline bool handleYouAreEntity( int& i, const byte* data, int length, int sender ); 118 inline bool handleRequestSync( int& i, const byte* data, int length, int sender ); 83 119 84 120 private: 85 byte* inBuffer; 86 byte* outBuffer; 121 std::vector<clientBuffer> outBuffer; 122 clientBuffer allOutBuffer; 123 static NetworkGameManager* singletonRef; 124 125 int newUniqueID; 126 bool hasRequestedWorld; 87 127 }; 88 128 -
branches/network_merged/src/lib/network/network_manager.cc
r6139 r6340 44 44 /* set the class id for the base object */ 45 45 this->setClassID(CL_NETWORK_MANAGER, "NetworkManager"); 46 PRINTF(0)("START\n"); 46 47 47 48 /* initialize the references */ … … 49 50 this->syncList = NULL; 50 51 this->tmpStream = NULL; 52 51 53 this->hostID = -1; 52 54 this->bGameServer = false; … … 107 109 int NetworkManager::createServer(unsigned int port) 108 110 { 111 this->hostID = 0; 112 this->bGameServer = true; 109 113 this->tmpStream = new NetworkStream(port); 110 114 this->bGameServer = true; … … 124 128 this->tmpStream = new NetworkStream(address); 125 129 this->tmpStream->connectSynchronizeable(sync); 126 }127 128 129 /**130 * creates a new NetworkStream of server type131 * @param sync: the listener132 */133 NetworkStream& NetworkManager::createServer(Synchronizeable& sync, unsigned int port)134 {135 PRINTF(0)("Create a new server socket\n");136 /* creating a new network stream, it will register itself automaticaly to the class list */137 this->tmpStream = new NetworkStream(port);138 this->tmpStream->connectSynchronizeable(sync);139 this->bGameServer = true;140 130 } 141 131 … … 178 168 void NetworkManager::setHostID(int id) 179 169 { 180 this->hostID = id;170 this->hostID = id; 181 171 } -
branches/network_merged/src/lib/network/network_manager.h
r6139 r6340 39 39 40 40 NetworkStream& establishConnection(IPaddress& address, Synchronizeable& sync); 41 NetworkStream& createServer(Synchronizeable& sync, unsigned int port);42 41 void shutdownConnection(); 43 42 -
branches/network_merged/src/lib/network/network_protocol.cc
r6139 r6340 30 30 #include "debug.h" 31 31 32 #include "converter.h" 33 32 34 /* using namespace std is default, this needs to be here */ 33 35 using namespace std; … … 41 43 /* set the class id for the base object */ 42 44 this->setClassID(CL_NETWORK_PROTOCOL, "NetworkProtocol"); 43 this->headerLength = sizeof(Header);45 this->headerLength = INTSIZE+FLOATSIZE; 44 46 } 45 47 … … 64 66 PRINTF(5)("create header length = %i, bufferLength = %i\n", length, bufferLength); 65 67 //If there isn't enough space for the header return -1 66 if (length + this->headerLength> bufferLength)68 if (length + 2*INTSIZE > bufferLength) 67 69 return -1; 68 70 … … 70 72 // FIXME: without move Create space for the header 71 73 for( int i = length - 1; i >= 0; i--) 72 data[i + this->headerLength] = data[i];74 data[i + INTSIZE+INTSIZE] = data[i]; 73 75 74 76 //Now create the header 75 77 /* sender ID: FIXME: there will be a better ID (for example unique:D)*/ 76 data[0] = (byte)(source.getUniqueID()); 78 //data[0] = (byte)(source.getUniqueID()); 79 int res = Converter::intToByteArray( source.getUniqueID(), data, bufferLength ); 80 77 81 /* data length*/ 78 data[1] = length; 82 //data[1] = length; 83 res += Converter::intToByteArray( length, data+res, bufferLength-res ); 79 84 80 85 81 return length + this->headerLength;86 return length + res; 82 87 } 83 88 … … 92 97 PRINTF(5)("extract Header\n"); 93 98 //Test if received data can contain a header 94 if (length < headerLength)99 if (length < 2*INTSIZE) 95 100 { 96 101 PRINTF(1)("Received data is to short; it can't contain a header!\n"); … … 105 110 106 111 /* unique ID */ 107 h.synchronizeableID = data[0]; 112 //h.synchronizeableID = data[0]; 113 int res = Converter::byteArrayToInt( data, &(h.synchronizeableID) ); 108 114 /* data length*/ 109 h.length = data[1]; 115 //h.length = data[1]; 116 Converter::byteArrayToInt( data+res, &(h.length) ); 110 117 111 118 -
branches/network_merged/src/lib/network/network_protocol.h
r6139 r6340 14 14 typedef struct Header 15 15 { 16 bytesynchronizeableID;17 bytelength;16 int synchronizeableID; 17 int length; 18 18 }; 19 19 -
branches/network_merged/src/lib/network/network_socket.cc
r6139 r6340 21 21 #define DEBUG_MODULE_NETWORK 22 22 23 #include "converter.h" 23 24 24 25 /* include your own header */ … … 398 399 { 399 400 PRINTF(5)("NetworkSocket::writePacket()\n"); 400 if (length>255) 401 { 402 PRINTF(1)("Packet length > 255!\n"); 403 return false; 404 } 405 406 byte blen = length; 407 408 writeBytes(&blen, 1); 401 402 byte blen[INTSIZE]; 403 404 Converter::intToByteArray( length, blen, INTSIZE ); 405 406 writeBytes(blen, INTSIZE); 409 407 writeBytes(data, length); 410 408 } … … 413 411 { 414 412 PRINTF(5)("NetworkSocket::readPacket()\n"); 415 if (incomingBufferLength<1) 416 { 417 return 0; 418 } 419 420 byte blen = incomingBuffer[0]; 413 if (incomingBufferLength<INTSIZE) 414 { 415 return 0; 416 } 417 418 int blen; 419 Converter::byteArrayToInt( incomingBuffer, &blen ); 421 420 422 421 if (blen>maxLength) … … 431 430 } 432 431 433 readBytes(&blen, 1); 432 byte t[INTSIZE]; 433 readBytes(t, INTSIZE); 434 434 int res = readBytes(data, blen); 435 435 -
branches/network_merged/src/lib/network/network_stream.cc
r6144 r6340 27 27 #include "synchronizeable.h" 28 28 #include "network_manager.h" 29 #include "network_game_manager.h" 30 29 31 #include "debug.h" 30 32 #include "class_list.h" … … 64 66 this->connectSynchronizeable(*hs); 65 67 PRINTF(0)("NetworkStream: %s\n", hs->getName()); 68 this->maxConnections = 1; 66 69 } 67 70 … … 77 80 this->handshakes.push_back( NULL ); 78 81 this->bActive = true; 82 this->networkGameManager = NetworkGameManager::getInstance(); 83 // setUniqueID( maxCon+2 ) because we need one id for every handshake 84 // and one for handshake to reject client maxCon+1 85 this->networkGameManager->setUniqueID( this->maxConnections+2 ); 86 this->connectSynchronizeable( *(this->networkGameManager) ); 79 87 80 88 this->setMaxConnections( 10 ); … … 88 96 this->bActive = false; 89 97 this->serverSocket = NULL; 98 this->networkGameManager = NULL; 90 99 myHostId = 0; 91 100 } … … 176 185 NetworkManager::getInstance()->setHostID( handshakes[i]->getHostId() ); 177 186 myHostId = NetworkManager::getInstance()->getHostID(); 178 } 179 PRINT(0)("handshake finished\n"); 187 188 this->networkGameManager = NetworkGameManager::getInstance(); 189 this->networkGameManager->setUniqueID( handshakes[i]->getNetworkGameManagerId() ); 190 this->connectSynchronizeable( *(this->networkGameManager) ); 191 } 192 else 193 { 194 //this->networkGameManager->sendEntityList( i ); 195 } 196 PRINT(0)("handshake finished id=%d\n", handshakes[i]->getNetworkGameManagerId()); 197 198 180 199 delete handshakes[i]; 181 200 handshakes[i] = NULL; 182 //TODO: replace handshake by entitymanager183 201 } 184 202 else … … 202 220 for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) 203 221 { 204 //TODO: remove items from synchronizeables if they dont exist 205 if ( (*it)!=NULL && (*it)->getOwner() == myHostId ) 222 if ( (*it)!=NULL /*&& (*it)->getOwner() == myHostId*/ ) 206 223 { 207 224 do { … … 217 234 dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it))); 218 235 219 //FIXME: this is a hack, find a better way220 236 Header* header = (Header*)downBuffer; 221 if ( header->synchronizeableID<100 ) 237 if ( header->synchronizeableID < this->maxConnections+2 ) 238 { 239 //if ( !isServer() ) PRINTF(0)("RESET UNIQUEID FROM %d TO 0 maxCon=%d\n", header->synchronizeableID, this->maxConnections); 222 240 header->synchronizeableID = 0; 241 } 242 else 243 { 244 //if ( !isServer() ) PRINTF(0)("UNIQUEID=%d\n", header->synchronizeableID); 245 } 223 246 224 247 if ( dataLength<=0 ) … … 229 252 if ( networkSockets[reciever] != NULL ) 230 253 { 231 PRINTF( 5)("write %d bytes to socket %d\n", dataLength, reciever);254 PRINTF(0)("write %d bytes to socket %d\n", dataLength, reciever); 232 255 networkSockets[reciever]->writePacket(downBuffer, dataLength); 233 256 } … … 243 266 if ( networkSockets[i] != NULL ) 244 267 { 245 PRINTF( 5)("write %d bytes to socket %d\n", dataLength, reciever);268 PRINTF(0)("write %d bytes to socket %d\n", dataLength, reciever); 246 269 networkSockets[i]->writePacket(downBuffer, dataLength); 247 270 } … … 251 274 } while( reciever!=0 ); 252 275 } 253 else254 {255 PRINTF(0)("synchronizeables == NULL");256 }257 276 } 258 277 … … 269 288 continue; 270 289 271 PRINTF(5)("read %d bytes from socket\n", dataLength);272 290 header = networkProtocol->extractHeader(upBuffer, dataLength); 273 291 dataLength -= sizeof(header); 274 292 293 PRINTF(0)("read %d bytes from socket uniqueID = %d\n", dataLength, header.synchronizeableID); 294 275 295 if ( dataLength != header.length ) 276 296 { … … 287 307 { 288 308 if ( *it && (*it)->getUniqueID()==header.synchronizeableID ) 289 (*it)->writeBytes(upBuffer+sizeof(header), dataLength); 309 { 310 if ( (*it)->writeBytes(upBuffer+sizeof(header), dataLength, i) != header.length ) 311 { 312 PRINTF(1)("%s did not read all the data!\n", (*it)->getClassName()); 313 break; 314 } 315 continue; 316 } 290 317 } 291 318 … … 309 336 freeSocketSlots.pop_back(); 310 337 networkSockets[clientId] = tempNetworkSocket; 311 handshakes[clientId] = new Handshake(true, clientId );338 handshakes[clientId] = new Handshake(true, clientId, this->networkGameManager->getUniqueID()); 312 339 handshakes[clientId]->setUniqueID(clientId); 313 340 } else … … 315 342 clientId = networkSockets.size(); 316 343 networkSockets.push_back(tempNetworkSocket); 317 Handshake* tHs = new Handshake(true, clientId );344 Handshake* tHs = new Handshake(true, clientId, this->networkGameManager->getUniqueID()); 318 345 tHs->setUniqueID(clientId); 319 346 handshakes.push_back(tHs); … … 376 403 return; 377 404 } 405 406 if ( n > MAX_CONNECTIONS ) 407 { 408 PRINTF(1)("Cannot set maxConnectiosn to %d because of hardcoded limit %d\n", n, MAX_CONNECTIONS); 409 return; 410 } 411 378 412 this->maxConnections = n; 379 } 380 381 413 this->networkGameManager->setUniqueID( n+2 ); 414 } 415 416 -
branches/network_merged/src/lib/network/network_stream.h
r6139 r6340 15 15 #include "handshake.h" 16 16 17 #define MAX_CONNECTIONS 1000 18 17 19 class Synchronizeable; 18 20 class NetworkSocket; … … 20 22 class ConnectionMonitor; 21 23 class NetworkProtocol; 24 class NetworkGameManager; 22 25 23 26 typedef std::list<Synchronizeable*> SynchronizeableList; … … 48 51 virtual void processData(); 49 52 53 inline SynchronizeableList::const_iterator getSyncBegin(){ return synchronizeables.begin(); } 54 inline SynchronizeableList::const_iterator getSyncEnd(){ return synchronizeables.end(); } 55 inline int getSyncCount(){ return synchronizeables.size(); } 56 50 57 private: 51 58 NetworkProtocol* networkProtocol; … … 63 70 int maxConnections; 64 71 72 NetworkGameManager* networkGameManager; 73 65 74 void updateConnectionList(); 66 75 }; -
branches/network_merged/src/lib/network/synchronizeable.cc
r6145 r6340 28 28 Synchronizeable::Synchronizeable() 29 29 { 30 30 this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable"); 31 31 owner = 0; 32 state = 0; 32 33 hostID = NetworkManager::getInstance()->getHostID(); 34 this->setIsServer(this->hostID == 0); 33 35 uniqueID = -1; 34 36 this->networkStream = NULL; 35 //state = ?; 36 37 this->setRequestedSync( false ); 37 38 } 38 39 39 /**40 * default constructor41 */42 Synchronizeable::Synchronizeable(const char* name)43 {44 this->setName(name);45 this->networkStream = NULL;46 }47 40 48 41 … … 59 52 * write data to NetworkStream 60 53 */ 61 void Synchronizeable::writeBytes(const byte* data, int length)54 int Synchronizeable::writeBytes(const byte* data, int length, int sender) 62 55 { 63 PRINTF( 1)("Synchronizeable::writeBytes was called\n");56 PRINTF(5)("Synchronizeable::writeBytes was called\n"); 64 57 } 65 58 … … 69 62 int Synchronizeable::readBytes(byte* data, int maxLength, int * reciever) 70 63 { 71 PRINTF( 1)("Synchronizeable::readBytes was called\n");64 PRINTF(5)("Synchronizeable::readBytes was called\n"); 72 65 } 73 66 … … 103 96 else 104 97 this->state = this->state & (~STATE_OUTOFSYNC); 98 //PRINTF(0)("isoutofsync %s %d\n", this->getClassName(), state); 105 99 } 106 100 … … 111 105 bool Synchronizeable::isServer() 112 106 { 113 return this->state & STATE_SERVER == STATE_SERVER;107 return (this->state & STATE_SERVER) >0; 114 108 } 115 109 … … 120 114 bool Synchronizeable::isOutOfSync() 121 115 { 122 return this->state & STATE_OUTOFSYNC == STATE_OUTOFSYNC; 116 return (this->state & STATE_OUTOFSYNC) >0; 117 } 118 119 /** 120 * Determines if the requestedSync flag is set 121 * @return true, if the requestedSync flag is true, false else 122 */ 123 bool Synchronizeable::requestedSync() 124 { 125 return (this->state & STATE_REQUESTEDSYNC) >0; 126 } 127 128 /** 129 * Sets the requestedsync flag to a given value 130 * @param requestedSync: the boolean value which the requestedsync flag is to set to 131 */ 132 void Synchronizeable::setRequestedSync( bool requestedSync ) 133 { 134 if( requestedSync ) 135 this->state = this->state | STATE_REQUESTEDSYNC; 136 else 137 this->state = this->state & (~STATE_REQUESTEDSYNC); 123 138 } 124 139 -
branches/network_merged/src/lib/network/synchronizeable.h
r6139 r6340 9 9 #include "base_object.h" 10 10 #include "netdefs.h" 11 #include "converter.h" 11 12 12 13 … … 18 19 #define STATE_SERVER 1 19 20 #define STATE_OUTOFSYNC 2 21 #define STATE_REQUESTEDSYNC 4 22 23 24 //macros to help writing data in byte buffer 25 /* 26 * Important: these macros must be used in 27 * SYNCHELP_READ_*: virtual void writeBytes(const byte* data, int length, int sender); 28 * SYNCHELP_WRITE_*: virtual int readBytes(byte* data, int maxLength, int * reciever); 29 * with the same argument names! 30 * 31 * SYNCHELP_WRITE_BEGIN() 32 * SYNCHELP_WRITE_INT(i) 33 * SYNCHELP_WRITE_FLOAT(f) 34 * SYNCHELP_WRITE_BYTE(b) 35 * SYNCHELP_WRITE_STRING(s) 36 * SYNCHELP_WRITE_N 37 * 38 * SYNCHELP_READ_BEGIN() 39 * SYNCHELP_READ_INT(i) 40 * SYNCHELP_READ_FLOAT(f) 41 * SYNCHELP_READ_STRING(s,l) l = size of buffer s 42 * SYNCHELP_READ_STRINGM(s) allocates memory for string! you have to free this after 43 * SYNCHELP_READ_BYTE(b) 44 * SYNCHELP_READ_N 45 * 46 * 47 * 48 * Example 1: 49 * SYNCHELP_READ_BEGIN(); 50 * SYNCHELP_READ_FLOAT(size); 51 * SYNCHELP_READ_STRING( textureName, 1024 ); //1024 is the length of textureName 52 * 53 * Example 2: 54 * SYNCHELP_WRITE_BEGIN(); 55 * SYNCHELP_WRITE_FLOAT(this->size); 56 * SYNCHELP_WRITE_STRING(this->textureName); 57 * return SYNCHELP_WRITE_N; 58 * 59 */ 60 #define SYNCHELP_WRITE_BEGIN() int __synchelp_write_i = 0; \ 61 int __synchelp_write_n 62 #define SYNCHELP_WRITE_INT(i) { __synchelp_write_n = \ 63 Converter::intToByteArray( i, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \ 64 if ( __synchelp_write_n <= 0) \ 65 { \ 66 PRINTF(1)("Buffer is too small to store a int\n"); \ 67 return 0; \ 68 } \ 69 __synchelp_write_i += __synchelp_write_n; \ 70 } 71 #define SYNCHELP_WRITE_FLOAT(f) { __synchelp_write_n = \ 72 Converter::floatToByteArray( f, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \ 73 if ( __synchelp_write_n <= 0) \ 74 { \ 75 PRINTF(1)("Buffer is too small to store a float\n"); \ 76 return 0; \ 77 } \ 78 __synchelp_write_i += __synchelp_write_n; \ 79 } 80 #define SYNCHELP_WRITE_BYTE(b) { \ 81 if (maxLength - __synchelp_write_i < 1) \ 82 { \ 83 PRINTF(1)("Buffer is too small to store string\n"); \ 84 return 0; \ 85 } \ 86 data[__synchelp_write_i] = b; \ 87 __synchelp_write_i++; \ 88 } 89 #define SYNCHELP_WRITE_STRING(s) { if (s!=NULL) \ 90 __synchelp_write_n = \ 91 Converter::stringToByteArray( s, data+__synchelp_write_i, strlen(s), maxLength-__synchelp_write_i ); \ 92 else \ 93 __synchelp_write_n = \ 94 Converter::stringToByteArray( "", data+__synchelp_write_i, strlen(""), maxLength-__synchelp_write_i ); \ 95 if ( __synchelp_write_n <= 0) \ 96 { \ 97 PRINTF(1)("Buffer is too small to store string\n"); \ 98 return 0; \ 99 } \ 100 __synchelp_write_i += __synchelp_write_n; \ 101 } 102 #define SYNCHELP_WRITE_N __synchelp_write_i 103 #define SYNCHELP_WRITE_FKT(f) { \ 104 __synchelp_write_i += \ 105 f( data+__synchelp_write_i, maxLength-__synchelp_write_i ); \ 106 } 107 108 109 #define SYNCHELP_READ_BEGIN() int __synchelp_read_i = 0; \ 110 int __synchelp_read_n 111 112 #define SYNCHELP_READ_INT(i) { \ 113 if ( length-__synchelp_read_i < INTSIZE ) \ 114 { \ 115 PRINTF(1)("There is not enough data to read an int\n"); \ 116 return 0; \ 117 } \ 118 __synchelp_read_i += Converter::byteArrayToInt( data+__synchelp_read_i, &i ); \ 119 } 120 #define SYNCHELP_READ_FLOAT(f) { \ 121 if ( length-__synchelp_read_i < FLOATSIZE ) \ 122 { \ 123 PRINTF(1)("There is not enough data to read a flaot\n"); \ 124 return 0; \ 125 } \ 126 __synchelp_read_i += Converter::byteArrayToFloat( data+__synchelp_read_i, &f ); \ 127 } 128 #define SYNCHELP_READ_STRING(s,l) { \ 129 __synchelp_read_n = Converter::byteArrayToString( data+__synchelp_read_i, s, l ); \ 130 if ( __synchelp_read_n <0 ) \ 131 { \ 132 PRINTF(1)("There is not enough data to read string\n"); \ 133 return 0; \ 134 } \ 135 __synchelp_read_i += __synchelp_read_n; \ 136 } 137 #define SYNCHELP_READ_STRINGM(s) { \ 138 __synchelp_read_n = Converter::byteArrayToStringM( data+__synchelp_read_i, s ); \ 139 if ( __synchelp_read_n <0 ) \ 140 { \ 141 PRINTF(1)("There is not enough data to read string\n"); \ 142 return 0; \ 143 } \ 144 __synchelp_read_i += __synchelp_read_n; \ 145 } 146 #define SYNCHELP_READ_BYTE(b) { \ 147 if ( length-__synchelp_read_i < 1 ) \ 148 { \ 149 PRINTF(1)("There is not enough data to read a byte\n"); \ 150 return 0; \ 151 } \ 152 b = data[__synchelp_read_i]; \ 153 __synchelp_read_i ++; \ 154 } 155 #define SYNCHELP_READ_FKT(f) { \ 156 __synchelp_read_i += \ 157 f( data+__synchelp_read_i, length-__synchelp_read_i, sender); \ 158 } 159 #define SYNCHELP_READ_N __synchelp_read_i 20 160 21 161 class NetworkStream; … … 25 165 { 26 166 public: 27 28 Synchronizeable(const char* name);29 167 Synchronizeable(); 30 168 ~Synchronizeable(); 31 169 32 virtual void writeBytes(const byte* data, int length);170 virtual int writeBytes(const byte* data, int length, int sender); 33 171 virtual int readBytes(byte* data, int maxLength, int * reciever); 34 172 virtual void writeDebug() const; … … 37 175 void setIsServer( bool isServer ); 38 176 void setIsOutOfSync( bool outOfSync ); 177 void setRequestedSync( bool requestedSync ); 39 178 bool isServer(); 40 179 bool isOutOfSync(); 41 void setUniqueID( int id ){ uniqueID = id; } 42 int getUniqueID() const { return uniqueID; }; 43 void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); } 180 bool requestedSync(); 181 inline void setUniqueID( int id ){ uniqueID = id; } 182 inline int getUniqueID() const { return uniqueID; }; 183 inline void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); } 184 inline int getRequestSync( void ){ if ( this->synchronizeRequests.size()>0 ){ int n = *(synchronizeRequests.begin()); synchronizeRequests.pop_front(); return n; } else { return -1; } }; 185 inline int getHostID() { return this->hostID; } 44 186 45 187 inline int getOwner(){ return owner; } … … 57 199 int owner; 58 200 int hostID; 201 202 std::list<int> synchronizeRequests; 203 204 protected: 205 NetworkStream* networkStream; 59 206 int state; 60 std::list<int> synchronizeRequests;61 62 NetworkStream* networkStream;63 207 64 208 }; -
branches/network_merged/src/story_entities/network_world.cc
r6222 r6340 69 69 #include "playable.h" 70 70 #include "network_manager.h" 71 #include "network_game_manager.h" 71 72 #include "playable.h" 72 73 … … 167 168 { 168 169 this->setClassID(CL_WORLD, "NetworkWorld"); 170 PRINTF(0)("START\n"); 169 171 170 172 this->setName(name); … … 337 339 // find WorldEntities // 338 340 //////////////////////// 339 if( NetworkManager::getInstance()->isGameServer())340 {}341 342 341 element = root->FirstChildElement("WorldEntities"); 343 342 if( element == NULL) … … 352 351 while( element != NULL) 353 352 { 354 if( NetworkManager::getInstance()->isGameServer() || !strcmp( element->Value(), "SkyBox") || !strcmp( element->Value(), "Terrain") 355 || !strcmp( element->Value(), "SpaceShip")) 353 if( NetworkManager::getInstance()->isGameServer()) 354 { 355 356 BaseObject* created = NetworkGameManager::getInstance()->createEntity(element); 357 if( created != NULL ) 358 { 359 if(created->isA(CL_WORLD_ENTITY)) 360 this->spawn(dynamic_cast<WorldEntity*>(created)); 361 printf("Created a %s: %s\n", created->getClassName(), created->getName()); 362 } 363 else 364 PRINTF(1)("NetworkWorld: could not create this entity\n"); 365 366 // if we load a 'Player' we use it as localPlayer 367 368 369 //todo do this more elegant 370 if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox")) 371 sky = dynamic_cast<SkyBox*>(created); 372 if( element->Value() != NULL && !strcmp( element->Value(), "Terrain")) 373 { 374 terrain = dynamic_cast<Terrain*>(created); 375 CDEngine::getInstance()->setTerrain(terrain); 376 } 377 378 } 379 else if( /* !strcmp( element->Value(), "SkyBox") || */ /* !strcmp( element->Value(), "Terrain") || */ !strcmp( element->Value(), "SpaceShip")) 356 380 { 357 381 BaseObject* created = Factory::fabricate(element); … … 362 386 printf("Created a %s: %s\n", created->getClassName(), created->getName()); 363 387 } 388 else 389 PRINTF(1)("NetworkWorld: could not create this entity\n"); 364 390 365 391 // if we load a 'Player' we use it as localPlayer … … 374 400 CDEngine::getInstance()->setTerrain(terrain); 375 401 } 376 377 402 } 378 403 element = element->NextSiblingElement(); … … 443 468 // this->spawn(testEntity); 444 469 445 for(int i = 0; i < 100; i++)446 {447 WorldEntity* tmp = new NPCTest1();448 char npcChar[10];449 sprintf (npcChar, "NPC_%d", i);450 tmp->setName(npcChar);451 tmp->setAbsCoor(((float)rand()/RAND_MAX) * 5000, 50/*+ (float)rand()/RAND_MAX*20*/, ((float)rand()/RAND_MAX -.5) *30);452 this->spawn(tmp);453 }470 // for(int i = 0; i < 100; i++) 471 // { 472 // WorldEntity* tmp = NetworkGameManager::; 473 // char npcChar[10]; 474 // sprintf (npcChar, "NPC_%d", i); 475 // tmp->setName(npcChar); 476 // tmp->setAbsCoor(((float)rand()/RAND_MAX) * 5000, 50/*+ (float)rand()/RAND_MAX*20*/, ((float)rand()/RAND_MAX -.5) *30); 477 // this->spawn(tmp); 478 // } 454 479 455 480 this->music = NULL; … … 865 890 // this->entities->add (entity); 866 891 entity->postSpawn (); 892 867 893 } 868 894 -
branches/network_merged/src/subprojects/network/Makefile.am
r6139 r6340 22 22 $(MAINSRCDIR)/lib/lang/class_list.cc \ 23 23 $(MAINSRCDIR)/util/loading/load_param.cc \ 24 $(MAINSRCDIR)/util/loading/factory.cc \ 24 25 $(MAINSRCDIR)/lib/util/substring.cc \ 25 26 $(MAINSRCDIR)/util/loading/load_param_description.cc \ -
branches/network_merged/src/subprojects/network/network_unit_test.cc
r6139 r6340 284 284 } 285 285 286 286 void testFloatConverter(float f) 287 { 288 char* s = Converter::floatToBinString(f); 289 printf("%f = ", f); 290 printf(s); printf("\n"); 291 292 byte* res = Converter::floatToByteArray(f); 293 printf("Byte Array: "); 294 for (int i = 0; i < 4; i++) 295 printf("%i ", res[i]); 296 printf("\n"); 297 298 float b = Converter::byteArrayToFloat(res); 299 printf("ReConvert: %f \n", b); 300 } 301 302 void testFloatConverter2(float f) 303 { 304 char* s = Converter::floatToBinString(f); 305 printf("### %f = ", f); 306 printf(s); printf("\n"); 307 308 byte* res = Converter::_floatToByteArray(f); 309 printf("Byte Array: "); 310 for (int i = 0; i < 4; i++) 311 printf("%i ", res[i]); 312 printf("\n"); 313 314 float b = Converter::_byteArrayToFloat(res); 315 printf("ReConvert: %f \n", b); 316 } 287 317 int converter(int argc, char** argv) 288 318 { 319 /* 289 320 int x = 200564786; 290 321 printf("To convert: %i\n", x); … … 305 336 printf("\n"); 306 337 338 */ 339 /* 307 340 float y; 308 341 char* s; … … 328 361 printf(s); printf("\n"); 329 362 330 363 y = -4.7824f; 364 s = Converter::floatToBinString(y); 365 printf("%f = ", y); 366 printf(s); printf("\n"); 367 368 y = -14.35e14f; 369 s = Converter::floatToBinString(y); 370 printf("%f = ", y); 371 printf(s); printf("\n"); 372 */ 373 374 375 /* 376 float a = 12.3f; 377 378 char* s = Converter::floatToBinString(a); 379 printf("%f = ", a); 380 printf(s); printf("\n"); 381 382 byte* res = Converter::floatToByteArray(a); 383 printf("Byte Array: \n"); 384 for (int i = 0; i < 4; i++) 385 printf("%i ", res[i]); 386 printf("\n"); 387 388 float b = Converter::byteArrayToFloat(res); 389 printf("ReConvert: %f \n", b); 390 */ 391 // testFloatConverter(12.3e-53f); printf("\n"); 392 // testFloatConverter(134.26455646546548741661675165f); printf("\n"); 393 // testFloatConverter(35.67e14f); printf("\n"); 394 395 testFloatConverter(12.3e-7f); printf("\n"); 396 testFloatConverter(134.26455646546548741661675165f); printf("\n"); 397 testFloatConverter(35.67e14f); printf("\n"); 331 398 332 399 return 0; -
branches/network_merged/src/subprojects/network/read_sync.cc
r6139 r6340 60 60 * write data to Synchronizeable 61 61 */ 62 void ReadSync::writeBytes(const byte* data, int length)62 int ReadSync::writeBytes(const byte* data, int length, int sender) 63 63 { 64 64 PRINTF(0)("ReadSync: got %i bytes of data\n", length); -
branches/network_merged/src/subprojects/network/read_sync.h
r6139 r6340 16 16 ~ReadSync(); 17 17 18 virtual void writeBytes(const byte* data, int length);18 virtual int writeBytes(const byte* data, int length, int sender); 19 19 virtual int readBytes(byte* data, int maxLength, int * reciever); 20 20 -
branches/network_merged/src/subprojects/network/simple_sync.cc
r6139 r6340 66 66 * write data to Synchronizeable 67 67 */ 68 void SimpleSync::writeBytes(const byte* data, int length)68 int SimpleSync::writeBytes(const byte* data, int length, int sender) 69 69 { 70 70 PRINTF(0)("SimpleSync: got %i bytes of data\n", length); -
branches/network_merged/src/subprojects/network/simple_sync.h
r6139 r6340 16 16 ~SimpleSync(); 17 17 18 virtual void writeBytes(const byte* data, int length);18 virtual int writeBytes(const byte* data, int length, int sender); 19 19 virtual int readBytes(byte* data, int maxLength, int * reciever); 20 20 -
branches/network_merged/src/subprojects/network/write_sync.cc
r6139 r6340 66 66 * write data to Synchronizeable 67 67 */ 68 void WriteSync::writeBytes(const byte* data, int length)68 int WriteSync::writeBytes(const byte* data, int length, int sender) 69 69 { 70 70 PRINTF(0)("WriteSync: got %i bytes of data\n", length); -
branches/network_merged/src/subprojects/network/write_sync.h
r6139 r6340 16 16 ~WriteSync(); 17 17 18 virtual void writeBytes(const byte* data, int length);18 virtual int writeBytes(const byte* data, int length, int sender); 19 19 virtual int readBytes(byte* data, int maxLength, int * reciever); 20 20 -
branches/network_merged/src/util/loading/factory.cc
r5984 r6340 155 155 } 156 156 157 157 158 /** 158 159 * Creates a new Object of type classID -
branches/network_merged/src/util/loading/factory.h
r5984 r6340 29 29 #include "debug.h" 30 30 #include <vector> 31 #include <list> 31 32 32 33 /** -
branches/network_merged/src/world_entities/npcs/npc.cc
r6222 r6340 62 62 this->collider = entity; 63 63 } 64 else if (entity->isA(CL_PLAYER))65 this->applyForce(Vector(0,0,0)-location*100);64 // else if (entity->isA(CL_PLAYER)) 65 // this->applyForce(Vector(0,0,0)-location*100); 66 66 else if (entity->isA(CL_NPC)) 67 67 { -
branches/network_merged/src/world_entities/npcs/npc.h
r6004 r6340 8 8 class AI; 9 9 10 class NPC : public WorldEntity , public PhysicsInterface{10 class NPC : public WorldEntity { 11 11 12 12 public: -
branches/network_merged/src/world_entities/skybox.cc
r6307 r6340 22 22 #include "static_model.h" 23 23 #include "material.h" 24 #include "network_game_manager.h" 25 #include "converter.h" 24 26 25 27 using namespace std; … … 77 79 } 78 80 this->setParentMode(PNODE_MOVEMENT); 81 82 this->textureName = NULL; 79 83 } 80 84 … … 208 212 this->setModel(model); 209 213 } 214 215 int SkyBox::writeBytes( const byte * data, int length, int sender ) 216 { 217 setRequestedSync( false ); 218 setIsOutOfSync( false ); 219 220 SYNCHELP_READ_BEGIN(); 221 222 SYNCHELP_READ_FKT( WorldEntity::writeState ); 223 224 SYNCHELP_READ_FLOAT( size ); 225 if ( textureName ) 226 { 227 delete[] textureName; 228 textureName = NULL; 229 } 230 SYNCHELP_READ_STRINGM( textureName ); 231 232 PRINT(0)("GOT DATA: size=%f texture='%s'\n", size, textureName); 233 234 this->setSize( size ); 235 this->setTextureAndType( textureName, "jpg" ); 236 this->rebuild(); 237 238 return SYNCHELP_READ_N; 239 } 240 241 242 243 int SkyBox::readBytes( byte * data, int maxLength, int * reciever ) 244 { 245 if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() ) 246 { 247 (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); 248 setRequestedSync( true ); 249 } 250 251 int rec = this->getRequestSync(); 252 if ( rec > 0 ) 253 { 254 PRINT(0)("SEND DATA: size=%f texture='%s'\n", size, textureName); 255 *reciever = rec; 256 257 SYNCHELP_WRITE_BEGIN(); 258 259 SYNCHELP_WRITE_FKT( WorldEntity::readState ); 260 261 SYNCHELP_WRITE_FLOAT(this->size); 262 SYNCHELP_WRITE_STRING(this->textureName); 263 264 return SYNCHELP_WRITE_N; 265 } 266 267 *reciever = 0; 268 return 0; 269 } 270 271 void SkyBox::writeDebug( ) const 272 { 273 } 274 275 void SkyBox::readDebug( ) const 276 { 277 } -
branches/network_merged/src/world_entities/skybox.h
r6307 r6340 35 35 void setSize(float size); 36 36 /** assumes jpg as input-format */ 37 void setTexture(const char* name) { this->setTextureAndType (name, "jpg"); };37 void setTexture(const char* name) { if (textureName) delete[] textureName; textureName = new char[strlen(name)+1]; strcpy(textureName, name); this->setTextureAndType (name, "jpg"); }; 38 38 39 39 void setTextureAndType(const char* name, const char* extension); 40 40 void setTextures(const char* top, const char* bottom, const char* left, 41 41 const char* right, const char* front, const char* back); 42 43 virtual int writeBytes(const byte* data, int length, int sender); 44 virtual int readBytes(byte* data, int maxLength, int * reciever); 45 virtual void writeDebug() const; 46 virtual void readDebug() const; 42 47 43 48 private: … … 46 51 Material* material[6]; //!< Materials for the SkyBox. sorted by number (0-5) top, bottom, left, right, front, back 47 52 float size; //!< Size of the SkyBox. This should match the frustum maximum range. 53 char* textureName; //!< Name of the Texture 48 54 49 55 }; -
branches/network_merged/src/world_entities/space_ships/space_ship.cc
r6306 r6340 121 121 PRINTF(4)("SPACESHIP INIT\n"); 122 122 123 EventHandler::getInstance()->grabEvents(true);123 // EventHandler::getInstance()->grabEvents(true); 124 124 125 125 bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false; -
branches/network_merged/src/world_entities/terrain.cc
r6142 r6340 24 24 #include "resource_manager.h" 25 25 #include "model.h" 26 #include "network_game_manager.h" 27 26 28 27 29 #include "glincl.h" … … 111 113 void Terrain::loadVegetation(const char* vegetationFile) 112 114 { 115 PRINTF(0)("loadVegetation: %s\n", vegetationFile); 113 116 if (this->vegetation) 114 117 ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL); … … 116 119 { 117 120 PRINTF(4)("fetching %s\n", vegetationFile); 118 121 this->vegetation = (Model*)ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN); 119 122 } 120 123 else … … 315 318 } 316 319 } 320 321 int Terrain::writeBytes( const byte * data, int length, int sender ) 322 { 323 setRequestedSync( false ); 324 setIsOutOfSync( false ); 325 326 SYNCHELP_READ_BEGIN(); 327 SYNCHELP_READ_FKT( WorldEntity::writeState ); 328 329 return SYNCHELP_READ_N; 330 } 331 332 int Terrain::readBytes( byte * data, int maxLength, int * reciever ) 333 { 334 if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() ) 335 { 336 (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); 337 setRequestedSync( true ); 338 } 339 340 int rec = this->getRequestSync(); 341 if ( rec > 0 ) 342 { 343 *reciever = rec; 344 345 return WorldEntity::readState( data, maxLength ); 346 347 } 348 349 *reciever = 0; 350 return 0; 351 } 352 353 void Terrain::writeDebug( ) const 354 { 355 } 356 357 void Terrain::readDebug( ) const 358 { 359 } -
branches/network_merged/src/world_entities/terrain.h
r5500 r6340 30 30 virtual ~Terrain(); 31 31 32 virtual int writeBytes(const byte* data, int length, int sender); 33 virtual int readBytes(byte* data, int maxLength, int * reciever); 34 virtual void writeDebug() const; 35 virtual void readDebug() const; 36 32 37 void init(); 33 38 void loadParams(const TiXmlElement* root); … … 44 49 Model* vegetation; 45 50 int objectList; 51 46 52 }; 47 53 -
branches/network_merged/src/world_entities/weapons/ground_turret.cc
r6142 r6340 19 19 20 20 #include "factory.h" 21 #include "network_game_manager.h" 21 22 #include "load_param.h" 22 23 … … 168 169 169 170 } 171 172 /** 173 * Writes data from network containing information about the state 174 * @param data pointer to data 175 * @param length length of data 176 * @param sender hostID of sender 177 */ 178 int GroundTurret::writeBytes( const byte * data, int length, int sender ) 179 { 180 setRequestedSync( false ); 181 setIsOutOfSync( false ); 182 183 SYNCHELP_READ_BEGIN(); 184 185 SYNCHELP_READ_FKT( WorldEntity::writeState ); 186 187 return SYNCHELP_READ_N; 188 } 189 190 /** 191 * data copied in data will bee sent to another host 192 * @param data pointer to data 193 * @param maxLength max length of data 194 * @return the number of bytes writen 195 */ 196 int GroundTurret::readBytes( byte * data, int maxLength, int * reciever ) 197 { 198 SYNCHELP_WRITE_BEGIN(); 199 200 if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() ) 201 { 202 (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); 203 setRequestedSync( true ); 204 } 205 206 int rec = this->getRequestSync(); 207 if ( rec > 0 ) 208 { 209 *reciever = rec; 210 211 SYNCHELP_WRITE_FKT( WorldEntity::readState ); 212 213 } 214 215 *reciever = 0; 216 return SYNCHELP_WRITE_N; 217 } -
branches/network_merged/src/world_entities/weapons/ground_turret.h
r5819 r6340 30 30 virtual void collidesWith (WorldEntity* entity, const Vector& location); 31 31 32 virtual int writeBytes(const byte* data, int length, int sender); 33 virtual int readBytes(byte* data, int maxLength, int * reciever); 34 32 35 private: 33 36 Weapon *left, *right; -
branches/network_merged/src/world_entities/world_entity.cc
r6281 r6340 105 105 void WorldEntity::loadModel(const char* fileName, float scaling, unsigned int modelNumber) 106 106 { 107 if ( fileName != NULL)107 if ( fileName != NULL && strcmp(fileName, "") ) 108 108 { 109 109 // search for the special character # in the LoadParam … … 144 144 } 145 145 else 146 { 146 147 this->setModel(NULL); 148 } 147 149 } 148 150 … … 340 342 glPopMatrix(); 341 343 } 344 345 /** 346 * Writes data from network containing information about the state 347 * @param data pointer to data 348 * @param length length of data 349 * @param sender hostID of sender 350 */ 351 int WorldEntity::writeState( const byte * data, int length, int sender ) 352 { 353 char* modelFileName; 354 SYNCHELP_READ_BEGIN(); 355 356 SYNCHELP_READ_FKT( PNode::writeState ); 357 358 SYNCHELP_READ_STRINGM( modelFileName ); 359 SYNCHELP_READ_FLOAT( scaling ); 360 //check if modelFileName is relative to datadir or absolute 361 if ( strstr(modelFileName, ResourceManager::getInstance()->getDataDir()) ) 362 { 363 loadModel( modelFileName+strlen(ResourceManager::getInstance()->getDataDir()), scaling ); 364 } 365 else 366 { 367 loadModel( modelFileName, scaling ); 368 } 369 delete[] modelFileName; 370 371 return SYNCHELP_READ_N; 372 } 373 374 /** 375 * data copied in data will bee sent to another host 376 * @param data pointer to data 377 * @param maxLength max length of data 378 * @return the number of bytes writen 379 */ 380 int WorldEntity::readState( byte * data, int maxLength ) 381 { 382 SYNCHELP_WRITE_BEGIN(); 383 384 SYNCHELP_WRITE_FKT( PNode::readState ); 385 386 SYNCHELP_WRITE_STRING( getModel( 0 )->getName() ); 387 SYNCHELP_WRITE_FLOAT( scaling ); 388 return SYNCHELP_WRITE_N; 389 } -
branches/network_merged/src/world_entities/world_entity.h
r6281 r6340 74 74 std::list<WorldEntity*>::iterator& getEntityIterator() { return this->objectListIterator; } 75 75 76 int writeState(const byte* data, int length, int sender); 77 int readState(byte* data, int maxLength ); 76 78 77 79 protected: 78 80 // CharacterAttributes* charAttr; //!< the character attributes of a world_entity 81 79 82 80 83 private: … … 89 92 std::list<WorldEntity*>::iterator objectListIterator; //!< The iterator position of this Entity in the given list of the ObjectManager. 90 93 94 float scaling; 95 96 91 97 }; 92 98
Note: See TracChangeset
for help on using the changeset viewer.