Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8184 in orxonox.OLD for branches/network/src


Ignore:
Timestamp:
Jun 7, 2006, 1:16:12 PM (18 years ago)
Author:
rennerc
Message:

do network cleanup in multiplayerworld destructor

Location:
branches/network/src
Files:
12 edited

Legend:

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

    r8068 r8184  
    1717
    1818#include "message_manager.h"
     19
     20#include "network_stream.h"
    1921
    2022using namespace std;
     
    312314         recieverType == RT_ALL_NOT_ME ||
    313315         recieverType == RT_USER && it->first == reciever ||
    314          recieverType == RT_NOT_USER && it->first != reciever
     316         recieverType == RT_NOT_USER && it->first != reciever ||
     317         recieverType == RT_SERVER && getNetworkStream()->isUserServer( it->first )
    315318       )
    316319    {
  • branches/network/src/lib/network/message_manager.h

    r8147 r8184  
    3030  TESTMESSAGEID = 1,
    3131  MSGID_DELETESYNCHRONIZEABLE,
    32   MSGID_PREFEREDTEAM
     32  MSGID_PREFEREDTEAM,
     33  MSGID_CHANGEPLAYERNAME
    3334};
    3435
     
    4041  RT_ALL_ME,           //!< message is sent to all users
    4142  RT_USER,             //!< message is only sent to reciever
    42   RT_NOT_USER          //!< message is sent to all but reciever
     43  RT_NOT_USER,         //!< message is sent to all but reciever
     44  RT_SERVER            //!< message is sent to server only
    4345};
    4446
  • branches/network/src/lib/network/network_manager.cc

    r8147 r8184  
    5151
    5252  /* initialize the references */
    53   this->netStreamList = NULL;
    54   this->syncList = NULL;
    55   this->defaultSyncStream = NULL;
     53  this->networkStream = NULL;
    5654  this->sharedNetworkData = SharedNetworkData::getInstance();
    5755  this->elapsedTime = 0.0f;
     
    7169 */
    7270NetworkManager::~NetworkManager()
    73 {}
     71{
     72  if ( this->networkStream )
     73  {
     74    delete this->networkStream;
     75    this->networkStream = NULL;
     76  }
     77}
    7478
    7579
     
    7983void NetworkManager::initialize()
    8084{
    81   /* get the synchronizeable list from the class list */
    82   this->netStreamList = ClassList::getList(CL_SYNCHRONIZEABLE);
    8385  PRINTF(0)("NetworkManager initzalized\n");
    84 
    8586}
    8687
     
    101102int NetworkManager::establishConnection(const std::string & name, unsigned int port)
    102103{
    103   this->defaultSyncStream = new NetworkStream( name, port );
    104   this->sharedNetworkData->setDefaultSyncStream(this->defaultSyncStream);
    105   this->defaultSyncStream->startHandshake();
     104  this->networkStream = new NetworkStream( name, port );
     105  this->sharedNetworkData->setDefaultSyncStream(this->networkStream);
     106  this->networkStream->startHandshake();
    106107  return 1;
    107108}
     
    116117  this->sharedNetworkData->setHostID(0);
    117118  this->sharedNetworkData->setGameServer(true);
    118   this->defaultSyncStream = new NetworkStream(port);
    119   this->sharedNetworkData->setDefaultSyncStream(this->defaultSyncStream);
    120   this->defaultSyncStream->createNetworkGameManager();
     119  this->networkStream = new NetworkStream(port);
     120  this->sharedNetworkData->setDefaultSyncStream(this->networkStream);
     121  this->networkStream->createNetworkGameManager();
    121122  PRINTF(0)("CREATE SERVER\n");
    122123  SDL_Delay(20);
     
    127128void NetworkManager::connectSynchronizeable(Synchronizeable& sync)
    128129{
    129   if( this->defaultSyncStream)
    130     this->defaultSyncStream->connectSynchronizeable(sync);
     130  if( this->networkStream)
     131    this->networkStream->connectSynchronizeable(sync);
    131132}
    132133
     
    143144  this->elapsedTime = 0.0f;
    144145
    145   if (this->netStreamList != NULL || (this->netStreamList = ClassList::getList(CL_NETWORK_STREAM)) != NULL)
    146   {
    147     std::list<BaseObject*>::const_iterator stream;
    148     for (stream = this->netStreamList->begin(); stream != this->netStreamList->end(); ++stream)
    149       if( static_cast<NetworkStream*>(*stream)->isActive())
    150         static_cast<NetworkStream*>(*stream)->processData();
    151   }
     146  if ( networkStream->isActive() )
     147    networkStream->processData();
    152148 
    153149  NetworkGameManager::getInstance()->tick( this->elapsedTime );
     
    162158{
    163159  PRINT(0)("=================Network::debug()=========\n");
    164   this->defaultSyncStream->debug();
     160  this->networkStream->debug();
    165161  PRINT(0)("===========================================\n");
    166162}
  • branches/network/src/lib/network/network_manager.h

    r7954 r8184  
    5555
    5656  private:
    57     const std::list<BaseObject*>*    netStreamList;           // list with refs to all network streams
    58     const std::list<BaseObject*>*    syncList;                // list of synchronizeables
    5957    static NetworkManager*           singletonRef;            //!< Pointer to the only instance of this Class
    60     NetworkStream*                   defaultSyncStream;       //!< FIXME: this is only for testing purposes
     58    NetworkStream*                   networkStream;       //!< pointer to network stream
    6159
    6260    SharedNetworkData*               sharedNetworkData;       //!< reference to the shared data
  • branches/network/src/lib/network/player_stats.cc

    r8153 r8184  
    6262  playableUniqueId_handle = registerVarId( new SynchronizeableInt( &playableUniqueId, &playableUniqueId, "playableUniqueId" ) );
    6363  modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName" ) );
     64  playerName_handle = registerVarId( new SynchronizeableString( &playerName, &playerName, "playerName" ) );
     65 
     66  MessageManager::getInstance()->registerMessageHandler( MSGID_CHANGEPLAYERNAME, changePlayernameHandler, NULL );
    6467 
    6568  PRINTF(0)("PlayerStats created\n");
     
    164167}
    165168
    166 
     169bool PlayerStats::changePlayernameHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
     170{
     171  if ( !SharedNetworkData::getInstance()->isGameServer() )
     172    return true;
     173 
     174  if ( Converter::byteArrayToString( data, getStats(userId)->playerName, dataLength ) != dataLength );
     175  {
     176    PRINTF(1)("recieved invalid message from user %d\n", userId);
     177    // TODO give user a name
     178    getStats( userId )->playerName = "invalid name";
     179  }
     180 
     181  return true;
     182}
     183
     184std::string PlayerStats::setPlayerName( std::string name )
     185{
     186  if ( isServer() )
     187  {
     188    this->playerName = name;
     189  }
     190  else
     191  {
     192    if ( userId == getHostID() )
     193    {
     194      byte * data = new byte[INTSIZE+playerName.length()];
     195     
     196      assert( Converter::stringToByteArray( playerName, data, INTSIZE+playerName.length() ) == INTSIZE+playerName.length() );
     197     
     198      MessageManager::getInstance()->sendMessage( MSGID_CHANGEPLAYERNAME, data, INTSIZE+playerName.length(), RT_SERVER, 0, MP_HIGHBANDWIDTH );
     199    }
     200    else
     201    {
     202      PRINTF(1)("Clients cannot set PlayerNames of other hosts!\n");
     203    }
     204  }
     205}
     206
     207
  • branches/network/src/lib/network/player_stats.h

    r8068 r8184  
    99#include "synchronizeable.h"
    1010#include "playable.h"
     11#include "message_manager.h"
    1112
    1213#include <string>
     
    3536    inline int getUserId(){ return userId; }
    3637   
     38    inline std::string getPlayerName(){ return playerName; }
     39    inline std::string setPlayerName( std::string name );
     40   
    3741    inline int getTeamId(){ return teamId; }
    3842    inline void setTeamId( int teamId ){ this->teamId = teamId; }
     
    5458   
    5559    Playable * getPlayable();
     60   
     61    static bool changePlayernameHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId );
    5662
    5763  private:
    5864    int userId;                //!< userId
     65
     66    std::string playerName;    //!< playername
     67
    5968    int teamId;                //!< teamId
    6069    int preferedTeamId;        //!< preferedTeamId
     
    6978    // handles for SynchronizeableVars
    7079    int userId_handle;
     80    int playerName_handle;
    7181    int teamId_handle;
    7282    int preferedTeamId_handle;
  • branches/network/src/lib/network/udp_server_socket.cc

    r7954 r8184  
    4444UdpServerSocket::~UdpServerSocket( )
    4545{
    46   for ( int i = 0; i < packetBuffer.size(); i++ )
     46  for ( int i = 0; i < (int)packetBuffer.size(); i++ )
    4747    removeUserPackets( i );
    4848 
     
    9999{
    100100 
    101   for ( int i = 0; i < packetBuffer.size(); i++ )
     101  for ( int i = 0; i < (int)packetBuffer.size(); i++ )
    102102    removeUserPackets( i );
    103103 
     
    115115void UdpServerSocket::removeUserPackets( int userId )
    116116{
    117   if ( userId >= packetBuffer.size() )
     117  if ( userId >= (int)packetBuffer.size() )
    118118    return;
    119119 
     
    141141  res.length = 0;
    142142 
    143   if ( packetBuffer.size() > userId && packetBuffer[userId].size() > 0 )
     143  if ( (int)packetBuffer.size() > userId && (int)packetBuffer[userId].size() > 0 )
    144144  {
    145145    res.data = packetBuffer[userId].front().data;
    146146    res.length = packetBuffer[userId].front().length;
    147147    packetBuffer[userId].pop_front();
     148   
     149    if ( res.length == 0 )
     150      res.length = -1;
    148151  }
    149152 
     
    158161int UdpServerSocket::getPacketCount( int userId )
    159162{
    160   if ( userId >= packetBuffer.size() )
     163  if ( userId >= (int)packetBuffer.size() )
    161164    return -1;
    162165 
     
    179182  }
    180183 
    181   if ( userId < packetBuffer.size() )
     184  if ( userId < (int)packetBuffer.size() )
    182185    removeUserPackets( userId );
    183186 
    184   if ( packetBuffer.size() <= userId )
     187  if ( (int)packetBuffer.size() <= userId )
    185188    packetBuffer.resize( userId + 1 );
    186189 
    187   if ( userList.size() <= userId )
     190  if ( (int)userList.size() <= userId )
    188191    userList.resize( userId + 1 );
    189192 
     
    199202  removeUserPackets( userId );
    200203 
    201   if ( userId >= userList.size() )
     204  if ( userId >= (int)userList.size() )
    202205    return;
    203206 
     
    244247    bool isNewConnection = false;
    245248   
    246     for ( userId =0; userId < userList.size(); userId++ )
     249    for ( userId =0; userId < (int)userList.size(); userId++ )
    247250      if ( userList[userId].host == packet->address.host && userList[userId].port == packet->address.port )
    248251        break;
    249252   
    250     if ( userId >= userList.size() )
     253    if ( userId >= (int)userList.size() )
    251254    {
    252255     
     
    260263      }
    261264     
    262       for ( userId =0; userId < userList.size(); userId++ )
     265      for ( userId =0; userId < (int)userList.size(); userId++ )
    263266        if ( userList[userId].host == 0 && userList[userId].port == 0 )
    264267          break;
     
    281284    else
    282285    {
    283       if ( isNewConnection )
    284         continue;
    285      
    286286      networkPacket.data = NULL;
    287287    }
  • branches/network/src/lib/network/udp_socket.cc

    r7954 r8184  
    8181UdpSocket::~UdpSocket( )
    8282{
     83  this->disconnectServer();
     84 
    8385  if ( serverSocket )
    8486    serverSocket->removeUser( userId );
    85  
    86   disconnectServer();
    87  
     87
    8888  if ( this->packet )
    8989    SDLNet_FreePacket( this->packet );
     
    128128    return;
    129129  }
    130  
     130}
     131
     132/**
     133 * disconnect from server
     134 */
     135void UdpSocket::disconnectServer( )
     136{
     137  PRINTF(0)("disconnect from server\n");
    131138  writePacket( NULL, 0 );
    132 }
    133 
    134 /**
    135  * disconnect from server
    136  */
    137 void UdpSocket::disconnectServer( )
    138 {
    139139  SDLNet_UDP_Unbind( socket, -1 );
    140140  SDLNet_UDP_Close( socket );
     
    196196    NetworkPacket networkPacket = serverSocket->getPacket( this->userId );
    197197   
     198    if ( networkPacket.length == -1 )
     199    {
     200      this->disconnectServer();
     201      return 0;
     202    }
     203   
    198204    if ( networkPacket.length > 0 )
    199205    {
     
    218224      assert( packet->len <= maxLength );
    219225     
     226      if ( packet->len == 0 )
     227      {
     228        this->disconnectServer();
     229        return 0;
     230      }
     231     
    220232      memcpy( data, packet->data, packet->len );
    221233      return packet->len;
  • branches/network/src/story_entities/multi_player_world.cc

    r8068 r8184  
    2626
    2727#include "network_manager.h"
     28#include "network_game_manager.h"
    2829
    2930
     
    119120  ((MultiPlayerWorldData*)this->dataTank)->debug();
    120121}
     122
     123/**
     124 * cleanup
     125 * @return
     126 */
     127ErrorMessage MultiPlayerWorld::unloadData( )
     128{
     129  GameWorld::unloadData();
     130 
     131  delete NetworkManager::getInstance();
     132  delete NetworkGameManager::getInstance();
     133}
  • branches/network/src/story_entities/multi_player_world.h

    r8068 r8184  
    2626
    2727  virtual void loadParams(const TiXmlElement* root);
     28 
     29  virtual ErrorMessage unloadData();
    2830
    2931  void debug();
  • branches/network/src/util/signal_handler.cc

    r8068 r8184  
    1717#include <assert.h>
    1818
     19SignalHandler * SignalHandler::singletonRef = NULL;
     20
    1921#ifndef __WIN32__
    20 
    21 SignalHandler * SignalHandler::singletonRef = NULL;
    2222
    2323SignalHandler::SignalHandler()
     
    5959void SignalHandler::sigHandler( int sig )
    6060{
     61  for ( SignalCallbackList::iterator it = SignalHandler::getInstance()->callbackList.begin(); it != SignalHandler::getInstance()->callbackList.end(); it++  )
     62  {
     63    (*(it->cb))( it->someData );
     64  }
     65 
    6166  std::string sigName = "UNKNOWN";
    6267
     
    113118}
    114119
     120void SignalHandler::registerCallback( SignalCallback cb, void * someData )
     121{
     122  SignalCallbackRec rec;
     123  rec.cb = cb;
     124  rec.someData = someData;
     125 
     126  callbackList.push_back(rec);
     127}
     128
    115129#endif /* __WIN32__ */
  • branches/network/src/util/signal_handler.h

    r7440 r8184  
    1717};
    1818
     19typedef bool (*SignalCallback)( void * someData );
    1920
    2021#ifndef __WIN32__
     
    2728};
    2829
     30struct SignalCallbackRec
     31{
     32  SignalCallback cb;
     33  void * someData;
     34};
     35
    2936
    3037typedef std::list<SignalRec> SignalRecList;
     38typedef std::list<SignalCallbackRec> SignalCallbackList;
    3139
    3240class SignalHandler
     
    3543    SignalHandler();
    3644  public:
    37     inline static SignalHandler* getInstance() { if (!SignalHandler::singletonRef) SignalHandler::singletonRef = new SignalHandler();
    38       return SignalHandler::singletonRef; }
     45    inline static SignalHandler* getInstance() { if (!SignalHandler::singletonRef) SignalHandler::singletonRef = new SignalHandler(); return SignalHandler::singletonRef; }
     46     
     47    void registerCallback( SignalCallback cb, void * someData );
    3948
    4049    void doCatch( std::string appName, GdbRunType type = GDB_RUN_WRITE_TO_FILE );
     
    4655    void catchSignal( int sig );
    4756    SignalRecList sigRecList;
     57   
     58    SignalCallbackList callbackList;
    4859
    4960    static SignalHandler * singletonRef;
     
    5869{
    5970 public:
    60   inline static SignalHandler* getInstance() {};
     71   inline static SignalHandler* getInstance() { if (!SignalHandler::singletonRef) SignalHandler::singletonRef = new SignalHandler(); return SignalHandler::singletonRef; };
    6172  void doCatch( std::string appName, GdbRunType type = GDB_RUN_WRITE_TO_FILE ) {};
    6273  void dontCatch() {};
     74  void registerCallback( SignalCallback cb, void * someData ) {};
    6375};
    6476#endif
Note: See TracChangeset for help on using the changeset viewer.