/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: claudio co-programmer: */ /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput */ #define DEBUG_MODULE_NETWORK #include "base_object.h" #include "network_protocol.h" #include "network_socket.h" #include "connection_monitor.h" #include "synchronizeable.h" #include "network_manager.h" #include "network_game_manager.h" #include "debug.h" #include "class_list.h" #include /* include your own header */ #include "network_stream.h" /* probably unnecessary */ using namespace std; #define PACKAGE_SIZE 256 NetworkStream::NetworkStream() : DataStream() { this->init(); /* initialize the references */ this->type = NET_CLIENT; this->networkProtocol = new NetworkProtocol(); this->connectionMonitor = new ConnectionMonitor(); } NetworkStream::NetworkStream(IPaddress& address) { this->type = NET_CLIENT; this->init(); this->networkSockets.push_back(new NetworkSocket(address)); this->networkProtocol = new NetworkProtocol(); this->connectionMonitor = new ConnectionMonitor(); this->maxConnections = 1; } NetworkStream::NetworkStream(unsigned int port) { this->type = NET_SERVER; this->init(); this->serverSocket = new ServerSocket(port); this->networkProtocol = new NetworkProtocol(); this->connectionMonitor = new ConnectionMonitor(); this->networkSockets.push_back( NULL ); this->networkSockets[0] = NULL; //TODO: remove this this->handshakes.push_back( NULL ); this->bActive = true; } void NetworkStream::init() { /* set the class id for the base object */ this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); this->bActive = false; this->serverSocket = NULL; this->networkGameManager = NULL; myHostId = 0; } NetworkStream::~NetworkStream() { if ( this->serverSocket ) { serverSocket->close(); delete serverSocket; } for (NetworkSocketVector::iterator i = networkSockets.begin(); i!=networkSockets.end(); i++) { if ( *i ) { (*i)->disconnectServer(); (*i)->destroy(); } } for (HandshakeVector::iterator i = handshakes.begin(); i!=handshakes.end(); i++) { if ( *i ) { delete (*i); } } delete connectionMonitor; delete networkProtocol; } void NetworkStream::createNetworkGameManager() { this->networkGameManager = NetworkGameManager::getInstance(); // setUniqueID( maxCon+2 ) because we need one id for every handshake // and one for handshake to reject client maxCon+1 this->networkGameManager->setUniqueID( this->maxConnections + 2 ); //this->connectSynchronizeable( *(this->networkGameManager) ); this->setMaxConnections( 10 ); } void NetworkStream::startHandshake() { Handshake* hs = new Handshake(false); hs->setUniqueID( 0 ); this->handshakes.push_back(hs); //this->connectSynchronizeable(*hs); PRINTF(0)("NetworkStream: %s\n", hs->getName()); } void NetworkStream::connectSynchronizeable(Synchronizeable& sync) { this->synchronizeables.push_back(&sync); sync.setNetworkStream( this ); if( this->networkSockets.size()>0 ) this->bActive = true; } void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync) { // removing the Synchronizeable from the List. std::list::iterator disconnectSynchro = std::find(this->synchronizeables.begin(), this->synchronizeables.end(), &sync); if (disconnectSynchro != this->synchronizeables.end()) this->synchronizeables.erase(disconnectSynchro); if( this->networkSockets.size()<=0 ) this->bActive = false; } void NetworkStream::processData() { if ( this->type == NET_SERVER ) this->updateConnectionList(); else { if ( networkSockets[0] && !networkSockets[0]->isOk() ) { PRINTF(1)("lost connection to server\n"); //delete networkSockets[i]; networkSockets[0]->disconnectServer(); networkSockets[0]->destroy(); networkSockets[0] = NULL; if ( handshakes[0] ) delete handshakes[0]; handshakes[0] = NULL; } } for (int i = 0; icompleted() ) { if ( handshakes[i]->ok() ) { if ( type != NET_SERVER ) { NetworkManager::getInstance()->setHostID( handshakes[i]->getHostId() ); myHostId = NetworkManager::getInstance()->getHostID(); this->networkGameManager = NetworkGameManager::getInstance(); this->networkGameManager->setUniqueID( handshakes[i]->getNetworkGameManagerId() ); //this->connectSynchronizeable( *(this->networkGameManager) ); } else { } PRINT(0)("handshake finished id=%d\n", handshakes[i]->getNetworkGameManagerId()); delete handshakes[i]; handshakes[i] = NULL; } else { PRINT(1)("handshake failed!\n"); networkSockets[i]->disconnectServer(); delete handshakes[i]; handshakes[i] = NULL; //TODO: handle error } } } } /* DOWNSTREAM */ int dataLength; int reciever; Header header; for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) { if ( (*it)!=NULL && (*it)->beSynchronized() /*&& (*it)->getOwner() == myHostId*/ ) { do { reciever = 0; dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, &reciever); if ( dataLength<=0 ){ reciever = 0; continue; } dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast(*(*it))); Header* header = (Header*)downBuffer; if ( header->synchronizeableID < this->maxConnections+2 ) { //if ( !isServer() ) PRINTF(0)("RESET UNIQUEID FROM %d TO 0 maxCon=%d\n", header->synchronizeableID, this->maxConnections); header->synchronizeableID = 0; } else { //if ( !isServer() ) PRINTF(0)("UNIQUEID=%d\n", header->synchronizeableID); } if ( dataLength<=0 ) continue; if ( reciever!=0 ) { if ( networkSockets[reciever] != NULL ) { PRINTF(5)("write %d bytes to socket %d\n", dataLength, reciever); networkSockets[reciever]->writePacket(downBuffer, dataLength); } else { PRINTF(1)("networkSockets[reciever] == NULL\n"); } } else { for ( int i = 0; iwritePacket(downBuffer, dataLength); } } } } while( reciever!=0 ); } } /* UPSTREAM */ for ( int i = 0; ireadPacket(upBuffer, DATA_STREAM_BUFFER_SIZE); if ( dataLength<=0 ) continue; header = networkProtocol->extractHeader(upBuffer, dataLength); dataLength -= sizeof(header); PRINTF(5)("read %d bytes from socket uniqueID = %d\n", dataLength, header.synchronizeableID); if ( dataLength != header.length ) { PRINTF(1)("packetsize in header and real packetsize do not match! %d:%d\n", dataLength, header.length); continue; } if ( header.synchronizeableID == 0 ) { header.synchronizeableID = i; } for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) { if ( *it && (*it)->getUniqueID()==header.synchronizeableID ) { if ( (*it)->writeBytes(upBuffer+sizeof(header), dataLength, i) != header.length ) { PRINTF(1)("%s did not read all the data id = %d!\n", (*it)->getClassName(), (*it)->getUniqueID()); break; } continue; } } } while ( dataLength>0 ); } } } void NetworkStream::updateConnectionList( ) { //check for new connections NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket(); if ( tempNetworkSocket ) { int clientId; if ( freeSocketSlots.size() >0 ) { clientId = freeSocketSlots.back(); freeSocketSlots.pop_back(); networkSockets[clientId] = tempNetworkSocket; handshakes[clientId] = new Handshake(true, clientId, this->networkGameManager->getUniqueID()); handshakes[clientId]->setUniqueID(clientId); } else { clientId = networkSockets.size(); networkSockets.push_back(tempNetworkSocket); Handshake* tHs = new Handshake(true, clientId, this->networkGameManager->getUniqueID()); tHs->setUniqueID(clientId); handshakes.push_back(tHs); } if ( clientId > this->maxConnections ) { handshakes[clientId]->doReject(); PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId); } else PRINTF(0)("New Client: %d\n", clientId); //this->connectSynchronizeable(*handshakes[clientId]); } //check if connections are ok else remove them for ( int i = 1; iisOk() ) { //TODO: tell EntityManager that this player left the game PRINTF(0)("Client is gone: %d\n", i); //delete networkSockets[i]; networkSockets[i]->disconnectServer(); networkSockets[i]->destroy(); networkSockets[i] = NULL; if ( handshakes[i] ) delete handshakes[i]; handshakes[i] = NULL; if ( i == networkSockets.size()-1 ) { networkSockets.pop_back(); handshakes.pop_back(); } else { freeSocketSlots.push_back(i); } } } } void NetworkStream::setMaxConnections( int n ) { if ( !this->isServer() ) { PRINTF(1)("Cannot set maxConnections because I am no server.\n"); } if ( this->networkSockets.size() > 1 ) { PRINTF(1)("Cannot set maxConnections because there are already %d connections.\n", this->networkSockets.size()); return; } if ( n > MAX_CONNECTIONS ) { PRINTF(1)("Cannot set maxConnectiosn to %d because of hardcoded limit %d\n", n, MAX_CONNECTIONS); return; } this->maxConnections = n; this->networkGameManager->setUniqueID( n+2 ); } void NetworkStream::debug() { if( this->isServer()) PRINT(0)(" Host ist Server with ID: %i\n", this->myHostId); else PRINT(0)(" Host ist Client with ID: %i\n", this->myHostId); PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size()); for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) { //if( (*it)->beSynchronized() == true) PRINT(0)(" Synchronizeable of class: %s::%s, with unique ID: %i, Synchronize: %i\n", (*it)->getClassName(), (*it)->getName(), (*it)->getUniqueID(), (*it)->beSynchronized()); } PRINT(0)(" Maximal Connections: %i\n", this->maxConnections); }