Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7230 in orxonox.OLD for trunk/src/lib/network


Ignore:
Timestamp:
Mar 21, 2006, 3:20:36 PM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the std::branche back

Location:
trunk/src/lib/network
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/network/converter.cc

    r6959 r7230  
    159159  if ( length< INTSIZE )
    160160  {
    161     PRINTF(1)("Byte Array to small\n");
     161    PRINTF(1)("Byte Array to small : %d\n", length);
    162162    return 0;
    163163  }
     
    457457 * @return: the used number of bytes in byte array
    458458 */
    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);
     459int 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);
    464464    return -1;
    465465  }
    466466
    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;
    472472}
    473473
     
    479479 * @return: the number of read bytes in byte array
    480480 */
    481 int Converter::byteArrayToString( const byte * a, char * s, int maxLength )
     481int Converter::byteArrayToString( const byte * a, std::string&s, int maxLength )
    482482{
    483483  int length;
     
    488488  if ( length+1 > maxLength )
    489489  {
    490     PRINTF(1)("There is not enough space in string (%d) to store %d bytes\n", maxLength, length+1 );
    491     strncpy(s,"",maxLength);
     490    PRINTF(1)("something went wrong length > remaining bytes in buffer\n" );
     491    s = "";
    492492    return -1;
    493493  }
    494494
    495   memcpy( s, a+n, length );
    496   s[length] = '\0';
     495  s[0] = '\0';
     496  s.append( (char*)a+n, length );
    497497
    498498  return n+length;
    499499}
    500500
     501#if 0
    501502/**
    502503 * reads a string out of a byte array and allocates memory for string
     
    525526  return n+length;
    526527}
    527 
     528#endif
    528529
    529530
  • trunk/src/lib/network/converter.h

    r6981 r7230  
    3434    static int _byteArrayToInt(const byte* a, int* x);
    3535
    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 );
    3939
    4040    //Test
  • trunk/src/lib/network/synchronizeable.h

    r6959 r7230  
    199199}
    200200#define SYNCHELP_WRITE_STRING(s,n) { SYNCHELP_WRITE_DEBUG(n); \
    201                                 if (s!=NULL) {\
    202201                                __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 ); \
    209204                                if ( __synchelp_write_n <= 0) \
    210205{ \
     
    216211#define SYNCHELP_WRITE_N        __synchelp_write_i
    217212#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 = \
    219222                                  f( data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
     223                                  __synchelp_write_i += __synchelp_write_n;  \
     224                                  Converter::intToByteArray( __synchelp_write_n, spos, INTSIZE ); \
    220225                                }
    221226
     
    244249                                    __synchelp_read_i += __synchelp_read_n; \
    245250}
    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 == strlen(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 ) ;\
    249254                                    if ( __synchelp_read_n <0 )  \
    250255{ \
     
    254259                                    __synchelp_read_i += __synchelp_read_n; \
    255260}
     261#if 0 //not needed any more
    256262#define SYNCHELP_READ_STRINGM(s,n)    { SYNCHELP_READ_DEBUG(n); \
    257263                                    __synchelp_read_n = Converter::byteArrayToStringM( data+__synchelp_read_i, s );  \
     
    264270                                    __synchelp_read_i += __synchelp_read_n; \
    265271}
     272#endif
    266273#define SYNCHELP_READ_BYTE(b,n)      { SYNCHELP_READ_DEBUG(n); \
    267274                                    if ( length-__synchelp_read_i < 1 ) \
     
    274281}
    275282#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; \
    276292                                  __synchelp_read_i += \
    277                                   f( data+__synchelp_read_i, length-__synchelp_read_i, sender); \
     293                                  f( data+__synchelp_read_i, s, sender); \
    278294                                  }
    279295#define SYNCHELP_READ_REMAINING() ( length-__synchelp_read_i )
Note: See TracChangeset for help on using the changeset viewer.