[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> |
---|
[196] | 17 | // enet library for networking support |
---|
| 18 | #include <enet/enet.h> |
---|
| 19 | // boost.thread library for multithreading support |
---|
| 20 | #include <boost/thread/thread.hpp> |
---|
| 21 | #include <boost/bind.hpp> |
---|
| 22 | // headerfile |
---|
[285] | 23 | #include "ConnectionManager.h" |
---|
| 24 | #include "PacketBuffer.h" |
---|
[173] | 25 | |
---|
| 26 | namespace network{ |
---|
| 27 | // |
---|
[211] | 28 | #define NETWORK_PORT 55556 |
---|
[173] | 29 | #define NETWORK_MAX_CONNECTIONS 50 |
---|
| 30 | #define NETWORK_WAIT_TIMEOUT 5000 |
---|
[196] | 31 | #define NETWORK_SEND_WAIT 5 |
---|
[173] | 32 | //just some defines to make life easier ;) |
---|
[196] | 33 | // #define ENetEvent std::ENetEvent |
---|
| 34 | // #define ENetHost std::ENetHost |
---|
| 35 | // #define ENetAddress std::ENetAddress |
---|
| 36 | // #define ENetPeer std::ENetPeer |
---|
[285] | 37 | |
---|
[173] | 38 | struct ClientList{ |
---|
[196] | 39 | ENetEvent *event; |
---|
[173] | 40 | int ID; |
---|
| 41 | ClientList *next; |
---|
| 42 | }; |
---|
[285] | 43 | |
---|
[173] | 44 | class ConnectionManager{ |
---|
| 45 | public: |
---|
| 46 | ConnectionManager(); |
---|
[230] | 47 | ConnectionManager(int port, const char *address); |
---|
| 48 | ConnectionManager(int port, std::string address); |
---|
[204] | 49 | ENetPacket *getPacket(ENetAddress &address); // thread1 |
---|
[196] | 50 | // check wheter the packet queue is empty |
---|
| 51 | bool queueEmpty(); |
---|
| 52 | // create a new listener thread |
---|
| 53 | void createListener(); |
---|
| 54 | bool quitListener(); |
---|
| 55 | // add a packet to queue for a specific client peer |
---|
| 56 | bool addPacket(ENetPacket *packet, ENetPeer *peer); |
---|
| 57 | // add ad packet to queue for a specific client ID |
---|
| 58 | bool addPacket(ENetPacket *packet, int ID); |
---|
| 59 | // add a packet to queue for all clients |
---|
| 60 | bool addPacketAll(ENetPacket *packet); |
---|
| 61 | // send out all queued packets |
---|
| 62 | bool sendPackets(ENetEvent *event); |
---|
[369] | 63 | bool sendPackets(); |
---|
[173] | 64 | private: |
---|
[196] | 65 | bool clientDisconnect(ENetPeer *peer); |
---|
| 66 | bool processData(ENetEvent *event); |
---|
| 67 | bool addClient(ENetEvent *event); |
---|
| 68 | // implementation of the listener |
---|
| 69 | void receiverThread(); //thread2 |
---|
[369] | 70 | void disconnectClients(); |
---|
[173] | 71 | //packetbuffer |
---|
| 72 | PacketBuffer buffer; |
---|
| 73 | // enet stuff |
---|
| 74 | ENetHost *server; |
---|
| 75 | ENetAddress bindAddress; |
---|
| 76 | // quit-variable (communication with threads) |
---|
| 77 | bool quit; |
---|
| 78 | // clientlist |
---|
| 79 | ClientList *client; |
---|
[196] | 80 | // thread group |
---|
| 81 | // boost::thread_group threads; |
---|
[173] | 82 | }; |
---|
[285] | 83 | |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | |
---|
[173] | 91 | } |
---|
| 92 | |
---|
[196] | 93 | #endif |
---|