Changeset 7230 in orxonox.OLD for trunk/src/lib/network
- Timestamp:
- Mar 21, 2006, 3:20:36 PM (19 years ago)
- Location:
- trunk/src/lib/network
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/network/converter.cc
r6959 r7230 159 159 if ( length< INTSIZE ) 160 160 { 161 PRINTF(1)("Byte Array to small \n");161 PRINTF(1)("Byte Array to small : %d\n", length); 162 162 return 0; 163 163 } … … 457 457 * @return: the used number of bytes in byte array 458 458 */ 459 int Converter::stringToByteArray( const char * s, byte * a, int length, int maxLength )460 { 461 if ( length+INTSIZE > maxLength )462 { 463 PRINTF(1)("Byte array is too small (%d) to store %d bytes\n", maxLength, length+INTSIZE);459 int Converter::stringToByteArray( const std::string & s, byte * a, int maxLength ) 460 { 461 if ( s.length()+INTSIZE > maxLength ) 462 { 463 PRINTF(1)("Byte array is too small (%d) to store %d bytes\n", maxLength, s.length()+INTSIZE); 464 464 return -1; 465 465 } 466 466 467 int n = Converter::intToByteArray( length, a, maxLength );468 469 memcpy( a+INTSIZE, s , length);470 471 return length+ INTSIZE;467 int n = Converter::intToByteArray( s.length(), a, maxLength ); 468 469 memcpy( a+INTSIZE, s.c_str(), s.length() ); 470 471 return s.length() + INTSIZE; 472 472 } 473 473 … … 479 479 * @return: the number of read bytes in byte array 480 480 */ 481 int Converter::byteArrayToString( const byte * a, char *s, int maxLength )481 int Converter::byteArrayToString( const byte * a, std::string&s, int maxLength ) 482 482 { 483 483 int length; … … 488 488 if ( length+1 > maxLength ) 489 489 { 490 PRINTF(1)(" There is not enough space in string (%d) to store %d bytes\n", maxLength, length+1);491 s trncpy(s,"",maxLength);490 PRINTF(1)("something went wrong length > remaining bytes in buffer\n" ); 491 s = ""; 492 492 return -1; 493 493 } 494 494 495 memcpy( s, a+n, length );496 s [length] = '\0';495 s[0] = '\0'; 496 s.append( (char*)a+n, length ); 497 497 498 498 return n+length; 499 499 } 500 500 501 #if 0 501 502 /** 502 503 * reads a string out of a byte array and allocates memory for string … … 525 526 return n+length; 526 527 } 527 528 #endif 528 529 529 530 -
trunk/src/lib/network/converter.h
r6981 r7230 34 34 static int _byteArrayToInt(const byte* a, int* x); 35 35 36 static int stringToByteArray(const char* s, byte* a, int length, int maxLength);37 static int byteArrayToString(const byte* a, char*s, int maxLength);38 static int byteArrayToStringM(const byte* a, char*& s );36 static int stringToByteArray(const std::string & s, byte* a, int maxLength); 37 static int byteArrayToString(const byte* a, std::string&s, int maxLength); 38 // static int byteArrayToStringM(const byte* a, char*& s ); 39 39 40 40 //Test -
trunk/src/lib/network/synchronizeable.h
r6959 r7230 199 199 } 200 200 #define SYNCHELP_WRITE_STRING(s,n) { SYNCHELP_WRITE_DEBUG(n); \ 201 if (s!=NULL) {\202 201 __synchelp_write_n = \ 203 Converter::stringToByteArray( s, data+__synchelp_write_i, strlen(s), maxLength-__synchelp_write_i ); \ 204 assert( __synchelp_write_n == strlen(s)+INTSIZE ); \ 205 } else {\ 206 __synchelp_write_n = \ 207 Converter::stringToByteArray( "", data+__synchelp_write_i, strlen(""), maxLength-__synchelp_write_i ); \ 208 assert( __synchelp_write_n == strlen("")+INTSIZE ); } \ 202 Converter::stringToByteArray( s, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \ 203 assert( __synchelp_write_n == ((std::string)s).length()+INTSIZE ); \ 209 204 if ( __synchelp_write_n <= 0) \ 210 205 { \ … … 216 211 #define SYNCHELP_WRITE_N __synchelp_write_i 217 212 #define SYNCHELP_WRITE_FKT(f,n) { SYNCHELP_WRITE_DEBUG(n); \ 218 __synchelp_write_i += \ 213 PRINTF(0)("calling %s with %d left\n", #f, maxLength - __synchelp_write_i); \ 214 byte * spos = data+__synchelp_write_i; \ 215 if (maxLength - __synchelp_write_i < INTSIZE) \ 216 { \ 217 PRINTF(1)("Buffer is too small to store more data\n"); \ 218 return 0; \ 219 } \ 220 __synchelp_write_i += INTSIZE; \ 221 __synchelp_write_n = \ 219 222 f( data+__synchelp_write_i, maxLength-__synchelp_write_i ); \ 223 __synchelp_write_i += __synchelp_write_n; \ 224 Converter::intToByteArray( __synchelp_write_n, spos, INTSIZE ); \ 220 225 } 221 226 … … 244 249 __synchelp_read_i += __synchelp_read_n; \ 245 250 } 246 #define SYNCHELP_READ_STRING(s, l,n) {SYNCHELP_READ_DEBUG(n); \247 __synchelp_read_n = Converter::byteArrayToString( data+__synchelp_read_i, s, l ); \248 assert( __synchelp_read_n == s trlen(s)+INTSIZE ) ;\251 #define SYNCHELP_READ_STRING(s,n) {SYNCHELP_READ_DEBUG(n); \ 252 __synchelp_read_n = Converter::byteArrayToString( data+__synchelp_read_i, s, length-__synchelp_read_i ); \ 253 assert( __synchelp_read_n == s.length()+INTSIZE ) ;\ 249 254 if ( __synchelp_read_n <0 ) \ 250 255 { \ … … 254 259 __synchelp_read_i += __synchelp_read_n; \ 255 260 } 261 #if 0 //not needed any more 256 262 #define SYNCHELP_READ_STRINGM(s,n) { SYNCHELP_READ_DEBUG(n); \ 257 263 __synchelp_read_n = Converter::byteArrayToStringM( data+__synchelp_read_i, s ); \ … … 264 270 __synchelp_read_i += __synchelp_read_n; \ 265 271 } 272 #endif 266 273 #define SYNCHELP_READ_BYTE(b,n) { SYNCHELP_READ_DEBUG(n); \ 267 274 if ( length-__synchelp_read_i < 1 ) \ … … 274 281 } 275 282 #define SYNCHELP_READ_FKT(f,n) { SYNCHELP_READ_DEBUG(n); \ 283 int s; \ 284 if ( length-__synchelp_read_i < INTSIZE ) \ 285 { \ 286 PRINTF(1)("There is not enough data to read an int\n"); \ 287 return 0; \ 288 } \ 289 __synchelp_read_n = Converter::byteArrayToInt( data+__synchelp_read_i, &s ); \ 290 assert( __synchelp_read_n == INTSIZE ); \ 291 __synchelp_read_i += __synchelp_read_n; \ 276 292 __synchelp_read_i += \ 277 f( data+__synchelp_read_i, length-__synchelp_read_i, sender); \293 f( data+__synchelp_read_i, s, sender); \ 278 294 } 279 295 #define SYNCHELP_READ_REMAINING() ( length-__synchelp_read_i )
Note: See TracChangeset
for help on using the changeset viewer.