Changeset 5996 in orxonox.OLD for trunk/src/lib/network
- Timestamp:
- Dec 9, 2005, 12:31:01 PM (19 years ago)
- Location:
- trunk/src/lib/network
- Files:
-
- 9 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/network/Makefile.am
r5822 r5996 10 10 network_stream.cc \ 11 11 data_stream.cc \ 12 network_protocol.cc 12 network_protocol.cc \ 13 server_socket.cc 13 14 14 15 … … 20 21 network_stream.h \ 21 22 data_stream.h \ 22 network_protocol.h 23 network_protocol.h \ 24 server_socket.h 23 25 -
trunk/src/lib/network/network_manager.cc
r5885 r5996 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 /** … … 163 159 std::list<BaseObject*>::const_iterator stream; 164 160 for (stream = this->netStreamList->begin(); stream != this->netStreamList->end(); ++stream) 165 static_cast<NetworkStream*>(*stream)->processData(); 161 if( static_cast<NetworkStream*>(*stream)->isActive()) 162 static_cast<NetworkStream*>(*stream)->processData(); 163 166 164 } 167 165 -
trunk/src/lib/network/network_manager.h
r5885 r5996 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(); 43 41 44 42 45 private: 43 const std::list<BaseObject*>* netStreamList; // list with refs to all network streams 44 const std::list<BaseObject*>* syncList; // list of synchronizeables 46 void connectSynchronizeable(Synchronizeable& sync); 47 void synchronize(); 48 49 50 private: 51 NetworkManager(); 52 53 54 private: 55 const std::list<BaseObject*>* netStreamList; // list with refs to all network streams 56 const std::list<BaseObject*>* syncList; // list of synchronizeables 57 static NetworkManager* singletonRef; //!< Pointer to the only instance of this Class 58 NetworkStream* tmpStream; //!< FIXME: this is only for testing purposes 45 59 46 60 }; -
trunk/src/lib/network/network_socket.cc
r5822 r5996 47 47 48 48 49 NetworkSocket::NetworkSocket( TCPsocket sock ) 50 { 51 this->init(); 52 this->tcpSocket = sock; 53 54 SDL_CreateThread(thread_read, (void*)this); 55 SDL_CreateThread(thread_write, (void*)this); 56 } 57 49 58 void NetworkSocket::init() 50 59 { … … 75 84 } 76 85 86 87 77 88 /** 78 89 * Default destructor … … 85 96 PRINTF(5)("SDL_net shutdown\n"); 86 97 87 _isListening = false;88 89 98 SDL_DestroyMutex(incomingBufferMutex); 90 99 SDL_DestroyMutex(outgoingBufferMutex); … … 113 122 } 114 123 115 _isListening = false;116 117 124 SDL_CreateThread(thread_read, (void*)this); 118 125 SDL_CreateThread(thread_write, (void*)this); 119 126 } 120 127 121 /**122 * Tells the NetworkSocket to listen on a specific port for incoming connections.123 * NetworkSocket::writeBytes(...) will have no effect until there is a valuable connection.124 * @param port125 */126 void NetworkSocket::listen(unsigned int port)127 {128 _isListening = true;129 //check if not already connected or listening130 if (tcpSocket)131 {132 PRINTF(1)("NetworkSocket::listen: tcpSocket!=NULL! maybe you already called listen or connectToServer or did not call disconnectServer()!\n");133 _isListening = false;134 return;135 }136 137 IPaddress ip;138 139 if (SDLNet_ResolveHost(&ip, NULL, port)==-1)140 {141 PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError());142 _isListening = false;143 return;144 }145 146 tcpSocket = SDLNet_TCP_Open(&ip);147 148 if (!tcpSocket)149 {150 PRINTF(1)("SDLNet_TCP_Open: %s\n", SDLNet_GetError());151 _isListening = false;152 return;153 }154 155 SDL_CreateThread(thread_listen, (void*)this);156 SDL_CreateThread(thread_write, (void*)this);157 }158 128 159 129 /** … … 184 154 PRINTF(0)("NetworkSocket::writeBytes()\n"); 185 155 #ifdef _USE_OUTGOING_BUFFER 186 187 //printf("length=%d, bufsize=%d\n", length, _OUTGOING_BUFFER_SIZE);188 // if (length>_OUTGOING_BUFFER_SIZE)189 // {190 // int res = 0;191 // int n = length / _OUTGOING_BUFFER_SIZE;192 // if (length % _OUTGOING_BUFFER_SIZE != 0)193 // n++;194 // // printf("n=%d\n", n);195 // SDL_Delay(500);196 // for (int i = 0; i<n; i++)197 // {198 // // printf("i=%d\n", i);199 // if (i==n-1)200 // {201 // res += writeBytes(data + i*_OUTGOING_BUFFER_SIZE, length-i*_OUTGOING_BUFFER_SIZE);202 // // printf("res = %d\n", res);203 // }204 // else205 // {206 // res += writeBytes(data + i*_OUTGOING_BUFFER_SIZE, _OUTGOING_BUFFER_SIZE);207 // // printf("res = %d\n", res);208 // }209 // }210 // return res;211 // }212 156 213 157 #define min(a,b) (a<b)?a:b … … 256 200 int NetworkSocket::readBytes(byte * data, int length) 257 201 { 202 PRINTF(0)("NetworkSocket::readBytes()\n"); 258 203 if (data==NULL) 259 204 return 0; … … 299 244 } 300 245 301 /**302 * used to create a thread to listen303 * will call thrad_read when established connection304 * @param data: pointer to NetwortSocket305 */306 int NetworkSocket::thread_listen( void * data )307 {308 NetworkSocket * self = (NetworkSocket*)data;309 self->_isListening = true;310 TCPsocket tempsocket;311 312 tempsocket = SDLNet_TCP_Accept(self->tcpSocket);313 314 while (!tempsocket && !self->terminateThread)315 tempsocket = SDLNet_TCP_Accept(self->tcpSocket);316 317 SDL_mutexP(self->socketMutex);318 SDLNet_TCP_Close(self->tcpSocket);319 self->tcpSocket = NULL;320 321 if (!tempsocket)322 {323 printf("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());324 //printf("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());325 SDL_mutexV(self->socketMutex);326 self->_isListening = false;327 return -1;328 }329 330 self->tcpSocket = tempsocket;331 332 SDL_mutexV(self->socketMutex);333 334 self->_isListening = false;335 return thread_read(data);336 }337 246 338 247 /** … … 354 263 355 264 //if buffer is full 356 if (nbytestoread<=0 || self->_isListening)265 if (nbytestoread<=0) 357 266 { 358 267 SDL_Delay(_MSECONDS_SLEEP_FULL_BUFFER); … … 405 314 406 315 //if buffer is full 407 if (nbytestowrite<=0 || self->_isListening)316 if (nbytestowrite<=0) 408 317 { 409 318 SDL_Delay(_MSECONDS_SLEEP_EMPTY_BUFFER); … … 442 351 } 443 352 353 bool NetworkSocket::writePacket( byte * data, int length ) 354 { 355 PRINTF(0)("NetworkSocket::writePacket()\n"); 356 if (length>255) 357 { 358 PRINTF(1)("Packet length > 255!\n"); 359 return false; 360 } 361 362 byte blen = length; 363 364 writeBytes(&blen, 1); 365 writeBytes(data, length); 366 } 367 368 int NetworkSocket::readPacket( byte * data, int maxLength ) 369 { 370 PRINTF(0)("NetworkSocket::readPacket()\n"); 371 if (incomingBufferLength<1) 372 { 373 return 0; 374 } 375 376 byte blen = incomingBuffer[0]; 377 378 if (blen>maxLength) 379 { 380 PRINTF(1)("Buffersize is too small (%d) for packet (%d)\n", maxLength, blen); 381 return 0; 382 } 383 384 if (blen>incomingBufferLength) 385 { 386 return 0; 387 } 388 389 readBytes(&blen, 1); 390 int res = readBytes(data, blen); 391 392 if (res!=blen) 393 return -1; 394 else 395 return blen; 396 397 } 398 399 -
trunk/src/lib/network/network_socket.h
r5822 r5996 18 18 //sleep if outgoing buffer is empty 19 19 #define _MSECONDS_SLEEP_EMPTY_BUFFER 10 20 20 21 21 22 /* contains memmove and memcpy */ … … 62 63 bool terminateThread; 63 64 64 static int thread_listen(void * data);65 65 static int thread_read(void * data); 66 66 #ifdef _USE_OUTGOING_BUFFER … … 68 68 #endif 69 69 70 bool _isListening; 70 int writeBytes(byte * data, int length); 71 int readBytes(byte * data, int length); 72 int readBlock(byte * data, int length); 73 74 void init(); 71 75 72 76 public: … … 74 78 NetworkSocket(); 75 79 NetworkSocket(IPaddress ip); 80 NetworkSocket(TCPsocket sock); 76 81 ~NetworkSocket(); 77 82 78 83 void connectToServer(IPaddress ip); 79 void listen(unsigned int port);80 84 void disconnectServer(); 81 int writeBytes(byte * data, int length);82 int readBytes(byte * data, int length);83 int readBlock(byte * data, int length);84 85 85 private: 86 void init();86 bool writePacket(byte * data, int length); 87 int readPacket(byte * data, int maxLength); 87 88 88 89 }; -
trunk/src/lib/network/network_stream.cc
r5822 r5996 40 40 41 41 NetworkStream::NetworkStream() 42 : DataStream()42 : DataStream() 43 43 { 44 44 this->init(); 45 45 /* initialize the references */ 46 this->type = NET_CLIENT; 46 47 this->networkSocket = new NetworkSocket(); 47 48 this->networkProtocol = new NetworkProtocol(); … … 50 51 } 51 52 53 NetworkStream::NetworkStream(IPaddress& address, NodeType type) 54 { 55 this->type = type; 56 this->init(); 57 this->networkSocket = new NetworkSocket(address); 58 this->networkProtocol = new NetworkProtocol(); 59 this->synchronizeables = NULL; 60 this->connectionMonitor = new ConnectionMonitor(); 61 } 62 63 64 NetworkStream::NetworkStream(unsigned int port, NodeType type) 65 { 66 this->type = type; 67 this->init(); 68 this->networkSocket = new NetworkSocket(); 69 // this->networkSocket->listen(port); 70 this->networkProtocol = new NetworkProtocol(); 71 this->synchronizeables = NULL; 72 this->connectionMonitor = new ConnectionMonitor(); 73 } 74 52 75 53 76 NetworkStream::NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type) 54 : DataStream() 55 { 77 : DataStream() 78 { 79 this->type = type; 56 80 this->init(); 57 81 this->networkSocket = new NetworkSocket(address); … … 59 83 this->synchronizeables = &sync; 60 84 this->connectionMonitor = new ConnectionMonitor(); 85 this->bActive = true; 61 86 } 62 87 63 88 64 89 NetworkStream::NetworkStream(unsigned int port, Synchronizeable& sync, NodeType type) 65 : DataStream() 66 { 90 : DataStream() 91 { 92 this->type = type; 67 93 this->init(); 68 94 this->networkSocket = new NetworkSocket(); 69 this->networkSocket->listen(port);95 // this->networkSocket->listen(port); 70 96 this->networkProtocol = new NetworkProtocol(); 71 97 this->synchronizeables = &sync; 72 98 this->connectionMonitor = new ConnectionMonitor(); 99 this->bActive = true; 73 100 } 74 101 … … 79 106 this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); 80 107 this->state = NET_REC_HEADER; 108 this->bActive = false; 81 109 } 82 110 … … 85 113 { 86 114 87 networkSocket->disconnectServer(); 88 89 if( this->networkSocket) 90 delete networkSocket; 91 92 delete connectionMonitor; 93 delete networkProtocol; 94 } 115 networkSocket->disconnectServer(); 116 117 if( this->networkSocket) 118 delete networkSocket; 119 120 delete connectionMonitor; 121 delete networkProtocol; 122 } 123 124 125 void NetworkStream::connectSynchronizeable(Synchronizeable& sync) 126 { 127 this->synchronizeables = &sync; 128 if( this->networkSocket != NULL) 129 this->bActive = true; 130 } 131 95 132 96 133 void NetworkStream::processData() … … 102 139 PRINT(0)("============= DOWNSTREAM:===============\n"); 103 140 /* first of all read the synchronizeable's data: */ 104 dataLength = this->synchronizeables->readBytes((byte*)downBuffer); 105 106 /* send the received data to connectionMonitor */ 107 this->connectionMonitor->processPacket((byte*)downBuffer, dataLength); 108 109 dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, 110 *(this->synchronizeables), 12/* some random number (no real id)*/); 111 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 141 if( this->isServer()) 142 dataLength = this->synchronizeables->readBytes((byte*)downBuffer); 143 144 if( dataLength > 0) 145 { 146 /* send the received data to connectionMonitor */ 147 this->connectionMonitor->processPacket((byte*)downBuffer, dataLength); 148 149 dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, 150 *(this->synchronizeables), 12/* some random number (no real id)*/); 151 152 /* pass the data to the network socket */ 153 // dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength); 154 /* check if there was an error */ 155 if( dataLength == -1) 156 { 157 PRINTF(0)("Error in writing data to the NetworkSocket\n"); 158 } 159 } 117 160 118 161 … … 124 167 if( this->state == NET_REC_HEADER) 125 168 { 126 dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));169 // dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header)); 127 170 if( dataLength == sizeof(Header)) 128 171 { … … 139 182 { 140 183 /* now read the data */ 141 dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);184 // dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length); 142 185 /* check if the data is available and process it if so */ 143 186 if( dataLength == this->packetHeader.length) 144 187 { 145 printf("NetworkStream::processData() - Got Data: \n");188 printf("NetworkStream::processData() - Got Data: %i bytes\n", dataLength); 146 189 /* send the received data to connectionMonitor */ 147 190 this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length); 148 191 /* now pass the data to the sync object */ 149 this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length); 192 if( !this->isServer()) 193 this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length); 150 194 151 195 this->state = NET_REC_HEADER; -
trunk/src/lib/network/network_stream.h
r5822 r5996 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 -
trunk/src/lib/network/synchronizeable.cc
r5822 r5996 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 -
trunk/src/lib/network/synchronizeable.h
r5822 r5996 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.