| 1 | // |
|---|
| 2 | // C++ Interface: ConnectionManager |
|---|
| 3 | // |
|---|
| 4 | // Description: |
|---|
| 5 | // |
|---|
| 6 | // |
|---|
| 7 | // Author: Oliver Scheuss, (C) 2007 |
|---|
| 8 | // |
|---|
| 9 | // Copyright: See COPYING file that comes with this distribution |
|---|
| 10 | // |
|---|
| 11 | // |
|---|
| 12 | #ifndef NETWORK_CONNECTIONMANAGER_H |
|---|
| 13 | #define NETWORK_CONNECTIONMANAGER_H |
|---|
| 14 | |
|---|
| 15 | #include <iostream> |
|---|
| 16 | #include <string> |
|---|
| 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 |
|---|
| 23 | #include "ConnectionManager.h" |
|---|
| 24 | #include "PacketBuffer.h" |
|---|
| 25 | |
|---|
| 26 | namespace network{ |
|---|
| 27 | // |
|---|
| 28 | #define NETWORK_PORT 55556 |
|---|
| 29 | #define NETWORK_MAX_CONNECTIONS 50 |
|---|
| 30 | #define NETWORK_WAIT_TIMEOUT 5000 |
|---|
| 31 | #define NETWORK_SEND_WAIT 5 |
|---|
| 32 | //just some defines to make life easier ;) |
|---|
| 33 | // #define ENetEvent std::ENetEvent |
|---|
| 34 | // #define ENetHost std::ENetHost |
|---|
| 35 | // #define ENetAddress std::ENetAddress |
|---|
| 36 | // #define ENetPeer std::ENetPeer |
|---|
| 37 | |
|---|
| 38 | struct ClientList{ |
|---|
| 39 | ENetEvent *event; |
|---|
| 40 | int ID; |
|---|
| 41 | ClientList *next; |
|---|
| 42 | }; |
|---|
| 43 | |
|---|
| 44 | class ConnectionManager{ |
|---|
| 45 | public: |
|---|
| 46 | ConnectionManager(); |
|---|
| 47 | ConnectionManager(int port, const char *address); |
|---|
| 48 | ConnectionManager(int port, std::string address); |
|---|
| 49 | ENetPacket *getPacket(ENetAddress &address); // thread1 |
|---|
| 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); |
|---|
| 63 | private: |
|---|
| 64 | bool clientDisconnect(ENetPeer *peer); |
|---|
| 65 | bool processData(ENetEvent *event); |
|---|
| 66 | bool addClient(ENetEvent *event); |
|---|
| 67 | // implementation of the listener |
|---|
| 68 | void receiverThread(); //thread2 |
|---|
| 69 | //packetbuffer |
|---|
| 70 | PacketBuffer buffer; |
|---|
| 71 | // enet stuff |
|---|
| 72 | ENetHost *server; |
|---|
| 73 | ENetAddress bindAddress; |
|---|
| 74 | // quit-variable (communication with threads) |
|---|
| 75 | bool quit; |
|---|
| 76 | // clientlist |
|---|
| 77 | ClientList *client; |
|---|
| 78 | // thread group |
|---|
| 79 | // boost::thread_group threads; |
|---|
| 80 | }; |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | #endif |
|---|