Changeset 777 for code/branches/FICN/src/network/ConnectionManager.cc
- Timestamp:
- Dec 31, 2007, 7:40:23 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/FICN/src/network/ConnectionManager.cc
r681 r777 1 1 /* 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 * ORXONOX - the hottest 3D action shooter ever to exist 3 * 4 * 5 * License notice: 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * as published by the Free Software Foundation; either version 2 10 * of the License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 * 21 * Author: 22 * Oliver Scheuss, (C) 2007 23 * Co-authors: 24 * ... 25 * 26 */ 27 27 28 28 // … … 37 37 // 38 38 39 #include <iostream> 40 // boost.thread library for multithreading support 41 #include <boost/thread/thread.hpp> 42 #include <boost/bind.hpp> 43 44 #include "core/CoreIncludes.h" 45 #include "ClientInformation.h" 39 46 #include "ConnectionManager.h" 40 47 41 namespace std{ 42 bool operator< (ENetAddress a, ENetAddress b){ 48 namespace std 49 { 50 bool operator< (ENetAddress a, ENetAddress b) { 43 51 if(a.host <= b.host) 44 52 return true; … … 48 56 } 49 57 50 namespace network {51 58 namespace network 59 { 52 60 boost::thread_group network_threads; 53 61 54 ConnectionManager::ConnectionManager(ClientInformation *head) {62 ConnectionManager::ConnectionManager(ClientInformation *head) { 55 63 quit=false; 56 64 bindAddress.host = ENET_HOST_ANY; … … 59 67 } 60 68 61 ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head) {69 ConnectionManager::ConnectionManager(int port, std::string address, ClientInformation *head) { 62 70 quit=false; 63 71 enet_address_set_host (& bindAddress, address.c_str()); … … 66 74 } 67 75 68 ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head) {76 ConnectionManager::ConnectionManager(int port, const char *address, ClientInformation *head) { 69 77 quit=false; 70 78 enet_address_set_host (& bindAddress, address); … … 73 81 } 74 82 75 ENetPacket *ConnectionManager::getPacket(ENetAddress &address) {83 ENetPacket *ConnectionManager::getPacket(ENetAddress &address) { 76 84 if(!buffer.isEmpty()) 77 85 return buffer.pop(address); 78 86 else 79 80 } 81 82 ENetPacket *ConnectionManager::getPacket(int &clientID) {87 return NULL; 88 } 89 90 ENetPacket *ConnectionManager::getPacket(int &clientID) { 83 91 ENetAddress address; 84 92 ENetPacket *packet=getPacket(address); … … 88 96 } 89 97 90 bool ConnectionManager::queueEmpty() {98 bool ConnectionManager::queueEmpty() { 91 99 return buffer.isEmpty(); 92 100 } 93 101 94 void ConnectionManager::createListener() {102 void ConnectionManager::createListener() { 95 103 network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); 96 // boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));104 // boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); 97 105 return; 98 106 } 99 107 100 bool ConnectionManager::quitListener() {108 bool ConnectionManager::quitListener() { 101 109 quit=true; 102 110 network_threads.join_all(); … … 104 112 } 105 113 106 bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) {114 bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer) { 107 115 if(enet_peer_send(peer, head_->findClient(&(peer->address))->getID() , packet)!=0) 108 116 return false; … … 110 118 } 111 119 112 bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) {120 bool ConnectionManager::addPacket(ENetPacket *packet, int clientID) { 113 121 if(enet_peer_send(head_->findClient(clientID)->getPeer(), clientID, packet)!=0) 114 122 return false; … … 116 124 } 117 125 118 bool ConnectionManager::addPacketAll(ENetPacket *packet) {126 bool ConnectionManager::addPacketAll(ENetPacket *packet) { 119 127 for(ClientInformation *i=head_->next(); i!=0; i=i->next()){ 120 128 if(enet_peer_send(i->getPeer(), i->getID(), packet)!=0) 121 122 } 123 return true; 124 } 125 126 bool ConnectionManager::sendPackets(ENetEvent *event) {129 return false; 130 } 131 return true; 132 } 133 134 bool ConnectionManager::sendPackets(ENetEvent *event) { 127 135 if(server==NULL) 128 136 return false; … … 133 141 } 134 142 135 bool ConnectionManager::sendPackets() {143 bool ConnectionManager::sendPackets() { 136 144 ENetEvent event; 137 145 if(server==NULL) … … 143 151 } 144 152 145 void ConnectionManager::receiverThread() {153 void ConnectionManager::receiverThread() { 146 154 // what about some error-handling here ? 147 155 enet_initialize(); … … 164 172 // log handling ================ 165 173 case ENET_EVENT_TYPE_CONNECT: 166 addClient(&event);167 break;168 case ENET_EVENT_TYPE_RECEIVE:169 //std::cout << "received data" << std::endl;170 processData(&event);171 break;172 case ENET_EVENT_TYPE_DISCONNECT:173 // add some error/log handling here174 clientDisconnect(event.peer);175 break;176 case ENET_EVENT_TYPE_NONE:177 break;174 addClient(&event); 175 break; 176 case ENET_EVENT_TYPE_RECEIVE: 177 //std::cout << "received data" << std::endl; 178 processData(&event); 179 break; 180 case ENET_EVENT_TYPE_DISCONNECT: 181 // add some error/log handling here 182 clientDisconnect(event.peer); 183 break; 184 case ENET_EVENT_TYPE_NONE: 185 break; 178 186 } 179 187 } … … 183 191 } 184 192 185 void ConnectionManager::disconnectClients() {193 void ConnectionManager::disconnectClients() { 186 194 ENetEvent event; 187 195 ClientInformation *temp = head_->next(); … … 194 202 switch (event.type) 195 203 { 196 197 198 199 200 201 202 203 204 205 204 case ENET_EVENT_TYPE_NONE: 205 case ENET_EVENT_TYPE_CONNECT: 206 case ENET_EVENT_TYPE_RECEIVE: 207 enet_packet_destroy(event.packet); 208 break; 209 case ENET_EVENT_TYPE_DISCONNECT: 210 std::cout << "disconnecting client" << std::endl; 211 delete head_->findClient(&(event.peer->address)); 212 temp = temp->next(); 213 break; 206 214 } 207 215 } … … 209 217 } 210 218 211 bool ConnectionManager::processData(ENetEvent *event) {219 bool ConnectionManager::processData(ENetEvent *event) { 212 220 // just add packet to the buffer 213 221 // this can be extended with some preprocessing … … 215 223 } 216 224 217 // bool ConnectionManager::clientDisconnect(ENetPeer *peer){ 218 // return clientDisconnect(*peer); 219 // } 220 221 222 223 bool ConnectionManager::clientDisconnect(ENetPeer *peer){ 225 //bool ConnectionManager::clientDisconnect(ENetPeer *peer) { 226 // return clientDisconnect(*peer); 227 //} 228 229 bool ConnectionManager::clientDisconnect(ENetPeer *peer) { 224 230 return head_->removeClient(peer); 225 231 } 226 232 227 bool ConnectionManager::addClient(ENetEvent *event) {233 bool ConnectionManager::addClient(ENetEvent *event) { 228 234 ClientInformation *temp = head_->insertBack(new ClientInformation); 229 235 if(temp->prev()->head) … … 238 244 } 239 245 240 int ConnectionManager::getClientID(ENetPeer peer) {246 int ConnectionManager::getClientID(ENetPeer peer) { 241 247 return getClientID(peer.address); 242 248 } 243 249 244 int ConnectionManager::getClientID(ENetAddress address) {250 int ConnectionManager::getClientID(ENetAddress address) { 245 251 return head_->findClient(&address)->getID(); 246 252 } 247 253 248 ENetPeer *ConnectionManager::getClientPeer(int clientID) {254 ENetPeer *ConnectionManager::getClientPeer(int clientID) { 249 255 return head_->findClient(clientID)->getPeer(); 250 256 } 251 257 252 void ConnectionManager::syncClassid(int clientID) {258 void ConnectionManager::syncClassid(int clientID) { 253 259 int i=0; 254 260 std::string classname;
Note: See TracChangeset
for help on using the changeset viewer.