[1056] | 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 | |
---|
[173] | 29 | // |
---|
| 30 | // C++ Interface: ConnectionManager |
---|
| 31 | // |
---|
[285] | 32 | // Description: |
---|
[173] | 33 | // |
---|
| 34 | // |
---|
[196] | 35 | // Author: Oliver Scheuss, (C) 2007 |
---|
[173] | 36 | // |
---|
| 37 | // Copyright: See COPYING file that comes with this distribution |
---|
| 38 | // |
---|
| 39 | // |
---|
[673] | 40 | #ifndef _ConnectionManager_H__ |
---|
| 41 | #define _ConnectionManager_H__ |
---|
[173] | 42 | |
---|
[1062] | 43 | #include "NetworkPrereqs.h" |
---|
| 44 | |
---|
[230] | 45 | #include <string> |
---|
[1021] | 46 | #include <map> |
---|
[196] | 47 | // enet library for networking support |
---|
| 48 | #include <enet/enet.h> |
---|
[777] | 49 | |
---|
[285] | 50 | #include "PacketBuffer.h" |
---|
[400] | 51 | #include "PacketManager.h" |
---|
[173] | 52 | |
---|
[777] | 53 | namespace std |
---|
| 54 | { |
---|
[381] | 55 | bool operator<(ENetAddress a, ENetAddress b); |
---|
| 56 | } |
---|
| 57 | |
---|
[777] | 58 | namespace network |
---|
| 59 | { |
---|
[211] | 60 | #define NETWORK_PORT 55556 |
---|
[173] | 61 | #define NETWORK_MAX_CONNECTIONS 50 |
---|
| 62 | #define NETWORK_WAIT_TIMEOUT 5000 |
---|
[196] | 63 | #define NETWORK_SEND_WAIT 5 |
---|
[285] | 64 | |
---|
[173] | 65 | struct ClientList{ |
---|
[196] | 66 | ENetEvent *event; |
---|
[173] | 67 | int ID; |
---|
| 68 | ClientList *next; |
---|
| 69 | }; |
---|
[777] | 70 | |
---|
[173] | 71 | class ConnectionManager{ |
---|
[777] | 72 | public: |
---|
[436] | 73 | ConnectionManager(ClientInformation *head); |
---|
| 74 | ConnectionManager(int port, const char *address, ClientInformation *head); |
---|
| 75 | ConnectionManager(int port, std::string address, ClientInformation *head); |
---|
[204] | 76 | ENetPacket *getPacket(ENetAddress &address); // thread1 |
---|
[380] | 77 | ENetPacket *getPacket(int &clientID); |
---|
[196] | 78 | bool queueEmpty(); |
---|
| 79 | void createListener(); |
---|
| 80 | bool quitListener(); |
---|
| 81 | bool addPacket(ENetPacket *packet, ENetPeer *peer); |
---|
| 82 | bool addPacket(ENetPacket *packet, int ID); |
---|
| 83 | bool addPacketAll(ENetPacket *packet); |
---|
| 84 | bool sendPackets(ENetEvent *event); |
---|
[369] | 85 | bool sendPackets(); |
---|
[1098] | 86 | |
---|
| 87 | //##### for testing purpose only ##### |
---|
| 88 | ConnectionManager(); |
---|
| 89 | std::map<int, int> testGetClientsShip() { |
---|
| 90 | return clientsShip; |
---|
| 91 | } |
---|
| 92 | void testAddClientsShipID( int clientID, int objectID ) { |
---|
| 93 | addClientsObjectID( clientID, objectID ); |
---|
| 94 | } |
---|
| 95 | int testGetClientsShipID( int clientID ) { |
---|
| 96 | return getClientsShipID( clientID ); |
---|
| 97 | } |
---|
| 98 | int testGetObjectsClientID( int objectID ) { |
---|
| 99 | return getObjectsClientID( objectID ); |
---|
| 100 | } |
---|
| 101 | void testDeleteClientsIDReg( int clientID ) { |
---|
| 102 | deleteClientIDReg( clientID ); |
---|
| 103 | } |
---|
| 104 | void testDeleteObjectIDReg( int objectID ) { |
---|
| 105 | deleteObjectIDReg( objectID ); |
---|
| 106 | } |
---|
| 107 | //##### for testing purpose only ##### |
---|
[436] | 108 | private: |
---|
[196] | 109 | bool clientDisconnect(ENetPeer *peer); |
---|
[436] | 110 | //bool clientDisconnect(ENetPeer peer); |
---|
[196] | 111 | bool processData(ENetEvent *event); |
---|
| 112 | bool addClient(ENetEvent *event); |
---|
[380] | 113 | void receiverThread(); |
---|
[369] | 114 | void disconnectClients(); |
---|
[380] | 115 | int getClientID(ENetPeer peer); |
---|
| 116 | int getClientID(ENetAddress address); |
---|
[400] | 117 | void syncClassid(int clientID); |
---|
[436] | 118 | ENetPeer *getClientPeer(int clientID); |
---|
[173] | 119 | PacketBuffer buffer; |
---|
[400] | 120 | PacketGenerator packet_gen; |
---|
[777] | 121 | |
---|
[173] | 122 | ENetHost *server; |
---|
| 123 | ENetAddress bindAddress; |
---|
[777] | 124 | |
---|
[380] | 125 | bool quit; // quit-variable (communication with threads) |
---|
[436] | 126 | ClientInformation *head_; |
---|
[1056] | 127 | |
---|
[1021] | 128 | //functions to map what object every clients uses |
---|
| 129 | std::map<int, int> clientsShip; |
---|
| 130 | void addClientsObjectID( int clientID, int objectID ); |
---|
| 131 | int getClientsShipID( int clientID ); |
---|
| 132 | int getObjectsClientID( int objectID ); |
---|
| 133 | void deleteClientIDReg( int clientID ); |
---|
| 134 | void deleteObjectIDReg( int objectID ); |
---|
[1098] | 135 | int getNumberOfClients(); |
---|
[173] | 136 | }; |
---|
[285] | 137 | |
---|
[173] | 138 | } |
---|
| 139 | |
---|
[673] | 140 | #endif /* _ConnectionManager_H__ */ |
---|