- Timestamp:
- Dec 4, 2007, 8:22:24 PM (17 years ago)
- Location:
- code/branches/FICN/src/network
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/FICN/src/network/ConnectionManager.cc
r369 r380 16 16 boost::thread_group network_threads; 17 17 18 void test(){19 return;20 }21 22 18 ConnectionManager::ConnectionManager(){ 23 19 quit=false; 24 client=NULL;25 20 bindAddress.host = ENET_HOST_ANY; 26 21 bindAddress.port = NETWORK_PORT; … … 29 24 ConnectionManager::ConnectionManager(int port, std::string address){ 30 25 quit=false; 31 client=NULL;32 26 enet_address_set_host (& bindAddress, address.c_str()); 33 27 bindAddress.port = NETWORK_PORT; … … 36 30 ConnectionManager::ConnectionManager(int port, const char *address){ 37 31 quit=false; 38 client=NULL;39 32 enet_address_set_host (& bindAddress, address); 40 33 bindAddress.port = NETWORK_PORT; … … 47 40 return NULL; 48 41 } 42 43 ENetPacket *ConnectionManager::getPacket(int &clientID){ 44 ENetAddress address; 45 ENetPacket *packet=getPacket(address); 46 clientID=clientMap.find(address)->second; 47 return packet; 48 } 49 49 50 50 bool ConnectionManager::queueEmpty(){ … … 65 65 66 66 bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer){ 67 if(client==NULL) 68 return false; 69 ClientList *temp=client; 70 while(peer->host != temp->event->peer->host){ 71 temp=temp->next; 72 if(temp==NULL) 73 return false; 74 } 75 if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0) 76 return false; 77 return true; 78 } 79 80 bool ConnectionManager::addPacket(ENetPacket *packet, int ID){ 81 if(client==NULL) 82 return false; 83 ClientList *temp=client; 84 while(ID != temp->ID){ 85 temp=temp->next; 86 if(temp==NULL) 87 return false; 88 } 89 if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0) 90 return false; 91 else 92 return true; 93 } 94 67 if(clientVector.size()==0) 68 return false; 69 if(enet_peer_send(peer, clientMap.find(peer->address)->second , packet)!=0) 70 return false; 71 return true; 72 } 73 74 bool ConnectionManager::addPacket(ENetPacket *packet, int clientID){ 75 if(clientVector.size()==0) 76 return false; 77 if(enet_peer_send(&(peerMap.find(clientVector[clientID])->second), clientID, packet)!=0) 78 return false; 79 return true; 80 } 81 95 82 bool ConnectionManager::addPacketAll(ENetPacket *packet){ 96 ClientList *temp=client; 97 while(temp!=NULL){ 98 if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0) 83 for(int i=0; i<clientVector.size(); i++){ 84 if(enet_peer_send(&(peerMap.find(clientVector[i])->second), i, packet)!=0) 99 85 return false; 100 86 } … … 163 149 bool disconnected=false; 164 150 ENetEvent event; 165 ClientList *temp=client; 166 while(temp!=NULL){ 167 enet_peer_disconnect(temp->event->peer, 0); 151 for(int i=0; i<clientVector.size(); i++){ 152 enet_peer_disconnect(&(peerMap.find(clientVector[i])->second), 0); 168 153 while( !disconnected && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){ 169 154 switch (event.type) … … 179 164 } 180 165 } 181 temp = temp->next;182 166 disconnected=false; 183 167 } … … 192 176 193 177 bool ConnectionManager::clientDisconnect(ENetPeer *peer){ 194 ClientList *temp=client; 195 // do we have to remove the first item ? 196 if(temp->event->peer->host==peer->host){ 197 if(temp->next==NULL){ 198 client=NULL; 199 } else{ 200 client=temp->next; 201 } 202 delete temp; 203 return true; 204 } else { 205 while(temp->next!=NULL){ 206 if(temp->next->event->peer->host==peer->host){ 207 // do a correct relink and delete the disconnected client 208 ClientList *temp2=temp->next; 209 temp->next=temp2->next; 210 delete temp2; 211 return true; 212 } else 213 //otherwise keep on searching ;) 214 temp=temp->next; 215 } 216 } 217 return false; 178 return clientDisconnect(*peer); 179 } 180 181 bool ConnectionManager::clientDisconnect(ENetPeer peer){ 182 std::map<ENetAddress, int>::iterator it; 183 it=clientMap.find(peer.address); 184 clientVector.erase(clientVector.begin()+it->second); 185 clientMap.erase(it); 186 peerMap.erase(peerMap.find(peer.address)); 187 return true; 218 188 } 219 189 220 190 bool ConnectionManager::addClient(ENetEvent *event){ 221 222 // first client? 223 if(client==NULL){ 224 client =new ClientList; 225 client->ID=1; 226 client->next=NULL; 227 client->event = event; 228 } else { 229 // otherwise add a new element to clientlist 230 ClientList *temp = client; 231 int i=1; 232 while(temp->next != NULL){ 233 temp=temp->next; 234 i++; 235 } 236 temp->next=new ClientList; 237 temp=temp->next; 238 temp->ID=i+1; 239 temp->event=event; 240 temp->next=NULL; 241 } 242 return true; 243 } 244 245 191 clientVector.push_back((event->peer->address)); 192 clientMap[event->peer->address]=clientVector.size()-1; 193 peerMap[event->peer->address]=*(event->peer); 194 return true; 195 } 196 197 int ConnectionManager::getClientID(ENetPeer peer){ 198 return clientMap.find(peer.address)->second; 199 } 200 201 int ConnectionManager::getClientID(ENetAddress address){ 202 return clientMap.find(address)->second; 203 } 204 205 ENetPeer ConnectionManager::getClientPeer(int clientID){ 206 return peerMap.find(clientVector[clientID])->second; 207 } 208 246 209 } -
code/branches/FICN/src/network/ConnectionManager.h
r369 r380 15 15 #include <iostream> 16 16 #include <string> 17 #include <map> 18 #include <vector> 17 19 // enet library for networking support 18 20 #include <enet/enet.h> … … 20 22 #include <boost/thread/thread.hpp> 21 23 #include <boost/bind.hpp> 22 // headerfile 24 // headerfiles 23 25 #include "ConnectionManager.h" 24 26 #include "PacketBuffer.h" … … 30 32 #define NETWORK_WAIT_TIMEOUT 5000 31 33 #define NETWORK_SEND_WAIT 5 32 //just some defines to make life easier ;)33 // #define ENetEvent std::ENetEvent34 // #define ENetHost std::ENetHost35 // #define ENetAddress std::ENetAddress36 // #define ENetPeer std::ENetPeer37 34 38 35 struct ClientList{ … … 48 45 ConnectionManager(int port, std::string address); 49 46 ENetPacket *getPacket(ENetAddress &address); // thread1 50 // check wheter the packet queue is empty47 ENetPacket *getPacket(int &clientID); 51 48 bool queueEmpty(); 52 // create a new listener thread53 49 void createListener(); 54 50 bool quitListener(); 55 // add a packet to queue for a specific client peer56 51 bool addPacket(ENetPacket *packet, ENetPeer *peer); 57 // add ad packet to queue for a specific client ID58 52 bool addPacket(ENetPacket *packet, int ID); 59 // add a packet to queue for all clients60 53 bool addPacketAll(ENetPacket *packet); 61 // send out all queued packets62 54 bool sendPackets(ENetEvent *event); 63 55 bool sendPackets(); 64 56 private: 65 57 bool clientDisconnect(ENetPeer *peer); 58 bool clientDisconnect(ENetPeer peer); 66 59 bool processData(ENetEvent *event); 67 60 bool addClient(ENetEvent *event); 68 // implementation of the listener 69 void receiverThread(); //thread2 61 void receiverThread(); 70 62 void disconnectClients(); 71 //packetbuffer 63 int getClientID(ENetPeer peer); 64 int getClientID(ENetAddress address); 65 ENetPeer getClientPeer(int clientID); 72 66 PacketBuffer buffer; 73 // enet stuff67 74 68 ENetHost *server; 75 69 ENetAddress bindAddress; 76 // quit-variable (communication with threads) 77 bool quit; 78 // clientlist 79 ClientList *client; 80 // thread group 81 // boost::thread_group threads; 70 71 bool quit; // quit-variable (communication with threads) 72 std::map<ENetAddress, int> clientMap; 73 std::map<ENetAddress, ENetPeer> peerMap; 74 std::vector<ENetAddress> clientVector; 82 75 }; 83 76 -
code/branches/FICN/src/network/Server.cc
r369 r380 85 85 } 86 86 87 /** 88 * Run this function once every tick 89 * calls processQueue and updateGamestate 90 */ 87 91 void Server::tick(){ 92 processQueue(); 93 updateGamestate(); 94 return; 95 } 96 97 /** 98 * processes all the packets waiting in the queue 99 */ 100 void Server::processQueue(){ 101 ENetPacket *packet; 102 while(!connection.queueEmpty()){ 103 packet = connection.getPacket() 104 } 105 } 106 107 /** 108 * takes a new snapshot of the gamestate and sends it to the clients 109 */ 110 void Server::updateGamestate(){ 88 111 } 89 112 -
code/branches/FICN/src/network/Server.h
r369 r380 40 40 GameStateManager gamestates; 41 41 PacketGenerator packet_gen; 42 42 43 void processQueue(); 44 void updateGamestate(); 43 45 }; 44 46
Note: See TracChangeset
for help on using the changeset viewer.