[173] | 1 | // |
---|
| 2 | // C++ Interface: ConnectionManager |
---|
| 3 | // |
---|
[285] | 4 | // Description: |
---|
[173] | 5 | // |
---|
| 6 | // |
---|
[196] | 7 | // Author: Oliver Scheuss, (C) 2007 |
---|
[173] | 8 | // |
---|
| 9 | // Copyright: See COPYING file that comes with this distribution |
---|
| 10 | // |
---|
| 11 | // |
---|
| 12 | #ifndef NETWORK_CONNECTIONMANAGER_H |
---|
| 13 | #define NETWORK_CONNECTIONMANAGER_H |
---|
| 14 | |
---|
[196] | 15 | #include <iostream> |
---|
[230] | 16 | #include <string> |
---|
[380] | 17 | #include <map> |
---|
| 18 | #include <vector> |
---|
[196] | 19 | // enet library for networking support |
---|
| 20 | #include <enet/enet.h> |
---|
| 21 | // boost.thread library for multithreading support |
---|
| 22 | #include <boost/thread/thread.hpp> |
---|
| 23 | #include <boost/bind.hpp> |
---|
[380] | 24 | // headerfiles |
---|
[285] | 25 | #include "ConnectionManager.h" |
---|
| 26 | #include "PacketBuffer.h" |
---|
[173] | 27 | |
---|
[381] | 28 | namespace std{ |
---|
| 29 | bool operator<(ENetAddress a, ENetAddress b); |
---|
| 30 | } |
---|
| 31 | |
---|
[173] | 32 | namespace network{ |
---|
| 33 | // |
---|
[211] | 34 | #define NETWORK_PORT 55556 |
---|
[173] | 35 | #define NETWORK_MAX_CONNECTIONS 50 |
---|
| 36 | #define NETWORK_WAIT_TIMEOUT 5000 |
---|
[196] | 37 | #define NETWORK_SEND_WAIT 5 |
---|
[285] | 38 | |
---|
[173] | 39 | struct ClientList{ |
---|
[196] | 40 | ENetEvent *event; |
---|
[173] | 41 | int ID; |
---|
| 42 | ClientList *next; |
---|
| 43 | }; |
---|
[381] | 44 | |
---|
[173] | 45 | class ConnectionManager{ |
---|
| 46 | public: |
---|
| 47 | ConnectionManager(); |
---|
[230] | 48 | ConnectionManager(int port, const char *address); |
---|
| 49 | ConnectionManager(int port, std::string address); |
---|
[204] | 50 | ENetPacket *getPacket(ENetAddress &address); // thread1 |
---|
[380] | 51 | ENetPacket *getPacket(int &clientID); |
---|
[196] | 52 | bool queueEmpty(); |
---|
| 53 | void createListener(); |
---|
| 54 | bool quitListener(); |
---|
| 55 | bool addPacket(ENetPacket *packet, ENetPeer *peer); |
---|
| 56 | bool addPacket(ENetPacket *packet, int ID); |
---|
| 57 | bool addPacketAll(ENetPacket *packet); |
---|
| 58 | bool sendPackets(ENetEvent *event); |
---|
[369] | 59 | bool sendPackets(); |
---|
[173] | 60 | private: |
---|
[196] | 61 | bool clientDisconnect(ENetPeer *peer); |
---|
[380] | 62 | bool clientDisconnect(ENetPeer peer); |
---|
[196] | 63 | bool processData(ENetEvent *event); |
---|
| 64 | bool addClient(ENetEvent *event); |
---|
[380] | 65 | void receiverThread(); |
---|
[369] | 66 | void disconnectClients(); |
---|
[380] | 67 | int getClientID(ENetPeer peer); |
---|
| 68 | int getClientID(ENetAddress address); |
---|
| 69 | ENetPeer getClientPeer(int clientID); |
---|
[173] | 70 | PacketBuffer buffer; |
---|
[380] | 71 | |
---|
[173] | 72 | ENetHost *server; |
---|
| 73 | ENetAddress bindAddress; |
---|
[380] | 74 | |
---|
| 75 | bool quit; // quit-variable (communication with threads) |
---|
| 76 | std::map<ENetAddress, int> clientMap; |
---|
| 77 | std::map<ENetAddress, ENetPeer> peerMap; |
---|
| 78 | std::vector<ENetAddress> clientVector; |
---|
[173] | 79 | }; |
---|
[285] | 80 | |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | |
---|
[173] | 88 | } |
---|
| 89 | |
---|
[196] | 90 | #endif |
---|