Changeset 1666
- Timestamp:
- Aug 26, 2008, 12:52:43 AM (16 years ago)
- Location:
- code/branches/network/src/network
- Files:
-
- 3 added
- 7 deleted
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network/src/network/CMakeLists.txt
r1505 r1666 11 11 Server.cc 12 12 Synchronisable.cc 13 Host.cc 14 packet/Packet.cc 15 packet/Chat.cc 16 packet/ClassID.cc 17 packet/Acknowledgement.cc 18 packet/Gamestate.cc 19 Host.cc 13 20 ) 14 21 -
code/branches/network/src/network/Client.cc
r1534 r1666 40 40 41 41 #include "Client.h" 42 #include "Host.h" 42 43 #include "Synchronisable.h" 43 44 #include "core/CoreIncludes.h" 44 45 #include "core/ConsoleCommand.h" 45 #include " Server.h"46 #include "packet/Packet.h" 46 47 47 48 namespace network 48 49 { 49 SetConsoleCommandShortcut(Client, Chat); 50 51 Client* Client::_sClient = 0; 52 53 Client* Client::createSingleton(){ 54 if(!_sClient){ 55 _sClient = new Client(); 56 } 57 return _sClient; 58 } 59 60 Client* Client::createSingleton(std::string address, int port){ 61 if(!_sClient) 62 _sClient = new Client(address, port); 63 return _sClient; 64 } 65 66 Client* Client::createSingleton(const char *address, int port){ 67 if(!_sClient) 68 _sClient = new Client(address, port); 69 return _sClient; 70 } 71 72 void Client::destroySingleton(){ 73 if(_sClient){ 74 delete _sClient; 75 _sClient = 0; 76 } 77 } 78 79 Client* Client::getSingleton(){ 80 return _sClient; 81 } 50 // SetConsoleCommandShortcut(Client, chat); 51 82 52 83 53 /** … … 144 114 } 145 115 146 147 148 void Client::Chat( std::string message ){ 149 if(Client::getSingleton()) 150 Client::getSingleton()->sendChat(message); 151 else if(Server::getSingleton()) 152 Server::getSingleton()->sendChat(message); 153 else 154 COUT(1) << "do you want to monologize ??" << std::endl; 155 } 156 116 bool Client::queuePacket(ENetPacket *packet, int clientID){ 117 return client_connection.addPacket(packet); 118 } 119 120 bool Client::processChat(packet::Chat *message, unsigned int clientID){ 121 return message->process(); 122 } 123 124 bool Client::sendChat(packet::Chat *chat){ 125 chat->process(); 126 packet::Packet *p = new packet::Packet(chat); 127 return p->send(); 128 } 157 129 158 130 /** … … 236 208 } 237 209 238 void Client::processChat( chat *data, int clientId){239 COUT(1) << data->message << std::endl;240 delete[] data->message;241 delete data;242 }210 // void Client::processChat( chat *data, int clientId){ 211 // COUT(1) << data->message << std::endl; 212 // delete[] data->message; 213 // delete data; 214 // } 243 215 244 216 bool Client::processWelcome( welcome *w ){ -
code/branches/network/src/network/Client.h
r1535 r1666 46 46 #include <string> 47 47 48 #include "Host.h" 49 #include "packet/Chat.h" 48 50 #include "ClientConnection.h" 49 51 #include "PacketManager.h" … … 62 64 * 63 65 */ 64 class _NetworkExport Client : PacketDecoder {66 class _NetworkExport Client : PacketDecoder, public Host{ 65 67 public: 66 67 static Client* createSingleton();68 static Client* createSingleton(std::string address, int port);69 static Client* createSingleton(const char *address, int port);70 static void destroySingleton();71 static Client *getSingleton();72 73 bool establishConnection();74 bool closeConnection();75 76 static void Chat( std::string message );77 78 int getShipID(){return shipID_;}79 int getClientID(){return clientID_;}80 81 void tick(float time);82 83 private:84 68 Client(); 85 69 Client(std::string address, int port); … … 87 71 ~Client(); 88 72 89 static Client* _sClient; 73 bool establishConnection(); 74 bool closeConnection(); 75 bool queuePacket(ENetPacket *packet, int clientID); 76 bool processChat(packet::Chat *message, unsigned int clientID); 77 bool sendChat(packet::Chat *chat); 78 79 // static void Chat( std::string message ); 80 81 int shipID(){return shipID_;} 82 int playerID(){return clientID_;} 83 84 void tick(float time); 85 86 private: 90 87 91 88 ClientConnection client_connection; … … 100 97 void processGamestate( GameStateCompressed *data, int clientID); 101 98 void processClassid(classid *clid); 102 void processChat( chat *data, int clientId );99 // void processChat( chat *data, int clientId ); 103 100 bool processWelcome( welcome *w ); 104 101 int clientID_; // this is the id the server gave to us -
code/branches/network/src/network/ClientInformation.cc
r1534 r1666 46 46 { 47 47 48 ClientInformation *ClientInformation::head_=0; 49 48 50 ClientInformation::ClientInformation() { 51 if(!head_) 52 head_=this; 49 53 gamestateID_=GAMESTATEID_INITIAL; 50 54 preve=0; 51 55 nexte=0; 52 56 partialGamestateID_=GAMESTATEID_INITIAL-1; 53 this->head_=false;54 57 synched_=false; 55 58 } 56 59 57 ClientInformation::ClientInformation(bool head) {58 gamestateID_=GAMESTATEID_INITIAL;59 preve=0;60 nexte=0;61 partialGamestateID_=GAMESTATEID_INITIAL-1;62 this->head_=head;63 synched_=false;64 }65 66 // ClientInformation::ClientInformation(ClientInformation *prev) {67 // if(prev->next()!=0){68 // this->nexte=prev->next();69 // this->nexte->setPrev(this);70 // }71 // else72 // this->nexte = 0;73 // prev->setNext(this);74 // this->preve = pref;75 // }76 //77 // ClientInformation::ClientInformation(ClientInformation *prev, ClientInformation *next){78 // this->nexte = next;79 // this->preve = prev;80 // this->preve->setNext(this);81 // this->nexte->setPrev(this);82 // }83 84 60 ClientInformation::~ClientInformation() { 61 if(this==head_) 62 head_=next(); 85 63 if(prev()!=0) 86 64 prev()->setNext(this->next()); … … 103 81 104 82 bool ClientInformation::setPrev(ClientInformation *prev) { 105 if(!head_) 106 this->preve = prev; 107 else 108 return false; 83 if(this==head_) 84 head_=prev; 85 this->preve = prev; 109 86 return true; 110 87 } … … 174 151 } 175 152 176 bool ClientInformation::getHead(){177 return head_;178 }179 180 void ClientInformation::setHead(bool h){181 head_=h;182 }183 184 153 int ClientInformation::getFailures(){ 185 154 return failures_; … … 215 184 216 185 ClientInformation *ClientInformation::insertBack(ClientInformation *ins) { 217 if(!this) 218 return NULL; 219 ClientInformation *temp = this; 186 ClientInformation *temp = head_; 187 if(temp==head_){ 188 return head_; 189 } 220 190 while(temp->next()!=0){ 221 191 temp = temp->next(); … … 227 197 228 198 bool ClientInformation::removeClient(int clientID) { 229 if( !this ||clientID==CLIENTID_UNKNOWN)230 return false; 231 ClientInformation *temp = this;199 if(clientID==CLIENTID_UNKNOWN) 200 return false; 201 ClientInformation *temp = head_; 232 202 while(temp!=0 && temp->getID()!=clientID) 233 203 temp = temp->next(); … … 239 209 240 210 bool ClientInformation::removeClient(ENetPeer *peer) { 241 if(! this || !peer)242 return false; 243 ClientInformation *temp = this;211 if(!peer) 212 return false; 213 ClientInformation *temp = head_; 244 214 while(temp!=0){ 245 if(!temp->head_) 246 if(temp->getPeer()->address.host==peer->address.host && temp->getPeer()->address.port==peer->address.port) 247 break; 215 if(temp->getPeer()->address.host==peer->address.host && temp->getPeer()->address.port==peer->address.port) 216 break; 248 217 temp = temp->next(); 249 218 } … … 261 230 */ 262 231 ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards) { 263 ClientInformation *temp = this; 264 if (temp->head_) 265 temp=temp->next(); 232 ClientInformation *temp = head_; 266 233 while(temp!=0 && temp->getID()!=clientID){ 267 234 temp = temp->next(); … … 278 245 */ 279 246 ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards) { 280 ClientInformation *temp = this;247 ClientInformation *temp = head_; 281 248 while(temp!=0){ 282 if(temp->head_){283 temp = temp->next();284 continue;285 }286 249 if(temp->getPeer()->address.host==address->host && temp->getPeer()->address.port == address->port) 287 250 break; -
code/branches/network/src/network/ClientInformation.h
r1534 r1666 60 60 public: 61 61 ClientInformation(); 62 ClientInformation(bool head);63 62 // ClientInformation(ClientInformation *prev, ClientInformation *next); 64 63 // ClientInformation(ClientInformation *prev); … … 66 65 ClientInformation *next(); 67 66 ClientInformation *prev(); 68 ClientInformation *insertBack(ClientInformation *ins);67 static ClientInformation *insertBack(ClientInformation *ins); 69 68 70 69 // set functions … … 81 80 int getPartialGamestateID(); 82 81 ENetPeer *getPeer(); 83 bool getHead();84 void setHead(bool h);85 82 86 83 int getFailures(); … … 90 87 enet_uint32 getPacketLoss(); 91 88 92 bool removeClient(int clientID); 93 bool removeClient(ENetPeer *peer); 94 //## add bool mask-function eventually 95 ClientInformation *findClient(int clientID, bool look_backwards=false); 96 //## add bool mask-function eventually 97 ClientInformation *findClient(ENetAddress *address, bool look_backwards=false); 89 static bool removeClient(int clientID); 90 static bool removeClient(ENetPeer *peer); 91 static ClientInformation *findClient(int clientID, bool look_backwards=false); 92 static ClientInformation *findClient(ENetAddress *address, bool look_backwards=false); 93 static ClientInformation *getBegin(){return head_;} 98 94 99 95 bool setSynched(bool s); … … 101 97 102 98 103 private: 104 bool setNext(ClientInformation *next); 105 bool setPrev(ClientInformation *prev); 99 private: 100 static ClientInformation *head_; 101 102 bool setNext(ClientInformation *next); 103 bool setPrev(ClientInformation *prev); 106 104 ClientInformation *insertAfter(ClientInformation *ins); 107 105 ClientInformation *insertBefore(ClientInformation *ins); … … 116 114 int ShipID_; // this is the unique objectID 117 115 bool synched_; 118 bool head_;119 116 unsigned short failures_; 120 117 -
code/branches/network/src/network/ConnectionManager.cc
r1534 r1666 39 39 40 40 #include <iostream> 41 #include <assert.h> 41 42 // boost.thread library for multithreading support 42 43 #include <boost/bind.hpp> … … 65 66 //boost::thread_group network_threads; 66 67 67 ConnectionManager::ConnectionManager():receiverThread_(0){} 68 boost::recursive_mutex ConnectionManager::enet_mutex_; 69 70 ConnectionManager::ConnectionManager(ClientInformation *head) : receiverThread_(0) { 68 ConnectionManager *ConnectionManager::instance_=0; 69 70 ConnectionManager::ConnectionManager():receiverThread_(0){ 71 assert(instance_==0); 72 instance_=this; 71 73 quit=false; 72 74 bindAddress.host = ENET_HOST_ANY; 73 75 bindAddress.port = NETWORK_PORT; 74 head_ = head; 75 } 76 77 ConnectionManager::ConnectionManager(ClientInformation *head, int port){ 76 } 77 boost::recursive_mutex ConnectionManager::enet_mutex_; 78 79 // ConnectionManager::ConnectionManager(ClientInformation *head) : receiverThread_(0) { 80 // assert(instance_==0); 81 // instance_=this; 82 // quit=false; 83 // bindAddress.host = ENET_HOST_ANY; 84 // bindAddress.port = NETWORK_PORT; 85 // } 86 87 ConnectionManager::ConnectionManager(int port){ 88 assert(instance_==0); 89 instance_=this; 78 90 quit=false; 79 91 bindAddress.host = ENET_HOST_ANY; 80 92 bindAddress.port = port; 81 head_ = head; 82 } 83 84 ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head) :receiverThread_(0) { 93 } 94 95 ConnectionManager::ConnectionManager(int port, std::string address) :receiverThread_(0) { 96 assert(instance_==0); 97 instance_=this; 85 98 quit=false; 86 99 enet_address_set_host (& bindAddress, address.c_str()); 87 100 bindAddress.port = NETWORK_PORT; 88 head_ = head; 89 } 90 91 ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head) : receiverThread_(0) { 101 } 102 103 ConnectionManager::ConnectionManager(int port, const char *address) : receiverThread_(0) { 104 assert(instance_==0); 105 instance_=this; 92 106 quit=false; 93 107 enet_address_set_host (& bindAddress, address); 94 108 bindAddress.port = NETWORK_PORT; 95 head_ = head; 109 } 110 111 ConnectionManager::~ConnectionManager(){ 112 instance_=0; 113 if(!quit) 114 quitListener(); 96 115 } 97 116 … … 140 159 return true; 141 160 } 142 161 162 // bool ConnectionManager::addPacket(Packet::Packet *packet){ 163 // ClientInformation *temp = instance_->head_->findClient(packet->getClientID()); 164 // if(!temp){ 165 // COUT(3) << "C.Man: addPacket findClient failed" << std::endl; 166 // return false; 167 // } 168 // ENetPacket *packet = new ENetPacket; 169 // // TODO: finish implementation 170 // } 171 // 172 143 173 bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) { 144 ClientInformation *temp = head_->findClient(&(peer->address)); 145 if(!temp) 146 return false; 147 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 148 if(enet_peer_send(peer, (enet_uint8)temp->getID() , packet)!=0) 174 boost::recursive_mutex::scoped_lock lock(instance_->enet_mutex_); 175 if(enet_peer_send(peer, NETWORK_DEFAULT_CHANNEL, packet)!=0) 149 176 return false; 150 177 return true; … … 152 179 153 180 bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) { 154 ClientInformation *temp = head_->findClient(clientID);181 ClientInformation *temp = ClientInformation::findClient(clientID); 155 182 if(!temp){ 156 183 COUT(3) << "C.Man: addPacket findClient failed" << std::endl; 157 184 return false; 158 185 } 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 } 164 return true; 186 return addPacket(packet, temp->getPeer()); 165 187 } 166 188 167 189 bool ConnectionManager::addPacketAll(ENetPacket *packet) { 168 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 169 for(ClientInformation *i=head_->next(); i!=0; i=i->next()){ 190 if(!instance_) 191 return false; 192 boost::recursive_mutex::scoped_lock lock(instance_->enet_mutex_); 193 for(ClientInformation *i=ClientInformation::getBegin()->next(); i!=0; i=i->next()){ 170 194 COUT(3) << "adding broadcast packet for client: " << i->getID() << std::endl; 171 195 if(enet_peer_send(i->getPeer(), 0, packet)!=0) … … 177 201 // we actually dont need that function, because host_service does that for us 178 202 bool ConnectionManager::sendPackets() { 179 if(server==NULL )203 if(server==NULL || !instance_) 180 204 return false; 181 205 boost::recursive_mutex::scoped_lock lock(enet_mutex_); … … 257 281 void ConnectionManager::disconnectClients() { 258 282 ENetEvent event; 259 ClientInformation *temp = head_->next();283 ClientInformation *temp = ClientInformation::getBegin()->next(); 260 284 while(temp!=0){ 261 285 { … … 267 291 } 268 292 //bugfix: might be the reason why server crashes when clients disconnects 269 temp = head_->next();293 temp = ClientInformation::getBegin()->next(); 270 294 boost::recursive_mutex::scoped_lock lock(enet_mutex_); 271 295 while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) >= 0){ … … 279 303 case ENET_EVENT_TYPE_DISCONNECT: 280 304 COUT(4) << "disconnecting all clients" << std::endl; 281 if( head_->findClient(&(event.peer->address)))282 delete head_->findClient(&(event.peer->address));305 if(ClientInformation::findClient(&(event.peer->address))) 306 delete ClientInformation::findClient(&(event.peer->address)); 283 307 //maybe needs bugfix: might also be a reason for the server to crash 284 308 temp = temp->next(); … … 302 326 303 327 int ConnectionManager::getClientID(ENetAddress address) { 304 return head_->findClient(&address)->getID();328 return ClientInformation::findClient(&address)->getID(); 305 329 } 306 330 307 331 ENetPeer *ConnectionManager::getClientPeer(int clientID) { 308 return head_->findClient(clientID)->getPeer();332 return ClientInformation::findClient(clientID)->getPeer(); 309 333 } 310 334 -
code/branches/network/src/network/ConnectionManager.h
r1505 r1666 52 52 #include "PacketBuffer.h" 53 53 #include "PacketManager.h" 54 #include "packet/Packet.h" 54 55 55 56 namespace std … … 63 64 #define NETWORK_MAX_CONNECTIONS 50 64 65 #define NETWORK_WAIT_TIMEOUT 1 66 #define NETWORK_DEFAULT_CHANNEL 0 65 67 66 68 struct ClientList{ … … 73 75 public: 74 76 ConnectionManager(); 75 ConnectionManager(ClientInformation *head); 76 ConnectionManager(ClientInformation *head, int port); 77 ConnectionManager(int port, const char *address, ClientInformation *head); 78 ConnectionManager(int port, std::string address, ClientInformation *head); 77 //ConnectionManager(ClientInformation *head); 78 ConnectionManager(int port); 79 ConnectionManager(int port, const char *address); 80 ConnectionManager(int port, std::string address); 81 ~ConnectionManager(); 79 82 //ENetPacket *getPacket(ENetAddress &address); // thread1 80 83 //ENetPacket *getPacket(int &clientID); … … 83 86 void createListener(); 84 87 bool quitListener(); 85 bool addPacket(ENetPacket *packet, ENetPeer *peer); 86 bool addPacket(ENetPacket *packet, int ID); 87 bool addPacketAll(ENetPacket *packet); 88 // bool addPacket(Packet::Packet *packet); 89 static bool addPacket(ENetPacket *packet, ENetPeer *peer); 90 static bool addPacket(ENetPacket *packet, int ID); 91 static bool addPacketAll(ENetPacket *packet); 88 92 // bool sendPackets(ENetEvent *event); 89 93 bool sendPackets(); … … 113 117 114 118 bool quit; // quit-variable (communication with threads) 115 ClientInformation *head_;116 119 117 120 boost::thread *receiverThread_; 118 121 static boost::recursive_mutex enet_mutex_; 119 // int getNumberOfClients(); 120 //functions to map what object every clients uses 121 /*std::map<int, int> clientsShip; 122 void addClientsObjectID( int clientID, int objectID ); 123 int getClientsShipID( int clientID ); 124 int getObjectsClientID( int objectID ); 125 void deleteClientIDReg( int clientID ); 126 void deleteObjectIDReg( int objectID );*/ 122 static ConnectionManager *instance_; 123 127 124 }; 128 125 -
code/branches/network/src/network/GameStateManager.cc
r1534 r1666 53 53 namespace network 54 54 { 55 GameStateManager::GameStateManager( ClientInformation *head) {55 GameStateManager::GameStateManager() { 56 56 id_=0; 57 head_=head;58 57 } 59 58 … … 130 129 //why are we searching the same client's gamestate id as we searched in 131 130 //Server::sendGameState? 132 int gID = head_->findClient(clientID)->getGamestateID();131 int gID = ClientInformation::findClient(clientID)->getGamestateID(); 133 132 COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id_ << " diffed from: " << gID << std::endl; 134 133 // COUT(3) << "gamestatemap: " << &gameStateMap << std::endl; … … 467 466 468 467 void GameStateManager::ackGameState(int clientID, int gamestateID) { 469 ClientInformation *temp = head_->findClient(clientID);468 ClientInformation *temp = ClientInformation::findClient(clientID); 470 469 if(temp==0) 471 470 return; … … 524 523 if(client->getGamestateID()>=0) 525 524 gameStateUsed[client->getGamestateID()]--; 526 head_->removeClient(client->getID());525 ClientInformation::removeClient(client->getID()); 527 526 } 528 527 -
code/branches/network/src/network/GameStateManager.h
r1505 r1666 69 69 class GameStateManager{ 70 70 public: 71 GameStateManager( ClientInformation *head);71 GameStateManager(); 72 72 ~GameStateManager(); 73 73 … … 96 96 std::map<int, GameStateCompressed*> gameStateQueue; 97 97 GameState *reference; 98 ClientInformation *head_;99 98 int id_; 100 99 -
code/branches/network/src/network/Server.cc
r1556 r1666 48 48 #include "GameStateManager.h" 49 49 #include "ClientInformation.h" 50 //#include "NetworkFrameListener.h"51 50 #include "util/Sleep.h" 52 51 #include "objects/SpaceShip.h" 53 52 #include "core/ConsoleCommand.h" 53 #include "packet/Chat.h" 54 #include "packet/Packet.h" 54 55 55 56 namespace network … … 58 59 #define NETWORK_FREQUENCY 30 59 60 60 Server *Server::instance_=0;61 62 Server *Server::createSingleton(){63 if(!instance_)64 instance_ = new Server();65 return instance_;66 }67 Server *Server::createSingleton(int port){68 if(!instance_)69 instance_ = new Server(port);70 return instance_;71 }72 Server *Server::createSingleton(int port, std::string bindAddress){73 if(!instance_)74 instance_ = new Server(port, bindAddress);75 return instance_;76 }77 Server *Server::createSingleton(int port, const char *bindAddress){78 if(!instance_)79 instance_ = new Server(port, bindAddress);80 return instance_;81 }82 83 Server *Server::getSingleton(){84 return instance_;85 }86 87 88 61 /** 89 62 * Constructor for default values (bindaddress is set to ENET_HOST_ANY … … 93 66 timeSinceLastUpdate_=0; 94 67 packet_gen = PacketGenerator(); 95 clients = new ClientInformation(true); 96 connection = new ConnectionManager(clients); 97 gamestates = new GameStateManager(clients); 68 connection = new ConnectionManager(); 69 gamestates = new GameStateManager(); 98 70 } 99 71 … … 101 73 timeSinceLastUpdate_=0; 102 74 packet_gen = PacketGenerator(); 103 clients = new ClientInformation(true); 104 connection = new ConnectionManager(clients, port); 105 gamestates = new GameStateManager(clients); 75 connection = new ConnectionManager(port); 76 gamestates = new GameStateManager(); 106 77 } 107 78 … … 114 85 timeSinceLastUpdate_=0; 115 86 packet_gen = PacketGenerator(); 116 clients = new ClientInformation(); 117 connection = new ConnectionManager(port, bindAddress, clients); 118 gamestates = new GameStateManager(clients); 87 connection = new ConnectionManager(port, bindAddress); 88 gamestates = new GameStateManager(); 119 89 } 120 90 … … 127 97 timeSinceLastUpdate_=0; 128 98 packet_gen = PacketGenerator(); 129 clients = new ClientInformation(); 130 connection = new ConnectionManager(port, bindAddress, clients); 131 gamestates = new GameStateManager(clients); 99 connection = new ConnectionManager(port, bindAddress); 100 gamestates = new GameStateManager(); 132 101 } 133 102 … … 148 117 } 149 118 119 bool Server::processChat(packet::Chat *message, unsigned int clientID){ 120 ClientInformation *temp = ClientInformation::getBegin(); 121 packet::Packet *pkt; 122 while(temp){ 123 pkt = new packet::Packet(message); 124 pkt->setClientID(temp->getID()); 125 if(!pkt->send()) 126 COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl; 127 } 128 return message->process(); 129 } 130 150 131 /** 151 132 * This function sends out a message to all clients … … 153 134 * @return true/false 154 135 */ 155 bool Server::sendChat(std::string msg) { 156 return sendChat(msg.c_str()); 136 bool Server::sendChat(packet::Chat *chat) { 137 //TODO: change this (no informations about who wrote a message) 138 ClientInformation *temp = ClientInformation::getBegin(); 139 packet::Packet *pkt; 140 while(temp){ 141 pkt = new packet::Packet(chat); 142 pkt->setClientID(temp->getID()); 143 if(!pkt->send()) 144 COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl; 145 } 146 return chat->process(); 157 147 } 158 148 … … 162 152 * @return true/false 163 153 */ 164 bool Server::sendChat(const char *msg) {165 char *message = new char [strlen(msg)+10+1];166 sprintf(message, "Player %d: %s", CLIENTID_SERVER, msg);167 COUT(1) << message << std::endl;168 ENetPacket *packet = packet_gen.chatMessage(message);169 COUT(5) <<"Server: adding Packets" << std::endl;170 return connection->addPacketAll(packet);171 }154 // bool Server::sendChat(const char *msg) { 155 // char *message = new char [strlen(msg)+10+1]; 156 // sprintf(message, "Player %d: %s", CLIENTID_SERVER, msg); 157 // COUT(1) << message << std::endl; 158 // ENetPacket *packet = packet_gen.chatMessage(message); 159 // COUT(5) <<"Server: adding Packets" << std::endl; 160 // return connection->addPacketAll(packet); 161 // } 172 162 173 163 /** … … 190 180 // usleep(5000); // TODO remove 191 181 return; 182 } 183 184 bool Server::queuePacket(ENetPacket *packet, int clientID){ 185 return connection->addPacket(packet, clientID); 192 186 } 193 187 … … 211 205 break; 212 206 case ENET_EVENT_TYPE_DISCONNECT: 213 if( clients->findClient(&event->peer->address))207 if(ClientInformation::findClient(&event->peer->address)) 214 208 disconnectClient(event); 215 209 break; 216 210 case ENET_EVENT_TYPE_RECEIVE: 217 if( clients->findClient(&event->peer->address)){218 clientID = clients->findClient(&event->peer->address)->getID();211 if(ClientInformation::findClient(&event->peer->address)){ 212 clientID = ClientInformation::findClient(&event->peer->address)->getID(); 219 213 if( !elaborate(event->packet, clientID) ) 220 214 COUT(3) << "Server: could not elaborate" << std::endl; … … 247 241 bool Server::sendGameState() { 248 242 COUT(5) << "Server: starting function sendGameState" << std::endl; 249 ClientInformation *temp = clients;243 ClientInformation *temp = ClientInformation::getBegin(); 250 244 bool added=false; 251 245 while(temp != NULL){ 252 if(temp->getHead()){253 temp=temp->next();254 //think this works without continue255 continue;256 }257 246 if( !(temp->getSynched()) ){ 258 247 COUT(5) << "Server: not sending gamestate" << std::endl; 259 248 temp=temp->next(); 249 if(!temp) 250 break; 260 251 //think this works without continue 261 252 continue; … … 320 311 } 321 312 322 void Server::processChat( chat *data, int clientId){323 char *message = new char [strlen(data->message)+10+1];324 sprintf(message, "Player %d: %s", clientId, data->message);325 COUT(1) << message << std::endl;326 ENetPacket *pck = packet_gen.chatMessage(message);327 connection->addPacketAll(pck);328 delete[] data->message;329 delete data;330 }313 // void Server::processChat( chat *data, int clientId){ 314 // char *message = new char [strlen(data->message)+10+1]; 315 // sprintf(message, "Player %d: %s", clientId, data->message); 316 // COUT(1) << message << std::endl; 317 // ENetPacket *pck = packet_gen.chatMessage(message); 318 // connection->addPacketAll(pck); 319 // delete[] data->message; 320 // delete data; 321 // } 331 322 332 323 bool Server::addClient(ENetEvent *event){ 333 ClientInformation *temp = clients->insertBack(new ClientInformation);324 ClientInformation *temp = ClientInformation::insertBack(new ClientInformation); 334 325 if(!temp){ 335 326 COUT(2) << "Server: could not add client" << std::endl; 336 327 return false; 337 328 } 338 if(temp->prev()->get Head()) { //not good if you use anything else than insertBack329 if(temp->prev()->getBegin()) { //not good if you use anything else than insertBack 339 330 temp->prev()->setID(0); //bugfix: not necessary but usefull 340 331 temp->setID(1); … … 348 339 349 340 bool Server::createClient(int clientID){ 350 ClientInformation *temp = clients->findClient(clientID);341 ClientInformation *temp = ClientInformation::findClient(clientID); 351 342 if(!temp){ 352 343 COUT(2) << "Conn.Man. could not create client with id: " << clientID << std::endl; … … 376 367 } 377 368 orxonox::SpaceShip *no = dynamic_cast<orxonox::SpaceShip *>(id->fabricate()); 369 no->classID = id->getNetworkID(); 370 client->setShipID(no->objectID); 378 371 no->setPosition(orxonox::Vector3(0,0,80)); 379 372 no->setScale(10); … … 388 381 no->setRotDamp(1.0); 389 382 no->setCamera("cam_"+client->getID()); 390 no->classID = id->getNetworkID();391 383 no->create(); 384 no->setBacksync(true); 392 385 393 client->setShipID(no->objectID);394 386 return true; 395 387 } … … 401 393 //boost::recursive_mutex::scoped_lock lock(head_->mutex_); 402 394 orxonox::Iterator<orxonox::SpaceShip> it = orxonox::ObjectList<orxonox::SpaceShip>::start(); 403 ClientInformation *client = clients->findClient(&event->peer->address);395 ClientInformation *client = ClientInformation::findClient(&event->peer->address); 404 396 if(!client) 405 397 return false; … … 412 404 ++it; 413 405 delete *temp; 414 return clients->removeClient(event->peer);406 return ClientInformation::removeClient(event->peer); 415 407 } 416 408 return false; … … 418 410 419 411 void Server::disconnectClient(int clientID){ 420 ClientInformation *client = clients->findClient(clientID);412 ClientInformation *client = ClientInformation::findClient(clientID); 421 413 if(client) 422 414 disconnectClient(client); -
code/branches/network/src/network/Server.h
r1535 r1666 46 46 #include <string> 47 47 48 #include "Host.h" 48 49 #include "PacketManager.h" 49 50 … … 58 59 * It implements all functions necessary for a Server 59 60 */ 60 class _NetworkExport Server : public PacketDecoder {61 class _NetworkExport Server : public PacketDecoder, public Host{ 61 62 public: 62 static Server *createSingleton(); 63 static Server *createSingleton(int port); 64 static Server *createSingleton(int port, std::string bindAddress); 65 static Server *createSingleton(int port, const char *bindAddress); 66 67 static Server *getSingleton(); 63 Server(); 64 Server(int port); 65 Server(int port, std::string bindAddress); 66 Server(int port, const char *bindAddress); 68 67 69 68 void open(); 70 69 void close(); 71 bool sendChat(std::string msg); 72 bool sendChat(const char *msg); 70 bool processChat(packet::Chat *message, unsigned int clientID); 71 bool sendChat(packet::Chat *chat); 72 bool queuePacket(ENetPacket *packet, int clientID); 73 73 void tick(float time); 74 74 protected: … … 76 76 void updateGamestate(); 77 77 private: 78 Server(); 79 Server(int port); 80 Server(int port, std::string bindAddress); 81 Server(int port, const char *bindAddress); 82 83 static Server *instance_; 78 int shipID(){return 0;} 79 int playerID(){return 0;} 84 80 85 81 bool addClient(ENetEvent *event); … … 93 89 bool processConnectRequest( connectRequest *con, int clientID ); 94 90 void processGamestate( GameStateCompressed *data, int clientID); 95 void processChat( chat *data, int clientId);91 //void processChat( chat *data, int clientId); 96 92 ConnectionManager *connection; 97 93 GameStateManager *gamestates; 98 94 PacketGenerator packet_gen; 99 95 100 ClientInformation *clients;96 101 97 float timeSinceLastUpdate_; 102 98 };
Note: See TracChangeset
for help on using the changeset viewer.