Changeset 436
- Timestamp:
- Dec 9, 2007, 12:42:46 PM (17 years ago)
- Location:
- code/branches/FICN/src/network
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/FICN/src/network/ClientInformation.cc
r432 r436 16 16 ClientInformation::ClientInformation() 17 17 { 18 gamestateID_=GAMESTATEID_INITIAL; 18 19 preve=0; 19 20 nexte=0; 21 this->head=false; 22 } 23 24 ClientInformation::ClientInformation(bool head) 25 { 26 gamestateID_=GAMESTATEID_INITIAL; 27 preve=0; 28 nexte=0; 29 this->head=head; 20 30 } 21 31 // … … 54 64 } 55 65 56 void ClientInformation::setPrev(ClientInformation *prev){ 57 this->preve = prev; 66 bool ClientInformation::setPrev(ClientInformation *prev){ 67 if(!head) 68 this->preve = prev; 69 else 70 return false; 71 return true; 58 72 } 59 73 60 voidClientInformation::setNext(ClientInformation *next){74 bool ClientInformation::setNext(ClientInformation *next){ 61 75 this->nexte = next; 76 return true; 62 77 } 63 78 64 voidClientInformation::insertAfter(ClientInformation *ins){79 ClientInformation *ClientInformation::insertAfter(ClientInformation *ins){ 65 80 this->nexte->setPrev(ins); 66 81 ins->setNext(this->nexte); 67 82 ins->setPrev(this); 68 83 this->nexte = ins; 84 return ins; 69 85 } 70 86 71 voidClientInformation::insertBefore(ClientInformation *ins){87 ClientInformation *ClientInformation::insertBefore(ClientInformation *ins){ 72 88 this->prev()->setNext(ins); 73 89 ins->setPrev(this->preve); 74 90 this->preve=ins; 75 91 ins->setNext(this); 92 return ins; 93 } 94 95 void ClientInformation::setID(int clientID){ 96 clientID_ = clientID; 97 } 98 void ClientInformation::setPeer(ENetPeer *peer){ 99 peer_ = peer; 100 } 101 102 void ClientInformation::setGamestateID(int id){ 103 gamestateID_=id; 104 } 105 106 int ClientInformation::getID(){ 107 return clientID_; 108 } 109 ENetPeer *ClientInformation::getPeer(){ 110 return peer_; 111 } 112 113 int ClientInformation::getGamestateID(){ 114 return gamestateID_; 115 } 116 117 ClientInformation *ClientInformation::insertBack(ClientInformation *ins){ 118 ClientInformation *temp = this; 119 while(temp->next()!=0){ 120 temp = temp->next(); 121 } 122 temp->setNext(ins); 123 ins->setPrev(temp); 124 return ins; 125 } 126 127 bool ClientInformation::removeClient(int clientID){ 128 ClientInformation *temp = this; 129 while(temp!=0 && temp->getID()!=clientID) 130 temp = temp->next(); 131 if(temp==0) 132 return false; 133 delete temp; 134 return true; 135 } 136 137 bool ClientInformation::removeClient(ENetPeer *peer){ 138 ClientInformation *temp = this; 139 while(temp!=0 && (temp->getPeer()->address.host!=peer->address.host || temp->getPeer()->address.port!=peer->address.port)) 140 temp = temp->next(); 141 if(temp==0) 142 return false; 143 delete temp; 144 return true; 145 } 146 147 /** 148 * This function goes forward through the list and looks for an element with clientID 149 * This function should only be applied to the head of the list 150 * @param clientID id to look for 151 * @return pointer to the element in the list or 0 if the search was unsuccessfull 152 */ 153 ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards){ 154 ClientInformation *temp = this; 155 while(temp!=0 && temp->getID()!=clientID) 156 temp = temp->next(); 157 // returns 0 if nothing has been found 158 return temp; 159 } 160 161 /** 162 * This function goes forward through the list and looks for an element with clientID 163 * This function should only be applied to the head of the list 164 * @param peer peer to look for 165 * @return pointer to the element in the list 166 */ 167 ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards){ 168 ClientInformation *temp = this; 169 while(temp!=0 && (temp->getPeer()->address.host!=address->host || temp->getPeer()->address.port != address->port)) 170 temp = temp->next(); 171 // returns 0 if nothing has been found 172 return temp; 76 173 } 77 174 -
code/branches/FICN/src/network/ClientInformation.h
r432 r436 13 13 #define NETWORKCLIENTINFORMATION_H 14 14 15 #include <enet/enet.h> 16 17 #define GAMESTATEID_INITIAL -1 18 15 19 namespace network { 16 20 … … 22 26 public: 23 27 ClientInformation(); 28 ClientInformation(bool head); 24 29 // ClientInformation(ClientInformation *prev, ClientInformation *next); 25 30 // ClientInformation(ClientInformation *prev); … … 27 32 ClientInformation *next(); 28 33 ClientInformation *prev(); 29 void setNext(ClientInformation *next); 30 void setPrev(ClientInformation *prev); 31 void insertAfter(ClientInformation *ins); 32 void insertBefore(ClientInformation *ins); 34 bool setNext(ClientInformation *next); 35 bool setPrev(ClientInformation *prev); 36 ClientInformation *insertAfter(ClientInformation *ins); 37 ClientInformation *insertBefore(ClientInformation *ins); 38 ClientInformation *insertBack(ClientInformation *ins); 39 void setID(int clientID); 40 void setPeer(ENetPeer *peer); 41 void setGamestateID(int id); 42 int getID(); 43 ENetPeer *getPeer(); 44 int getGamestateID(); 45 bool removeClient(int clientID); 46 bool removeClient(ENetPeer *peer); 47 ClientInformation *findClient(int clientID, bool look_backwards=false); 48 ClientInformation *findClient(ENetAddress *address, bool look_backwards=false); 49 bool head; 33 50 34 51 private: 35 52 ClientInformation *preve; 36 53 ClientInformation *nexte; 54 //actual information: 55 ENetPeer *peer_; 56 int clientID_; 57 int gamestateID_; 37 58 }; 38 59 -
code/branches/FICN/src/network/ConnectionManager.cc
r415 r436 25 25 boost::thread_group network_threads; 26 26 27 ConnectionManager::ConnectionManager( ){27 ConnectionManager::ConnectionManager(ClientInformation *head){ 28 28 quit=false; 29 29 bindAddress.host = ENET_HOST_ANY; 30 30 bindAddress.port = NETWORK_PORT; 31 } 32 33 ConnectionManager::ConnectionManager(int port, std::string address){ 31 head_ = head; 32 } 33 34 ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head){ 34 35 quit=false; 35 36 enet_address_set_host (& bindAddress, address.c_str()); 36 37 bindAddress.port = NETWORK_PORT; 37 } 38 39 ConnectionManager::ConnectionManager(int port, const char *address){ 38 head_ = head; 39 } 40 41 ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head){ 40 42 quit=false; 41 43 enet_address_set_host (& bindAddress, address); 42 44 bindAddress.port = NETWORK_PORT; 45 head_ = head; 43 46 } 44 47 … … 53 56 ENetAddress address; 54 57 ENetPacket *packet=getPacket(address); 55 clientID= clientMap.find(address)->second;58 clientID=head_->findClient(&address)->getID(); 56 59 return packet; 57 60 } … … 74 77 75 78 bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer){ 76 if(clientVector.size()==0) 77 return false; 78 if(enet_peer_send(peer, clientMap.find(peer->address)->second , packet)!=0) 79 if(enet_peer_send(peer, head_->findClient(&(peer->address))->getID() , packet)!=0) 79 80 return false; 80 81 return true; … … 82 83 83 84 bool ConnectionManager::addPacket(ENetPacket *packet, int clientID){ 84 if(clientVector.size()==0) 85 return false; 86 if(enet_peer_send(&(peerMap.find(clientVector[clientID])->second), clientID, packet)!=0) 85 if(enet_peer_send(head_->findClient(clientID)->getPeer(), clientID, packet)!=0) 87 86 return false; 88 87 return true; … … 90 89 91 90 bool ConnectionManager::addPacketAll(ENetPacket *packet){ 92 for( unsigned int i=0; i<clientVector.size(); i++){93 if(enet_peer_send( &(peerMap.find(clientVector[i])->second), i, packet)!=0)91 for(ClientInformation *i=head_->next(); i!=0; i=i->next()){ 92 if(enet_peer_send(i->getPeer(), i->getID(), packet)!=0) 94 93 return false; 95 94 } … … 156 155 157 156 void ConnectionManager::disconnectClients(){ 158 bool disconnected=false;159 157 ENetEvent event; 160 for(unsigned int i=0; i<clientVector.size(); i++){ 161 enet_peer_disconnect(&(peerMap.find(clientVector[i])->second), 0); 162 while( !disconnected && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){ 163 switch (event.type) 164 { 165 case ENET_EVENT_TYPE_NONE: 166 case ENET_EVENT_TYPE_CONNECT: 167 case ENET_EVENT_TYPE_RECEIVE: 168 enet_packet_destroy(event.packet); 169 break; 170 case ENET_EVENT_TYPE_DISCONNECT: 171 disconnected=true; 172 break; 173 } 174 } 175 disconnected=false; 158 ClientInformation *temp = head_->next(); 159 while(temp!=0){ 160 enet_peer_disconnect(temp->getPeer(), 0); 161 temp = temp->next(); 162 } 163 temp = temp->next(); 164 while( temp!=0 && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){ 165 switch (event.type) 166 { 167 case ENET_EVENT_TYPE_NONE: 168 case ENET_EVENT_TYPE_CONNECT: 169 case ENET_EVENT_TYPE_RECEIVE: 170 enet_packet_destroy(event.packet); 171 break; 172 case ENET_EVENT_TYPE_DISCONNECT: 173 delete head_->findClient(&(event.peer->address)); 174 temp = temp->next(); 175 break; 176 } 176 177 } 177 178 return; … … 184 185 } 185 186 187 // bool ConnectionManager::clientDisconnect(ENetPeer *peer){ 188 // return clientDisconnect(*peer); 189 // } 190 191 192 186 193 bool ConnectionManager::clientDisconnect(ENetPeer *peer){ 187 return clientDisconnect(*peer); 188 } 189 190 bool ConnectionManager::clientDisconnect(ENetPeer peer){ 191 std::map<ENetAddress, int>::iterator it; 192 it=clientMap.find(peer.address); 193 clientVector.erase(clientVector.begin()+it->second); 194 clientMap.erase(it); 195 peerMap.erase(peerMap.find(peer.address)); 194 return head_->removeClient(peer); 196 195 return true; 197 196 } 198 197 199 198 bool ConnectionManager::addClient(ENetEvent *event){ 200 int id=clientVector.size(); 201 clientVector.push_back((event->peer->address)); 202 clientMap[event->peer->address]=id; 203 peerMap[event->peer->address]=*(event->peer); 204 syncClassid(id); 199 ClientInformation *temp = head_->insertBack(new ClientInformation); 200 temp->setID(temp->prev()->getID()); 201 temp->setPeer(event->peer); 205 202 return true; 206 203 } 207 204 208 205 int ConnectionManager::getClientID(ENetPeer peer){ 209 return clientMap.find(peer.address)->second;206 return getClientID(peer.address); 210 207 } 211 208 212 209 int ConnectionManager::getClientID(ENetAddress address){ 213 return clientMap.find(address)->second;214 } 215 216 ENetPeer ConnectionManager::getClientPeer(int clientID){217 return peerMap.find(clientVector[clientID])->second;210 return head_->findClient(&address)->getID(); 211 } 212 213 ENetPeer *ConnectionManager::getClientPeer(int clientID){ 214 return head_->findClient(clientID)->getPeer(); 218 215 } 219 216 -
code/branches/FICN/src/network/ConnectionManager.h
r400 r436 23 23 #include <boost/bind.hpp> 24 24 // headerfiles 25 #include "ClientInformation.h" 25 26 #include "ConnectionManager.h" 26 27 #include "PacketBuffer.h" … … 47 48 class ConnectionManager{ 48 49 public: 49 ConnectionManager( );50 ConnectionManager(int port, const char *address );51 ConnectionManager(int port, std::string address );50 ConnectionManager(ClientInformation *head); 51 ConnectionManager(int port, const char *address, ClientInformation *head); 52 ConnectionManager(int port, std::string address, ClientInformation *head); 52 53 ENetPacket *getPacket(ENetAddress &address); // thread1 53 54 ENetPacket *getPacket(int &clientID); … … 60 61 bool sendPackets(ENetEvent *event); 61 62 bool sendPackets(); 62 63 private: 63 64 bool clientDisconnect(ENetPeer *peer); 64 bool clientDisconnect(ENetPeer peer);65 //bool clientDisconnect(ENetPeer peer); 65 66 bool processData(ENetEvent *event); 66 67 bool addClient(ENetEvent *event); … … 70 71 int getClientID(ENetAddress address); 71 72 void syncClassid(int clientID); 72 ENetPeer getClientPeer(int clientID);73 ENetPeer *getClientPeer(int clientID); 73 74 PacketBuffer buffer; 74 75 PacketGenerator packet_gen; … … 78 79 79 80 bool quit; // quit-variable (communication with threads) 80 std::map<ENetAddress, int> clientMap; 81 std::map<ENetAddress, ENetPeer> peerMap; 82 std::vector<ENetAddress> clientVector; 81 ClientInformation *head_; 83 82 }; 84 83 -
code/branches/FICN/src/network/GameStateClient.cc
r416 r436 13 13 14 14 bool GameStateClient::pushGameState(GameStateCompressed compstate){ 15 return loadSnapshot(decode(reference, compstate)); 15 if(compstate.diffed) 16 return loadSnapshot(decode(reference, compstate)); 17 else 18 return loadSnapshot(decode(compstate)); 16 19 } 17 20 … … 127 130 gamestate.size = normsize; 128 131 gamestate.data = dest; 132 gamestate.diffed = a.diffed; 129 133 130 134 return gamestate; … … 137 141 } 138 142 143 GameState GameStateClient::decode(GameStateCompressed x){ 144 GameState t = decompress(x); 145 return t; 146 } 147 139 148 140 149 -
code/branches/FICN/src/network/GameStateClient.h
r416 r436 33 33 GameState decompress(GameStateCompressed a); 34 34 GameState decode(GameState a, GameStateCompressed x); 35 GameState decode(GameStateCompressed x); 35 36 void removeObject(orxonox::Iterator<Synchronisable> &it); 36 37 -
code/branches/FICN/src/network/GameStateManager.cc
r425 r436 14 14 namespace network { 15 15 16 GameStateManager::GameStateManager( )16 GameStateManager::GameStateManager(ClientInformation *head) 17 17 { 18 18 id=0; 19 head_=head; 19 20 } 20 21 … … 25 26 void GameStateManager::update(){ 26 27 reference = getSnapshot(id); 27 idGameState[id]=reference; 28 gameStateMap.insert(std::pair<int, GameState*>(id, reference)); 29 gameStateUsed[id]=0; 28 30 ++id; 29 31 return; … … 31 33 32 34 GameStateCompressed GameStateManager::popGameState(int clientID){ 33 GameState *client = clientGameState[clientID]; 35 int gID = head_->findClient(clientID)->getGamestateID(); 36 if(gID!=GAMESTATEID_INITIAL){ 37 GameState *client = gameStateMap[gID]; 38 GameState *server = reference; 39 return encode(client, server); 40 } 34 41 GameState *server = reference; 35 return encode(client, server); 42 return encode(server); 43 // return an undiffed gamestate and set appropriate flags 36 44 } 37 45 … … 85 93 86 94 GameStateCompressed GameStateManager::encode(GameState *a, GameState *b){ 87 GameState r = diff(a,b); 88 return compress_(r); 95 GameState r = diff(a,b); 96 r.diffed = true; 97 return compress_(&r); 89 98 } 90 99 100 GameStateCompressed GameStateManager::encode(GameState *a){ 101 a->diffed=false; 102 return compress_(a); 103 } 91 104 92 105 GameState GameStateManager::diff(GameState *a, GameState *b){ … … 122 135 } 123 136 124 GameStateCompressed GameStateManager::compress_(GameState a) {125 int size = a .size;126 uLongf buffer = (uLongf)((a .size + 12)*1.01)+1;137 GameStateCompressed GameStateManager::compress_(GameState *a) { 138 int size = a->size; 139 uLongf buffer = (uLongf)((a->size + 12)*1.01)+1; 127 140 unsigned char* dest = (unsigned char*)malloc( buffer ); 128 141 int retval; 129 retval = compress( dest, &buffer, a .data, (uLong)size );142 retval = compress( dest, &buffer, a->data, (uLong)size ); 130 143 131 144 switch ( retval ) { … … 141 154 compressedGamestate.id = GAMESTATE; 142 155 compressedGamestate.data = dest; 156 compressedGamestate.diffed = a->diffed; 143 157 144 158 return compressedGamestate; … … 146 160 147 161 void GameStateManager::ackGameState(int clientID, int gamestateID){ 162 ClientInformation *temp = head_->findClient(clientID); 163 int curid = temp->getID(); 164 // decrease usage of gamestate and save it 165 deleteUnusedGameState(curid); 166 //increase gamestateused 167 ++gameStateUsed.find(gamestateID)->second; 168 temp->setGamestateID(gamestateID); 169 /* 148 170 GameState *old = clientGameState[clientID]; 149 171 deleteUnusedGameState(old); 150 clientGameState[clientID]=idGameState[gamestateID]; 172 clientGameState[clientID]=idGameState[gamestateID];*/ 151 173 } 152 174 153 bool GameStateManager::deleteUnusedGameState(GameState *state){ 154 for(unsigned int i=0; i<clientGameState.size(); i++){ 155 if(clientGameState[i]==state) 156 return false; 175 bool GameStateManager::deleteUnusedGameState(int gamestateID){ 176 int used = --(gameStateUsed.find(gamestateID)->second); 177 if(id-gamestateID>KEEP_GAMESTATES && used==0){ 178 // delete gamestate 179 delete gameStateMap.find(gamestateID)->second; 180 gameStateMap.erase(gamestateID); 181 return true; 157 182 } 158 delete state; 159 return true; 160 } 161 162 void GameStateManager::removeClient(int clientID){ 163 clientGameState.erase(clientGameState.begin()+clientID); 183 return false; 164 184 } 165 185 -
code/branches/FICN/src/network/GameStateManager.h
r425 r436 13 13 #define NETWORK_GAMESTATEMANAGER_H 14 14 15 #include < vector>15 #include <map> 16 16 17 17 #include "zlib.h" 18 18 19 #include "ClientInformation.h" 19 20 #include "Synchronisable.h" 20 21 #include "orxonox/core/IdentifierIncludes.h" … … 24 25 namespace network { 25 26 26 27 #define KEEP_GAMESTATES 20 27 28 28 29 /** … … 43 44 class GameStateManager{ 44 45 public: 45 GameStateManager( );46 GameStateManager(ClientInformation *head); 46 47 ~GameStateManager(); 47 48 void update(); 48 49 GameStateCompressed popGameState(int clientID); 49 50 void ackGameState(int clientID, int gamestateID); 50 void removeClient(int clientID);51 51 private: 52 52 GameState *getSnapshot(int id); 53 53 GameStateCompressed encode(GameState *a, GameState *b); 54 GameStateCompressed encode(GameState *a); 54 55 GameState diff(GameState *a, GameState *b); 55 GameStateCompressed compress_(GameState a);56 bool deleteUnusedGameState( GameState *state);56 GameStateCompressed compress_(GameState *a); 57 bool deleteUnusedGameState(int gamestateID); 57 58 58 std:: vector<GameState *> clientGameState;59 std:: vector<GameState *> idGameState;59 std::map<int, GameState*> gameStateMap; //map gsID to gamestate* 60 std::map<int, int> gameStateUsed; // save the number of clients, that use the specific gamestate 60 61 GameState *reference; 61 62 int id; 63 ClientInformation *head_; 62 64 }; 63 65 -
code/branches/FICN/src/network/PacketTypes.h
r417 r436 33 33 int id; 34 34 int size; 35 // new ---- change functions 36 bool diffed; 35 37 unsigned char *data; 36 38 }; … … 46 48 int compsize; 47 49 int normsize; 50 // new ----- change functions 51 bool diffed; 48 52 unsigned char *data; 49 53 }; -
code/branches/FICN/src/network/Server.cc
r425 r436 21 21 */ 22 22 Server::Server(){ 23 connection = ConnectionManager();24 gamestates = GameStateManager();25 23 packet_gen = PacketGenerator(); 24 clients = new ClientInformation(); 25 connection = new ConnectionManager(clients); 26 gamestates = new GameStateManager(clients); 26 27 } 27 28 … … 32 33 */ 33 34 Server::Server(int port, std::string bindAddress){ 34 connection = ConnectionManager(port, bindAddress);35 gamestates = GameStateManager();36 35 packet_gen = PacketGenerator(); 36 clients = new ClientInformation(); 37 connection = new ConnectionManager(port, bindAddress, clients); 38 gamestates = new GameStateManager(clients); 37 39 } 38 40 … … 43 45 */ 44 46 Server::Server(int port, const char *bindAddress){ 45 connection = ConnectionManager(port, bindAddress);46 gamestates = GameStateManager();47 47 packet_gen = PacketGenerator(); 48 clients = new ClientInformation(); 49 connection = new ConnectionManager(port, bindAddress, clients); 50 gamestates = new GameStateManager(clients); 48 51 } 49 52 … … 52 55 */ 53 56 void Server::open(){ 54 connection .createListener();57 connection->createListener(); 55 58 return; 56 59 } … … 60 63 */ 61 64 void Server::close(){ 62 connection .quitListener();65 connection->quitListener(); 63 66 return; 64 67 } … … 71 74 bool Server::sendMSG(std::string msg){ 72 75 ENetPacket *packet = packet_gen.chatMessage(msg.c_str()); 73 connection .addPacketAll(packet);74 return connection .sendPackets();76 connection->addPacketAll(packet); 77 return connection->sendPackets(); 75 78 } 76 79 /** … … 81 84 bool Server::sendMSG(const char *msg){ 82 85 ENetPacket *packet = packet_gen.chatMessage(msg); 83 connection .addPacketAll(packet);84 return connection .sendPackets();86 connection->addPacketAll(packet); 87 return connection->sendPackets(); 85 88 } 86 89 … … 101 104 ENetPacket *packet; 102 105 int clientID=-1; 103 while(!connection .queueEmpty()){104 packet = connection .getPacket(clientID);106 while(!connection->queueEmpty()){ 107 packet = connection->getPacket(clientID); 105 108 elaborate(packet, clientID); 106 109 } … … 111 114 */ 112 115 void Server::updateGamestate(){ 113 gamestates .update();116 gamestates->update(); 114 117 sendGameState(); 115 118 } -
code/branches/FICN/src/network/Server.h
r422 r436 20 20 #include "GameStateManager.h" 21 21 #include "enet/enet.h" 22 #include "ClientInformation.h" 22 23 23 24 namespace network{ … … 39 40 private: 40 41 bool sendGameState(); 41 ConnectionManager connection;42 GameStateManager gamestates;42 ConnectionManager *connection; 43 GameStateManager *gamestates; 43 44 PacketGenerator packet_gen; 44 45 45 46 void processQueue(); 46 47 void updateGamestate(); 48 ClientInformation *clients; 47 49 }; 48 50 -
code/branches/FICN/src/network/TODO
r429 r436 3 3 - change ConnectionManager clientlist 4 4 - change gamestatemanager clientlist 5 - put them together into ClientInformation 5 - put them together into ClientInformation [done] 6 6 7 7 - do better gamestate-update/snapshot-handling 8 - integrate ClientInformation into gamestatemanager [done] 8 9 9 10 - write dummyserver and dummyclient (chat system)
Note: See TracChangeset
for help on using the changeset viewer.