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