Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6219 in orxonox.OLD


Ignore:
Timestamp:
Dec 21, 2005, 1:39:20 PM (18 years ago)
Author:
rennerc
Message:

network_game_manager: implemented some functions

Location:
branches/network/src/lib/network
Files:
4 edited

Legend:

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

    r6197 r6219  
    5454{
    5555  const int mod = 256; // = 2^8
    56  
    57  
     56
     57
    5858  byte* result = new byte[INTSIZE];
    5959  int sgn;
     
    6565    x = -x;
    6666  }
    67  
     67
    6868  for (int i = 0; i < INTSIZE; i++)
    6969  {
     
    7171    x /= mod;
    7272  }
    73  
     73
    7474  if (sgn == -1)
    7575    result[INTSIZE - 1] += sgnadd;
    76  
    77  
     76
     77
    7878  return result;
    7979}
     
    8484 * @return: An int that accords the given byte-array
    8585 */
    86 int Converter::byteArrayToInt(byte* a)
     86int Converter::byteArrayToInt(const byte* a)
    8787{
    8888  int mult = 1;
     
    102102  else
    103103    result += a[INTSIZE - 1] * mult;
    104  
     104
    105105  return result;
    106106}
     
    113113  char* result = new char[200];
    114114  int pos = 0;
    115  
     115
    116116  if (x < 0)
    117117  {
     
    119119    x = -x;
    120120  }
    121  
     121
    122122  float sub = 1;
    123123  while (sub < x)
     
    135135    pos++;
    136136    sub /= 2;
    137    
     137
    138138    if (sub == 0.5f)
    139139      result[pos++] = '.';
    140140  }
    141  
     141
    142142  return result;
    143143}
     
    154154  int mantisse = 0;
    155155  int exponent = 128;
    156  
     156
    157157  int sgn;
    158158  if (x < 0)
     
    163163  else
    164164    sgn = 1;
    165  
     165
    166166  float sub = 1;
    167167  while (sub < x)
     
    178178      x -= sub;
    179179    }
    180    
     180
    181181    mantisse *= 2;
    182182    exponent--;
     
    190190    exponent--;
    191191  }
    192  
    193   //printf("mantisse = %i exponent = %i \n", mantisse, exponent); 
    194  
     192
     193  //printf("mantisse = %i exponent = %i \n", mantisse, exponent);
     194
    195195  mantisse -= expmult;
    196  
     196
    197197  int hx = mantisse + expmult * exponent;
    198198  byte* result = intToByteArray(hx);
    199199  if (sgn == -1)
    200200    result[3] += sgnadd;
    201  
     201
    202202  return result;
    203203}
     
    214214  int mantisse = hmant % expmult;
    215215  mantisse += expmult;
    216  
     216
    217217  int hexp = a[2] + a[3] * 256;
    218218  int exponent = (hexp / 128) % 256 - 128;
    219  
     219
    220220  int sgn;
    221221  if (a[3] >= sgnadd)
     
    223223  else
    224224    sgn = 1;
    225  
     225
    226226  //printf("mantisse = %i exponent = %i \n", mantisse, exponent);
    227  
     227
    228228  float emult = 1;
    229229  if (exponent > 0)
     
    233233    for (int i = 0; i > exponent; i--)
    234234      emult /= 2;
    235  
     235
    236236  float result = mantisse * emult;
    237237  if (sgn == -1)
    238238    result = -result;
    239  
     239
    240240  return result;
    241241}
  • branches/network/src/lib/network/converter.h

    r6197 r6219  
    33 *  Is able to convert int to byte-array and vice versa
    44 */
    5  
     5
    66#ifndef _CONVERTER
    77#define _CONVERTER
     
    2323  public:
    2424    static byte* intToByteArray(int x);
    25     static int byteArrayToInt(byte* a);
    26    
     25    static int byteArrayToInt(const byte* a);
     26
    2727    //byte* floatToByteArray(float x);
    2828    //float byteArrayToFloat(byte[] a);
    29    
     29
    3030    //Test
    3131    static char* floatToBinString(float x);
    32    
     32
    3333    static byte* floatToByteArray(float x);
    3434    static float byteArrayToFloat(byte* a);
    35    
     35
    3636    static byte* _floatToByteArray(float x);
    3737    static float _byteArrayToFloat(byte* a);
  • branches/network/src/lib/network/network_game_manager.cc

    r6214 r6219  
    7373    b = data[i++];
    7474
    75     if ( b == REQUEST_CREATE )
    76     {
    77       return;
    78     }
    79     if ( b == REQUEST_REMOVE )
    80     {
    81       return;
    82     }
    83     if ( b == CREATE_ENTITY )
    84     {
    85       return;
    86     }
    87     if ( b == REMOVE_ENTITY )
    88     {
    89       return;
    90     }
     75    if ( isServer() )
     76    {
     77      if ( b == REQUEST_CREATE )
     78      {
     79        if ( !handleRequestCreate( i, data, length, sender ) )
     80          return;
     81        continue;
     82      }
     83      if ( b == REQUEST_REMOVE )
     84      {
     85        if ( !handleRequestRemove( i, data, length, sender ) )
     86          return;
     87        continue;
     88      }
     89    }
     90
     91    if ( !isServer() )
     92    {
     93      if ( b == CREATE_ENTITY )
     94      {
     95        if ( !handleCreateEntity( i, data, length, sender ) )
     96          return;
     97        continue;
     98      }
     99      if ( b == REMOVE_ENTITY )
     100      {
     101        if ( !handleRemoveEntity( i, data, length, sender ) )
     102          return;
     103        continue;
     104      }
     105      if ( b == CREATE_ENTITY_LIST )
     106      {
     107        if ( !handleCreateEntityList( i, data, length, sender ) )
     108          return;
     109        continue;
     110      }
     111      if ( b == REMOVE_ENTITY_LIST )
     112      {
     113        if ( !handleRemoveEntityList( i, data, length, sender ) )
     114          return;
     115        continue;
     116      }
     117      if ( b == YOU_ARE_ENTITY )
     118      {
     119        if ( !handleYouAreEntity( i, data, length, sender ) )
     120          return;
     121        continue;
     122      }
     123    }
     124
    91125    if ( b == REQUEST_SYNC )
    92126    {
    93       return;
    94     }
    95     if ( b == YOU_ARE_ENTITY )
    96     {
    97       return;
    98     }
    99     if ( b == CREATE_ENTITY_LIST )
    100     {
    101       return;
    102     }
    103     if ( b == REMOVE_ENTITY_LIST )
    104     {
    105       return;
    106     }
     127      if ( !handleRequestSync( i, data, length, sender ) )
     128        return;
     129      continue;
     130    }
     131
     132    //if we get her something with data is wrong
     133    PRINTF(1)("Data is not in the right format! i=%d\n", i);
     134    return;
    107135  }
    108136}
     
    156184 * @param classID: The ID of the class of which an entity should be created
    157185 */
    158 void NetworkGameManager::createEntity(ClassID classID)
     186void NetworkGameManager::createEntity( ClassID classID, int owner )
    159187{
    160188  if ( this->isServer() )
     
    166194    }
    167195
    168     this->executeCreateEntity( classID, newUniqueID++ );
     196    this->executeCreateEntity( classID, newUniqueID++, owner );
    169197  }
    170198  else
    171199  {
    172     this->requestCreateEntity( classID);
     200    this->requestCreateEntity( classID );
    173201  }
    174202}
     
    570598}
    571599
     600bool NetworkGameManager::handleRequestCreate( int & i, const byte * data, int length, int sender )
     601{
     602  if ( INTSIZE > length-i )
     603  {
     604    PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
     605    return false;
     606  }
     607  int classID = Converter::byteArrayToInt( data );
     608  i += INTSIZE;
     609
     610  createEntity( (ClassID)classID );
     611
     612  return true;
     613}
     614
     615bool NetworkGameManager::handleRequestRemove( int & i, const byte * data, int length, int sender )
     616{
     617  if ( INTSIZE > length-i )
     618  {
     619    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
     620    return false;
     621  }
     622  int uniqueID = Converter::byteArrayToInt( data );
     623  i += INTSIZE;
     624
     625  removeEntity( uniqueID );
     626
     627  return true;
     628}
     629
     630bool NetworkGameManager::handleCreateEntity( int & i, const byte * data, int length, int sender )
     631{
     632  if ( INTSIZE > length-i )
     633  {
     634    PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
     635    return false;
     636  }
     637  int classID = Converter::byteArrayToInt( data );
     638  i += INTSIZE;
     639
     640  if ( INTSIZE > length-i )
     641  {
     642    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
     643    return false;
     644  }
     645  int uniqueID = Converter::byteArrayToInt( data );
     646  i += INTSIZE;
     647
     648  if ( INTSIZE > length-i )
     649  {
     650    PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n");
     651    return false;
     652  }
     653  int owner = Converter::byteArrayToInt( data );
     654  i += INTSIZE;
     655
     656  doCreateEntity( (ClassID)classID, uniqueID, owner );
     657
     658  return true;
     659}
     660
     661bool NetworkGameManager::handleRemoveEntity( int & i, const byte * data, int length, int sender )
     662{
     663  if ( INTSIZE > length-i )
     664  {
     665    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
     666    return false;
     667  }
     668  int uniqueID = Converter::byteArrayToInt( data );
     669  i += INTSIZE;
     670
     671  doRemoveEntity( uniqueID );
     672
     673  return true;
     674}
     675
     676bool NetworkGameManager::handleCreateEntityList( int & i, const byte * data, int length, int sender )
     677{
     678  if ( INTSIZE > length-i )
     679  {
     680    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
     681    return false;
     682  }
     683  int n = Converter::byteArrayToInt( data );
     684  i += INTSIZE;
     685
     686  int classID, uniqueID, owner;
     687
     688  for ( int j = 0; j<n; j++ )
     689  {
     690
     691    if ( INTSIZE > length-i )
     692    {
     693      PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
     694      return false;
     695    }
     696    classID = Converter::byteArrayToInt( data );
     697    i += INTSIZE;
     698
     699    if ( INTSIZE > length-i )
     700    {
     701      PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
     702      return false;
     703    }
     704    uniqueID = Converter::byteArrayToInt( data );
     705    i += INTSIZE;
     706
     707    if ( INTSIZE > length-i )
     708    {
     709      PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n");
     710      return false;
     711    }
     712    owner = Converter::byteArrayToInt( data );
     713    i += INTSIZE;
     714
     715    doCreateEntity( (ClassID)classID, uniqueID, owner );
     716
     717  }
     718  return true;
     719}
     720
     721bool NetworkGameManager::handleRemoveEntityList( int & i, const byte * data, int length, int sender )
     722{
     723  if ( INTSIZE > length-i )
     724  {
     725    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
     726    return false;
     727  }
     728  int n = Converter::byteArrayToInt( data );
     729  i += INTSIZE;
     730
     731  int uniqueID;
     732
     733  for ( int j = 0; j<n; j++ )
     734  {
     735
     736    if ( INTSIZE > length-i )
     737    {
     738      PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
     739      return false;
     740    }
     741    uniqueID = Converter::byteArrayToInt( data );
     742    i += INTSIZE;
     743
     744    doRemoveEntity( uniqueID );
     745  }
     746
     747  return true;
     748}
     749
     750bool NetworkGameManager::handleYouAreEntity( int & i, const byte * data, int length, int sender )
     751{
     752  if ( INTSIZE > length-i )
     753  {
     754    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
     755    return false;
     756  }
     757  int uniqueID = Converter::byteArrayToInt( data );
     758  i += INTSIZE;
     759
     760  doYouAre( uniqueID );
     761
     762  return true;
     763}
     764
     765bool NetworkGameManager::handleRequestSync( int & i, const byte * data, int length, int sender )
     766{
     767  if ( INTSIZE > length-i )
     768  {
     769    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
     770    return false;
     771  }
     772  int uniqueID = Converter::byteArrayToInt( data );
     773  i += INTSIZE;
     774
     775  doRequestSync( uniqueID, sender );
     776
     777  return true;
     778}
     779
  • branches/network/src/lib/network/network_game_manager.h

    r6214 r6219  
    6969    virtual void readDebug() const;
    7070
    71     void createEntity(ClassID classID);
    72     void removeEntity(int uniqueID);
     71    void createEntity( ClassID classID, int owner = 0 );
     72    void removeEntity( int uniqueID );
    7373    void sendYouAre( int uniqueID, int userID );
    7474
     
    9696    inline bool readFromClientBuffer( clientBuffer &cb, byte*data, int length );
    9797
     98    //helper functions for writeBytes
     99    inline bool handleRequestCreate( int& i, const byte* data, int length, int sender );
     100    inline bool handleRequestRemove( int& i, const byte* data, int length, int sender );
     101    inline bool handleCreateEntity( int& i, const byte* data, int length, int sender );
     102    inline bool handleRemoveEntity( int& i, const byte* data, int length, int sender );
     103    inline bool handleCreateEntityList( int& i, const byte* data, int length, int sender );
     104    inline bool handleRemoveEntityList( int& i, const byte* data, int length, int sender );
     105    inline bool handleYouAreEntity( int& i, const byte* data, int length, int sender );
     106    inline bool handleRequestSync( int& i, const byte* data, int length, int sender );
     107
    98108  private:
    99109    std::vector<clientBuffer>     outBuffer;
Note: See TracChangeset for help on using the changeset viewer.