[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 |
---|
| 40 | // check wheter the packet queue is empty |
---|
| 41 | bool queueEmpty(); |
---|
| 42 | // create a new listener thread |
---|
[229] | 43 | bool createConnection(); |
---|
[217] | 44 | bool closeConnection(); |
---|
| 45 | // add a packet to queue for the server |
---|
| 46 | bool addPacket(ENetPacket *packet); |
---|
| 47 | // send out all queued packets |
---|
[229] | 48 | bool sendPackets(); |
---|
| 49 | // send out all queued packets and save result in event |
---|
[217] | 50 | bool sendPackets(ENetEvent *event); |
---|
| 51 | bool waitEstablished(int milisec); |
---|
| 52 | private: |
---|
| 53 | bool processData(ENetEvent *event); |
---|
| 54 | // implementation of the listener |
---|
| 55 | void receiverThread(); //thread2 |
---|
| 56 | //packetbuffer |
---|
| 57 | bool establishConnection(); |
---|
| 58 | bool disconnectConnection(); |
---|
| 59 | PacketBuffer buffer; |
---|
| 60 | // enet stuff |
---|
| 61 | ENetHost *client; |
---|
| 62 | ENetAddress serverAddress; |
---|
| 63 | // quit-variable (communication with threads) |
---|
| 64 | bool quit; |
---|
| 65 | bool established; |
---|
| 66 | // clientlist |
---|
| 67 | ENetPeer *server; |
---|
| 68 | }; |
---|
[285] | 69 | |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | |
---|
[217] | 77 | } |
---|
| 78 | |
---|
| 79 | #endif |
---|