Changeset 1502 for code/trunk/src/network
- Timestamp:
- Jun 1, 2008, 3:54:20 PM (17 years ago)
- Location:
- code/trunk/src/network
- Files:
-
- 25 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/network/Client.cc
r1360 r1502 85 85 isConnected=false; 86 86 isSynched_=false; 87 gameStateFailure_=false; 87 88 } 88 89 … … 95 96 isConnected=false; 96 97 isSynched_=false; 98 gameStateFailure_=false; 97 99 } 98 100 … … 105 107 isConnected=false; 106 108 isSynched_=false; 109 gameStateFailure_=false; 107 110 } 108 111 … … 120 123 isConnected=client_connection.createConnection(); 121 124 if(isConnected){ 122 COUT(3) << "sending connectrequest" << std::endl;123 if(!client_connection.addPacket(pck_gen.generateConnectRequest()) || !client_connection.sendPackets())124 COUT(1) << "could not create connection" << std::endl;125 // COUT(3) << "sending connectrequest" << std::endl; 126 // if(!client_connection.addPacket(pck_gen.generateConnectRequest()) || !client_connection.sendPackets()) 127 // COUT(1) << "could not send connection request !!!!!!!!!" << std::endl; 125 128 }else 126 COUT(1) << "could not create connection " << std::endl;129 COUT(1) << "could not create connection laber" << std::endl; 127 130 return isConnected; 128 131 } … … 137 140 } 138 141 139 /** 140 * submits a MouseAction to the server 141 * @param x x Coordinate 142 * @param y y Coordinate 143 * @return true/false 144 */ 145 bool Client::sendMouse(double x, double y){ 146 // generate packet and add it to the queue 147 if(!isConnected) 148 return false; 149 if(!client_connection.addPacket(pck_gen.mousem(x, y))) 150 return false; 151 // send packets 152 return client_connection.sendPackets(); 153 } 154 155 /** 156 * submits a Keystrike to the server 157 * @param key_code code to submit 158 * @return true/false 159 */ 160 bool Client::sendKeyboard(char key_code){ 161 // generate packet and add it to queue 162 if(!isConnected) 163 return false; 164 if(!client_connection.addPacket(pck_gen.keystrike(key_code))) 165 return false; 166 // send packets 167 return client_connection.sendPackets(); 168 } 142 143 144 169 145 170 146 /** … … 177 153 if(!isConnected) 178 154 return false; 179 if(client_connection.addPacket(pck_gen.chatMessage( message.c_str() )))180 return client_connection.sendPackets();155 return client_connection.addPacket(pck_gen.chatMessage( message.c_str() )); 156 //return client_connection.sendPackets(); 181 157 // send packets 182 158 return false; 183 }184 185 /**186 * Adds a MouseAction to the PacketQueue187 * @param x x Coordinate188 * @param y y Coordinate189 * @return true/false190 */191 bool Client::addMouse(double x, double y){192 // generate packet and add it to the queue193 if(client_connection.addPacket(pck_gen.mousem(x, y)))194 return true;195 else196 return false;197 }198 199 /**200 * Adds a Keystrike to the PacketQueue201 * @param key_code202 * @return true/false203 */204 bool Client::addKeyboard(char key_code){205 // generate packet and add it to queue206 if(client_connection.addPacket(pck_gen.keystrike(key_code)))207 return true;208 else209 return false;210 }211 212 /**213 * Sends out all the packets queued by addXXX214 */215 bool Client::sendPackets(){216 if(!isConnected)217 return false;218 ENetEvent event;219 // send packets220 client_connection.sendPackets(&event);221 if(event.type==ENET_EVENT_TYPE_NONE)222 return true;223 else224 return false;225 159 } 226 160 … … 243 177 } 244 178 } 245 ENet Packet *packet;179 ENetEvent *event; 246 180 // stop if the packet queue is empty 247 181 while(!(client_connection.queueEmpty())){ 248 packet = client_connection.getPacket(); 249 COUT(5) << "tick packet size " << packet->dataLength << std::endl; 250 elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server) 251 } 252 if(!client_connection.sendPackets()) 253 COUT(3) << "Problem sending packets to server" << std::endl; 254 return; 255 } 256 257 void Client::processGamestate( GameStateCompressed *data, int clientID){ 258 int id = data->id; 259 COUT(5) << "received gamestate id: " << data->id << std::endl; 260 if(gamestate.pushGameState(data)){ 182 event = client_connection.getEvent(); 183 COUT(5) << "tick packet size " << event->packet->dataLength << std::endl; 184 elaborate(event->packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server) 185 } 186 int gameStateID = gamestate.processGameState(); 187 if(gameStateID==GAMESTATEID_INITIAL) 188 if(gameStateFailure_){ 189 if(!client_connection.addPacket(pck_gen.acknowledgement(GAMESTATEID_INITIAL))) 190 COUT(3) << "could not (negatively) ack gamestate" << std::endl; 191 else 192 COUT(4) << "negatively acked a gamestate" << std::endl; 193 } 194 else 195 gameStateFailure_=true; 196 else if(gameStateID!=0){ 197 // ack gamestate and set synched 261 198 if(!isSynched_) 262 199 isSynched_=true; 263 if(!client_connection.addPacket(pck_gen.acknowledgement(id))) 264 return; 265 // we do this at the end of a tick 266 if(!client_connection.sendPackets()) 267 COUT(2) << "Could not send acknowledgment" << std::endl; 268 } 200 gameStateFailure_=false; 201 if(!client_connection.addPacket(pck_gen.acknowledgement(gameStateID))) 202 COUT(3) << "could not ack gamestate" << std::endl; 203 }// otherwise we had no gamestate to load 204 gamestate.cleanup(); 205 /*if(!client_connection.sendPackets()) 206 COUT(3) << "Problem sending packets to server" << std::endl;*/ 207 return; 208 } 209 210 void Client::processGamestate( GameStateCompressed *data, int clientID){ 211 COUT(5) << "received gamestate id: " << data->id << std::endl; 212 gamestate.addGameState(data); 269 213 } 270 214 … … 275 219 id->setNetworkID(clid->clid); 276 220 COUT(4) << "Client: received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl; 221 COUT(4) << "id(classid)->getName " << ID((unsigned int)clid->clid)->getName() << std::endl; 277 222 delete clid; 278 223 return; -
code/trunk/src/network/Client.h
r1293 r1502 75 75 bool closeConnection(); 76 76 77 bool sendMouse(double x, double y);78 bool sendKeyboard(char key_code);79 77 bool sendChat( std::string message ); 80 81 bool addMouse(double x, double y);82 bool addKeyboard(char key_code);83 84 bool sendPackets();85 78 86 79 int getShipID(){return shipID_;} … … 110 103 int clientID_; // this is the id the server gave to us 111 104 int shipID_; 105 bool gameStateFailure_; 112 106 }; 113 107 -
code/trunk/src/network/ClientConnection.cc
r1293 r1502 50 50 { 51 51 //static boost::thread_group network_threads; 52 53 boost::recursive_mutex ClientConnection::enet_mutex_; 52 54 53 55 ClientConnection::ClientConnection(int port, std::string address) { … … 75 77 76 78 77 ENetPacket *ClientConnection::getPacket(ENetAddress &address) {79 /*ENetPacket *ClientConnection::getPacket(ENetAddress &address) { 78 80 if(!buffer.isEmpty()) { 79 81 //std::cout << "###BUFFER IS NOT EMPTY###" << std::endl; … … 88 90 ENetAddress address; //sems that address is not needed 89 91 return getPacket(address); 92 }*/ 93 94 ENetEvent *ClientConnection::getEvent(){ 95 if(!buffer.isEmpty()) 96 return buffer.pop(); 97 else 98 return NULL; 90 99 } 91 100 … … 98 107 //network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this)); 99 108 // wait 10 seconds for the connection to be established 100 return waitEstablished( 10000);109 return waitEstablished(3000); 101 110 } 102 111 … … 117 126 return false; 118 127 } 128 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 119 129 if(enet_peer_send(server, 0, packet)<0) 120 130 return false; … … 123 133 } 124 134 125 bool ClientConnection::sendPackets( ENetEvent *event) {135 bool ClientConnection::sendPackets() { 126 136 if(server==NULL) 127 137 return false; 128 if(enet_host_service(client, event, NETWORK_SEND_WAIT)>=0){ 129 return true;} 130 else 131 return false; 132 } 133 134 bool ClientConnection::sendPackets() { 135 ENetEvent event; 136 if(server==NULL) 137 return false; 138 if(enet_host_service(client, &event, NETWORK_SEND_WAIT)>=0){ 139 return true;} 140 else 141 return false; 138 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 139 enet_host_flush(client); 140 lock.unlock(); 141 return true; 142 142 } 143 143 144 144 void ClientConnection::receiverThread() { 145 145 // what about some error-handling here ? 146 enet_initialize();147 146 atexit(enet_deinitialize); 148 ENetEvent event; 149 client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0); 150 if(client==NULL) 147 ENetEvent *event; 148 { 149 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 150 enet_initialize(); 151 client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0); 152 lock.unlock(); 153 } 154 if(client==NULL) { 155 COUT(2) << "ClientConnection: could not create client host" << std::endl; 151 156 // add some error handling here ========================== 152 157 quit=true; 158 } 153 159 //connect to the server 154 160 if(!establishConnection()){ 161 COUT(2) << "clientConn: receiver thread: could not establishConnection" << std::endl; 155 162 quit=true; 156 163 return; 157 164 } 165 event = new ENetEvent; 158 166 //main loop 159 167 while(!quit){ 160 168 //std::cout << "connection loop" << std::endl; 161 if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)<0){ 162 // we should never reach this point 163 quit=true; 164 // add some error handling here ======================== 169 { 170 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 171 if(enet_host_service(client, event, NETWORK_CLIENT_TIMEOUT)<0){ 172 // we should never reach this point 173 quit=true; 174 continue; 175 // add some error handling here ======================== 176 } 177 lock.unlock(); 165 178 } 166 switch(event .type){179 switch(event->type){ 167 180 // log handling ================ 168 181 case ENET_EVENT_TYPE_CONNECT: 182 break; 169 183 case ENET_EVENT_TYPE_RECEIVE: 170 184 COUT(5) << "Cl.Con: receiver-Thread while loop: got new packet" << std::endl; 171 if ( !processData( &event) ) COUT(2) << "Current packet was not pushed to packetBuffer -> ev ongoing SegFault" << std::endl;185 if ( !processData(event) ) COUT(2) << "Current packet was not pushed to packetBuffer -> ev ongoing SegFault" << std::endl; 172 186 COUT(5) << "Cl.Con: processed Data in receiver-thread while loop" << std::endl; 187 event = new ENetEvent; 173 188 break; 174 189 case ENET_EVENT_TYPE_DISCONNECT: … … 178 193 break; 179 194 case ENET_EVENT_TYPE_NONE: 180 continue; 195 //receiverThread_->yield(); 196 usleep(1000); 197 break; 181 198 } 182 //receiverThread_->yield();183 199 } 184 200 // now disconnect … … 186 202 if(!disconnectConnection()) 187 203 // if disconnecting failed destroy conn. 204 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 188 205 enet_peer_reset(server); 189 206 return; … … 192 209 bool ClientConnection::disconnectConnection() { 193 210 ENetEvent event; 211 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 194 212 enet_peer_disconnect(server, 0); 195 213 while(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT) > 0){ … … 212 230 ENetEvent event; 213 231 // connect to peer (server is type ENetPeer*) 232 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 214 233 server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS); 215 if(server==NULL) 234 if(server==NULL) { 235 COUT(2) << "ClientConnection: server == NULL" << std::endl; 216 236 // error handling 217 237 return false; 238 } 218 239 // handshake 219 if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)> 0 && event.type == ENET_EVENT_TYPE_CONNECT){240 if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)>=0 && event.type == ENET_EVENT_TYPE_CONNECT){ 220 241 established=true; 221 242 return true; 222 243 } 223 else 224 return false; 244 else { 245 COUT(2) << "ClientConnection: enet_host_service < 0 or event.type != ENET_EVENT_TYPE_CONNECT # EVENT:" << event.type << std::endl; 246 return false; 247 } 225 248 } 226 249 -
code/trunk/src/network/ClientConnection.h
r1293 r1502 54 54 #define NETWORK_PORT 55556 55 55 #define NETWORK_CLIENT_MAX_CONNECTIONS 5 56 #define NETWORK_CLIENT_TIMEOUT 100 57 #define NETWORK_SEND_WAIT 5 56 #define NETWORK_CLIENT_TIMEOUT 1 58 57 #define NETWORK_CLIENT_CHANNELS 2 59 58 … … 63 62 ClientConnection(int port, std::string address); 64 63 ClientConnection(int port, const char* address); 65 ENetPacket *getPacket(ENetAddress &address); // thread1 66 ENetPacket *getPacket(); // thread1 64 //ENetPacket *getPacket(ENetAddress &address); // thread1 65 //ENetPacket *getPacket(); // thread1 66 ENetEvent *getEvent(); 67 67 // check wheter the packet queue is empty 68 68 bool queueEmpty(); … … 75 75 bool sendPackets(); 76 76 // send out all queued packets and save result in event 77 bool sendPackets(ENetEvent *event);77 //bool sendPackets(ENetEvent *event); 78 78 bool waitEstablished(int milisec); 79 79 bool isConnected(){return established;} … … 95 95 ENetPeer *server; 96 96 boost::thread *receiverThread_; 97 98 static boost::recursive_mutex enet_mutex_; 97 99 }; 98 100 -
code/trunk/src/network/ClientInformation.cc
r1360 r1502 45 45 namespace network 46 46 { 47 boost::recursive_mutex ClientInformation::mutex_;48 47 49 48 ClientInformation::ClientInformation() { … … 84 83 85 84 ClientInformation::~ClientInformation() { 86 boost::recursive_mutex::scoped_lock lock(mutex_); 87 if(preve!=0) 88 preve->setNext(this->nexte); 89 if(nexte!=0) 90 nexte->setPrev(this->preve); 85 if(prev()!=0) 86 prev()->setNext(this->next()); 87 if(next()!=0) 88 next()->setPrev(this->prev()); 91 89 } 92 90 93 91 ClientInformation *ClientInformation::next() { 94 boost::recursive_mutex::scoped_lock lock(mutex_);95 92 if(this!=0) 96 93 return this->nexte; … … 99 96 } 100 97 ClientInformation *ClientInformation::prev() { 101 boost::recursive_mutex::scoped_lock lock(mutex_);102 98 if(this!=0) 103 99 return this->preve; … … 107 103 108 104 bool ClientInformation::setPrev(ClientInformation *prev) { 109 boost::recursive_mutex::scoped_lock lock(mutex_);110 105 if(!head_) 111 106 this->preve = prev; … … 116 111 117 112 bool ClientInformation::setNext(ClientInformation *next) { 118 boost::recursive_mutex::scoped_lock lock(mutex_);119 113 this->nexte = next; 120 114 return true; … … 122 116 123 117 ClientInformation *ClientInformation::insertAfter(ClientInformation *ins) { 124 boost::recursive_mutex::scoped_lock lock(mutex_); 125 this->nexte->setPrev(ins); 126 ins->setNext(this->nexte); 118 this->next()->setPrev(ins); 119 ins->setNext(this->next()); 127 120 ins->setPrev(this); 128 121 this->nexte = ins; … … 131 124 132 125 ClientInformation *ClientInformation::insertBefore(ClientInformation *ins){ 133 boost::recursive_mutex::scoped_lock lock(mutex_);134 126 if(!this) 135 127 return NULL; 136 128 this->prev()->setNext(ins); 137 ins->setPrev(this->prev e);129 ins->setPrev(this->prev()); 138 130 this->preve=ins; 139 131 ins->setNext(this); … … 142 134 143 135 void ClientInformation::setID(int clientID){ 144 boost::recursive_mutex::scoped_lock lock(mutex_); 136 if(!this) 137 return; 145 138 clientID_ = clientID; 146 139 } 147 140 148 141 bool ClientInformation::setPeer(ENetPeer *peer){ 149 boost::recursive_mutex::scoped_lock lock(mutex_);150 142 if(!this) 151 143 return false; … … 154 146 } 155 147 156 bool ClientInformation::setGamestateID(int id){ 157 boost::recursive_mutex::scoped_lock lock(mutex_); 148 bool ClientInformation::setGameStateID(int id){ 158 149 if(!this) 159 150 return false; … … 163 154 164 155 bool ClientInformation::setPartialGamestateID(int id){ 165 boost::recursive_mutex::scoped_lock lock(mutex_);166 156 if(!this) 167 157 return false; … … 171 161 172 162 int ClientInformation::getID() { 173 boost::recursive_mutex::scoped_lock lock(mutex_);174 163 if(!this) 175 164 return CLIENTID_UNKNOWN; … … 179 168 180 169 ENetPeer *ClientInformation::getPeer() { 181 boost::recursive_mutex::scoped_lock lock(mutex_);182 170 if(this) 183 171 return peer_; … … 187 175 188 176 bool ClientInformation::getHead(){ 189 boost::recursive_mutex::scoped_lock lock(mutex_);190 177 return head_; 191 178 } 192 179 193 180 void ClientInformation::setHead(bool h){ 194 boost::recursive_mutex::scoped_lock lock(mutex_);195 181 head_=h; 196 182 } 197 183 198 184 int ClientInformation::getFailures(){ 199 boost::recursive_mutex::scoped_lock lock(mutex_);200 185 return failures_; 201 186 } 202 187 void ClientInformation::addFailure(){ 203 boost::recursive_mutex::scoped_lock lock(mutex_);204 188 failures_++; 205 189 } 206 190 void ClientInformation::resetFailures(){ 207 boost::recursive_mutex::scoped_lock lock(mutex_);208 191 failures_=0; 209 192 } 210 193 211 194 int ClientInformation::getGamestateID() { 212 boost::recursive_mutex::scoped_lock lock(mutex_);213 195 if(this) 214 196 return gamestateID_; … … 218 200 219 201 int ClientInformation::getPartialGamestateID() { 220 boost::recursive_mutex::scoped_lock lock(mutex_);221 202 if(this) 222 203 return partialGamestateID_; … … 226 207 227 208 ClientInformation *ClientInformation::insertBack(ClientInformation *ins) { 228 boost::recursive_mutex::scoped_lock lock(mutex_);229 209 if(!this) 230 210 return NULL; … … 239 219 240 220 bool ClientInformation::removeClient(int clientID) { 241 boost::recursive_mutex::scoped_lock lock(mutex_);242 221 if(!this || clientID==CLIENTID_UNKNOWN) 243 222 return false; … … 252 231 253 232 bool ClientInformation::removeClient(ENetPeer *peer) { 254 boost::recursive_mutex::scoped_lock lock(mutex_);255 233 if(!this || !peer) 256 234 return false; … … 275 253 */ 276 254 ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards) { 277 boost::recursive_mutex::scoped_lock lock(mutex_);278 255 ClientInformation *temp = this; 279 256 if (temp->head_) … … 293 270 */ 294 271 ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards) { 295 boost::recursive_mutex::scoped_lock lock(mutex_);296 272 ClientInformation *temp = this; 297 273 while(temp!=0){ … … 309 285 310 286 bool ClientInformation::setSynched(bool s) { 311 boost::recursive_mutex::scoped_lock lock(mutex_);312 287 if(!this) 313 288 return false; … … 317 292 318 293 bool ClientInformation::getSynched() { 319 boost::recursive_mutex::scoped_lock lock(mutex_);320 294 if(this) 321 295 return synched_; -
code/trunk/src/network/ClientInformation.h
r1360 r1502 49 49 #define CLIENTID_UNKNOWN -2 50 50 51 // WATCH OUT: THE CLIENTINFORMATION LIST IS NOT THREADSAFE ANYMORE 52 51 53 namespace network 52 54 { … … 69 71 void setID(int clientID); 70 72 bool setPeer(ENetPeer *peer); 71 bool setGame stateID(int id);73 bool setGameStateID(int id); 72 74 bool setPartialGamestateID(int id); 73 75 inline void setShipID(int id){ShipID_=id;} … … 114 116 bool head_; 115 117 unsigned short failures_; 116 static boost::recursive_mutex mutex_;117 118 118 119 }; -
code/trunk/src/network/ConnectionManager.cc
r1360 r1502 44 44 #include "core/CoreIncludes.h" 45 45 #include "core/BaseObject.h" 46 #include "objects/SpaceShip.h" 46 47 #include "util/Math.h" 47 #include " objects/SpaceShip.h"48 #include "util/Sleep.h" 48 49 #include "ClientInformation.h" 49 50 #include "ConnectionManager.h" … … 65 66 66 67 ConnectionManager::ConnectionManager():receiverThread_(0){} 68 boost::recursive_mutex ConnectionManager::enet_mutex_; 67 69 68 70 ConnectionManager::ConnectionManager(ClientInformation *head) : receiverThread_(0) { … … 72 74 head_ = head; 73 75 } 76 77 ConnectionManager::ConnectionManager(ClientInformation *head, int port){ 78 quit=false; 79 bindAddress.host = ENET_HOST_ANY; 80 bindAddress.port = port; 81 head_ = head; 82 } 74 83 75 84 ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head) :receiverThread_(0) { … … 87 96 } 88 97 89 ENetPacket *ConnectionManager::getPacket(ENetAddress &address) {98 /*ENetPacket *ConnectionManager::getPacket(ENetAddress &address) { 90 99 if(!buffer.isEmpty()) 91 100 return buffer.pop(address); 92 101 else 93 102 return NULL; 94 } 103 }*/ 95 104 /** 96 105 This function only pops the first element in PacketBuffer (first in first out) 97 106 used by processQueue in Server.cc 98 107 */ 99 ENetPacket *ConnectionManager::getPacket(int &clientID) {108 /*ENetPacket *ConnectionManager::getPacket(int &clientID) { 100 109 ENetAddress address; 101 110 ENetPacket *packet=getPacket(address); … … 105 114 clientID=temp->getID(); 106 115 return packet; 116 }*/ 117 118 ENetEvent *ConnectionManager::getEvent(){ 119 if(!buffer.isEmpty()) 120 return buffer.pop(); 121 else 122 return NULL; 107 123 } 108 124 … … 129 145 if(!temp) 130 146 return false; 147 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 131 148 if(enet_peer_send(peer, (enet_uint8)temp->getID() , packet)!=0) 132 149 return false; … … 136 153 bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) { 137 154 ClientInformation *temp = head_->findClient(clientID); 138 if(!temp) 139 return false; 140 if(enet_peer_send(temp->getPeer(), (enet_uint8)clientID, packet)!=0) 141 return false; 155 if(!temp){ 156 COUT(3) << "C.Man: addPacket findClient failed" << std::endl; 157 return false; 158 } 159 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 160 if(enet_peer_send(temp->getPeer(), 0, packet)!=0){ 161 COUT(3) << "C.Man: addPacket enet_peer_send failed" << std::endl; 162 return false; 163 } 142 164 return true; 143 165 } 144 166 145 167 bool ConnectionManager::addPacketAll(ENetPacket *packet) { 168 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 146 169 for(ClientInformation *i=head_->next(); i!=0; i=i->next()){ 147 170 if(enet_peer_send(i->getPeer(), (enet_uint8)i->getID(), packet)!=0) … … 151 174 } 152 175 153 bool ConnectionManager::sendPackets(ENetEvent *event) { 176 // we actually dont need that function, because host_service does that for us 177 bool ConnectionManager::sendPackets() { 154 178 if(server==NULL) 155 179 return false; 156 if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0) 157 return true; 158 else 159 return false; 160 } 161 162 bool ConnectionManager::sendPackets() { 163 ENetEvent event; 164 if(server==NULL) 165 return false; 166 if(enet_host_service(server, &event, NETWORK_SEND_WAIT)>=0) 167 return true; 168 else 169 return false; 180 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 181 enet_host_flush(server); 182 lock.unlock(); 183 return true; 170 184 } 171 185 172 186 void ConnectionManager::receiverThread() { 173 187 // what about some error-handling here ? 174 enet_initialize();188 ENetEvent *event; 175 189 atexit(enet_deinitialize); 176 ENetEvent *event = new ENetEvent; 177 server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0); 190 { //scope of the mutex 191 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 192 enet_initialize(); 193 server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0); 194 lock.unlock(); 195 } 178 196 if(server==NULL){ 179 197 // add some error handling here ========================== … … 182 200 } 183 201 202 event = new ENetEvent; 184 203 while(!quit){ 185 if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){ 186 // we should never reach this point 187 quit=true; 188 // add some error handling here ======================== 204 { //mutex scope 205 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 206 if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){ 207 // we should never reach this point 208 quit=true; 209 continue; 210 // add some error handling here ======================== 211 } 212 lock.unlock(); 189 213 } 190 214 switch(event->type){ 191 215 // log handling ================ 192 216 case ENET_EVENT_TYPE_CONNECT: 193 addClient(event); 217 COUT(3) << "adding event_type_connect to queue" << std::endl; 218 case ENET_EVENT_TYPE_DISCONNECT: 219 //addClient(event); 194 220 //this is a workaround to ensure thread safety 195 COUT(5) << "Con.Man: connection event has occured" << std::endl;196 break;221 //COUT(5) << "Con.Man: connection event has occured" << std::endl; 222 //break; 197 223 case ENET_EVENT_TYPE_RECEIVE: 198 224 //std::cout << "received data" << std::endl; 199 225 COUT(5) << "Con.Man: receive event has occured" << std::endl; 200 226 // only add, if client has connected yet and not been disconnected 201 if(head_->findClient(&event->peer->address))227 //if(head_->findClient(&event->peer->address)) 202 228 processData(event); 203 else 204 COUT(3) << "received a packet from a client we don't know" << std::endl; 229 event = new ENetEvent; 230 // else 231 // COUT(3) << "received a packet from a client we don't know" << std::endl; 205 232 break; 206 case ENET_EVENT_TYPE_DISCONNECT: 207 clientDisconnect(event->peer); 233 //case ENET_EVENT_TYPE_DISCONNECT: 234 //clientDisconnect(event->peer); 235 //break; 236 case ENET_EVENT_TYPE_NONE: 237 //receiverThread_->yield(); 238 usleep(1000); 208 239 break; 209 case ENET_EVENT_TYPE_NONE:210 receiverThread_->yield();211 break;212 240 } 213 241 // usleep(100); 214 receiverThread_->yield(); //TODO: find apropriate242 //receiverThread_->yield(); //TODO: find apropriate 215 243 } 216 244 disconnectClients(); 217 245 // if we're finishied, destroy server 218 enet_host_destroy(server); 246 { 247 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 248 enet_host_destroy(server); 249 lock.unlock(); 250 } 219 251 } 220 252 … … 226 258 ClientInformation *temp = head_->next(); 227 259 while(temp!=0){ 228 enet_peer_disconnect(temp->getPeer(), 0); 260 { 261 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 262 enet_peer_disconnect(temp->getPeer(), 0); 263 lock.unlock(); 264 } 229 265 temp = temp->next(); 230 266 } 231 267 //bugfix: might be the reason why server crashes when clients disconnects 232 //temp = temp->next();233 268 temp = head_->next(); 234 while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){ 269 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 270 while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) >= 0){ 235 271 switch (event.type) 236 272 { … … 258 294 } 259 295 260 bool ConnectionManager::clientDisconnect(ENetPeer *peer) { 261 COUT(4) << "removing client from list" << std::endl; 262 return removeClient(head_->findClient(&(peer->address))->getID()); 263 } 264 /** 265 This function adds a client that connects to the clientlist of the server 266 NOTE: if you change this, don't forget to change the test function 267 addClientTest in diffTest.cc since addClient is not good for testing because of syncClassid 268 */ 269 bool ConnectionManager::addClient(ENetEvent *event) { 270 ClientInformation *temp = head_->insertBack(new ClientInformation); 271 if(!temp){ 272 COUT(2) << "Conn.Man. could not add client" << std::endl; 273 return false; 274 } 275 if(temp->prev()->getHead()) { //not good if you use anything else than insertBack 276 temp->prev()->setID(0); //bugfix: not necessary but usefull 277 temp->setID(1); 278 } 279 else 280 temp->setID(temp->prev()->getID()+1); 281 temp->setPeer(event->peer); 282 COUT(3) << "Con.Man: added client id: " << temp->getID() << std::endl; 283 return true; 284 } 296 285 297 286 298 int ConnectionManager::getClientID(ENetPeer peer) { … … 316 328 ++it; 317 329 } 318 sendPackets();330 //sendPackets(); 319 331 COUT(4) << "syncClassid:\tall synchClassID packets have been sent" << std::endl; 320 332 } 321 333 322 bool ConnectionManager::createClient(int clientID){ 323 ClientInformation *temp = head_->findClient(clientID); 324 if(!temp){ 325 COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl; 326 return false; 327 } 328 COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl; 329 syncClassid(temp->getID()); 330 COUT(4) << "creating spaceship for clientid: " << temp->getID() << std::endl; 331 // TODO: this is only a hack, untill we have a possibility to define default player-join actions 332 if(!createShip(temp)) 333 COUT(2) << "Con.Man. could not create ship for clientid: " << clientID << std::endl; 334 else 335 COUT(3) << "created spaceship" << std::endl; 336 temp->setSynched(true); 337 COUT(3) << "sending welcome" << std::endl; 338 sendWelcome(temp->getID(), temp->getShipID(), true); 339 return true; 340 } 341 342 bool ConnectionManager::removeClient(int clientID){ 343 orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start(); 344 ClientInformation *client = head_->findClient(clientID); 345 if(!client) 346 return false; 347 while(it){ 348 if(it->objectID!=client->getShipID()){ 349 ++it; 350 continue; 351 } 352 orxonox::Iterator<orxonox::SpaceShip> temp=it; 353 ++it; 354 delete *temp; 355 return head_->removeClient(clientID); 356 } 357 return false; 358 } 359 360 bool ConnectionManager::createShip(ClientInformation *client){ 361 if(!client) 362 return false; 363 orxonox::Identifier* id = ID("SpaceShip"); 364 if(!id){ 365 COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl; 366 return false; 367 } 368 orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate()); 369 no->setPosition(orxonox::Vector3(0,80,0)); 370 no->setScale(10); 371 no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0)); 372 no->setMesh("assff.mesh"); 373 no->setMaxSpeed(500); 374 no->setMaxSideAndBackSpeed(50); 375 no->setMaxRotation(1.0); 376 no->setTransAcc(200); 377 no->setRotAcc(3.0); 378 no->setTransDamp(75); 379 no->setRotDamp(1.0); 380 no->setCamera("cam_"+client->getID()); 381 no->classID = id->getNetworkID(); 382 no->create(); 383 384 client->setShipID(no->objectID); 385 return true; 386 } 334 387 335 388 336 bool ConnectionManager::removeShip(ClientInformation *client){ … … 399 347 bool ConnectionManager::sendWelcome(int clientID, int shipID, bool allowed){ 400 348 if(addPacket(packet_gen.generateWelcome(clientID, shipID, allowed),clientID)){ 401 sendPackets();349 //sendPackets(); 402 350 return true; 403 351 }else … … 406 354 407 355 void ConnectionManager::disconnectClient(ClientInformation *client){ 408 enet_peer_disconnect(client->getPeer(), 0); 356 { 357 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 358 enet_peer_disconnect(client->getPeer(), 0); 359 lock.unlock(); 360 } 409 361 removeShip(client); 410 362 } … … 418 370 419 371 420 // int ConnectionManager::getNumberOfClients() { 421 // 422 // return clientsShip.size(); 423 // } 424 425 /*void ConnectionManager::addClientsObjectID( int clientID, int objectID ) { 426 COUT(4) << "ship of client: " << clientID << ": " << objectID << " mapped" << std::endl; 427 clientsShip.insert( std::make_pair( clientID, objectID ) ); 372 428 373 } 429 430 int ConnectionManager::getClientsShipID( int clientID ) {431 return clientsShip[clientID];432 }433 434 int ConnectionManager::getObjectsClientID( int objectID ) {435 std::map<int, int>::iterator iter;436 for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) {437 if( iter->second == objectID ) return iter->first;438 }439 return -99;440 }441 442 void ConnectionManager::deleteClientIDReg( int clientID ) {443 clientsShip.erase( clientID );444 }445 446 void ConnectionManager::deleteObjectIDReg( int objectID ) {447 std::map<int, int>::iterator iter = clientsShip.begin();448 for( iter = clientsShip.begin(); iter != clientsShip.end(); iter++ ) {449 if( iter->second == objectID ) break;450 }451 clientsShip.erase( iter->first );452 }*/453 } -
code/trunk/src/network/ConnectionManager.h
r1293 r1502 48 48 #include <enet/enet.h> 49 49 #include <boost/thread/thread.hpp> 50 #include <boost/thread/recursive_mutex.hpp> 50 51 51 52 #include "PacketBuffer.h" … … 61 62 #define NETWORK_PORT 55556 62 63 #define NETWORK_MAX_CONNECTIONS 50 63 #define NETWORK_WAIT_TIMEOUT 5000 64 #define NETWORK_SEND_WAIT 5 64 #define NETWORK_WAIT_TIMEOUT 1 65 65 66 66 struct ClientList{ … … 74 74 ConnectionManager(); 75 75 ConnectionManager(ClientInformation *head); 76 ConnectionManager(ClientInformation *head, int port); 76 77 ConnectionManager(int port, const char *address, ClientInformation *head); 77 78 ConnectionManager(int port, std::string address, ClientInformation *head); 78 ENetPacket *getPacket(ENetAddress &address); // thread1 79 ENetPacket *getPacket(int &clientID); 79 //ENetPacket *getPacket(ENetAddress &address); // thread1 80 //ENetPacket *getPacket(int &clientID); 81 ENetEvent *getEvent(); 80 82 bool queueEmpty(); 81 83 void createListener(); … … 84 86 bool addPacket(ENetPacket *packet, int ID); 85 87 bool addPacketAll(ENetPacket *packet); 86 bool sendPackets(ENetEvent *event);88 // bool sendPackets(ENetEvent *event); 87 89 bool sendPackets(); 88 bool createClient(int clientID);90 //bool createClient(int clientID); 89 91 void disconnectClient(ClientInformation *client); 92 void syncClassid(int clientID); 93 bool sendWelcome(int clientID, int shipID, bool allowed); 90 94 91 95 private: 92 bool clientDisconnect(ENetPeer *peer);93 bool removeClient(int clientID);96 // bool clientDisconnect(ENetPeer *peer); 97 // bool removeClient(int clientID); 94 98 bool processData(ENetEvent *event); 95 bool addClient(ENetEvent *event);99 //bool addClient(ENetEvent *event); 96 100 void receiverThread(); 97 101 void disconnectClients(); 98 102 int getClientID(ENetPeer peer); 99 103 int getClientID(ENetAddress address); 100 void syncClassid(int clientID);101 104 ENetPeer *getClientPeer(int clientID); 102 bool createShip(ClientInformation *client);105 //bool createShip(ClientInformation *client); 103 106 bool removeShip(ClientInformation *client); 104 bool sendWelcome(int clientID, int shipID, bool allowed);105 107 bool addFakeConnectRequest(ENetEvent *ev); 106 108 PacketBuffer buffer; … … 114 116 115 117 boost::thread *receiverThread_; 118 static boost::recursive_mutex enet_mutex_; 116 119 // int getNumberOfClients(); 117 120 //functions to map what object every clients uses -
code/trunk/src/network/GameStateClient.cc
r1360 r1502 21 21 * 22 22 * Author: 23 * ...23 * Oliver Scheuss 24 24 * Co-authors: 25 * ...25 * Dumeni Manatschal 26 26 * 27 27 */ … … 35 35 #include "Synchronisable.h" 36 36 37 #define GAMESTATEID_INITIAL -138 37 39 38 namespace network … … 43 42 int id; 44 43 }; 45 44 46 45 GameStateClient::GameStateClient() { 47 46 COUT(5) << "this: " << this << std::endl; 48 47 last_diff_=0; 49 48 last_gamestate_=GAMESTATEID_INITIAL-1; 49 tempGameState_=NULL; 50 myShip_=NULL; 50 51 } 51 52 … … 95 96 return false; 96 97 } 97 98 98 99 GameStateCompressed *GameStateClient::popPartialGameState(){ 99 100 GameState *gs = getPartialSnapshot(); 101 if(!gs) 102 return NULL; 100 103 GameStateCompressed *cgs = compress_(gs); 101 104 delete[] gs->data; … … 103 106 return cgs; 104 107 } 105 108 109 void GameStateClient::addGameState(GameStateCompressed *gs){ 110 if(tempGameState_!=NULL){ 111 //delete the obsolete gamestate 112 if(tempGameState_->id>gs->id) 113 return; 114 delete[] tempGameState_->data; 115 delete tempGameState_; 116 } 117 tempGameState_=gs; 118 } 119 int GameStateClient::processGameState(){ 120 if(tempGameState_==NULL) 121 return 0; 122 int id=tempGameState_->id; 123 bool b = saveShipCache(); 124 if(pushGameState(tempGameState_)){ 125 if(b) 126 loadShipCache(); 127 return id; 128 } 129 else 130 return GAMESTATEID_INITIAL; 131 } 132 106 133 107 134 /** … … 151 178 orxonox::Identifier* id = ID((unsigned int)sync.classID); 152 179 if(!id){ 153 COUT( 4) << "We could not identify a new object; classid: " << sync.classID<< std::endl;154 return false; 180 COUT(3) << "We could not identify a new object; classid: " << sync.classID << " uint: " << (unsigned int)sync.classID << " objectID: " << sync.objectID << " size: " << sync.length << std::endl; 181 return false; // most probably the gamestate is corrupted 155 182 } 156 183 Synchronisable *no = dynamic_cast<Synchronisable *>(id->fabricate()); … … 168 195 } 169 196 if( !no->create() ) 197 { 170 198 COUT(1) << "We couldn't manifest (create() ) the object: " << sync.objectID << std::endl; 199 } 171 200 it=orxonox::ObjectList<Synchronisable>::end(); 172 201 } … … 174 203 // we have our object 175 204 if(! it->updateData(sync)) 205 { 176 206 COUT(1) << "We couldn't update objectID: " \ 177 207 << sync.objectID << "; classID: " << sync.classID << std::endl; 208 } 178 209 } 179 210 ++it; … … 213 244 } 214 245 //retval->data = (unsigned char*)malloc(size); 246 if(size==0) 247 return NULL; 215 248 retval->data = new unsigned char[size]; 216 249 if(!retval->data){ … … 245 278 return retval; 246 279 } 247 248 280 281 249 282 GameState *GameStateClient::undiff(GameState *old, GameState *diff) { 250 283 if(!old || !diff) … … 253 286 int of=0; // pointers offset 254 287 int dest_length=0; 255 if(old->size>=diff->size)288 /*if(old->size>=diff->size) 256 289 dest_length=old->size; 257 else 290 else*/ 258 291 dest_length=diff->size; 259 292 // unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); 293 if(dest_length==0) 294 return NULL; 260 295 unsigned char *dp = new unsigned char[dest_length*sizeof(unsigned char)]; 261 296 while(of<old->size && of<diff->size){ … … 270 305 of++; 271 306 } 272 } else{307 } /*else{ 273 308 while(of<dest_length){ 274 309 *(dp+of)=*(ap+of)^n; 275 310 of++; 276 311 } 277 } 312 }*/ 278 313 } 279 314 // should be finished now … … 297 332 298 333 uLongf buffer = (uLongf)((a->size + 12)*1.01)+1; 334 if(buffer==0) 335 return NULL; 299 336 unsigned char *dest = new unsigned char[buffer]; 300 337 int retval; … … 303 340 switch ( retval ) { 304 341 case Z_OK: COUT(5) << "G.St.Cl: compress: successfully compressed" << std::endl; break; 305 case Z_MEM_ERROR: COUT(1) << "G.St.Cl: compress: not enough memory available in gamestate.compress" << std::endl; 342 case Z_MEM_ERROR: COUT(1) << "G.St.Cl: compress: not enough memory available in gamestate.compress" << std::endl; 306 343 return NULL; 307 344 case Z_BUF_ERROR: COUT(2) << "G.St.Cl: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; … … 321 358 return compressedGamestate; 322 359 } 323 324 360 361 325 362 GameState *GameStateClient::decompress(GameStateCompressed *a) { 326 363 //COUT(4) << "GameStateClient: uncompressing gamestate. id: " << a->id << ", baseid: " << a->base_id << ", normsize: " << a->normsize << ", compsize: " << a->compsize << std::endl; … … 333 370 bufsize = normsize; 334 371 // unsigned char* dest = (unsigned char*)malloc( bufsize ); 372 if(bufsize==0) 373 return NULL; 335 374 unsigned char *dest = new unsigned char[bufsize]; 336 375 int retval; … … 376 415 return t; 377 416 } 378 417 379 418 void GameStateClient::cleanup(){ 380 419 std::map<int, GameState*>::iterator temp, it = gameStateMap.begin(); … … 388 427 gameStateMap.erase(temp); 389 428 } 429 tempGameState_=NULL; 390 430 } 391 431 … … 397 437 } 398 438 COUT(4) << std::endl; 399 400 } 401 402 439 440 } 441 442 bool GameStateClient::saveShipCache(){ 443 if(myShip_==NULL) 444 myShip_ = orxonox::SpaceShip::getLocalShip(); 445 if(myShip_){ 446 // unsigned char *data = new unsigned char[myShip_->getSize()]; 447 int size=myShip_->getSize(0x3); 448 if(size==0) 449 return false; 450 unsigned char *data = new unsigned char [size]; 451 shipCache_ = myShip_->getData(data, 0x1); 452 return true; 453 }else 454 return false; 455 } 456 457 bool GameStateClient::loadShipCache(){ 458 if(myShip_){ 459 myShip_->updateData(shipCache_, 0x2); 460 if(shipCache_.data){ 461 delete[] shipCache_.data; 462 } 463 return true; 464 }else 465 return false; 466 } 467 403 468 //##### ADDED FOR TESTING PURPOSE ##### 404 469 GameState* GameStateClient::testDecompress( GameStateCompressed* gc ) { 405 470 return decompress( gc ); 406 471 } 407 472 408 473 GameState* GameStateClient::testUndiff( GameState* g_old, GameState* g_diffed ) { 409 474 return undiff( g_old, g_diffed ); 410 475 } 411 476 //##### ADDED FOR TESTING PURPOSE ##### 412 413 477 478 414 479 } 415 480 -
code/trunk/src/network/GameStateClient.h
r1360 r1502 21 21 * 22 22 * Author: 23 * ...23 * Oliver Scheuss 24 24 * Co-authors: 25 * ...25 * Dumeni Manatschal 26 26 * 27 27 */ … … 33 33 // 34 34 // 35 // Author: <>, (C) 200735 // Author: Oliver Scheuss, (C) 2007 36 36 // 37 37 // Copyright: See COPYING file that comes with this distribution … … 46 46 #include "core/CorePrereqs.h" 47 47 #include "PacketTypes.h" 48 #include "objects/SpaceShip.h" 48 49 50 51 #define GAMESTATEID_INITIAL -1 49 52 50 53 namespace network … … 56 59 ~GameStateClient(); 57 60 61 void addGameState(GameStateCompressed *gs); 62 int processGameState(); 63 GameStateCompressed *popPartialGameState(); 64 void cleanup(); 65 private: 58 66 bool pushGameState(GameStateCompressed *compstate); 59 GameStateCompressed *popPartialGameState();60 private:61 67 bool loadSnapshot(GameState *state); 62 68 GameState *getPartialSnapshot(); 63 void cleanup();64 69 GameState *undiff(GameState *old, GameState *diff); 65 70 GameStateCompressed *compress_(GameState *a); … … 69 74 void removeObject(orxonox::Iterator<Synchronisable> &it); 70 75 void printGameStateMap(); 76 bool saveShipCache(); 77 bool loadShipCache(); 71 78 72 79 int last_diff_; 73 80 int last_gamestate_; 74 81 std::map<int, GameState *> gameStateMap; 82 GameStateCompressed *tempGameState_; // we save the received gamestates here during processQueue 83 orxonox::SpaceShip *myShip_; 84 syncData shipCache_; 75 85 76 86 -
code/trunk/src/network/GameStateManager.cc
r1360 r1502 68 68 printGameStates(); 69 69 return; 70 } 71 72 void GameStateManager::addGameState(GameStateCompressed *gs, int clientID){ 73 if(!gs) 74 return; 75 std::map<int, GameStateCompressed*>::iterator it = gameStateQueue.find(clientID); 76 if(it!=gameStateQueue.end()){ 77 // delete obsolete gamestate 78 delete[] it->second->data; 79 delete it->second; 80 } 81 gameStateQueue[clientID] = gs; 82 return; 83 } 84 85 void GameStateManager::processGameStates(){ 86 std::map<int, GameStateCompressed*>::iterator it; 87 // now push only the most recent gamestates we received (ignore obsolete ones) 88 for(it = gameStateQueue.begin(); it!=gameStateQueue.end(); it++){ 89 pushGameState(it->second, it->first); 90 } 91 // now clear the queue 92 gameStateQueue.clear(); 70 93 } 71 94 … … 117 140 client = it->second; 118 141 GameState *server = reference; 119 COUT( 3) << "client: " << client << " server: " << server << " gamestatemap: " << &gameStateMap<< std::endl;142 COUT(4) << "client: " << client << " server: " << server << " gamestatemap: " << &gameStateMap << " size: " << server->size << std::endl; 120 143 if(client) 121 144 return encode(client, server); … … 170 193 } 171 194 //retval->data = (unsigned char*)malloc(size); 195 if(size==0) 196 return NULL; 172 197 retval->data = new unsigned char[size]; 173 198 if(!retval->data){ … … 237 262 data+=sizeof(int); 238 263 sync.classID = *(int*)data; 264 if(sync.classID == 0) // TODO: remove this 265 COUT(3) << "received a classid 0" << std::endl; 239 266 data+=sizeof(int); 240 267 sync.data = data; … … 246 273 247 274 if(!it){ 248 // the object does not exist yet275 // the objectaber ich glaub die does not exist yet 249 276 COUT(4) << "loadsnapshot: creating new object " << std::endl; 250 277 //COUT(4) << "loadSnapshot:\tclassid: " << sync.classID << ", name: " << ID((unsigned int) sync.classID)->getName() << std::endl; … … 314 341 } 315 342 316 GameState *GameStateManager::diff(GameState *a , GameState *b) {317 unsigned char *ap = a ->data, *bp = b->data;343 GameState *GameStateManager::diff(GameState *alt, GameState *neu) { 344 unsigned char *ap = alt->data, *bp = neu->data; 318 345 int of=0; // pointers offset 319 346 int dest_length=0; 320 if(a->size>=b->size) 321 dest_length=a->size; 322 else 323 dest_length=b->size; 347 /*if(alt->size>neu->size) 348 dest_length=alt->size; 349 else*/ 350 dest_length=neu->size; 351 if(dest_length==0) 352 return NULL; 324 353 //unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); 325 354 unsigned char *dp = new unsigned char[dest_length*sizeof(unsigned char)]; 326 while(of<a ->size && of<b->size){355 while(of<alt->size && of<neu->size){ 327 356 *(dp+of)=*(ap+of)^*(bp+of); // do the xor 328 357 ++of; 329 358 } 330 if(a ->size!=b->size){ // do we have to fill up ?359 if(alt->size!=neu->size){ // do we have to fill up ? 331 360 unsigned char n=0; 332 if(a ->size<b->size){361 if(alt->size<neu->size){ 333 362 while(of<dest_length){ 334 363 *(dp+of)=n^*(bp+of); 335 364 of++; 336 365 } 337 } else{366 } /*else{ 338 367 while(of<dest_length){ 339 368 *(dp+of)=*(ap+of)^n; 340 369 of++; 341 370 } 342 } 371 }*/ 343 372 } 344 373 345 374 GameState *r = new GameState; 346 r->id = b->id;375 r->id = neu->id; 347 376 r->size = dest_length; 348 377 r->diffed = true; 349 r->base_id = a ->id;378 r->base_id = alt->id; 350 379 r->data = dp; 351 380 r->complete = true; … … 362 391 //COUT(4) << "size: " << size << ", buffer: " << buffer << std::endl; 363 392 //unsigned char* dest = (unsigned char*)malloc( buffer ); 393 if(buffer==0) 394 return NULL; 364 395 unsigned char *dest = new unsigned char[buffer]; 365 396 //COUT(4) << "dest: " << dest << std::endl; … … 404 435 bufsize = normsize; 405 436 // unsigned char* dest = (unsigned char*)malloc( bufsize ); 437 if(bufsize==0) 438 return NULL; 406 439 unsigned char *dest = new unsigned char[bufsize]; 407 440 int retval; … … 436 469 return; 437 470 int curid = temp->getGamestateID(); 471 472 if(gamestateID == GAMESTATEID_INITIAL){ 473 temp->setGameStateID(GAMESTATEID_INITIAL); 474 if(curid!=GAMESTATEID_INITIAL) 475 --(gameStateUsed.find(curid)->second); 476 return; 477 } 438 478 if(curid > gamestateID) 439 479 // the network packets got messed up … … 446 486 --(gameStateUsed.find(curid)->second); 447 487 ++(gameStateUsed.find(gamestateID)->second); 448 temp->setGame stateID(gamestateID);488 temp->setGameStateID(gamestateID); 449 489 /* 450 490 GameState *old = clientGameState[clientID]; -
code/trunk/src/network/GameStateManager.h
r1293 r1502 72 72 ~GameStateManager(); 73 73 74 void addGameState(GameStateCompressed *gs, int clientID); 75 void processGameStates(); 76 74 77 void update(); 75 78 GameStateCompressed *popGameState(int clientID); 76 bool pushGameState(GameStateCompressed *gs, int clientID);77 79 void ackGameState(int clientID, int gamestateID); 78 80 void removeClient(ClientInformation *client); 79 private: 81 private: 82 bool pushGameState(GameStateCompressed *gs, int clientID); 80 83 void cleanup(); // "garbage handler" 81 84 GameState *getSnapshot(); … … 83 86 GameStateCompressed *encode(GameState *a, GameState *b); 84 87 GameStateCompressed *encode(GameState *a); 85 GameState *diff(GameState *a , GameState *b);88 GameState *diff(GameState *alt, GameState *neu); 86 89 GameStateCompressed *compress_(GameState *a); 87 90 GameState *decompress(GameStateCompressed *a); … … 91 94 std::map<int, GameState*> gameStateMap; //map gsID to gamestate* 92 95 std::map<int, int> gameStateUsed; // save the number of clients, that use the specific gamestate 96 std::map<int, GameStateCompressed*> gameStateQueue; 93 97 GameState *reference; 94 98 ClientInformation *head_; -
code/trunk/src/network/NetworkPrereqs.h
r1360 r1502 35 35 #define _NetworkPrereqs_H__ 36 36 37 #include " OrxonoxPlatform.h"37 #include "util/OrxonoxPlatform.h" 38 38 39 39 //----------------------------------------------------------------------- … … 54 54 #else 55 55 # define _NetworkExport 56 #endif57 58 //-----------------------------------------------------------------------59 // fixed width integers60 //-----------------------------------------------------------------------61 #if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC62 typedef __int8 int8_t;63 typedef __int16 int16_t;64 typedef __int32 int32_t;65 typedef __int64 int64_t;66 typedef unsigned __int8 uint8_t;67 typedef unsigned __int16 uint16_t;68 typedef unsigned __int32 uint32_t;69 typedef unsigned __int64 uint64_t;70 #else71 # include "inttypes.h"72 56 #endif 73 57 -
code/trunk/src/network/PacketBuffer.cc
r1360 r1502 62 62 last->next=NULL; 63 63 // change this!!!!!!! ---- we are not doing stl so we won't change this 64 last->packet = ev->packet; 65 last->address = ev->peer->address; 64 last->event = ev; 66 65 //last->address = ev->peer->address; 67 66 } else { … … 72 71 last->next=NULL; 73 72 // save the packet to the new element 74 last->packet = ev->packet; 75 last->address = ev->peer->address; 73 last->event = ev; 76 74 //last->address = ev->peer->address; 77 75 } … … 82 80 //returns the first element in the list without deleting it but 83 81 //moving first pointer to next element 84 ENetPacket *PacketBuffer::pop() {82 /*ENetPacket *PacketBuffer::pop() { 85 83 ENetAddress address; 86 84 return pop(address); 87 } 88 89 ENet Packet *PacketBuffer::pop(ENetAddress &address){85 }*/ 86 87 ENetEvent *PacketBuffer::pop(){ 90 88 boost::recursive_mutex::scoped_lock lock(mutex_); 91 89 //std::cout << "packetbuffer pop(address)" << std::endl; … … 93 91 QueueItem *temp = first; 94 92 // get packet 95 ENetPacket *pck=first->packet; 96 address = first->address; 93 ENetEvent *ev=first->event; 94 //address = first->address; 95 // remove first element 96 first = first->next; 97 delete temp; 98 lock.unlock(); 99 //std::cout << "pop(address) size of packet " << pck->dataLength << std::endl; 100 return ev; 101 } else{ 102 lock.unlock(); 103 return NULL; 104 } 105 } 106 107 /*ENetPacket *PacketBuffer::pop(ENetAddress &address) { 108 boost::recursive_mutex::scoped_lock lock(mutex_); 109 //std::cout << "packetbuffer pop(address)" << std::endl; 110 if(first!=NULL ){ 111 QueueItem *temp = first; 112 // get packet 113 ENetPacket *pck=first->event->packet; 114 address = first->event->peer->address; 97 115 // remove first element 98 116 first = first->next; … … 105 123 return NULL; 106 124 } 107 } 125 }*/ 108 126 109 127 bool PacketBuffer::isEmpty() { -
code/trunk/src/network/PacketBuffer.h
r1360 r1502 55 55 56 56 struct QueueItem{ 57 ENet Packet *packet;58 ENetAddress address;57 ENetEvent *event; 58 //ENetAddress address; 59 59 QueueItem *next; 60 60 }; … … 68 68 void print(); 69 69 // pops a packet from the queue 70 ENetPacket *pop(); 71 ENetPacket *pop(ENetAddress &address); 70 //ENetPacket *pop(); 71 //ENetPacket *pop(ENetAddress &address); 72 ENetEvent *pop(); 72 73 // pushs a packet to the queue 73 74 bool push(ENetEvent *ev); -
code/trunk/src/network/PacketDecoder.cc
r1360 r1502 68 68 case COMMAND: 69 69 return command( packet, clientId ); 70 case MOUSE:71 mousem( packet, clientId );72 return true;73 case KEYBOARD:74 keystrike( packet, clientId );75 return true;76 70 case CHAT: 77 71 chatMessage( packet, clientId ); … … 90 84 return false; 91 85 } 92 86 93 87 bool PacketDecoder::testAndRemoveCRC(ENetPacket *packet){ 94 88 uint32_t submittetcrc; … … 108 102 return false; 109 103 } 110 104 111 105 // ATTENTION: TODO watch, that arguments we pass over to the processFunction gets deleted in THE PROCESSXXX function 112 106 … … 124 118 enet_packet_destroy( packet ); 125 119 } 126 120 127 121 bool PacketDecoder::command( ENetPacket* packet, int clientId ){ 128 122 int length = *(int*)((unsigned char *)packet->data+sizeof(int)); 123 if(length<=0) 124 return false; 129 125 void *data = (void *)new unsigned char[length]; 130 126 memcpy(data, (void *)(packet->data+2*sizeof(int)), length); … … 133 129 } 134 130 135 void PacketDecoder::mousem( ENetPacket* packet, int clientId )136 {137 mouse* mouseMove = new mouse;138 //copy data of packet->data to new struct139 *mouseMove = *(mouse*)packet->data;140 141 //clean memory142 enet_packet_destroy( packet );143 printMouse( mouseMove ); //debug info144 }145 146 void PacketDecoder::keystrike( ENetPacket* packet, int clientId )147 {148 keyboard* key = new keyboard;149 *key = *(keyboard*)packet->data; //see above150 151 //clean memory152 enet_packet_destroy( packet );153 printKey( key ); //debug info154 155 }156 157 131 void PacketDecoder::chatMessage( ENetPacket* packet, int clientId ) 158 132 { 159 133 chat* chatting = new chat; 134 if(packet->dataLength==4) 135 return; 160 136 chatting->id = (int)*packet->data; //first copy id into new struct 161 137 //since the chat message is a char*, allocate the memory needed … … 185 161 return; 186 162 } 187 //since it's not alowed to use void* for pointer arithmetic188 //FIXME: variable never used189 unsigned char* data = (unsigned char *)(packet->data);190 163 //copy the GameStateCompressed id into the struct, which is located at second place data+sizeof( int ) 191 164 memcpy( (void*)&(currentState->id), (const void*)(packet->data+1*sizeof( int )), sizeof( int) ); … … 202 175 //since data is not allocated, because it's just a pointer, allocate it with size of gamestatedatastream 203 176 if(currentState->compsize==0) 177 { 204 178 COUT(2) << "PacketDecoder: compsize is 0" << std::endl; 179 } 205 180 // currentState->data = (unsigned char*)(malloc( currentState->compsize )); 181 if(currentState->compsize==0) 182 return; 206 183 currentState->data = new unsigned char[currentState->compsize]; 207 184 if(currentState->data==NULL) 185 { 208 186 COUT(2) << "PacketDecoder: Gamestatepacket-decoder: memory leak" << std::endl; 187 } 209 188 //copy the GameStateCompressed data 210 189 memcpy( (void*)(currentState->data), (const void*)(packet->data+5*sizeof( int ) + 2*sizeof(bool)), currentState->compsize ); … … 221 200 cid->id = ((classid *)(packet->data))->id; 222 201 cid->clid = ((classid *)(packet->data))->clid; 202 if(cid->length==0) 203 return; 223 204 // cid->message = (const char *)malloc(cid->length); 224 205 cid->message = new char[cid->length]; … … 229 210 processClassid(cid); 230 211 } 231 232 212 213 233 214 bool PacketDecoder::decodeWelcome( ENetPacket* packet, int clientID ){ 234 215 welcome *w = new welcome; … … 240 221 return processWelcome(w); 241 222 } 242 223 243 224 bool PacketDecoder::decodeConnectRequest( ENetPacket *packet, int clientID ){ 244 225 connectRequest *con = new connectRequest; … … 279 260 return; 280 261 } 281 262 282 263 bool PacketDecoder::processWelcome( welcome *w ){ 283 264 delete w; 284 265 return true; 285 266 } 286 267 287 268 bool PacketDecoder::processConnectRequest( connectRequest *con, int clientID ){ 288 269 COUT(3) << "packetdecoder: processing connectRequest" << std::endl; … … 299 280 } 300 281 301 void PacketDecoder::printMouse( mouse* data )302 {303 COUT(5) << "data id: " << data->id << std::endl;304 COUT(5) << "data: " << data->x << " " << data->y << std::endl;305 }306 307 void PacketDecoder::printKey( keyboard* data )308 {309 COUT(5) << "data id: " << data->id << std::endl;310 COUT(5) << "data: " << (char)data->press << std::endl;311 }312 282 313 283 void PacketDecoder::printChat( chat* data, int clientId ) 314 284 { 315 285 if(clientId!=CLIENTID_CLIENT) 286 { 316 287 COUT(5) << "client: " << clientId << std::endl; 288 } 317 289 COUT(5) << "data id: " << data->id << std::endl; 318 290 COUT(5) << "data: " << data->message << std::endl; -
code/trunk/src/network/PacketGenerator.cc
r1360 r1502 78 78 { 79 79 COUT(4) << "PacketGenerator: generating new acknowledgement, id: " << state << std::endl; 80 ack* ackreq = new ack; 80 81 ENetPacket *packet = enet_packet_create( NULL , sizeof( ack ), reliable ); 82 ack* ackreq = (ack *)packet->data; 81 83 ackreq->id = ACK; 82 84 ackreq->a = state; 83 84 ENetPacket *packet = enet_packet_create( ackreq , sizeof( *ackreq ), reliable ); 85 delete ackreq; 85 //delete ackreq; 86 86 return packet; 87 87 } … … 89 89 ENetPacket* command( int dataLength, void *data, int reliable = ENET_PACKET_FLAG_RELIABLE ) 90 90 { 91 if(dataLength==0) 92 return NULL; 91 93 unsigned char *stream = new unsigned char[dataLength + 2*sizeof(int)]; 92 94 if(!stream) … … 101 103 } 102 104 103 /*### mouseupdates */104 ENetPacket* PacketGenerator::mousem( double x, double y, int reliable )105 {106 COUT(4) << "PacketGenerator: generating new mouse" << std::endl;107 mouse* mousemove = new mouse;108 mousemove->id = MOUSE;109 mousemove->x = x;110 mousemove->y = y;111 112 ENetPacket *packet = enet_packet_create( mousemove , sizeof( *mousemove ), reliable );113 delete mousemove;114 return packet;115 }116 117 /*### keystrikes updates */118 ENetPacket* PacketGenerator::keystrike( char press, int reliable )119 {120 COUT(4) << "PacketGenerator: generating new keyboard" << std::endl;121 keyboard* key = new keyboard;122 key->id = KEYBOARD;123 key->press = press;124 125 ENetPacket *packet = enet_packet_create( key , sizeof( *key ), reliable );126 delete key;127 return packet;128 }129 105 130 106 /*### chat messages packet */ … … 150 126 //std::cout << "totalLen " << totalLen << std::endl; 151 127 //unsigned char *data = (unsigned char*)malloc( totalLen ); //allocate the memory for datastream 152 unsigned char *data = new unsigned char[totalLen]; 128 if(totalLen==0) 129 return NULL; 130 ENetPacket *packet = enet_packet_create( NULL , totalLen+sizeof(uint32_t), reliable ); 131 //unsigned char *data = new unsigned char[totalLen]; 132 unsigned char *data = packet->data; 153 133 memcpy( (void*)(data), (const void*)&gid, sizeof( int ) ); //this is the enet id 154 134 memcpy( (void*)(data+sizeof(int)), (const void*)&(states->id), sizeof(int) ); //the GameStateCompressed id … … 163 143 //create an enet packet with the generated bytestream 164 144 COUT(4) << "PacketGenerator generating totalLen " << totalLen << std::endl; 165 ENetPacket *packet = enet_packet_create( data , totalLen, reliable ); 166 delete[] data; 145 //delete[] data; 167 146 if(!addCRC(packet)) 168 147 COUT(3) << "could not add crc to gamestate packet" << std::endl; … … 173 152 { 174 153 //unsigned char* data = (unsigned char *)malloc(3*sizeof(int)+classname.length()+1); 154 if(classname.length()==0) 155 return NULL; 175 156 unsigned char *data = new unsigned char[3*sizeof(int)+classname.length()+1]; 176 157 std::cout << "PacketGenerator: classid: " << classid << ", name: " << classname << std::endl; … … 206 187 bool PacketGenerator::addCRC( ENetPacket *packet){ 207 188 unsigned char *data = packet->data; 208 uint32_t crc32=calcCRC(data, packet->dataLength );189 uint32_t crc32=calcCRC(data, packet->dataLength-sizeof(uint32_t)); 209 190 // now append the crc to the packet data 210 int oldlength = packet->dataLength ;211 if(enet_packet_resize(packet, packet->dataLength+sizeof(uint32_t))==0){191 int oldlength = packet->dataLength-sizeof(uint32_t); 192 //if(enet_packet_resize(packet, packet->dataLength+sizeof(uint32_t))==0){ 212 193 memcpy(&packet->data[oldlength], &crc32, sizeof(uint32_t)); 213 194 return true; 214 }else{215 COUT(3) << "could not add crc to gamestate" << std::endl;216 return false;217 }195 //}else{ 196 // COUT(3) << "could not add crc to gamestate" << std::endl; 197 // return false; 198 //} 218 199 } 219 200 -
code/trunk/src/network/PacketManager.h
r1360 r1502 59 59 ENetPacket* acknowledgement( int state, int reliable = 0 ); // we do not want reliability 60 60 ENetPacket* command( int dataLength, void *data, int reliable = ENET_PACKET_FLAG_RELIABLE ); 61 ENetPacket* mousem( double x, double y, int reliable = ENET_PACKET_FLAG_RELIABLE );62 ENetPacket* keystrike( char press, int reliable = ENET_PACKET_FLAG_RELIABLE );63 61 ENetPacket* chatMessage( const char* message, int reliable = ENET_PACKET_FLAG_RELIABLE ); 64 62 ENetPacket* gstate( GameStateCompressed *states, int reliable = 0 ); // we do not want reliability of gamestates … … 94 92 void acknowledgement( ENetPacket* packet, int clientId = CLIENTID_CLIENT ); 95 93 bool command( ENetPacket* packet, int clientId ); 96 void mousem( ENetPacket* packet, int clientId = CLIENTID_CLIENT );97 void keystrike( ENetPacket* packet, int clientId = CLIENTID_CLIENT );98 94 void chatMessage( ENetPacket* packet, int clientId = CLIENTID_CLIENT ); 99 95 void gstate( ENetPacket* packet, int clientID = CLIENTID_CLIENT ); … … 113 109 //print functions 114 110 void printAck( ack* data ); 115 void printMouse( mouse* data );116 void printKey( keyboard* data );117 111 void printChat( chat* data, int clientId ); 118 112 void printGamestate( GameStateCompressed *data ); -
code/trunk/src/network/PacketTypes.h
r1293 r1502 48 48 enum packet_id { 49 49 ACK, 50 MOUSE,51 50 COMMAND, 52 KEYBOARD,53 51 CHAT, 54 52 GAMESTATE , … … 104 102 }; 105 103 106 struct mouse {107 int id;108 double x;109 double y;110 };111 112 struct keyboard {113 int id;114 char press;115 };116 104 //only in this class, not PacketGenerator, used as pattern to put incoming 117 105 //bytes inside -
code/trunk/src/network/Server.cc
r1360 r1502 42 42 43 43 #include <iostream> 44 44 45 45 46 #include "ConnectionManager.h" … … 49 50 //#include "NetworkFrameListener.h" 50 51 #include "util/Sleep.h" 52 #include "objects/SpaceShip.h" 51 53 52 54 … … 56 58 57 59 #define MAX_FAILURES 20; 58 60 #define NETWORK_FREQUENCY 30 59 61 60 62 /** … … 63 65 */ 64 66 Server::Server() { 67 timeSinceLastUpdate_=0; 65 68 packet_gen = PacketGenerator(); 66 69 clients = new ClientInformation(true); … … 68 71 gamestates = new GameStateManager(clients); 69 72 } 73 74 Server::Server(int port){ 75 timeSinceLastUpdate_=0; 76 packet_gen = PacketGenerator(); 77 clients = new ClientInformation(true); 78 connection = new ConnectionManager(clients, port); 79 gamestates = new GameStateManager(clients); 80 } 70 81 71 82 /** … … 75 86 */ 76 87 Server::Server(int port, std::string bindAddress) { 88 timeSinceLastUpdate_=0; 77 89 packet_gen = PacketGenerator(); 78 90 clients = new ClientInformation(); … … 87 99 */ 88 100 Server::Server(int port, const char *bindAddress) { 101 timeSinceLastUpdate_=0; 89 102 packet_gen = PacketGenerator(); 90 103 clients = new ClientInformation(); … … 117 130 ENetPacket *packet = packet_gen.chatMessage(msg.c_str()); 118 131 //std::cout <<"adding packets" << std::endl; 119 if(connection->addPacketAll(packet)) 120 //std::cout <<"added packets" << std::endl; 121 return connection->sendPackets(); 122 else 123 return false; 132 return connection->addPacketAll(packet); 124 133 } 125 134 … … 132 141 ENetPacket *packet = packet_gen.chatMessage(msg); 133 142 COUT(4) <<"Server: adding Packets" << std::endl; 134 connection->addPacketAll(packet); 135 //std::cout <<"added packets" << std::endl; 136 if (connection->sendPackets()){ 137 COUT(4) << "Server: Sucessfully" << std::endl; 138 return true; 139 } 140 return false; 143 return connection->addPacketAll(packet); 141 144 } 142 145 … … 148 151 void Server::tick(float time) { 149 152 processQueue(); 150 updateGamestate(); 151 // usleep(500000); // TODO remove 153 //this steers our network frequency 154 timeSinceLastUpdate_+=time; 155 if(timeSinceLastUpdate_>=(1./NETWORK_FREQUENCY)){ 156 timeSinceLastUpdate_-=(1./NETWORK_FREQUENCY); 157 gamestates->processGameStates(); 158 updateGamestate(); 159 } 160 // usleep(5000); // TODO remove 152 161 return; 153 162 } … … 157 166 */ 158 167 void Server::processQueue() { 159 ENet Packet *packet;168 ENetEvent *event; 160 169 int clientID=-1; 161 170 while(!connection->queueEmpty()){ 162 171 //std::cout << "Client " << clientID << " sent: " << std::endl; 163 172 //clientID here is a reference to grab clientID from ClientInformation 164 packet = connection->getPacket(clientID); 165 if(!packet) 166 continue; 173 event = connection->getEvent(); 174 if(!event) 175 continue; 176 assert(event->type != ENET_EVENT_TYPE_NONE); 177 switch( event->type ) { 178 case ENET_EVENT_TYPE_CONNECT: 179 COUT(3) << "processing event_Type_connect" << std::endl; 180 addClient(event); 181 break; 182 case ENET_EVENT_TYPE_DISCONNECT: 183 if(clients->findClient(&event->peer->address)) 184 disconnectClient(event); 185 break; 186 case ENET_EVENT_TYPE_RECEIVE: 187 if(clients->findClient(&event->peer->address)){ 188 clientID = clients->findClient(&event->peer->address)->getID(); 189 if( !elaborate(event->packet, clientID) ) 190 COUT(3) << "Server: could not elaborate" << std::endl; 191 } 192 break; 193 } 194 delete event; 167 195 //if statement to catch case that packetbuffer is empty 168 if( !elaborate(packet, clientID) )169 COUT(3) << "Server: could not elaborate" << std::endl;170 196 } 171 197 } … … 210 236 if(gs==NULL){ 211 237 COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl; 212 return false;238 continue; 213 239 } 214 240 //std::cout << "adding gamestate" << std::endl; 215 if ( !(connection->addPacket(packet_gen.gstate(gs), cid)) ){ 241 ENetPacket *packet = packet_gen.gstate(gs); 242 if(!packet) 243 continue; 244 if ( !(connection->addPacket(packet, cid)) ){ 216 245 COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; 217 246 temp->addFailure(); 218 if(temp->getFailures() > 20 )219 disconnectClient(temp); 247 /*if(temp->getFailures() > 0 ) 248 disconnectClient(temp);*/ 220 249 //std::cout << "added gamestate" << std::endl; 221 } 250 }else 251 temp->resetFailures(); 222 252 added=true; 223 253 temp=temp->next(); … … 226 256 delete gs; 227 257 } 228 if(added) {258 /*if(added) { 229 259 //std::cout << "send gamestates from server.cc in sendGameState" << std::endl; 230 260 return connection->sendPackets(); 231 } 232 COUT(5) << "Server: had no gamestates to send" << std::endl;233 return false;261 }*/ 262 //COUT(5) << "Server: had no gamestates to send" << std::endl; 263 return true; 234 264 } 235 265 236 266 void Server::processAck( ack *data, int clientID) { 237 COUT(4) << " \b\b\b\n\n\n\n\nServer: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl;267 COUT(4) << "Server: processing ack from client: " << clientID << "; ack-id: " << data->a << std::endl; 238 268 gamestates->ackGameState(clientID, data->a); 239 269 delete data; … … 241 271 242 272 bool Server::processConnectRequest( connectRequest *con, int clientID ){ 243 COUT(3) << "processing connectRequest " << std::endl;273 //(COUT(3) << "processing connectRequest " << std::endl; 244 274 //connection->addPacket(packet_gen.gstate(gamestates->popGameState(clientID)) , clientID); 245 connection->createClient(clientID);275 //createClient(clientID); 246 276 delete con; 247 277 return true; … … 250 280 void Server::processGamestate( GameStateCompressed *data, int clientID){ 251 281 COUT(4) << "processing partial gamestate from client " << clientID << std::endl; 252 if(!gamestates->pushGameState(data, clientID))253 COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl;282 gamestates->addGameState(data, clientID); 283 /*COUT(3) << "Could not push gamestate\t\t\t\t=====" << std::endl; 254 284 else 255 285 if(clients->findClient(clientID)) 256 clients->findClient(clientID)->resetFailures(); 286 clients->findClient(clientID)->resetFailures();*/ 287 } 288 289 bool Server::addClient(ENetEvent *event){ 290 ClientInformation *temp = clients->insertBack(new ClientInformation); 291 if(!temp){ 292 COUT(2) << "Server: could not add client" << std::endl; 293 return false; 294 } 295 if(temp->prev()->getHead()) { //not good if you use anything else than insertBack 296 temp->prev()->setID(0); //bugfix: not necessary but usefull 297 temp->setID(1); 298 } 299 else 300 temp->setID(temp->prev()->getID()+1); 301 temp->setPeer(event->peer); 302 COUT(3) << "Server: added client id: " << temp->getID() << std::endl; 303 return createClient(temp->getID()); 304 } 305 306 bool Server::createClient(int clientID){ 307 ClientInformation *temp = clients->findClient(clientID); 308 if(!temp){ 309 COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl; 310 return false; 311 } 312 COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl; 313 connection->syncClassid(temp->getID()); 314 COUT(4) << "creating spaceship for clientid: " << temp->getID() << std::endl; 315 // TODO: this is only a hack, untill we have a possibility to define default player-join actions 316 if(!createShip(temp)) 317 COUT(2) << "Con.Man. could not create ship for clientid: " << clientID << std::endl; 318 else 319 COUT(3) << "created spaceship" << std::endl; 320 temp->setSynched(true); 321 COUT(3) << "sending welcome" << std::endl; 322 connection->sendWelcome(temp->getID(), temp->getShipID(), true); 323 return true; 324 } 325 326 bool Server::createShip(ClientInformation *client){ 327 if(!client) 328 return false; 329 orxonox::Identifier* id = ID("SpaceShip"); 330 if(!id){ 331 COUT(4) << "We could not create the SpaceShip for client: " << client->getID() << std::endl; 332 return false; 333 } 334 orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate()); 335 no->setPosition(orxonox::Vector3(0,0,80)); 336 no->setScale(10); 337 //no->setYawPitchRoll(orxonox::Degree(-90),orxonox::Degree(-90),orxonox::Degree(0)); 338 no->setMesh("assff.mesh"); 339 no->setMaxSpeed(500); 340 no->setMaxSideAndBackSpeed(50); 341 no->setMaxRotation(1.0); 342 no->setTransAcc(200); 343 no->setRotAcc(3.0); 344 no->setTransDamp(75); 345 no->setRotDamp(1.0); 346 no->setCamera("cam_"+client->getID()); 347 no->classID = id->getNetworkID(); 348 no->create(); 349 350 client->setShipID(no->objectID); 351 return true; 352 } 353 354 bool Server::disconnectClient(ENetEvent *event){ 355 COUT(4) << "removing client from list" << std::endl; 356 //return removeClient(head_->findClient(&(peer->address))->getID()); 357 358 //boost::recursive_mutex::scoped_lock lock(head_->mutex_); 359 orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start(); 360 ClientInformation *client = clients->findClient(&event->peer->address); 361 if(!client) 362 return false; 363 while(it){ 364 if(it->objectID!=client->getShipID()){ 365 ++it; 366 continue; 367 } 368 orxonox::Iterator<orxonox::SpaceShip> temp=it; 369 ++it; 370 delete *temp; 371 return clients->removeClient(event->peer); 372 } 373 return false; 257 374 } 258 375 -
code/trunk/src/network/Server.h
r1293 r1502 60 60 public: 61 61 Server(); 62 Server(int port); 62 63 Server(int port, std::string bindAddress); 63 64 Server(int port, const char *bindAddress); … … 71 72 void updateGamestate(); 72 73 private: 74 bool addClient(ENetEvent *event); 75 bool createClient(int clientID); 76 bool createShip(ClientInformation *client); 77 bool disconnectClient(ENetEvent *event); 73 78 void disconnectClient(int clientID); 74 79 void disconnectClient( ClientInformation *client); … … 82 87 83 88 ClientInformation *clients; 89 float timeSinceLastUpdate_; 84 90 }; 85 91 -
code/trunk/src/network/Synchronisable.cc
r1360 r1502 66 66 67 67 Synchronisable::~Synchronisable(){ 68 } 69 70 bool Synchronisable::create(){ 71 this->classID = this->getIdentifier()->getNetworkID(); 72 COUT(4) << "creating synchronisable: setting classid from " << this->getIdentifier()->getName() << " to: " << classID << std::endl; 73 return true; 68 74 } 69 75 … … 151 157 * @return data containing all variables and their sizes 152 158 */ 153 syncData Synchronisable::getData(unsigned char *mem ){159 syncData Synchronisable::getData(unsigned char *mem, int mode){ 154 160 //std::cout << "inside getData" << std::endl; 161 if(mode==0x0) 162 mode=state_; 155 163 if(classID==0) 156 164 COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl; … … 167 175 for(i=syncList->begin(); n<datasize && i!=syncList->end(); ++i){ 168 176 //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int)); 169 if( ((*i)->mode & state_) == 0 ){177 if( ((*i)->mode & mode) == 0 ){ 170 178 COUT(5) << "not getting data: " << std::endl; 171 179 continue; // this variable should only be received … … 194 202 * @return true/false 195 203 */ 196 bool Synchronisable::updateData(syncData vars){ 204 bool Synchronisable::updateData(syncData vars, int mode){ 205 if(mode==0x0) 206 mode=state_; 197 207 unsigned char *data=vars.data; 198 208 std::list<synchronisableVariable *>::iterator i; … … 203 213 COUT(5) << "Synchronisable: objectID " << vars.objectID << ", classID " << vars.classID << " size: " << vars.length << " synchronising data" << std::endl; 204 214 for(i=syncList->begin(); i!=syncList->end(); i++){ 205 if( ((*i)->mode ^ state_) == 0 ){215 if( ((*i)->mode ^ mode) == 0 ){ 206 216 COUT(5) << "synchronisable: not updating variable " << std::endl; 207 continue; // this variable should only be updated217 continue; // this variable should only be set 208 218 } 209 219 COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl; … … 230 240 * @return amount of bytes 231 241 */ 232 int Synchronisable::getSize( ){242 int Synchronisable::getSize(int mode){ 233 243 int tsize=0; 244 if(mode==0x0) 245 mode=state_; 234 246 std::list<synchronisableVariable *>::iterator i; 235 247 for(i=syncList->begin(); i!=syncList->end(); i++){ 236 if( ((*i)->mode & state_) == 0 )248 if( ((*i)->mode & mode) == 0 ) 237 249 continue; // this variable should only be received, so dont add its size to the send-size 238 250 switch((*i)->type){ -
code/trunk/src/network/Synchronisable.h
r1293 r1502 83 83 void registerVar(void *var, int size, variableType t, int mode=1); 84 84 // syncData getData(); 85 syncData getData(unsigned char *mem );86 int getSize( );87 bool updateData(syncData vars );85 syncData getData(unsigned char *mem, int mode=0x0); 86 int getSize(int mode=0x0); 87 bool updateData(syncData vars, int mode=0x0); 88 88 void setBacksync(bool sync); 89 89 bool getBacksync(); 90 90 virtual void registerAllVariables()=0; 91 virtual bool create() =0;91 virtual bool create(); 92 92 static void setClient(bool b); 93 93 protected: -
code/trunk/src/network/diffTest.cc
r1360 r1502 549 549 550 550 std::cout << "test setGamestateID" << std::endl; 551 temp->setGame stateID( 8 );551 temp->setGameStateID( 8 ); 552 552 printClientInformationBox( temp ); 553 553 … … 559 559 newInfo->setSynched( true ); 560 560 newInfo->setPeer( NULL ); 561 toool->setGame stateID( 199 );561 toool->setGameStateID( 199 ); 562 562 toool->setID( numberOfClients+1); 563 563 toool->setSynched( true ); -
code/trunk/src/network/dummyclient3.cc
r1293 r1502 51 51 Client client( str, PORT ); 52 52 if ( client.establishConnection() ) 53 { 53 54 COUT(3) << "connection established" << std::endl; 54 else COUT(0) << "problems establishing connection" << std::endl; 55 } 56 else 57 { 58 COUT(0) << "problems establishing connection" << std::endl; 59 } 55 60 char message[10000]; 56 61 char signs[] = "abcdefghijklmnopqrstuvwxy";
Note: See TracChangeset
for help on using the changeset viewer.