[5566] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
[5601] | 12 | main-programmer: claudio |
---|
[5800] | 13 | co-programmer: |
---|
[5566] | 14 | */ |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module |
---|
| 18 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
| 19 | */ |
---|
| 20 | #define DEBUG_MODULE_NETWORK |
---|
| 21 | |
---|
[5747] | 22 | |
---|
[5647] | 23 | #include "base_object.h" |
---|
[5731] | 24 | #include "network_protocol.h" |
---|
[5647] | 25 | #include "network_socket.h" |
---|
| 26 | #include "connection_monitor.h" |
---|
| 27 | #include "synchronizeable.h" |
---|
| 28 | #include "list.h" |
---|
[5649] | 29 | #include "debug.h" |
---|
[5647] | 30 | |
---|
[5566] | 31 | /* include your own header */ |
---|
| 32 | #include "network_stream.h" |
---|
| 33 | |
---|
[5595] | 34 | /* probably unnecessary */ |
---|
[5594] | 35 | using namespace std; |
---|
| 36 | |
---|
[5595] | 37 | |
---|
[5747] | 38 | #define PACKAGE_SIZE 256 |
---|
[5647] | 39 | |
---|
[5747] | 40 | |
---|
[5800] | 41 | NetworkStream::NetworkStream() |
---|
[5996] | 42 | : DataStream() |
---|
[5647] | 43 | { |
---|
| 44 | this->init(); |
---|
[5648] | 45 | /* initialize the references */ |
---|
[5996] | 46 | this->type = NET_CLIENT; |
---|
[5741] | 47 | this->networkProtocol = new NetworkProtocol(); |
---|
[5648] | 48 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[5647] | 49 | } |
---|
| 50 | |
---|
[6007] | 51 | NetworkStream::NetworkStream(IPaddress& address) |
---|
[5996] | 52 | { |
---|
[6007] | 53 | this->type = NET_CLIENT; |
---|
[5996] | 54 | this->init(); |
---|
[6007] | 55 | this->networkSockets.push_back(new NetworkSocket(address)); |
---|
[5996] | 56 | this->networkProtocol = new NetworkProtocol(); |
---|
| 57 | this->connectionMonitor = new ConnectionMonitor(); |
---|
| 58 | } |
---|
[5647] | 59 | |
---|
[5996] | 60 | |
---|
[6007] | 61 | NetworkStream::NetworkStream(unsigned int port) |
---|
[5996] | 62 | { |
---|
[6007] | 63 | this->type = NET_SERVER; |
---|
[5996] | 64 | this->init(); |
---|
[6007] | 65 | this->serverSocket = new ServerSocket(port); |
---|
[5996] | 66 | this->networkProtocol = new NetworkProtocol(); |
---|
| 67 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[6018] | 68 | this->networkSockets.push_back( NULL ); |
---|
| 69 | this->bActive = true; |
---|
[5996] | 70 | } |
---|
| 71 | |
---|
| 72 | |
---|
[5647] | 73 | void NetworkStream::init() |
---|
| 74 | { |
---|
| 75 | /* set the class id for the base object */ |
---|
| 76 | this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); |
---|
[5996] | 77 | this->bActive = false; |
---|
[6007] | 78 | this->serverSocket = NULL; |
---|
[5594] | 79 | } |
---|
| 80 | |
---|
[5647] | 81 | |
---|
[5566] | 82 | NetworkStream::~NetworkStream() |
---|
[5598] | 83 | { |
---|
[6007] | 84 | if ( this->serverSocket ) |
---|
| 85 | { |
---|
| 86 | serverSocket->close(); |
---|
| 87 | delete serverSocket; |
---|
| 88 | } |
---|
[5723] | 89 | |
---|
[6007] | 90 | for (NetworkSocketVector::iterator i = networkSockets.begin(); i!=networkSockets.end(); i++) |
---|
| 91 | { |
---|
| 92 | if ( *i ) |
---|
| 93 | { |
---|
| 94 | (*i)->disconnectServer(); |
---|
[6025] | 95 | (*i)->destroy(); |
---|
[6007] | 96 | } |
---|
| 97 | } |
---|
[5723] | 98 | |
---|
[5996] | 99 | delete connectionMonitor; |
---|
| 100 | delete networkProtocol; |
---|
[5598] | 101 | } |
---|
| 102 | |
---|
[5996] | 103 | |
---|
| 104 | void NetworkStream::connectSynchronizeable(Synchronizeable& sync) |
---|
| 105 | { |
---|
[6007] | 106 | this->synchronizeables.push_back(&sync); |
---|
| 107 | |
---|
| 108 | if( this->networkSockets.size()>0 ) |
---|
[5996] | 109 | this->bActive = true; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | |
---|
[5604] | 113 | void NetworkStream::processData() |
---|
| 114 | { |
---|
[6025] | 115 | //PRINTF(0)("processData()\n"); |
---|
[6018] | 116 | if ( this->type == NET_SERVER ) |
---|
| 117 | this->updateConnectionList(); |
---|
| 118 | |
---|
[6007] | 119 | #if 0 |
---|
[5809] | 120 | int dataLength = 0; |
---|
[5741] | 121 | |
---|
[5650] | 122 | /* DOWNSTREAM */ |
---|
[5804] | 123 | printf("\nSynchronizeable: %s\n", this->synchronizeables->getName()); |
---|
[5800] | 124 | PRINT(0)("============= DOWNSTREAM:===============\n"); |
---|
[5650] | 125 | /* first of all read the synchronizeable's data: */ |
---|
[5996] | 126 | if( this->isServer()) |
---|
| 127 | dataLength = this->synchronizeables->readBytes((byte*)downBuffer); |
---|
[5800] | 128 | |
---|
[5996] | 129 | if( dataLength > 0) |
---|
| 130 | { |
---|
| 131 | /* send the received data to connectionMonitor */ |
---|
| 132 | this->connectionMonitor->processPacket((byte*)downBuffer, dataLength); |
---|
[5730] | 133 | |
---|
[5996] | 134 | dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, |
---|
| 135 | *(this->synchronizeables), 12/* some random number (no real id)*/); |
---|
[5800] | 136 | |
---|
[5996] | 137 | /* pass the data to the network socket */ |
---|
| 138 | // dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength); |
---|
| 139 | /* check if there was an error */ |
---|
| 140 | if( dataLength == -1) |
---|
| 141 | { |
---|
| 142 | PRINTF(0)("Error in writing data to the NetworkSocket\n"); |
---|
| 143 | } |
---|
| 144 | } |
---|
[5741] | 145 | |
---|
| 146 | |
---|
| 147 | /* UPSTREAM */ |
---|
[5809] | 148 | dataLength = 0; |
---|
[5800] | 149 | PRINT(0)("============== UPSTREAM:================\n"); |
---|
[5809] | 150 | /* first of all read the next Orxonox Network Header */ |
---|
| 151 | |
---|
[5810] | 152 | if( this->state == NET_REC_HEADER) |
---|
[5802] | 153 | { |
---|
[5996] | 154 | // dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header)); |
---|
[5810] | 155 | if( dataLength == sizeof(Header)) |
---|
| 156 | { |
---|
| 157 | this->packetHeader = this->networkProtocol->extractHeader((byte*) upBuffer , dataLength); |
---|
| 158 | printf("NetworkStream::processData() - Got Header: Protocol %u, Version: %u, Sender: %u, Receiver: %u, Length: %u\n", |
---|
| 159 | this->packetHeader.protocol, this->packetHeader.version, this->packetHeader.senderID, |
---|
| 160 | this->packetHeader.receiverID, this->packetHeader.length); |
---|
| 161 | /* FIXME: what if it was no this->packetHeader? catch? eg: the protocol identifier, receiver id*/ |
---|
[5730] | 162 | |
---|
[5810] | 163 | this->state = NET_REC_DATA; |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | if( this->state == NET_REC_DATA) |
---|
| 167 | { |
---|
| 168 | /* now read the data */ |
---|
[5996] | 169 | // dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length); |
---|
[5810] | 170 | /* check if the data is available and process it if so */ |
---|
| 171 | if( dataLength == this->packetHeader.length) |
---|
| 172 | { |
---|
[5996] | 173 | printf("NetworkStream::processData() - Got Data: %i bytes\n", dataLength); |
---|
[5810] | 174 | /* send the received data to connectionMonitor */ |
---|
| 175 | this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length); |
---|
| 176 | /* now pass the data to the sync object */ |
---|
[5996] | 177 | if( !this->isServer()) |
---|
| 178 | this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length); |
---|
[5730] | 179 | |
---|
[5810] | 180 | this->state = NET_REC_HEADER; |
---|
| 181 | } |
---|
[5802] | 182 | } |
---|
[5800] | 183 | |
---|
[6007] | 184 | #endif |
---|
[5604] | 185 | } |
---|
[6018] | 186 | |
---|
| 187 | void NetworkStream::updateConnectionList( ) |
---|
| 188 | { |
---|
| 189 | //check for new connections |
---|
[6025] | 190 | |
---|
[6018] | 191 | NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket(); |
---|
| 192 | |
---|
| 193 | if ( tempNetworkSocket ) |
---|
| 194 | { |
---|
| 195 | int clientId; |
---|
| 196 | if ( freeSocketSlots.size() >0 ) |
---|
| 197 | { |
---|
| 198 | clientId = freeSocketSlots.back(); |
---|
| 199 | freeSocketSlots.pop_back(); |
---|
| 200 | networkSockets[clientId] = tempNetworkSocket; |
---|
| 201 | } else |
---|
| 202 | { |
---|
[6025] | 203 | clientId = networkSockets.size(); |
---|
[6018] | 204 | networkSockets.push_back(tempNetworkSocket); |
---|
| 205 | } |
---|
| 206 | |
---|
[6025] | 207 | PRINTF(0)("New Client: %d\n", clientId); |
---|
[6018] | 208 | //TODO: start handshake |
---|
| 209 | //new Handshake(true, clientId); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | |
---|
| 213 | //check if connections are ok else remove them |
---|
| 214 | for ( int i = 1; i<networkSockets.size(); i++) |
---|
| 215 | { |
---|
| 216 | if ( networkSockets[i] && !networkSockets[i]->isOk() ) |
---|
| 217 | { |
---|
| 218 | //TODO: tell EntityManager that this player left the game |
---|
[6025] | 219 | PRINTF(0)("Client is gone: %d\n", i); |
---|
[6018] | 220 | |
---|
[6025] | 221 | //delete networkSockets[i]; |
---|
| 222 | networkSockets[i]->destroy(); |
---|
[6018] | 223 | networkSockets[i] = NULL; |
---|
| 224 | |
---|
| 225 | if ( i == networkSockets.size()-1 ) |
---|
| 226 | { |
---|
| 227 | networkSockets.pop_back(); |
---|
| 228 | } |
---|
| 229 | else |
---|
| 230 | { |
---|
| 231 | freeSocketSlots.push_back(i); |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | |
---|
[6025] | 236 | |
---|
[6018] | 237 | } |
---|