Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6340 in orxonox.OLD for branches


Ignore:
Timestamp:
Dec 30, 2005, 1:52:26 AM (19 years ago)
Author:
bensch
Message:

orxonox/branches/newtork: merged the network branche for merging to the trunk
confilcts maily resolved in favor of trunk with some minor fixes

merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/network . -r6146:HEAD

Location:
branches/network_merged/src
Files:
43 edited

Legend:

Unmodified
Added
Removed
  • branches/network_merged/src/defs/class_id.h

    r6287 r6340  
    253253  CL_CONNECTION_MONITOR         =    0x00000b05,
    254254  CL_HANDSHAKE                  =    0x00000b06,
    255   CL_ENTITY_MANAGER             =    0x00000b07,
     255  CL_NETWORK_GAME_MANAGER       =    0x00000b07,
    256256
    257257
  • branches/network_merged/src/lib/coord/p_node.cc

    r6307 r6340  
    2727#include "glincl.h"
    2828#include "color.h"
     29
     30#include "synchronizeable.h"
    2931
    3032using namespace std;
     
    10071009    return (PNODE_ROTATE_AND_MOVE);
    10081010}
     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 */
     1018int 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 */
     1092int 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  
    2020
    2121#include "base_object.h"
     22#include "stdincl.h"
    2223
    2324#include "vector.h"
     
    178179  static PARENT_MODE charToParentingMode(const char* parentingMode);
    179180
     181  int       writeState(const byte* data, int length, int sender);
     182  int       readState(byte* data, int maxLength );
    180183
    181184 private:
  • branches/network_merged/src/lib/lang/base_object.cc

    r6282 r6340  
    2222#include "compiler.h"
    2323#include "class_list.h"
     24
     25#include "synchronizeable.h"
    2426
    2527using namespace std;
     
    180182
    181183}
     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 */
     191int 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 */
     211int 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  
    1515#define NULL     0    //!< NULL
    1616#endif
     17
     18#include "stdincl.h"
    1719
    1820class TiXmlElement;
     
    4547  bool operator==(ClassID classID) { return this->isA(classID); };
    4648
     49  int       writeState(const byte* data, int length, int sender);
     50  int       readState(byte* data, int maxLength );
    4751 protected:
    4852  void setClassID(ClassID classID, const char* className);
  • branches/network_merged/src/lib/network/converter.cc

    r6139 r6340  
    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  
    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 */
     87int 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;
    79116}
    80117
     
    84121 * @return: An int that accords the given byte-array
    85122 */
    86 int Converter::byteArrayToInt(byte* a)
     123int Converter::byteArrayToInt(const byte* a, int* x)
    87124{
    88125  int mult = 1;
    89126  const int step = 256; // = 2 ^ 8
    90   int result = 0;
     127  *x = 0;
    91128  for (int i = 0; i < INTSIZE - 1; i++)
    92129  {
    93     result += a[i] * mult;
     130    *x += a[i] * mult;
    94131    mult *= step;
    95132  }
    96  
    97   printf("tara: %i", result);
    98  
     133
    99134  if (a[INTSIZE - 1] >= sgnadd)
    100135  {
    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 */
     148char* Converter::floatToBinString(float x)
    111149{
    112150  char* result = new char[200];
    113151  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  {
    154165    if (x >= sub)
    155166    {
     
    161172    pos++;
    162173    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
     182const 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 */
     189byte* 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
    174203  float sub = 1;
    175204  while (sub < x)
     205  {
    176206    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)
    182211  {
    183212    if (x >= sub)
    184213    {
    185       result[pos] = '1';
     214      mantisse += 1;
    186215      x -= sub;
    187216    }
    188     else
    189       result[pos] = '0';
    190     pos++;
     217
     218    mantisse *= 2;
     219    exponent--;
    191220    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 */
     248float 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 */
     285byte* 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 */
     300float 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 */
     312int 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)
    200348  {
    201349    if (x >= sub)
    202350    {
    203       result[pos] = '1';
     351      mantisse += 1;
    204352      x -= sub;
    205353    }
    206     else
    207       result[pos] = '0';
    208     pos++;
     354
     355    mantisse *= 2;
     356    exponent--;
    209357    sub /= 2;
    210358  }
    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 */
     385int 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 */
     437int 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 */
     459int 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 */
     486int 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  
    33 *  Is able to convert int to byte-array and vice versa
    44 */
    5  
     5
    66#ifndef _CONVERTER
    77#define _CONVERTER
     
    1515/* The size of an int in byte */
    1616#define INTSIZE 4
     17/* The size of a float in byte */
     18#define FLOATSIZE 4
    1719
    1820/*!
     
    2325  public:
    2426    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
    3039    //Test
    3140    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);
    3347  private:
    3448    Converter();
  • branches/network_merged/src/lib/network/data_stream.h

    r5822 r6340  
    1313#include "netdefs.h"
    1414
    15 #define DATA_STREAM_BUFFER_SIZE 1024
     15#define DATA_STREAM_BUFFER_SIZE 10240
    1616
    1717class DataStream : public BaseObject
  • branches/network_merged/src/lib/network/handshake.cc

    r6139 r6340  
    3737}
    3838
    39 void Handshake::writeBytes( const byte * data, int length )
     39int Handshake::writeBytes( const byte * data, int length, int sender)
    4040{
    4141  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);
    4242
     43  SYNCHELP_READ_BEGIN();
     44
    4345  if ( hasState( HS_COMPLETED ) )
    44        return;
     46       return 0;
    4547
    4648  if ( !hasState( HS_RECVD_INIT ) )
     
    5052      PRINTF(0)("initial packet has wrong size %d instead of %d\n", length, _INITIAL_DATA_LENGTH);
    5153      setState( HS_COMPLETED );
    52       return;
     54      return 0;
    5355    }
    5456
     
    5759      PRINTF(0)("initial packed does not match\n");
    5860      setState( HS_COMPLETED );
    59       return;
     61      return length;
    6062    }
    6163
    6264    setState( HS_RECVD_INIT );
    6365    PRINTF(0)("got valid initial packet from client %d\n", clientId);
    64     return;
     66    return length;
    6567  }
    6668
     
    7173      PRINTF(0)("version number packet has wrong size %d instead of %d\n", length, _ORXONOX_VERSION_LENGTH);
    7274      setState( HS_COMPLETED );
    73       return;
     75      return 0;
    7476    }
    7577
     
    7880      PRINTF(0)("versions do not match\n");
    7981      setState( HS_COMPLETED );
    80       return;
     82      return length;
    8183    }
    8284
     
    8486
    8587    PRINTF(0)("client %d's version does match\n", clientId);
    86     return;
     88    return length;
    8789  }
    8890
    8991  if ( !isServer() && hasState( HS_RECVD_VER ) && !hasState( HS_RECVD_HID ) )
    9092  {
    91     if ( length != 2 )
     93    if ( length != INTSIZE+INTSIZE )
    9294    {
    9395      PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, 1);
    9496      setState( HS_COMPLETED );
    95       return;
     97      return 0;
    9698    }
    9799
     
    99101    setState( HS_RECVD_HID );
    100102    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 );
    103105
    104106    if ( newHostId == 0 )
     
    112114      PRINTF(0)("got my hostID: %d and networkGameManagerId: %d\n", newHostId, newNetworkGameManagerId);
    113115    }
    114     return;
     116    return SYNCHELP_READ_N;
    115117  }
    116118
     
    120122{
    121123  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();
    122126
    123127  if ( hasState( HS_COMPLETED ) )
     
    172176      isOk = false;
    173177      //memcpy(data, (byte*)0, 4);
    174       data[0] = 0;
     178      SYNCHELP_WRITE_INT( 0 );
     179      SYNCHELP_WRITE_INT( 0 );
    175180    }
    176181    else
     
    178183      isOk = true;
    179184      //memcpy(data, &clientId, 4);
    180       data[0] = (byte)clientId;
    181       data[1] = (byte)networkGameManagerId;
     185      SYNCHELP_WRITE_INT( clientId );
     186      SYNCHELP_WRITE_INT( networkGameManagerId );
    182187    }
    183188    *reciever = clientId;
    184     return 2;
     189    return SYNCHELP_WRITE_N;
    185190  }
    186191
  • branches/network_merged/src/lib/network/handshake.h

    r6139 r6340  
    4242    inline void       doReject(){ setState(HS_DO_REJECT); }
    4343
    44     virtual void      writeBytes(const byte* data, int length);
     44    virtual int       writeBytes(const byte* data, int length, int sender);
    4545    virtual int       readBytes(byte* data, int maxLength, int * reciever);
    4646    virtual void      writeDebug() const;
  • branches/network_merged/src/lib/network/network_game_manager.cc

    r6139 r6340  
    2020#define DEBUG_MODULE_NETWORK
    2121
     22#include "factory.h"
     23#include "network_stream.h"
     24#include "converter.h"
     25
    2226/* include your own header */
    2327#include "network_game_manager.h"
     
    2731using namespace std;
    2832
     33NetworkGameManager* NetworkGameManager::singletonRef = NULL;
     34
    2935/*!
    3036 * Standard constructor
     
    3238NetworkGameManager::NetworkGameManager()
    3339{
     40  PRINTF(0)("START\n");
     41
    3442  /* 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;
    3654}
    3755
     
    4159NetworkGameManager::~NetworkGameManager()
    4260{
    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
     72int 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;
    48152}
    49153
    50154int NetworkGameManager::readBytes(byte* data, int maxLength, int * reciever)
    51155{
     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;
    52199}
    53200
     
    63210/*!
    64211 * Checks whether this is connected to a server or a client
    65  * and afterwards creates the needed entity if possible
     212 * and afterwards creates the needed entity
    66213 * @param classID: The ID of the class of which an entity should be created
    67214 */
    68 void NetworkGameManager::createEntity(int classID)
    69 {
    70 }
     215void 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 */
     239BaseObject* 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
    71280
    72281/*!
     
    77286void NetworkGameManager::removeEntity(int uniqueID)
    78287{
     288  if ( this->isServer() )
     289  {
     290    this->executeRemoveEntity( uniqueID );
     291  }
     292  else
     293  {
     294    this->requestRemoveEntity( uniqueID );
     295  }
    79296}
    80297
     
    85302 * @param classID: The ID of the class of which an entity should be created
    86303 */
    87 void NetworkGameManager::requestCreateEntity(int classID)
    88 {
     304void NetworkGameManager::requestCreateEntity(ClassID classID)
     305{
     306  if ( !writeToClientBuffer( allOutBuffer, (byte)REQUEST_CREATE ) )
     307    return;
     308  if ( !writeToClientBuffer( allOutBuffer, (int)classID ) )
     309    return;
    89310}
    90311
     
    95316void NetworkGameManager::requestRemoveEntity(int uniqueID)
    96317{
     318  if ( !writeToClientBuffer( allOutBuffer, (byte)REQUEST_REMOVE ) )
     319    return;
     320  if ( !writeToClientBuffer( allOutBuffer, uniqueID ) )
     321    return;
    97322}
    98323
     
    102327 * @param classID: The ID of the class of which an entity should be created
    103328 */
    104 void NetworkGameManager::executeCreateEntity(int classID)
    105 {
     329void 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 );
    106341}
    107342
     
    113348void NetworkGameManager::executeRemoveEntity(int uniqueID)
    114349{
     350  if ( !writeToClientBuffer( allOutBuffer, (byte)REMOVE_ENTITY ) )
     351    return;
     352  if ( !writeToClientBuffer( allOutBuffer, uniqueID ) )
     353    return;
     354
     355  doRemoveEntity(uniqueID);
    115356}
    116357
     
    119360 * @return: true if the entity can be created, false otherwise
    120361 */
    121 bool NetworkGameManager::canCreateEntity(int classID)
    122 {
    123 }
     362bool 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 */
     371void 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 */
     416void 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 */
     438void 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 */
     469void 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 */
     491void 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 */
     515bool 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 */
     534bool 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 */
     550void 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 */
     560void 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
     579bool 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
     594bool 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
     609bool 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
     640bool 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
     655bool 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
     704bool 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
     732bool 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
     748bool 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
     763bool 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
     776bool 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
     790void 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
     801void 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  
    1414
    1515
     16class TiXmlElement;
    1617
    1718/**
    1819 * protocol definition
    1920 *
    20  *  CREATE_ENTITY:      CLASS_ID, UNIQUE_ID, OWNER
    21  *  REMOVE_ENTITY:      UNIQUE_ID
     21 *  CREATE_ENTITY:       CLASS_ID, UNIQUE_ID, OWNER
     22 *  REMOVE_ENTITY:       UNIQUE_ID
    2223 *
    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]
    2526 *
    26  *  REQUEST_CREATE:     CLASS_ID
    27  *  REQUEST_REMOVE:     UNIQUE_ID
     27 *  REQUEST_CREATE:      CLASS_ID
     28 *  REQUEST_REMOVE:      UNIQUE_ID
    2829 *
    29  *  REQUEST_SYNC:       UNIQUE_ID
    30  *  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]
    3132 *
     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
    3238 *
    3339 */
    3440
     41typedef 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};
    3552
    36 
    37 
    38 
    39 
    40 
    41 
    42 
     53struct clientBuffer
     54{
     55  int length;
     56  int maxLength;
     57  byte * buffer;
     58};
    4359
    4460/*!
     
    4864{
    4965  public:
    50     NetworkGameManager();
    5166    ~NetworkGameManager();
    5267
    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);
    5472    virtual int readBytes(byte* data, int maxLength, int * reciever);
    5573    virtual void writeDebug() const;
    5674    virtual void readDebug() const;
    5775
    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 );
    6280
    63     void sync(int uniqueID);
    64     void syncList(int* uniqueIDList);
     81    void sync(int uniqueID, int owner);
    6582
     83    void sendEntityList(int userID);
    6684
    6785  private:
    68     void requestCreateEntity(int classID);
    69     void executeCreateEntity(int classID);
    70     void requestCreateEntityList(int* classIDList);
    71     void executeCreateEntityList(int* classIDList);
     86    NetworkGameManager();
     87
     88    void requestCreateEntity(ClassID classID);
     89    void executeCreateEntity(ClassID classID, int uniqueID = 0, int owner = 0);
    7290
    7391    void requestRemoveEntity(int uniqueID);
    7492    void executeRemoveEntity(int uniqueID);
    75     void requestRemoveEntityList(int* uniqueIDList);
    76     void executeRemoveEntityList(int* uniqueIDList);
    7793
     94    void executeRequestSync( int uniqueID, int user );
    7895
     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 );
    79100
    80     bool canCreateEntity(int classID);
     101    bool canCreateEntity(ClassID classID);
    81102
     103    void resizeBufferVector(int n);
    82104
     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 );
    83119
    84120  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;
    87127};
    88128
  • branches/network_merged/src/lib/network/network_manager.cc

    r6139 r6340  
    4444  /* set the class id for the base object */
    4545  this->setClassID(CL_NETWORK_MANAGER, "NetworkManager");
     46  PRINTF(0)("START\n");
    4647
    4748  /* initialize the references */
     
    4950  this->syncList = NULL;
    5051  this->tmpStream = NULL;
     52
    5153  this->hostID = -1;
    5254  this->bGameServer = false;
     
    107109int NetworkManager::createServer(unsigned int port)
    108110{
     111  this->hostID = 0;
     112  this->bGameServer = true;
    109113  this->tmpStream = new NetworkStream(port);
    110114  this->bGameServer = true;
     
    124128  this->tmpStream = new NetworkStream(address);
    125129  this->tmpStream->connectSynchronizeable(sync);
    126 }
    127 
    128 
    129 /**
    130  *  creates a new NetworkStream of server type
    131  * @param sync: the listener
    132  */
    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;
    140130}
    141131
     
    178168void NetworkManager::setHostID(int id)
    179169{
    180   this->hostID = id;
     170    this->hostID = id;
    181171}
  • branches/network_merged/src/lib/network/network_manager.h

    r6139 r6340  
    3939
    4040    NetworkStream& establishConnection(IPaddress& address, Synchronizeable& sync);
    41     NetworkStream& createServer(Synchronizeable& sync, unsigned int port);
    4241    void shutdownConnection();
    4342
  • branches/network_merged/src/lib/network/network_protocol.cc

    r6139 r6340  
    3030#include "debug.h"
    3131
     32#include "converter.h"
     33
    3234/* using namespace std is default, this needs to be here */
    3335using namespace std;
     
    4143  /* set the class id for the base object */
    4244  this->setClassID(CL_NETWORK_PROTOCOL, "NetworkProtocol");
    43   this->headerLength = sizeof(Header);
     45  this->headerLength = INTSIZE+FLOATSIZE;
    4446}
    4547
     
    6466  PRINTF(5)("create header length = %i, bufferLength = %i\n", length, bufferLength);
    6567  //If there isn't enough space for the header return -1
    66   if (length + this->headerLength > bufferLength)
     68  if (length + 2*INTSIZE > bufferLength)
    6769    return -1;
    6870
     
    7072  // FIXME: without move Create space for the header
    7173  for( int i = length - 1; i >= 0; i--)
    72     data[i + this->headerLength] = data[i];
     74    data[i + INTSIZE+INTSIZE] = data[i];
    7375
    7476  //Now create the header
    7577  /* 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
    7781  /* data length*/
    78   data[1] = length;
     82  //data[1] = length;
     83  res += Converter::intToByteArray( length, data+res, bufferLength-res );
    7984
    8085
    81   return length + this->headerLength;
     86  return length + res;
    8287}
    8388
     
    9297  PRINTF(5)("extract Header\n");
    9398  //Test if received data can contain a header
    94   if (length < headerLength)
     99  if (length < 2*INTSIZE)
    95100  {
    96101    PRINTF(1)("Received data is to short; it can't contain a header!\n");
     
    105110
    106111  /* unique ID */
    107   h.synchronizeableID = data[0];
     112  //h.synchronizeableID = data[0];
     113  int res = Converter::byteArrayToInt( data, &(h.synchronizeableID) );
    108114  /* data length*/
    109   h.length = data[1];
     115  //h.length = data[1];
     116  Converter::byteArrayToInt( data+res, &(h.length) );
    110117
    111118
  • branches/network_merged/src/lib/network/network_protocol.h

    r6139 r6340  
    1414typedef struct Header
    1515{
    16   byte synchronizeableID;
    17   byte length;
     16  int synchronizeableID;
     17  int length;
    1818};
    1919
  • branches/network_merged/src/lib/network/network_socket.cc

    r6139 r6340  
    2121#define DEBUG_MODULE_NETWORK
    2222
     23#include "converter.h"
    2324
    2425/* include your own header */
     
    398399{
    399400  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);
    409407  writeBytes(data, length);
    410408}
     
    413411{
    414412  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 );
    421420
    422421  if (blen>maxLength)
     
    431430  }
    432431
    433   readBytes(&blen, 1);
     432  byte t[INTSIZE];
     433  readBytes(t, INTSIZE);
    434434  int res = readBytes(data, blen);
    435435
  • branches/network_merged/src/lib/network/network_stream.cc

    r6144 r6340  
    2727#include "synchronizeable.h"
    2828#include "network_manager.h"
     29#include "network_game_manager.h"
     30
    2931#include "debug.h"
    3032#include "class_list.h"
     
    6466  this->connectSynchronizeable(*hs);
    6567  PRINTF(0)("NetworkStream: %s\n", hs->getName());
     68  this->maxConnections = 1;
    6669}
    6770
     
    7780  this->handshakes.push_back( NULL );
    7881  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) );
    7987
    8088  this->setMaxConnections( 10 );
     
    8896  this->bActive = false;
    8997  this->serverSocket = NULL;
     98  this->networkGameManager = NULL;
    9099  myHostId = 0;
    91100}
     
    176185            NetworkManager::getInstance()->setHostID( handshakes[i]->getHostId() );
    177186            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
    180199          delete handshakes[i];
    181200          handshakes[i] = NULL;
    182           //TODO: replace handshake by entitymanager
    183201        }
    184202        else
     
    202220  for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
    203221  {
    204     //TODO: remove items from synchronizeables if they dont exist
    205     if ( (*it)!=NULL && (*it)->getOwner() == myHostId )
     222    if ( (*it)!=NULL /*&& (*it)->getOwner() == myHostId*/ )
    206223    {
    207224      do {
     
    217234        dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it)));
    218235
    219         //FIXME: this is a hack, find a better way
    220236        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);
    222240          header->synchronizeableID = 0;
     241        }
     242        else
     243        {
     244          //if ( !isServer() ) PRINTF(0)("UNIQUEID=%d\n", header->synchronizeableID);
     245        }
    223246
    224247        if ( dataLength<=0 )
     
    229252          if ( networkSockets[reciever] != NULL )
    230253          {
    231             PRINTF(5)("write %d bytes to socket %d\n", dataLength, reciever);
     254            PRINTF(0)("write %d bytes to socket %d\n", dataLength, reciever);
    232255            networkSockets[reciever]->writePacket(downBuffer, dataLength);
    233256          }
     
    243266            if ( networkSockets[i] != NULL )
    244267            {
    245               PRINTF(5)("write %d bytes to socket %d\n", dataLength, reciever);
     268              PRINTF(0)("write %d bytes to socket %d\n", dataLength, reciever);
    246269              networkSockets[i]->writePacket(downBuffer, dataLength);
    247270            }
     
    251274      } while( reciever!=0 );
    252275    }
    253     else
    254     {
    255       PRINTF(0)("synchronizeables == NULL");
    256     }
    257276  }
    258277
     
    269288          continue;
    270289
    271         PRINTF(5)("read %d bytes from socket\n", dataLength);
    272290        header = networkProtocol->extractHeader(upBuffer, dataLength);
    273291        dataLength -= sizeof(header);
    274292
     293        PRINTF(0)("read %d bytes from socket uniqueID = %d\n", dataLength, header.synchronizeableID);
     294
    275295        if ( dataLength != header.length )
    276296        {
     
    287307        {
    288308          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          }
    290317        }
    291318
     
    309336      freeSocketSlots.pop_back();
    310337      networkSockets[clientId] = tempNetworkSocket;
    311       handshakes[clientId] = new Handshake(true, clientId);
     338      handshakes[clientId] = new Handshake(true, clientId, this->networkGameManager->getUniqueID());
    312339      handshakes[clientId]->setUniqueID(clientId);
    313340    } else
     
    315342      clientId = networkSockets.size();
    316343      networkSockets.push_back(tempNetworkSocket);
    317       Handshake* tHs = new Handshake(true, clientId);
     344      Handshake* tHs = new Handshake(true, clientId, this->networkGameManager->getUniqueID());
    318345      tHs->setUniqueID(clientId);
    319346      handshakes.push_back(tHs);
     
    376403    return;
    377404  }
     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
    378412  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  
    1515#include "handshake.h"
    1616
     17#define MAX_CONNECTIONS 1000
     18
    1719class Synchronizeable;
    1820class NetworkSocket;
     
    2022class ConnectionMonitor;
    2123class NetworkProtocol;
     24class NetworkGameManager;
    2225
    2326typedef std::list<Synchronizeable*>  SynchronizeableList;
     
    4851    virtual void processData();
    4952
     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
    5057  private:
    5158    NetworkProtocol*       networkProtocol;
     
    6370    int                    maxConnections;
    6471
     72    NetworkGameManager*   networkGameManager;
     73
    6574    void updateConnectionList();
    6675};
  • branches/network_merged/src/lib/network/synchronizeable.cc

    r6145 r6340  
    2828Synchronizeable::Synchronizeable()
    2929{
    30 
     30  this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable");
    3131  owner = 0;
     32  state = 0;
    3233  hostID = NetworkManager::getInstance()->getHostID();
     34  this->setIsServer(this->hostID == 0);
    3335  uniqueID = -1;
    3436  this->networkStream = NULL;
    35   //state = ?;
    36 
     37  this->setRequestedSync( false );
    3738}
    3839
    39 /**
    40  *  default constructor
    41  */
    42 Synchronizeable::Synchronizeable(const char* name)
    43 {
    44   this->setName(name);
    45   this->networkStream = NULL;
    46 }
    4740
    4841
     
    5952 *  write data to NetworkStream
    6053 */
    61 void Synchronizeable::writeBytes(const byte* data, int length)
     54int Synchronizeable::writeBytes(const byte* data, int length, int sender)
    6255{
    63   PRINTF(1)("Synchronizeable::writeBytes was called\n");
     56  PRINTF(5)("Synchronizeable::writeBytes was called\n");
    6457}
    6558
     
    6962int Synchronizeable::readBytes(byte* data, int maxLength, int * reciever)
    7063{
    71   PRINTF(1)("Synchronizeable::readBytes was called\n");
     64  PRINTF(5)("Synchronizeable::readBytes was called\n");
    7265}
    7366
     
    10396  else
    10497    this->state = this->state & (~STATE_OUTOFSYNC);
     98  //PRINTF(0)("isoutofsync %s %d\n", this->getClassName(), state);
    10599}
    106100
     
    111105bool Synchronizeable::isServer()
    112106{
    113   return this->state & STATE_SERVER == STATE_SERVER;
     107  return (this->state & STATE_SERVER) >0;
    114108}
    115109
     
    120114bool Synchronizeable::isOutOfSync()
    121115{
    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 */
     123bool 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 */
     132void Synchronizeable::setRequestedSync( bool requestedSync )
     133{
     134  if( requestedSync )
     135    this->state = this->state | STATE_REQUESTEDSYNC;
     136  else
     137    this->state = this->state & (~STATE_REQUESTEDSYNC);
    123138}
    124139
  • branches/network_merged/src/lib/network/synchronizeable.h

    r6139 r6340  
    99#include "base_object.h"
    1010#include "netdefs.h"
     11#include "converter.h"
    1112
    1213
     
    1819#define STATE_SERVER 1
    1920#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
    20160
    21161class NetworkStream;
     
    25165  {
    26166  public:
    27 
    28     Synchronizeable(const char* name);
    29167    Synchronizeable();
    30168    ~Synchronizeable();
    31169
    32     virtual void      writeBytes(const byte* data, int length);
     170    virtual int       writeBytes(const byte* data, int length, int sender);
    33171    virtual int       readBytes(byte* data, int maxLength, int * reciever);
    34172    virtual void      writeDebug() const;
     
    37175    void setIsServer( bool isServer );
    38176    void setIsOutOfSync( bool outOfSync );
     177    void setRequestedSync( bool requestedSync );
    39178    bool isServer();
    40179    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; }
    44186
    45187    inline int getOwner(){ return owner; }
     
    57199    int owner;
    58200    int hostID;
     201
     202    std::list<int> synchronizeRequests;
     203
     204  protected:
     205    NetworkStream* networkStream;
    59206    int state;
    60     std::list<int> synchronizeRequests;
    61 
    62     NetworkStream* networkStream;
    63207
    64208  };
  • branches/network_merged/src/story_entities/network_world.cc

    r6222 r6340  
    6969#include "playable.h"
    7070#include "network_manager.h"
     71#include "network_game_manager.h"
    7172#include "playable.h"
    7273
     
    167168{
    168169  this->setClassID(CL_WORLD, "NetworkWorld");
     170  PRINTF(0)("START\n");
    169171
    170172  this->setName(name);
     
    337339  // find WorldEntities //
    338340  ////////////////////////
    339   if( NetworkManager::getInstance()->isGameServer())
    340   {}
    341 
    342341  element = root->FirstChildElement("WorldEntities");
    343342  if( element == NULL)
     
    352351    while( element != NULL)
    353352    {
    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"))
    356380      {
    357381        BaseObject* created = Factory::fabricate(element);
     
    362386          printf("Created a %s: %s\n", created->getClassName(), created->getName());
    363387        }
     388        else
     389          PRINTF(1)("NetworkWorld: could not create this entity\n");
    364390
    365391          // if we load a 'Player' we use it as localPlayer
     
    374400          CDEngine::getInstance()->setTerrain(terrain);
    375401        }
    376 
    377402      }
    378403      element = element->NextSiblingElement();
     
    443468//   this->spawn(testEntity);
    444469
    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//   }
    454479
    455480  this->music = NULL;
     
    865890//   this->entities->add (entity);
    866891  entity->postSpawn ();
     892
    867893}
    868894
  • branches/network_merged/src/subprojects/network/Makefile.am

    r6139 r6340  
    2222                  $(MAINSRCDIR)/lib/lang/class_list.cc \
    2323                  $(MAINSRCDIR)/util/loading/load_param.cc \
     24                  $(MAINSRCDIR)/util/loading/factory.cc \
    2425                  $(MAINSRCDIR)/lib/util/substring.cc \
    2526                  $(MAINSRCDIR)/util/loading/load_param_description.cc \
  • branches/network_merged/src/subprojects/network/network_unit_test.cc

    r6139 r6340  
    284284}
    285285
    286 
     286void 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
     302void 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}
    287317int converter(int argc, char** argv)
    288318{
     319  /*
    289320  int x = 200564786;
    290321  printf("To convert: %i\n", x);
     
    305336  printf("\n");
    306337 
     338  */
     339  /*
    307340  float y;
    308341  char* s;
     
    328361  printf(s); printf("\n");
    329362 
    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");
    331398 
    332399  return 0;
  • branches/network_merged/src/subprojects/network/read_sync.cc

    r6139 r6340  
    6060 *  write data to Synchronizeable
    6161 */
    62 void ReadSync::writeBytes(const byte* data, int length)
     62int ReadSync::writeBytes(const byte* data, int length, int sender)
    6363{
    6464  PRINTF(0)("ReadSync: got %i bytes of data\n", length);
  • branches/network_merged/src/subprojects/network/read_sync.h

    r6139 r6340  
    1616    ~ReadSync();
    1717
    18     virtual void writeBytes(const byte* data, int length);
     18    virtual int writeBytes(const byte* data, int length, int sender);
    1919    virtual int readBytes(byte* data, int maxLength, int * reciever);
    2020
  • branches/network_merged/src/subprojects/network/simple_sync.cc

    r6139 r6340  
    6666 *  write data to Synchronizeable
    6767 */
    68 void SimpleSync::writeBytes(const byte* data, int length)
     68int SimpleSync::writeBytes(const byte* data, int length, int sender)
    6969{
    7070  PRINTF(0)("SimpleSync: got %i bytes of data\n", length);
  • branches/network_merged/src/subprojects/network/simple_sync.h

    r6139 r6340  
    1616    ~SimpleSync();
    1717
    18     virtual void writeBytes(const byte* data, int length);
     18    virtual int writeBytes(const byte* data, int length, int sender);
    1919    virtual int readBytes(byte* data, int maxLength, int * reciever);
    2020
  • branches/network_merged/src/subprojects/network/write_sync.cc

    r6139 r6340  
    6666 *  write data to Synchronizeable
    6767 */
    68 void WriteSync::writeBytes(const byte* data, int length)
     68int WriteSync::writeBytes(const byte* data, int length, int sender)
    6969{
    7070  PRINTF(0)("WriteSync: got %i bytes of data\n", length);
  • branches/network_merged/src/subprojects/network/write_sync.h

    r6139 r6340  
    1616    ~WriteSync();
    1717
    18     virtual void writeBytes(const byte* data, int length);
     18    virtual int writeBytes(const byte* data, int length, int sender);
    1919    virtual int readBytes(byte* data, int maxLength, int * reciever);
    2020
  • branches/network_merged/src/util/loading/factory.cc

    r5984 r6340  
    155155}
    156156
     157
    157158/**
    158159 * Creates a new Object of type classID
  • branches/network_merged/src/util/loading/factory.h

    r5984 r6340  
    2929#include "debug.h"
    3030#include <vector>
     31#include <list>
    3132
    3233/**
  • branches/network_merged/src/world_entities/npcs/npc.cc

    r6222 r6340  
    6262      this->collider = entity;
    6363  }
    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);
    6666  else if (entity->isA(CL_NPC))
    6767  {
  • branches/network_merged/src/world_entities/npcs/npc.h

    r6004 r6340  
    88class AI;
    99
    10 class NPC : public WorldEntity, public PhysicsInterface {
     10class NPC : public WorldEntity {
    1111
    1212 public:
  • branches/network_merged/src/world_entities/skybox.cc

    r6307 r6340  
    2222#include "static_model.h"
    2323#include "material.h"
     24#include "network_game_manager.h"
     25#include "converter.h"
    2426
    2527using namespace std;
     
    7779    }
    7880  this->setParentMode(PNODE_MOVEMENT);
     81
     82  this->textureName = NULL;
    7983}
    8084
     
    208212  this->setModel(model);
    209213}
     214
     215int 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
     243int 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
     271void SkyBox::writeDebug( ) const
     272{
     273}
     274
     275void SkyBox::readDebug( ) const
     276{
     277}
  • branches/network_merged/src/world_entities/skybox.h

    r6307 r6340  
    3535  void setSize(float size);
    3636  /** 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"); };
    3838
    3939  void setTextureAndType(const char* name, const char* extension);
    4040  void setTextures(const char* top, const char* bottom, const char* left,
    4141                   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;
    4247
    4348 private:
     
    4651  Material*       material[6];     //!< Materials for the SkyBox. sorted by number (0-5) top, bottom, left, right, front, back
    4752  float           size;            //!< Size of the SkyBox. This should match the frustum maximum range.
     53  char*           textureName;     //!< Name of the Texture
    4854
    4955};
  • branches/network_merged/src/world_entities/space_ships/space_ship.cc

    r6306 r6340  
    121121  PRINTF(4)("SPACESHIP INIT\n");
    122122
    123   EventHandler::getInstance()->grabEvents(true);
     123  //  EventHandler::getInstance()->grabEvents(true);
    124124
    125125  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
  • branches/network_merged/src/world_entities/terrain.cc

    r6142 r6340  
    2424#include "resource_manager.h"
    2525#include "model.h"
     26#include "network_game_manager.h"
     27
    2628
    2729#include "glincl.h"
     
    111113void Terrain::loadVegetation(const char* vegetationFile)
    112114{
     115  PRINTF(0)("loadVegetation: %s\n", vegetationFile);
    113116  if (this->vegetation)
    114117    ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL);
     
    116119  {
    117120    PRINTF(4)("fetching %s\n", vegetationFile);
    118       this->vegetation = (Model*)ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN);
     121    this->vegetation = (Model*)ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN);
    119122  }
    120123  else
     
    315318    }
    316319}
     320
     321int 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
     332int 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
     353void Terrain::writeDebug( ) const
     354{
     355}
     356
     357void Terrain::readDebug( ) const
     358{
     359}
  • branches/network_merged/src/world_entities/terrain.h

    r5500 r6340  
    3030  virtual ~Terrain();
    3131
     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
    3237  void init();
    3338  void loadParams(const TiXmlElement* root);
     
    4449   Model*              vegetation;
    4550   int                 objectList;
     51
    4652};
    4753
  • branches/network_merged/src/world_entities/weapons/ground_turret.cc

    r6142 r6340  
    1919
    2020#include "factory.h"
     21#include "network_game_manager.h"
    2122#include "load_param.h"
    2223
     
    168169
    169170}
     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 */
     178int 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 */
     196int 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  
    3030  virtual void collidesWith (WorldEntity* entity, const Vector& location);
    3131
     32  virtual int writeBytes(const byte* data, int length, int sender);
     33  virtual int readBytes(byte* data, int maxLength, int * reciever);
     34
    3235 private:
    3336   Weapon *left, *right;
  • branches/network_merged/src/world_entities/world_entity.cc

    r6281 r6340  
    105105void WorldEntity::loadModel(const char* fileName, float scaling, unsigned int modelNumber)
    106106{
    107   if (fileName != NULL)
     107  if ( fileName != NULL && strcmp(fileName, "") )
    108108  {
    109109   // search for the special character # in the LoadParam
     
    144144  }
    145145  else
     146  {
    146147    this->setModel(NULL);
     148  }
    147149}
    148150
     
    340342  glPopMatrix();
    341343}
     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 */
     351int 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 */
     380int 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  
    7474  std::list<WorldEntity*>::iterator& getEntityIterator() { return this->objectListIterator; }
    7575
     76  int       writeState(const byte* data, int length, int sender);
     77  int       readState(byte* data, int maxLength );
    7678
    7779 protected:
    7880  //  CharacterAttributes*    charAttr;         //!< the character attributes of a world_entity
     81
    7982
    8083 private:
     
    8992  std::list<WorldEntity*>::iterator objectListIterator; //!< The iterator position of this Entity in the given list of the ObjectManager.
    9093
     94  float                   scaling;
     95
     96
    9197};
    9298
Note: See TracChangeset for help on using the changeset viewer.