Changeset 5829 in orxonox.OLD for branches/network/src/lib
- Timestamp:
- Nov 30, 2005, 9:43:05 AM (19 years ago)
- Location:
- branches/network/src/lib/network
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/network_manager.cc
r5822 r5829 35 35 36 36 37 /************************************ 38 What you will see here are the function definitions from the header file (network_manager.h) with doxygen documentation. Here is an example: 39 40 41 In file network_manager.h 42 43 class NetworkManager 44 { 45 int doSomeStuff(float argument, float* pointer); 46 } 47 48 will be implemented in the source file as follows: 49 50 In file network_manager.cc 51 52 / ** 53 * this is the short description for this function: it just does some stuff 54 * @param argument: this is the first argument, stuff... 55 * @param pointer: this is the pointer to nowhereland 56 * return: whatever the function returns: for example an index, number, etc. 57 * / 58 int NetworkManager::doSomeStuff(float argument, float* pointer) 59 { 60 // whaterver you want to do 61 } 62 63 64 if you want to make automake compile your files: you will want to add the file names to the local Makefile.am 65 66 ************************************/ 67 68 37 NetworkManager* NetworkManager::singletonRef = NULL; 69 38 70 39 /** … … 79 48 this->netStreamList = NULL; 80 49 this->syncList = NULL; 50 this->tmpStream = NULL; 81 51 82 52 PRINTF(0)("NetworkManager created\n"); … … 114 84 * creates a connection from one object to a host 115 85 * @param hostName: the name of the destination host 116 * @param synchronizeable: reference to the sync object117 86 */ 118 NetworkStream& establishConnection(const char& hostName, const Synchronizeable& sync) 119 {} 87 int NetworkManager::establishConnection(const char* name, unsigned int port) 88 { 89 IPaddress ipAddress; 90 int error = SDLNet_ResolveHost(&ipAddress, name, port); 91 if( error == -1) { 92 printf("\n\nerror on address resolution, program inconsistency\n\n"); 93 return -1; 94 } 95 96 this->tmpStream = new NetworkStream(ipAddress, NET_CLIENT); 97 return 1; 98 } 99 100 101 /** 102 * creates a new NetworkStream of server type 103 * @param port: number of the TCP port 104 */ 105 int NetworkManager::createServer(unsigned int port) 106 { 107 this->tmpStream = new NetworkStream(port, NET_SERVER); 108 SDL_Delay(20); 109 return 1; 110 } 120 111 121 112 … … 127 118 NetworkStream& NetworkManager::establishConnection(IPaddress& address, Synchronizeable& sync) 128 119 { 129 printf("Establish connection to server %s, on port %u\n", SDLNet_ResolveIP(&address), address.port);130 120 /* creating a new network stream, it will register itself automaticaly to the class list */ 131 NetworkStream* netStream = new NetworkStream(address, sync, NET_CLIENT);121 this->tmpStream = new NetworkStream(address, sync, NET_CLIENT); 132 122 } 123 133 124 134 125 /** … … 140 131 PRINTF(0)("Create a new server socket\n"); 141 132 /* creating a new network stream, it will register itself automaticaly to the class list */ 142 NetworkStream* netStream = new NetworkStream(port, sync, NET_SERVER);133 this->tmpStream = new NetworkStream(port, sync, NET_SERVER); 143 134 } 144 135 … … 153 144 154 145 146 void NetworkManager::connectSynchronizeable(Synchronizeable& sync) 147 { 148 this->tmpStream->connectSynchronizeable(sync); 149 } 150 155 151 156 152 /** … … 161 157 if (this->netStreamList != NULL || (this->netStreamList = ClassList::getList(CL_NETWORK_STREAM)) != NULL) 162 158 { 159 // tIterator<BaseObject>* iterator = this->netStreamList->getIterator(); 160 // NetworkStream* stream = (NetworkStream*)(iterator->firstElement()); 161 // while( stream) 162 // { 163 // if(stream->isActive()) 164 // stream->processData(); 165 // stream = (NetworkStream*)(iterator->nextElement()); 166 // } 167 // delete iterator; 163 168 std::list<BaseObject*>::iterator stream; 164 169 for (stream = this->netStreamList->begin(); stream != this->netStreamList->end(); ++stream) 165 static_cast<NetworkStream*>(*stream)->processData(); 170 if( static_cast<NetworkStream*>(*stream)->isActive()) 171 static_cast<NetworkStream*>(*stream)->processData(); 172 166 173 } 167 174 -
branches/network/src/lib/network/network_manager.h
r5822 r5829 19 19 class NetworkStream; 20 20 class Synchronizeable; 21 template<typename> class tList; 21 template<typename> 22 class tList; 22 23 23 24 /* and here is the class itsself*/ … … 25 26 { 26 27 27 public:28 public: 28 29 29 NetworkManager(); 30 ~NetworkManager(); 30 inline static NetworkManager* getInstance() { if (!NetworkManager::singletonRef) NetworkManager::singletonRef = new NetworkManager(); 31 return NetworkManager::singletonRef; } 32 ~NetworkManager(); 31 33 32 void initialize();33 void shutdown();34 void initialize(); 35 void shutdown(); 34 36 35 NetworkStream& establishConnection(IPaddress& address, Synchronizeable& sync); 36 NetworkStream& establishConnection(const char& hostName, const Synchronizeable& sync); 37 NetworkStream& createServer(Synchronizeable& sync, unsigned int port); 38 void shutdownConnection(); 37 int establishConnection(const char* name, unsigned int port); 38 int createServer(unsigned int port); 39 39 40 void synchronize(); 40 NetworkStream& establishConnection(IPaddress& address, Synchronizeable& sync); 41 NetworkStream& createServer(Synchronizeable& sync, unsigned int port); 42 void shutdownConnection(); 41 43 42 private: 43 std::list<BaseObject*>* netStreamList; // list with refs to all network streams 44 std::list<BaseObject*>* syncList; // list of synchronizeables 44 void connectSynchronizeable(Synchronizeable& sync); 45 46 void synchronize(); 47 48 private: 49 NetworkManager(); 50 51 52 private: 53 std::list<BaseObject*>* netStreamList; // list with refs to all network streams 54 std::list<BaseObject*>* syncList; // list of synchronizeables 55 static NetworkManager* singletonRef; //!< Pointer to the only instance of this Class 56 NetworkStream* tmpStream; //!< FIXME: this is only for testing purposes 45 57 46 58 }; -
branches/network/src/lib/network/network_socket.cc
r5822 r5829 313 313 314 314 while (!tempsocket && !self->terminateThread) 315 { 315 316 tempsocket = SDLNet_TCP_Accept(self->tcpSocket); 317 SDL_Delay(_MSECONDS_SLEEP_LISTEN); 318 } 316 319 317 320 SDL_mutexP(self->socketMutex); -
branches/network/src/lib/network/network_socket.h
r5822 r5829 18 18 //sleep if outgoing buffer is empty 19 19 #define _MSECONDS_SLEEP_EMPTY_BUFFER 10 20 //sleep when waiting for connections 21 #define _MSECONDS_SLEEP_LISTEN 100 20 22 21 23 /* contains memmove and memcpy */ -
branches/network/src/lib/network/network_stream.cc
r5822 r5829 40 40 41 41 NetworkStream::NetworkStream() 42 : DataStream()42 : DataStream() 43 43 { 44 44 this->init(); … … 50 50 } 51 51 52 NetworkStream::NetworkStream(IPaddress& address, NodeType type) 53 { 54 this->init(); 55 this->networkSocket = new NetworkSocket(address); 56 this->networkProtocol = new NetworkProtocol(); 57 this->synchronizeables = NULL; 58 this->connectionMonitor = new ConnectionMonitor(); 59 } 60 61 62 NetworkStream::NetworkStream(unsigned int port, NodeType type) 63 { 64 this->init(); 65 this->networkSocket = new NetworkSocket(); 66 this->networkSocket->listen(port); 67 this->networkProtocol = new NetworkProtocol(); 68 this->synchronizeables = NULL; 69 this->connectionMonitor = new ConnectionMonitor(); 70 } 71 52 72 53 73 NetworkStream::NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type) 54 : DataStream()74 : DataStream() 55 75 { 56 76 this->init(); … … 59 79 this->synchronizeables = &sync; 60 80 this->connectionMonitor = new ConnectionMonitor(); 81 this->bActive = true; 61 82 } 62 83 63 84 64 85 NetworkStream::NetworkStream(unsigned int port, Synchronizeable& sync, NodeType type) 65 : DataStream()86 : DataStream() 66 87 { 67 88 this->init(); … … 71 92 this->synchronizeables = &sync; 72 93 this->connectionMonitor = new ConnectionMonitor(); 94 this->bActive = true; 73 95 } 74 96 … … 79 101 this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); 80 102 this->state = NET_REC_HEADER; 103 this->bActive = false; 81 104 } 82 105 … … 85 108 { 86 109 87 networkSocket->disconnectServer();110 networkSocket->disconnectServer(); 88 111 89 if( this->networkSocket)90 delete networkSocket;112 if( this->networkSocket) 113 delete networkSocket; 91 114 92 delete connectionMonitor;93 delete networkProtocol;115 delete connectionMonitor; 116 delete networkProtocol; 94 117 } 118 119 120 void NetworkStream::connectSynchronizeable(Synchronizeable& sync) 121 { 122 this->synchronizeables = &sync; 123 if( this->networkSocket != NULL) 124 this->bActive = true; 125 } 126 95 127 96 128 void NetworkStream::processData() … … 102 134 PRINT(0)("============= DOWNSTREAM:===============\n"); 103 135 /* first of all read the synchronizeable's data: */ 136 //if(this->isServer()) 104 137 dataLength = this->synchronizeables->readBytes((byte*)downBuffer); 105 138 106 /* send the received data to connectionMonitor */ 107 this->connectionMonitor->processPacket((byte*)downBuffer, dataLength); 139 if( dataLength > 0) 140 { 141 /* send the received data to connectionMonitor */ 142 this->connectionMonitor->processPacket((byte*)downBuffer, dataLength); 108 143 109 dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE,110 *(this->synchronizeables), 12/* some random number (no real id)*/);144 dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, 145 *(this->synchronizeables), 12/* some random number (no real id)*/); 111 146 112 /* pass the data to the network socket */ 113 dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength); 114 /* check if there was an error */ 115 if( dataLength == -1) { PRINTF(0)("Error in writing data to the NetworkSocket\n");} 116 147 /* pass the data to the network socket */ 148 dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength); 149 /* check if there was an error */ 150 if( dataLength == -1) 151 { 152 PRINTF(0)("Error in writing data to the NetworkSocket\n"); 153 } 154 } 117 155 118 156 … … 147 185 this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length); 148 186 /* now pass the data to the sync object */ 187 //if(!this->isServer()) 149 188 this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length); 150 189 -
branches/network/src/lib/network/network_stream.h
r5822 r5829 28 28 { 29 29 30 public: 31 NetworkStream(); 32 NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type); 33 NetworkStream(unsigned int port, Synchronizeable& sync, NodeType type); 34 ~NetworkStream(); 30 public: 31 NetworkStream(); 32 NetworkStream(IPaddress& address, NodeType type); 33 NetworkStream(unsigned int port, NodeType type); 35 34 36 void init(); 35 NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type); 36 NetworkStream(unsigned int port, Synchronizeable& sync, NodeType type); 37 ~NetworkStream(); 38 void init(); 37 39 38 inline bool isServer() { return (this->type == NET_SERVER)? true:false; } 39 virtual void processData(); 40 void connectSynchronizeable(Synchronizeable& sync); 40 41 41 private: 42 NetworkProtocol* networkProtocol; 43 //NetworkSocket* networkSockets; 44 ConnectionMonitor* connectionMonitor; 45 // tList<Synchronizeable>* synchronizeables; 46 Synchronizeable* synchronizeables; 47 NetworkSocket* networkSocket; 48 int type; 49 int state; 50 Header packetHeader; 42 inline bool isServer() const { return (this->type == NET_SERVER)? true:false; } 43 inline bool isActive() const { return this->bActive; } 44 45 virtual void processData(); 46 47 private: 48 NetworkProtocol* networkProtocol; 49 ConnectionMonitor* connectionMonitor; 50 Synchronizeable* synchronizeables; 51 NetworkSocket* networkSocket; 52 int type; 53 int state; 54 Header packetHeader; 55 bool bActive; 51 56 }; 52 57 #endif /* _NETWORK_STREAM */ 53 -
branches/network/src/lib/network/synchronizeable.cc
r5822 r5829 18 18 #include "netdefs.h" 19 19 20 21 /** 22 * default constructor 23 */ 24 Synchronizeable::Synchronizeable() 25 {} 26 20 27 /** 21 28 * default constructor … … 25 32 this->setName(name); 26 33 } 34 27 35 28 36 /** … … 41 49 * read data from NetworkStream 42 50 */ 43 int Synchronizeable::readBytes(byte* data) const51 int Synchronizeable::readBytes(byte* data) 44 52 {} 45 53 -
branches/network/src/lib/network/synchronizeable.h
r5822 r5829 15 15 16 16 Synchronizeable(const char* name); 17 Synchronizeable(); 17 18 ~Synchronizeable(); 18 19 19 20 virtual void writeBytes(const byte* data, int length); 20 virtual int readBytes(byte* data) const;21 virtual int readBytes(byte* data); 21 22 virtual void writeDebug() const; 22 23 virtual void readDebug() const;
Note: See TracChangeset
for help on using the changeset viewer.