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