Changeset 6753 in orxonox.OLD for trunk/src/lib/network
- Timestamp:
- Jan 26, 2006, 1:08:23 AM (19 years ago)
- Location:
- trunk/src/lib/network
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/network/converter.cc
r6737 r6753 200 200 201 201 /*! 202 * Converts a float value into a byte-array203 * @param x: The float which is to convert204 * @return: A byte-array which accords the given float205 */206 byte* Converter::floatToByteArray(float x)207 {208 byte* result = new byte[4];209 floatToByteArray(x, result, 4);210 return result;211 /*212 int mantisse = 0;213 int exponent = 128;214 215 int sgn;216 if (x < 0)217 {218 x = -x;219 sgn = -1;220 }221 else222 sgn = 1;223 224 if (x == 0)225 {226 exponent = 255;227 mantisse = 0;228 }229 else230 {231 //if (x < getDenormConst())232 // printf("Denormalisiert!\n");233 //printf("DenormConst = %e", getDenormConst());234 235 float sub = 1;236 while (sub < x)237 {238 sub *= 2;239 exponent++;240 }241 242 while (x > 0)243 {244 if (x >= sub)245 {246 mantisse += 1;247 x -= sub;248 }249 250 mantisse *= 2;251 exponent--;252 sub /= 2;253 }254 exponent++;255 mantisse /= 2;256 257 printf("Conv: mantisse = %i exponent = %i \n", mantisse, exponent);258 259 260 if (mantisse != 0)261 {262 while (mantisse < expmult)263 {264 mantisse *= 2;265 exponent--;266 }267 268 mantisse -= expmult;269 }270 }271 272 printf("Conv: mantisse = %i exponent = %i \n", mantisse, exponent);273 274 275 int hx = mantisse + expmult * exponent;276 byte* result = intToByteArray(hx);277 if (sgn == -1)278 result[3] += sgnadd;279 280 return result;281 */282 }283 284 285 /*!286 * Converts a byte-array into a float value287 * @param a: The byte-array which is to convert288 * @return: A float value which accords the given byte-array289 */290 float Converter::byteArrayToFloat(byte* a)291 {292 byte* h = new byte[4];293 float result = 0.0f;294 byteArrayToFloat(a, &result);295 return result;296 /*297 int hexp = a[2] + a[3] * 256;298 int exponent = (hexp / 128) % 256;299 300 int hmant = a[0] + a[1] * 256 + a[2] * 65536;301 int mantisse = hmant % expmult;302 if (mantisse == 0 && exponent == 255)303 return 0;304 305 mantisse += expmult;306 exponent -= 128;307 308 309 int sgn;310 if (a[3] >= sgnadd)311 sgn = -1;312 else313 sgn = 1;314 315 printf("ReConv: mantisse = %i exponent = %i \n", mantisse, exponent);316 317 float emult = 1;318 if (exponent > 0)319 for (int i = 0; i < exponent; i++)320 emult *= 2;321 else if (exponent < 0)322 for (int i = 0; i > exponent; i--)323 emult /= 2;324 325 float result = mantisse * emult;326 if (sgn == -1)327 result = -result;328 329 return result;330 */331 }332 333 /*!334 202 * Converts a float value into a byte-array and stores the result into a given byte-array 335 203 * @param x: The float which is to convert … … 338 206 * @return: The number of written bytes 339 207 */ 340 int Converter:: floatToByteArray(float x, byte* a, int length)208 int Converter::_floatToByteArray(float x, byte* a, int length) 341 209 { 342 210 if (length < FLOATSIZE) … … 452 320 * @return: The number of read bytes 453 321 */ 454 int Converter:: byteArrayToFloat(const byte* a, float* x)322 int Converter::_byteArrayToFloat(const byte* a, float* x) 455 323 { 456 324 //handle 0 … … 527 395 * @return: The number of written bytes 528 396 */ 529 int Converter:: _floatToByteArray(float x, byte* a, int length)397 int Converter::floatToByteArray(float x, byte* a, int length) 530 398 { 531 399 if ( length< FLOATSIZE ) … … 538 406 for (int i = 0; i < 4; i++) 539 407 a[i] = p[i]; 540 408 541 409 return FLOATSIZE; 542 410 } … … 549 417 * @return: The number of read bytes 550 418 */ 551 int Converter:: _byteArrayToFloat(const byte* a, float* x)419 int Converter::byteArrayToFloat(const byte* a, float* x) 552 420 { 553 421 *x = *((float*)a); … … 651 519 printf("To Convert: %e\n", x); 652 520 653 byte* res = floatToByteArray(x);654 for (int i = 0; i < 4; i++)655 printf("%i ", res[i]);656 printf("\n");657 658 float y = byteArrayToFloat(res);659 printf("ReConvert: %e\n", y);660 661 if (x == y)662 printf("equal\n");663 else664 printf("different\n");521 // byte* res = floatToByteArray(x); 522 // for (int i = 0; i < 4; i++) 523 // printf("%i ", res[i]); 524 // printf("\n"); 525 526 // float y = byteArrayToFloat(res); 527 // printf("ReConvert: %e\n", y); 528 529 // if (x == y) 530 // printf("equal\n"); 531 // else 532 // printf("different\n"); 665 533 } 666 534 … … 702 570 //floatTest(-0.0f); 703 571 //floatTest(-5.67e-29f); 704 705 572 573 706 574 //floatTest(-45.7e-32f); 707 575 //floatTest(45.7e-33f); -
trunk/src/lib/network/converter.h
r6737 r6753 31 31 static int byteArrayToInt(const byte* a, int* x); 32 32 33 static int floatToByteArray(float x, byte* a, int length);34 static int byteArrayToFloat(const byte* a, float* x);35 36 33 static int stringToByteArray(const char* s, byte* a, int length, int maxLength); 37 34 static int byteArrayToString(const byte* a, char* s, int maxLength); … … 41 38 static char* floatToBinString(float x); 42 39 43 static byte* floatToByteArray(float x); 44 static float byteArrayToFloat(byte* a); 40 // static byte* floatToByteArray(float x); 41 // static float byteArrayToFloat(byte* a); 42 43 static int floatToByteArray(float x, byte* a, int length); 44 static int byteArrayToFloat(const byte* a, float* x); 45 45 46 46 static int _floatToByteArray(float x, byte* a, int length); 47 47 static int _byteArrayToFloat(const byte* a, float* x); 48 48 49 49 50 -
trunk/src/lib/network/handshake.cc
r6695 r6753 21 21 22 22 #include "handshake.h" 23 24 #include <cassert> 23 25 24 26 Handshake::Handshake( bool server, int clientId, int networkGameManagerId ) -
trunk/src/lib/network/synchronizeable.cc
r6695 r6753 23 23 #include "state.h" 24 24 25 #include "assert.h"25 #include <cassert> 26 26 27 27 #include "synchronizeable.h" … … 35 35 { 36 36 this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable"); 37 this->owner = 0;37 this->owner = -1; 38 38 this->state = 0; 39 39 this->hostID = SharedNetworkData::getInstance()->getHostID(); -
trunk/src/lib/network/synchronizeable.h
r6695 r6753 62 62 #define SYNCHELP_WRITE_INT(i) { __synchelp_write_n = \ 63 63 Converter::intToByteArray( i, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \ 64 assert( __synchelp_write_n == INTSIZE ); \ 64 65 if ( __synchelp_write_n <= 0) \ 65 66 { \ … … 71 72 #define SYNCHELP_WRITE_FLOAT(f) { __synchelp_write_n = \ 72 73 Converter::floatToByteArray( f, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \ 74 assert( __synchelp_write_n == FLOATSIZE ); \ 73 75 if ( __synchelp_write_n <= 0) \ 74 76 { \ … … 87 89 __synchelp_write_i++; \ 88 90 } 89 #define SYNCHELP_WRITE_STRING(s) { if (s!=NULL) \91 #define SYNCHELP_WRITE_STRING(s) { if (s!=NULL) {\ 90 92 __synchelp_write_n = \ 91 93 Converter::stringToByteArray( s, data+__synchelp_write_i, strlen(s), maxLength-__synchelp_write_i ); \ 92 else \ 94 assert( __synchelp_write_n == strlen(s)+INTSIZE ); \ 95 } else {\ 93 96 __synchelp_write_n = \ 94 97 Converter::stringToByteArray( "", data+__synchelp_write_i, strlen(""), maxLength-__synchelp_write_i ); \ 98 assert( __synchelp_write_n == strlen("")+INTSIZE ); } \ 95 99 if ( __synchelp_write_n <= 0) \ 96 100 { \ … … 116 120 return 0; \ 117 121 } \ 118 __synchelp_read_i += Converter::byteArrayToInt( data+__synchelp_read_i, &i ); \ 122 __synchelp_read_n = Converter::byteArrayToInt( data+__synchelp_read_i, &i ); \ 123 assert( __synchelp_read_n == INTSIZE ); \ 124 __synchelp_read_i += __synchelp_read_n; \ 119 125 } 120 126 #define SYNCHELP_READ_FLOAT(f) { \ … … 124 130 return 0; \ 125 131 } \ 126 __synchelp_read_i += Converter::byteArrayToFloat( data+__synchelp_read_i, &f ); \ 132 __synchelp_read_n = Converter::byteArrayToFloat( data+__synchelp_read_i, &f ); \ 133 assert( __synchelp_read_n == FLOATSIZE ) ;\ 134 __synchelp_read_i += __synchelp_read_n; \ 127 135 } 128 136 #define SYNCHELP_READ_STRING(s,l) { \ 129 137 __synchelp_read_n = Converter::byteArrayToString( data+__synchelp_read_i, s, l ); \ 138 assert( __synchelp_read_n == strlen(s)+INTSIZE ) ;\ 130 139 if ( __synchelp_read_n <0 ) \ 131 140 { \ … … 137 146 #define SYNCHELP_READ_STRINGM(s) { \ 138 147 __synchelp_read_n = Converter::byteArrayToStringM( data+__synchelp_read_i, s ); \ 148 assert( __synchelp_read_n == strlen(s)+INTSIZE ) ;\ 139 149 if ( __synchelp_read_n <0 ) \ 140 150 { \
Note: See TracChangeset
for help on using the changeset viewer.