[1168] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Oliver Scheuss, (C) 2007 |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | // |
---|
| 30 | // C++ Interface: ClientConnection |
---|
| 31 | // |
---|
| 32 | // Description: The Class ClientConnection manages the servers conenctions to the clients. |
---|
| 33 | // each connection is provided by a new process. communication between master process and |
---|
| 34 | // connection processes is provided by ... |
---|
| 35 | // |
---|
| 36 | // |
---|
| 37 | // Author: Oliver Scheuss |
---|
| 38 | // |
---|
| 39 | |
---|
| 40 | #include "ClientConnection.h" |
---|
| 41 | |
---|
| 42 | #include <iostream> |
---|
| 43 | // boost.thread library for multithreading support |
---|
| 44 | #include <boost/bind.hpp> |
---|
| 45 | |
---|
| 46 | #include "util/Sleep.h" |
---|
| 47 | #include "core/Debug.h" |
---|
| 48 | |
---|
| 49 | namespace network |
---|
| 50 | { |
---|
[1279] | 51 | //static boost::thread_group network_threads; |
---|
[1168] | 52 | |
---|
| 53 | ClientConnection::ClientConnection(int port, std::string address) { |
---|
| 54 | quit=false; |
---|
| 55 | server=NULL; |
---|
| 56 | enet_address_set_host(&serverAddress, address.c_str()); |
---|
| 57 | serverAddress.port = NETWORK_PORT; |
---|
| 58 | established=false; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | ClientConnection::ClientConnection(int port, const char *address) { |
---|
| 62 | quit=false; |
---|
| 63 | server=NULL; |
---|
| 64 | enet_address_set_host(&serverAddress, address); |
---|
| 65 | serverAddress.port = NETWORK_PORT; |
---|
| 66 | established=false; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | bool ClientConnection::waitEstablished(int milisec) { |
---|
| 70 | for(int i=0; i<=milisec && !established; i++) |
---|
| 71 | usleep(1000); |
---|
| 72 | |
---|
| 73 | return established; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | |
---|
[1409] | 77 | /*ENetPacket *ClientConnection::getPacket(ENetAddress &address) { |
---|
[1168] | 78 | if(!buffer.isEmpty()) { |
---|
| 79 | //std::cout << "###BUFFER IS NOT EMPTY###" << std::endl; |
---|
| 80 | return buffer.pop(address); |
---|
| 81 | } |
---|
| 82 | else{ |
---|
| 83 | return NULL; |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | ENetPacket *ClientConnection::getPacket() { |
---|
| 88 | ENetAddress address; //sems that address is not needed |
---|
| 89 | return getPacket(address); |
---|
[1409] | 90 | }*/ |
---|
| 91 | |
---|
| 92 | ENetEvent *ClientConnection::getEvent(){ |
---|
| 93 | if(!buffer.isEmpty()) |
---|
| 94 | return buffer.pop(); |
---|
| 95 | else |
---|
| 96 | return NULL; |
---|
[1168] | 97 | } |
---|
| 98 | |
---|
| 99 | bool ClientConnection::queueEmpty() { |
---|
| 100 | return buffer.isEmpty(); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | bool ClientConnection::createConnection() { |
---|
[1279] | 104 | receiverThread_ = new boost::thread(boost::bind(&ClientConnection::receiverThread, this)); |
---|
| 105 | //network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this)); |
---|
[1168] | 106 | // wait 10 seconds for the connection to be established |
---|
[1377] | 107 | return waitEstablished(3000); |
---|
[1168] | 108 | } |
---|
| 109 | |
---|
| 110 | bool ClientConnection::closeConnection() { |
---|
| 111 | quit=true; |
---|
[1279] | 112 | //network_threads.join_all(); |
---|
| 113 | receiverThread_->join(); |
---|
[1168] | 114 | established=false; |
---|
| 115 | return true; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | bool ClientConnection::addPacket(ENetPacket *packet) { |
---|
| 120 | if(server==NULL) |
---|
| 121 | return false; |
---|
[1261] | 122 | if(packet==NULL){ |
---|
| 123 | COUT(3) << "Cl.con: addpacket: invalid packet" << std::endl; |
---|
[1168] | 124 | return false; |
---|
[1253] | 125 | } |
---|
| 126 | if(enet_peer_send(server, 0, packet)<0) |
---|
| 127 | return false; |
---|
| 128 | else |
---|
| 129 | return true; |
---|
[1168] | 130 | } |
---|
| 131 | |
---|
[1377] | 132 | // bool ClientConnection::sendPackets(ENetEvent *event) { |
---|
| 133 | // if(server==NULL) |
---|
| 134 | // return false; |
---|
| 135 | // if(enet_host_service(client, event, NETWORK_SEND_WAIT)>=0){ |
---|
| 136 | // return true;} |
---|
| 137 | // else |
---|
| 138 | // return false; |
---|
| 139 | // } |
---|
[1168] | 140 | |
---|
| 141 | bool ClientConnection::sendPackets() { |
---|
| 142 | ENetEvent event; |
---|
| 143 | if(server==NULL) |
---|
| 144 | return false; |
---|
[1377] | 145 | enet_host_flush(client); |
---|
| 146 | return true; |
---|
[1168] | 147 | } |
---|
| 148 | |
---|
| 149 | void ClientConnection::receiverThread() { |
---|
| 150 | // what about some error-handling here ? |
---|
| 151 | enet_initialize(); |
---|
| 152 | atexit(enet_deinitialize); |
---|
[1409] | 153 | ENetEvent *event; |
---|
[1168] | 154 | client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0); |
---|
[1377] | 155 | if(client==NULL) { |
---|
| 156 | COUT(2) << "ClientConnection: could not create client host" << std::endl; |
---|
[1168] | 157 | // add some error handling here ========================== |
---|
| 158 | quit=true; |
---|
[1377] | 159 | } |
---|
[1168] | 160 | //connect to the server |
---|
| 161 | if(!establishConnection()){ |
---|
[1377] | 162 | COUT(2) << "clientConn: receiver thread: could not establishConnection" << std::endl; |
---|
[1168] | 163 | quit=true; |
---|
| 164 | return; |
---|
| 165 | } |
---|
| 166 | //main loop |
---|
| 167 | while(!quit){ |
---|
[1409] | 168 | event = new ENetEvent; |
---|
[1168] | 169 | //std::cout << "connection loop" << std::endl; |
---|
[1409] | 170 | if(enet_host_service(client, event, NETWORK_CLIENT_TIMEOUT)<0){ |
---|
[1168] | 171 | // we should never reach this point |
---|
| 172 | quit=true; |
---|
| 173 | // add some error handling here ======================== |
---|
| 174 | } |
---|
[1409] | 175 | switch(event->type){ |
---|
[1168] | 176 | // log handling ================ |
---|
| 177 | case ENET_EVENT_TYPE_CONNECT: |
---|
[1377] | 178 | break; |
---|
[1168] | 179 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 180 | COUT(5) << "Cl.Con: receiver-Thread while loop: got new packet" << std::endl; |
---|
[1409] | 181 | if ( !processData(event) ) COUT(2) << "Current packet was not pushed to packetBuffer -> ev ongoing SegFault" << std::endl; |
---|
[1168] | 182 | COUT(5) << "Cl.Con: processed Data in receiver-thread while loop" << std::endl; |
---|
| 183 | break; |
---|
| 184 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
| 185 | quit=true; |
---|
| 186 | // server closed the connection |
---|
| 187 | return; |
---|
| 188 | break; |
---|
| 189 | case ENET_EVENT_TYPE_NONE: |
---|
| 190 | continue; |
---|
| 191 | } |
---|
[1279] | 192 | //receiverThread_->yield(); |
---|
[1168] | 193 | } |
---|
| 194 | // now disconnect |
---|
| 195 | |
---|
| 196 | if(!disconnectConnection()) |
---|
| 197 | // if disconnecting failed destroy conn. |
---|
| 198 | enet_peer_reset(server); |
---|
| 199 | return; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | bool ClientConnection::disconnectConnection() { |
---|
| 203 | ENetEvent event; |
---|
| 204 | enet_peer_disconnect(server, 0); |
---|
| 205 | while(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT) > 0){ |
---|
| 206 | switch (event.type) |
---|
| 207 | { |
---|
| 208 | case ENET_EVENT_TYPE_NONE: |
---|
| 209 | case ENET_EVENT_TYPE_CONNECT: |
---|
| 210 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 211 | enet_packet_destroy(event.packet); |
---|
| 212 | break; |
---|
| 213 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
| 214 | return true; |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | enet_peer_reset(server); |
---|
| 218 | return false; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | bool ClientConnection::establishConnection() { |
---|
| 222 | ENetEvent event; |
---|
| 223 | // connect to peer (server is type ENetPeer*) |
---|
| 224 | server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS); |
---|
[1377] | 225 | if(server==NULL) { |
---|
| 226 | COUT(2) << "ClientConnection: server == NULL" << std::endl; |
---|
[1168] | 227 | // error handling |
---|
| 228 | return false; |
---|
[1377] | 229 | } |
---|
[1168] | 230 | // handshake |
---|
[1377] | 231 | if(enet_host_service(client, &event, NETWORK_CLIENT_TIMEOUT)>=0 && event.type == ENET_EVENT_TYPE_CONNECT){ |
---|
[1168] | 232 | established=true; |
---|
| 233 | return true; |
---|
| 234 | } |
---|
[1377] | 235 | else { |
---|
| 236 | COUT(2) << "ClientConnection: enet_host_service < 0 or event.type != ENET_EVENT_TYPE_CONNECT # EVENT:" << event.type << std::endl; |
---|
[1168] | 237 | return false; |
---|
[1377] | 238 | } |
---|
[1168] | 239 | } |
---|
| 240 | |
---|
| 241 | bool ClientConnection::processData(ENetEvent *event) { |
---|
| 242 | COUT(5) << "Cl.Con: got packet, pushing to queue" << std::endl; |
---|
| 243 | // just add packet to the buffer |
---|
| 244 | // this can be extended with some preprocessing |
---|
| 245 | return buffer.push(event); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | } |
---|