[217] | 1 | // |
---|
| 2 | // C++ Interface: ClientConnection |
---|
| 3 | // |
---|
[285] | 4 | // Description: |
---|
[217] | 5 | // |
---|
| 6 | // |
---|
| 7 | // Author: Oliver Scheuss, (C) 2007 |
---|
| 8 | // |
---|
| 9 | // Copyright: See COPYING file that comes with this distribution |
---|
| 10 | // |
---|
| 11 | // |
---|
| 12 | #ifndef NETWORK_CLIENTCONNECTION_H |
---|
| 13 | #define NETWORK_CLIENTCONNECTION_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 |
---|
[285] | 23 | #include "ClientConnection.h" |
---|
| 24 | #include "PacketBuffer.h" |
---|
[217] | 25 | |
---|
| 26 | namespace network{ |
---|
| 27 | // |
---|
| 28 | #define NETWORK_PORT 55556 |
---|
| 29 | #define NETWORK_CLIENT_MAX_CONNECTIONS 5 |
---|
| 30 | #define NETWORK_WAIT_TIMEOUT 5000 |
---|
| 31 | #define NETWORK_SEND_WAIT 5 |
---|
| 32 | #define NETWORK_CLIENT_CHANNELS 2 |
---|
[285] | 33 | |
---|
| 34 | |
---|
[217] | 35 | class ClientConnection{ |
---|
| 36 | public: |
---|
| 37 | ClientConnection(int port, std::string address); |
---|
| 38 | ClientConnection(int port, const char* address); |
---|
| 39 | ENetPacket *getPacket(ENetAddress &address); // thread1 |
---|
[369] | 40 | ENetPacket *getPacket(); // thread1 |
---|
[217] | 41 | // check wheter the packet queue is empty |
---|
| 42 | bool queueEmpty(); |
---|
| 43 | // create a new listener thread |
---|
[229] | 44 | bool createConnection(); |
---|
[217] | 45 | bool closeConnection(); |
---|
| 46 | // add a packet to queue for the server |
---|
| 47 | bool addPacket(ENetPacket *packet); |
---|
| 48 | // send out all queued packets |
---|
[229] | 49 | bool sendPackets(); |
---|
| 50 | // send out all queued packets and save result in event |
---|
[217] | 51 | bool sendPackets(ENetEvent *event); |
---|
| 52 | bool waitEstablished(int milisec); |
---|
| 53 | private: |
---|
| 54 | bool processData(ENetEvent *event); |
---|
| 55 | // implementation of the listener |
---|
| 56 | void receiverThread(); //thread2 |
---|
| 57 | //packetbuffer |
---|
| 58 | bool establishConnection(); |
---|
| 59 | bool disconnectConnection(); |
---|
| 60 | PacketBuffer buffer; |
---|
| 61 | // enet stuff |
---|
| 62 | ENetHost *client; |
---|
| 63 | ENetAddress serverAddress; |
---|
| 64 | // quit-variable (communication with threads) |
---|
| 65 | bool quit; |
---|
| 66 | bool established; |
---|
| 67 | // clientlist |
---|
| 68 | ENetPeer *server; |
---|
| 69 | }; |
---|
[285] | 70 | |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | |
---|
[217] | 78 | } |
---|
| 79 | |
---|
| 80 | #endif |
---|