[514] | 1 | /* |
---|
| 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 | |
---|
[217] | 28 | // |
---|
| 29 | // C++ Interface: ClientConnection |
---|
| 30 | // |
---|
[285] | 31 | // Description: The Class ClientConnection manages the servers conenctions to the clients. |
---|
| 32 | // each connection is provided by a new process. communication between master process and |
---|
[217] | 33 | // connection processes is provided by ... |
---|
| 34 | // |
---|
| 35 | // |
---|
| 36 | // Author: Oliver Scheuss |
---|
| 37 | // |
---|
| 38 | |
---|
[285] | 39 | #include "ClientConnection.h" |
---|
[217] | 40 | |
---|
[448] | 41 | #ifdef WIN32 |
---|
| 42 | #include <windows.h> |
---|
| 43 | #define usleep(x) Sleep((x)/1000) |
---|
| 44 | #else |
---|
| 45 | #include <unistd.h> |
---|
| 46 | #endif |
---|
| 47 | |
---|
[217] | 48 | namespace network{ |
---|
[285] | 49 | |
---|
[346] | 50 | static boost::thread_group network_threads; |
---|
[285] | 51 | |
---|
[217] | 52 | ClientConnection::ClientConnection(int port, std::string address){ |
---|
| 53 | quit=false; |
---|
| 54 | server=NULL; |
---|
| 55 | enet_address_set_host(&serverAddress, address.c_str()); |
---|
| 56 | serverAddress.port = NETWORK_PORT; |
---|
| 57 | established=false; |
---|
| 58 | } |
---|
[285] | 59 | |
---|
[217] | 60 | ClientConnection::ClientConnection(int port, const char *address){ |
---|
| 61 | quit=false; |
---|
| 62 | server=NULL; |
---|
| 63 | enet_address_set_host(&serverAddress, address); |
---|
| 64 | serverAddress.port = NETWORK_PORT; |
---|
| 65 | established=false; |
---|
| 66 | } |
---|
[285] | 67 | |
---|
[217] | 68 | bool ClientConnection::waitEstablished(int milisec){ |
---|
| 69 | for(int i=0; i<=milisec && !established; i++) |
---|
| 70 | usleep(1000); |
---|
[448] | 71 | |
---|
[217] | 72 | return established; |
---|
| 73 | } |
---|
[285] | 74 | |
---|
| 75 | |
---|
[217] | 76 | ENetPacket *ClientConnection::getPacket(ENetAddress &address){ |
---|
| 77 | if(!buffer.isEmpty()) |
---|
| 78 | return buffer.pop(address); |
---|
| 79 | else |
---|
| 80 | return NULL; |
---|
| 81 | } |
---|
[285] | 82 | |
---|
[369] | 83 | ENetPacket *ClientConnection::getPacket(){ |
---|
| 84 | ENetAddress address; |
---|
| 85 | return getPacket(address); |
---|
| 86 | } |
---|
[514] | 87 | |
---|
[217] | 88 | bool ClientConnection::queueEmpty(){ |
---|
| 89 | return buffer.isEmpty(); |
---|
| 90 | } |
---|
[285] | 91 | |
---|
[229] | 92 | bool ClientConnection::createConnection(){ |
---|
[217] | 93 | network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this)); |
---|
[229] | 94 | // wait 10 seconds for the connection to be established |
---|
| 95 | return waitEstablished(10000); |
---|
[217] | 96 | } |
---|
[285] | 97 | |
---|
[217] | 98 | bool ClientConnection::closeConnection(){ |
---|
| 99 | quit=true; |
---|
| 100 | network_threads.join_all(); |
---|
| 101 | established=false; |
---|
| 102 | return true; |
---|
| 103 | } |
---|
[285] | 104 | |
---|
| 105 | |
---|
[217] | 106 | bool ClientConnection::addPacket(ENetPacket *packet){ |
---|
| 107 | if(server==NULL) |
---|
| 108 | return false; |
---|
| 109 | if(enet_peer_send(server, 1, packet)!=0) |
---|
| 110 | return false; |
---|
[448] | 111 | return true; |
---|
[217] | 112 | } |
---|
[285] | 113 | |
---|
[217] | 114 | bool ClientConnection::sendPackets(ENetEvent *event){ |
---|
| 115 | if(server==NULL) |
---|
| 116 | return false; |
---|
[448] | 117 | if(enet_host_service(client, event, NETWORK_SEND_WAIT)>=0){ |
---|
| 118 | return true;} |
---|
[285] | 119 | else |
---|
[217] | 120 | return false; |
---|
| 121 | } |
---|
[285] | 122 | |
---|
[229] | 123 | bool ClientConnection::sendPackets(){ |
---|
| 124 | ENetEvent event; |
---|
| 125 | if(server==NULL) |
---|
| 126 | return false; |
---|
[448] | 127 | if(enet_host_service(client, &event, NETWORK_SEND_WAIT)>=0){ |
---|
| 128 | return true;} |
---|
[285] | 129 | else |
---|
[229] | 130 | return false; |
---|
| 131 | } |
---|
[285] | 132 | |
---|
[217] | 133 | void ClientConnection::receiverThread(){ |
---|
| 134 | // what about some error-handling here ? |
---|
| 135 | enet_initialize(); |
---|
| 136 | atexit(enet_deinitialize); |
---|
| 137 | ENetEvent event; |
---|
| 138 | client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0); |
---|
| 139 | if(client==NULL) |
---|
| 140 | // add some error handling here ========================== |
---|
| 141 | quit=true; |
---|
| 142 | //connect to the server |
---|
[369] | 143 | if(!establishConnection()){ |
---|
[217] | 144 | quit=true; |
---|
[369] | 145 | return; |
---|
| 146 | } |
---|
[217] | 147 | //main loop |
---|
| 148 | while(!quit){ |
---|
| 149 | if(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT)<0){ |
---|
| 150 | // we should never reach this point |
---|
| 151 | quit=true; |
---|
| 152 | // add some error handling here ======================== |
---|
| 153 | } |
---|
| 154 | switch(event.type){ |
---|
| 155 | // log handling ================ |
---|
[352] | 156 | case ENET_EVENT_TYPE_CONNECT: |
---|
[217] | 157 | case ENET_EVENT_TYPE_RECEIVE: |
---|
[448] | 158 | std::cout << "got packet" << std::endl; |
---|
[217] | 159 | processData(&event); |
---|
| 160 | break; |
---|
| 161 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
[369] | 162 | quit=true; |
---|
| 163 | // server closed the connection |
---|
| 164 | return; |
---|
[217] | 165 | break; |
---|
[352] | 166 | case ENET_EVENT_TYPE_NONE: |
---|
| 167 | continue; |
---|
[217] | 168 | } |
---|
| 169 | } |
---|
| 170 | // now disconnect |
---|
[285] | 171 | |
---|
| 172 | if(!disconnectConnection()) |
---|
[217] | 173 | // if disconnecting failed destroy conn. |
---|
| 174 | enet_peer_reset(server); |
---|
| 175 | return; |
---|
| 176 | } |
---|
[285] | 177 | |
---|
[217] | 178 | bool ClientConnection::disconnectConnection(){ |
---|
| 179 | ENetEvent event; |
---|
[298] | 180 | enet_peer_disconnect(server, 0); |
---|
[217] | 181 | while(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT) > 0){ |
---|
| 182 | switch (event.type) |
---|
| 183 | { |
---|
[352] | 184 | case ENET_EVENT_TYPE_NONE: |
---|
| 185 | case ENET_EVENT_TYPE_CONNECT: |
---|
[217] | 186 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 187 | enet_packet_destroy(event.packet); |
---|
| 188 | break; |
---|
| 189 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
| 190 | return true; |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | enet_peer_reset(server); |
---|
[352] | 194 | return false; |
---|
[217] | 195 | } |
---|
[285] | 196 | |
---|
[217] | 197 | bool ClientConnection::establishConnection(){ |
---|
| 198 | ENetEvent event; |
---|
| 199 | // connect to peer |
---|
| 200 | server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS); |
---|
| 201 | if(server==NULL) |
---|
| 202 | // error handling |
---|
| 203 | return false; |
---|
| 204 | // handshake |
---|
| 205 | if(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT)>0 && event.type == ENET_EVENT_TYPE_CONNECT){ |
---|
| 206 | established=true; |
---|
| 207 | return true; |
---|
| 208 | } |
---|
| 209 | else |
---|
| 210 | return false; |
---|
| 211 | } |
---|
[285] | 212 | |
---|
[217] | 213 | bool ClientConnection::processData(ENetEvent *event){ |
---|
| 214 | // just add packet to the buffer |
---|
| 215 | // this can be extended with some preprocessing |
---|
| 216 | return buffer.push(event); |
---|
| 217 | } |
---|
[285] | 218 | |
---|
| 219 | |
---|
[217] | 220 | } |
---|