/* 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 "list.h" #include "debug.h" #include "class_list.h" /* 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(); Handshake* hs = new Handshake(false); this->handshakes.push_back(hs); this->connectSynchronizeable(*hs); PRINTF(0)("NetworkStream: %s\n", hs->getName()); } 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->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; 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::connectSynchronizeable(Synchronizeable& sync) { this->synchronizeables.push_back(&sync); if( this->networkSockets.size()>0 ) this->bActive = true; } void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync) { this->synchronizeables.remove(&sync); if( this->networkSockets.size()<=0 ) this->bActive = false; } void NetworkStream::processData() { if ( this->type == NET_SERVER ) this->updateConnectionList(); for (int i = 0; icompleted() ) { if ( handshakes[i]->ok() ) { if ( type != NET_SERVER ) { NetworkManager::getInstance()->setHostID( handshakes[i]->getHostId() ); myHostId = NetworkManager::getInstance()->getHostID(); } PRINT(0)("handshake finished\n"); //TODO: replace handshake by entitymanager } else { PRINT(1)("handshake failed!\n"); networkSockets[i]->disconnectServer(); //TODO: handle error } } } } /* DOWNSTREAM */ int dataLength; int reciever; Header header; for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) { //TODO: remove items from synchronizeables if they dont exist if ( (*it)!=NULL && ClassList::exists(*it) && (*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))); if ( dataLength<=0 ) continue; if ( reciever!=0 ) { if ( networkSockets[reciever] != NULL ) { PRINTF(0)("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 ); } else { PRINTF(0)("synchronizeables == NULL"); } } /* UPSTREAM */ for ( int i = 0; ireadPacket(upBuffer, DATA_STREAM_BUFFER_SIZE); if ( dataLength<=0 ) continue; PRINTF(0)("read %d bytes from socket\n", dataLength); header = networkProtocol->extractHeader(upBuffer, dataLength); dataLength -= sizeof(header); if ( dataLength != header.length ) { PRINTF(1)("packetsize in header and real packetsize do not match! %d:%d\n", dataLength, header.length); continue; } for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) { if ( *it && (*it)->getUniqueID()==header.synchronizeableID ) (*it)->writeBytes(upBuffer+sizeof(header), dataLength); } } while ( dataLength>0 ); } } #if 0 int dataLength = 0; /* DOWNSTREAM */ printf("\nSynchronizeable: %s\n", this->synchronizeables->getName()); PRINT(0)("============= DOWNSTREAM:===============\n"); /* first of all read the synchronizeable's data: */ if( this->isServer()) dataLength = this->synchronizeables->readBytes((byte*)downBuffer); if( dataLength > 0) { /* send the received data to connectionMonitor */ this->connectionMonitor->processPacket((byte*)downBuffer, dataLength); dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, *(this->synchronizeables), 12/* some random number (no real id)*/); /* pass the data to the network socket */ // dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength); /* check if there was an error */ if( dataLength == -1) { PRINTF(0)("Error in writing data to the NetworkSocket\n"); } } /* UPSTREAM */ dataLength = 0; PRINT(0)("============== UPSTREAM:================\n"); /* first of all read the next Orxonox Network Header */ if( this->state == NET_REC_HEADER) { // dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header)); if( dataLength == sizeof(Header)) { this->packetHeader = this->networkProtocol->extractHeader((byte*) upBuffer , dataLength); printf("NetworkStream::processData() - Got Header: Protocol %u, Version: %u, Sender: %u, Receiver: %u, Length: %u\n", this->packetHeader.protocol, this->packetHeader.version, this->packetHeader.senderID, this->packetHeader.receiverID, this->packetHeader.length); /* FIXME: what if it was no this->packetHeader? catch? eg: the protocol identifier, receiver id*/ this->state = NET_REC_DATA; } } if( this->state == NET_REC_DATA) { /* now read the data */ // dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length); /* check if the data is available and process it if so */ if( dataLength == this->packetHeader.length) { printf("NetworkStream::processData() - Got Data: %i bytes\n", dataLength); /* send the received data to connectionMonitor */ this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length); /* now pass the data to the sync object */ if( !this->isServer()) this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length); this->state = NET_REC_HEADER; } } #endif } 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); } else { clientId = networkSockets.size(); networkSockets.push_back(tempNetworkSocket); handshakes.push_back(new Handshake(true, clientId)); } 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; //TODO: delete handshake from synchronizeable list so i can delete it delete handshakes[i]; handshakes[i] = NULL; if ( i == networkSockets.size()-1 ) { networkSockets.pop_back(); handshakes.pop_back(); } else { freeSocketSlots.push_back(i); } } } }