[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); |
---|
[173] | 63 | private: |
---|
[196] | 64 | bool clientDisconnect(ENetPeer *peer); |
---|
| 65 | bool processData(ENetEvent *event); |
---|
| 66 | bool addClient(ENetEvent *event); |
---|
| 67 | // implementation of the listener |
---|
| 68 | void receiverThread(); //thread2 |
---|
[173] | 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; |
---|
[196] | 78 | // thread group |
---|
| 79 | // boost::thread_group threads; |
---|
[173] | 80 | }; |
---|
[285] | 81 | |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | |
---|
[173] | 89 | } |
---|
| 90 | |
---|
[196] | 91 | #endif |
---|