Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7954 in orxonox.OLD for trunk/src/subprojects/network


Ignore:
Timestamp:
May 29, 2006, 3:28:41 PM (19 years ago)
Author:
patrick
Message:

trunk: merged the network branche back to trunk.

Location:
trunk/src/subprojects/network
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/subprojects/network/Makefile.am

    r7160 r7954  
    11MAINSRCDIR=../..
    22include $(MAINSRCDIR)/defs/include_paths.am
     3
     4LIB_PREFIX=$(MAINSRCDIR)/lib
     5include $(MAINSRCDIR)/lib/BuildLibs.am
     6
    37
    48bin_PROGRAMS = network
    59
    610
    7 network_CPPFLAGS = -DNO_SHELL -DNO_SHELL_COMMAND
     11network_CPPFLAGS =
     12#-DNO_SHELL -DNO_SHELL_COMMAND
    813
    9 DEPENDENCIES = \
     14network_DEPENDENCIES = \
    1015                $(MAINSRCDIR)/world_entities/libORXwe.a \
    1116                $(libORXlibs_a_LIBRARIES_) \
    1217                $(MAINSRCDIR)/util/libORXutils.a
    1318
    14 LDADD = \
     19network_LDADD = \
    1520                $(MAINSRCDIR)/util/libORXutils.a \
    1621                $(libORXlibs_a_LIBRARIES_) \
  • trunk/src/subprojects/network/network_unit_test.cc

    r6424 r7954  
    99#include "network_socket.h"
    1010#include "server_socket.h"
     11#include "udp_server_socket.h"
     12#include "udp_socket.h"
    1113#include "network_stream.h"
    1214#include "synchronizeable.h"
    1315#include "converter.h"
     16#include "state.h"
     17#include "shared_network_data.h"
     18#include "message_manager.h"
     19#include "network_log.h"
    1420
    1521#include "simple_sync.h"
     
    3642int testSocket(int argc, char** argv)
    3743{
    38   IPaddress ip;
    39   SDLNet_ResolveHost(&ip, "127.0.0.1", 9999);
    40   ServerSocket server;
    41   server.listen(9999);
    42 
    43   NetworkSocket* client1 = new NetworkSocket(ip);
    44 
    45   NetworkSocket* server1 = server.getNewSocket();
    46 
    47   NetworkSocket* client2 = new NetworkSocket(ip);
    48 
    49   NetworkSocket* server2 = server.getNewSocket();
     44  UdpServerSocket server(9999);
     45
     46  NetworkSocket* client1 = new UdpSocket("localhost", 9999);
     47
     48  NetworkSocket* server1 = NULL;
     49  while ( server1 == NULL )
     50  {
     51    server.update();
     52    server1 = server.getNewSocket();
     53  }
     54 
     55  assert( server1->isOk() );
     56
     57  NetworkSocket* client2 = new UdpSocket("localhost", 9999);
     58
     59  NetworkSocket* server2 = NULL;
     60  while ( server2 == NULL )
     61  {
     62    server.update();
     63    server2 = server.getNewSocket();
     64  }
    5065
    5166  char buf[1024];
     
    7085  n = server2->writePacket((byte*)str4, strlen(str4)+1);
    7186  printf("%d bytes send from server2\n", n);
    72   SDL_Delay(1000);
     87  SDL_Delay(10);
     88 
     89  server.update();
    7390
    7491  printf("read from server1\n");
     
    103120
    104121  printf("data: '%s'\n", buf);
     122 
     123 
     124  //22222222222222222222222222222222222222222
     125  n = client1->writePacket((byte*)str1, strlen(str1)+1);
     126  printf("%d bytes send from client1\n", n);
     127  n = server1->writePacket((byte*)str2, strlen(str2)+1);
     128  printf("%d bytes send from server1\n", n);
     129  n = client2->writePacket((byte*)str3, strlen(str3)+1);
     130  printf("%d bytes send from client2\n", n);
     131  n = server2->writePacket((byte*)str4, strlen(str4)+1);
     132  printf("%d bytes send from server2\n", n);
     133  SDL_Delay(10);
     134 
     135  server.update();
     136
     137  printf("read from server1\n");
     138  n = server1->readPacket((byte*)buf, 1024);
     139  printf("read %d bytes\n", n);
     140  if (n<0)
     141    return -1;
     142
     143  printf("data: '%s'\n", buf);
     144
     145  printf("read from server2\n");
     146  n = server2->readPacket((byte*)buf, 1024);
     147  printf("read %d bytes\n", n);
     148  if (n<0)
     149    return -1;
     150
     151  printf("data: '%s'\n", buf);
     152
     153  printf("read from client1\n");
     154  n = client1->readPacket((byte*)buf, 1024);
     155  printf("read %d bytes\n", n);
     156  if (n<0)
     157    return -1;
     158
     159  printf("data: '%s'\n", buf);
     160
     161  printf("read from client2\n");
     162  n = client2->readPacket((byte*)buf, 1024);
     163  printf("read %d bytes\n", n);
     164  if (n<0)
     165    return -1;
     166
     167  printf("data: '%s'\n", buf);
    105168
    106169  //sending bigger packets than 255 is not supported
    107 
     170#if 0
    108171  printf("try to send more than 255 bytes\n");
    109172  printf("result: %d\n", client1->writePacket((byte*)buf, 1000));
     
    113176  printf("try to read with a too small buffer\n");
    114177  printf("result: %d\n", client1->readPacket((byte*)buf, strlen(str1)));
    115 
     178#endif
     179
     180  delete client1;
     181  delete client2;
     182  delete server1;
     183  delete server2;
     184 
    116185  return 0;
    117186}
     
    124193  printf("=================\n");
    125194
    126   Synchronizeable* clientSync = new SimpleSync("Client\0");
    127   Synchronizeable* serverSync = new SimpleSync("Server\0");
     195  Synchronizeable* clientSync = new SimpleSync( std::string("Client") );
     196  Synchronizeable* serverSync = new SimpleSync( std::string("Server") );
    128197
    129198  unsigned int port = 9999;
     
    144213  if(error == -1)
    145214    printf("\n\nerror on address resolution, program inconsistancy\n\n");
    146   nm->establishConnection(ip, *clientSync);
     215  nm->establishConnection("localhost", port);
     216  nm->connectSynchronizeable( *clientSync );
    147217  /* adding some break for connection setup */
    148218  SDL_Delay(20);
     
    151221  for( int i = 0; i < 3; i++)
    152222  {
    153     nm->synchronize();
     223    nm->synchronize( 1000 );
    154224    /* simulate the network delay */
    155225    SDL_Delay(50);
     
    168238}
    169239
    170 
     240bool testCB( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
     241{
     242  printf("GOT MESSAGE: %s\n", data);
     243  return true;
     244}
    171245
    172246/**
     
    178252int startServer(int argc, char** argv)
    179253{
    180   if( argc <= 2)
    181   {
    182     printf(" Wrong arguments try following notations:\n");
    183     printf("   --server [port number]\n");
    184     return 0;
    185   }
    186 
    187   int port = atoi(argv[2]);
    188   printf("Starting Server on port %i\n", port);
     254
     255
     256  State::setOnline(true);
     257  SharedNetworkData::getInstance()->setGameServer( true );
     258  printf("Starting Server on port %i\n", 9999);
    189259
    190260  NetworkManager* netMan = NetworkManager::getInstance();
    191   Synchronizeable* ss = new SimpleSync("Server\0");
    192 
    193   netMan->createServer(/**ss, */port);
     261 
     262  netMan->initialize();
     263 
     264  netMan->createServer(/**ss, */ 9999);
     265 
     266  SimpleSync* ss = new SimpleSync("Server");
     267  ss->setSynchronized( true );
     268 
     269  NetworkLog::getInstance()->listen( 8888 );
     270
     271  //MessageManager::getInstance()->initUser( 1 );
     272  MessageManager::getInstance()->registerMessageHandler( TESTMESSAGEID, testCB, NULL );
     273 
    194274  SDL_Delay(20);
    195275
    196276  for(;;)
    197277  {
    198     netMan->synchronize();
    199     SDL_Delay(1000);
     278    MessageManager::getInstance()->sendMessage( TESTMESSAGEID, (byte*)"server to client", 18, RT_ALL, 0, MP_HIGHBANDWIDTH );
     279    netMan->synchronize( 100 );
     280    SDL_Delay(100);
     281    ss->debug();
    200282  }
    201283
     
    210292int startClient(int argc, char** argv)
    211293{
     294
     295
     296  State::setOnline(true);
     297  NetworkManager* netMan = NetworkManager::getInstance();
     298 
     299
     300  netMan->initialize();
     301 
     302  std::string host;
     303 
     304  if ( argc > 2 )
     305    host = argv[2];
     306  else
     307    host = "localhost";
     308 
     309  netMan->establishConnection(host, 9999);
     310 
     311//  SimpleSync* ss = new SimpleSync("SimpleSync");
     312//  ss->setSynchronized( true );
     313//  netMan->connectSynchronizeable( *ss );
     314 
     315  NetworkLog::getInstance()->listen( 7777 );
     316
     317  SimpleSync * ss = NULL;
     318 
     319  //MessageManager::getInstance()->initUser( 0 );
     320  MessageManager::getInstance()->registerMessageHandler( TESTMESSAGEID, testCB, NULL );
     321 
     322  for(;;)
     323  {
     324    netMan->synchronize( 100 );
     325    MessageManager::getInstance()->sendMessage( TESTMESSAGEID, (byte*)"client to server", 18, RT_ALL, 0, MP_HIGHBANDWIDTH );
     326    ss = dynamic_cast<SimpleSync*>(ClassList::getObject( "Server", CL_SIMPLE_SYNC ) );
     327    SDL_Delay(100);
     328    if (ss)
     329      ss->debug();
     330  }
     331
     332
     333  delete netMan;
     334  delete ss;
     335
     336  return 0;
     337}
     338
     339
     340
     341int startListen(int argc, char** argv)
     342{
    212343  if( argc < 3)
    213344  {
    214345    printf(" Wrong arguments try following notations:\n");
    215     printf("   --client [server ip] [port number]\n");
    216     printf("   --client [server name] [port number]\n");
     346    printf("   --listen [server ip] [port number]\n");
     347    printf("   --listen [server name] [port number]\n");
    217348    return 0;
    218349  }
     
    228359
    229360  NetworkManager* netMan = NetworkManager::getInstance();
    230   Synchronizeable* ss = new SimpleSync("Client\0");
    231 
    232   netMan->establishConnection((const char*)"localhost", port/*,ip, *ss*/);
     361  Synchronizeable* ss = new SimpleSync("SimpleSync\0");
     362
     363  netMan->establishConnection( name, port );
     364  netMan->connectSynchronizeable( *ss );
    233365
    234366  for(;;)
    235367  {
    236     netMan->synchronize();
    237     SDL_Delay(500);
     368    netMan->synchronize( 1000 );
     369    SDL_Delay(10);
    238370  }
    239371
     
    245377}
    246378
    247 
    248 
    249 int startListen(int argc, char** argv)
    250 {
    251   if( argc < 3)
    252   {
    253     printf(" Wrong arguments try following notations:\n");
    254     printf("   --listen [server ip] [port number]\n");
    255     printf("   --listen [server name] [port number]\n");
    256     return 0;
    257   }
    258 
    259   char* name = argv[2];
    260   int port = atoi(argv[3]);
    261   printf("Connecting to %s, on port %i\n", name, port);
    262 
    263   IPaddress ip;
    264   int error = SDLNet_ResolveHost(&ip, name, port);
    265   if(error == -1)
    266     printf("\n\nerror on address resolution, program inconsistancy\n\n");
    267 
    268   NetworkManager* netMan = NetworkManager::getInstance();
    269   Synchronizeable* ss = new ReadSync("WriteSync\0");
    270 
    271   netMan->establishConnection(ip, *ss);
    272 
    273   for(;;)
    274   {
    275     netMan->synchronize();
    276     SDL_Delay(10);
    277   }
    278 
    279 
    280   delete netMan;
    281   delete ss;
    282 
    283   return 0;
    284 }
    285 
    286379void testFloatConverter(float f)
    287380{
     381#if 0
    288382  char* s = Converter::floatToBinString(f);
    289383  printf("%f = ", f);
     
    293387  printf("Byte Array: ");
    294388  for (int i = 0; i < 4; i++)
    295     printf("%i  ", res[i]);
     389//    printf("%i  ", res[i]);
    296390  printf("\n");
    297391
    298392  float b = Converter::byteArrayToFloat(res);
    299393  printf("ReConvert: %f \n", b);
     394#endif
    300395}
    301396
    302397void testFloatConverter2(float f)
    303398{
     399#if 0
    304400  char* s = Converter::floatToBinString(f);
    305401  printf("### %f = ", f);
     
    314410  float b = Converter::_byteArrayToFloat(res);
    315411  printf("ReConvert: %f \n", b);
     412#endif
    316413}
    317414int converter(int argc, char** argv)
     
    435532
    436533
    437 bool ShellBuffer::addBufferLineStatic(const char* line, ...)
    438 {
     534//bool ShellBuffer::addBufferLineStatic(const char* line, ...)
     535//{
    439536  //va_list arguments;
    440537  //vprintf(line, arguments);
    441   printf("%s", line);
    442 }
     538  //  printf("%s", line);
     539//}
  • trunk/src/subprojects/network/simple_sync.cc

    r6634 r7954  
    2222#include "simple_sync.h"
    2323
     24#include "class_id.h"
     25#include "fast_factory.h"
     26#include "lib/util/loading/factory.h"
     27
    2428#include "debug.h"
     29
     30CREATE_FACTORY(SimpleSync, CL_SIMPLE_SYNC);
    2531
    2632
     
    2834 *  default constructor
    2935 */
    30 SimpleSync::SimpleSync(const char* name)
    31   : Synchronizeable()
     36SimpleSync::SimpleSync( std::string name )
    3237{
    33   /* define the local buffer size */
    34   this->outLength = 10;
    35   this->recLength = 0;
    36   this->inLength = 40;
    37   this->outData = new byte[this->outLength];
    38   this->inData = new byte[this->inLength];
    39 
    40   /* init the buffer data */
    41   for( int i = 0; i < this->outLength; i++)
    42   {
    43     this->outData[i] = i;
    44   }
    45   for( int i = 0; i < this->inLength; i++)
    46   {
    47     this->inData[i] = 0;
    48   }
    49 
     38  setName( name );
     39  this->setClassID( CL_SIMPLE_SYNC, "SimpleSync" );
     40  in = 0;
     41  out = 1;
     42  syncStr = "hallo test test";
     43  id = this->registerVarId( new SynchronizeableInt( &in, &out, "var", PERMISSION_ALL ) );
     44  registerVar( new SynchronizeableString( &syncStr, &syncStr, "syncStr" ) );
    5045}
    5146
     
    5651SimpleSync::~SimpleSync()
    5752{
    58   if( this->outData)
    59     delete[] this->outData;
    60   if( this->inData)
    61     delete[] this->inData;
     53}
     54
     55void SimpleSync::debug( )
     56{
     57  printf("IN: %d OUT: %d\n", in, out);
     58  printf("str: %s\n", syncStr.c_str());
     59}
     60
     61SimpleSync::SimpleSync( const TiXmlElement * root )
     62{
     63  setName( "" );
     64  this->setClassID( CL_SIMPLE_SYNC, "SimpleSync" );
     65  in = 0;
     66  out = 1;
     67  syncStr = "hallo test test";
     68  id = this->registerVarId( new SynchronizeableInt( &in, &out, "var", PERMISSION_ALL ) );
     69  registerVar( new SynchronizeableString( &syncStr, &syncStr, "syncStr" ) );
    6270}
    6371
    6472
    65 /**
    66  *  write data to Synchronizeable
    67  */
    68 int SimpleSync::writeBytes(const byte* data, int length, int sender)
    69 {
    70   PRINTF(0)("SimpleSync: got %i bytes of data\n", length);
    71   this->recLength = length;
    72   if(this->inLength < length)
    73     PRINTF(0)("ERROR: local buffer is smaller than the data to receive.\n");
    74 
    75   /* copy the data localy */
    76   for( int i = 0; i < length; i++)
    77   {
    78     this->inData[i] = data[i];
    79   }
    80   /* and debug output */
    81   this->writeDebug();
    82 }
    8373
    8474
    85 /**
    86  *  read data from Synchronizeable
    87  */
    88 int SimpleSync::readBytes(byte* data, int maxLength, int * reciever)
    89 {
    90   PRINTF(0)("SimpleSync: sent %i bytes of data\n", this->outLength);
    91 
    92   /* debug msg */
    93   this->readDebug();
    94 
    95   /* write the test message */
    96   for( int i = 0; i < this->outLength; i++)
    97     data[i] = this->outData[i];
    98 
    99   /* return the length of the test */
    100   return this->outLength;
    101 }
    102 
    103 
    104 void SimpleSync::writeDebug() const
    105 {
    106   PRINTF(0)("Write in bytes: \t(0 <-) |");
    107   for(int i = 0; i < this->recLength; i++)
    108   {
    109     PRINT(0)(" [%u] ",this->inData[i]);
    110   }
    111   PRINT(0)("|\n");
    112 }
    113 
    114 
    115 void SimpleSync::readDebug() const
    116 {
    117   PRINTF(0)("Read out bytes: \t(0 ->) |");
    118   for(int i = 0; i < this->outLength; i++)
    119   {
    120     PRINT(0)(" [%u] ",this->outData[i]);
    121   }
    122   PRINT(0)("|\n");
    123 }
  • trunk/src/subprojects/network/simple_sync.h

    r6981 r7954  
    1313  {
    1414  public:
    15     SimpleSync(const char* name);
     15    SimpleSync( std::string name);
     16    SimpleSync(const TiXmlElement* root = NULL);
    1617    virtual ~SimpleSync();
    17 
    18     virtual int writeBytes(const byte* data, int length, int sender);
    19     virtual int readBytes(byte* data, int maxLength, int * reciever);
     18   
     19    void debug();
    2020
    2121
    2222  private:
    23     virtual void writeDebug() const;
    24     virtual void readDebug() const;
    25 
    26 
    27   private:
    28     byte* inData;
    29     int   inLength;
    30     int   recLength;
    31     byte* outData;
    32     int   outLength;
     23    int in;
     24    int out;
     25    std::string syncStr;
     26    int id;
    3327  };
    3428
Note: See TracChangeset for help on using the changeset viewer.