| 1 | /*! | 
|---|
| 2 | * @file tcp_socket.h | 
|---|
| 3 | *  tcp socket network interface | 
|---|
| 4 |  | 
|---|
| 5 | */ | 
|---|
| 6 |  | 
|---|
| 7 | #ifndef _TCP_SOCKET | 
|---|
| 8 | #define _TCP_SOCKET | 
|---|
| 9 |  | 
|---|
| 10 | //TODO HACK else gdb will not work on server | 
|---|
| 11 | #define DONTEXITTHREADS | 
|---|
| 12 |  | 
|---|
| 13 | //if you want to use outgoing buffer define _USE_OUTGOING_BUFFER | 
|---|
| 14 | #define _USE_OUTGOING_BUFFER | 
|---|
| 15 |  | 
|---|
| 16 | #define _INCOMING_BUFFER_SIZE 2024000 | 
|---|
| 17 | #define _OUTGOING_BUFFER_SIZE 2024000 | 
|---|
| 18 | #define _LOCAL_BUFFER_SIZE 2048 | 
|---|
| 19 | //sleep if incoming buffer is full | 
|---|
| 20 | #define _MSECONDS_SLEEP_FULL_BUFFER 10 | 
|---|
| 21 | //sleep if outgoing buffer is empty | 
|---|
| 22 | #define _MSECONDS_SLEEP_EMPTY_BUFFER 10 | 
|---|
| 23 |  | 
|---|
| 24 |  | 
|---|
| 25 | /* contains memmove and memcpy */ | 
|---|
| 26 | #include <string.h> | 
|---|
| 27 |  | 
|---|
| 28 | #ifdef HAVE_SDL_H | 
|---|
| 29 | #include <SDL_thread.h> | 
|---|
| 30 | #else | 
|---|
| 31 | #include <SDL/SDL_thread.h> | 
|---|
| 32 | #endif | 
|---|
| 33 | /* include this file, it contains some default definitions */ | 
|---|
| 34 | #include "netdefs.h" | 
|---|
| 35 |  | 
|---|
| 36 |  | 
|---|
| 37 | /* include network_socket.h since all classes are derived from this one */ | 
|---|
| 38 | #include "network_socket.h" | 
|---|
| 39 |  | 
|---|
| 40 | class TcpSocket : public NetworkSocket | 
|---|
| 41 | { | 
|---|
| 42 | ObjectListDeclaration(TcpSocket); | 
|---|
| 43 | public: | 
|---|
| 44 | TcpSocket(); | 
|---|
| 45 | TcpSocket( std::string host, int port ); | 
|---|
| 46 | TcpSocket(TCPsocket sock); | 
|---|
| 47 | virtual ~TcpSocket(); | 
|---|
| 48 |  | 
|---|
| 49 | virtual void connectToServer( std::string host, int port ); | 
|---|
| 50 |  | 
|---|
| 51 | virtual void disconnectServer(); | 
|---|
| 52 | virtual void reconnectToServer( std::string host, int port); | 
|---|
| 53 | virtual void reconnectToServerSoft( std::string host, int port); | 
|---|
| 54 |  | 
|---|
| 55 | virtual bool writePacket(byte * data, int length); | 
|---|
| 56 |  | 
|---|
| 57 | virtual int readPacket(byte * data, int maxLength); | 
|---|
| 58 |  | 
|---|
| 59 | private: | 
|---|
| 60 | TCPsocket tcpSocket; | 
|---|
| 61 |  | 
|---|
| 62 | byte incomingBuffer[_INCOMING_BUFFER_SIZE]; | 
|---|
| 63 | #ifdef _USE_OUTGOING_BUFFER | 
|---|
| 64 | byte outgoingBuffer[_OUTGOING_BUFFER_SIZE]; | 
|---|
| 65 | #endif | 
|---|
| 66 | int incomingBufferLength; | 
|---|
| 67 | #ifdef _USE_OUTGOING_BUFFER | 
|---|
| 68 | int outgoingBufferLength; | 
|---|
| 69 | #endif | 
|---|
| 70 |  | 
|---|
| 71 | SDL_mutex * incomingBufferMutex; | 
|---|
| 72 | #ifdef _USE_OUTGOING_BUFFER | 
|---|
| 73 | SDL_mutex * outgoingBufferMutex; | 
|---|
| 74 | #endif | 
|---|
| 75 | SDL_mutex * socketMutex; | 
|---|
| 76 | bool terminateThread; | 
|---|
| 77 |  | 
|---|
| 78 | SDL_mutex* threadTerminationMutex; | 
|---|
| 79 | static int thread_read(void * data); | 
|---|
| 80 | bool thread_read_running; | 
|---|
| 81 | bool thread_write_running; | 
|---|
| 82 |  | 
|---|
| 83 | SDL_Thread*            readThread; | 
|---|
| 84 | SDL_Thread*            writeThread; | 
|---|
| 85 |  | 
|---|
| 86 | #ifdef _USE_OUTGOING_BUFFER | 
|---|
| 87 | static int thread_write(void * data); | 
|---|
| 88 | #endif | 
|---|
| 89 |  | 
|---|
| 90 | int writeBytes(byte * data, int length); | 
|---|
| 91 | int readBytes(byte * data, int length); | 
|---|
| 92 | int readBlock(byte * data, int length); | 
|---|
| 93 |  | 
|---|
| 94 | void init(); | 
|---|
| 95 |  | 
|---|
| 96 | }; | 
|---|
| 97 |  | 
|---|
| 98 |  | 
|---|
| 99 |  | 
|---|
| 100 | #endif /* _NETWORK_SOCKET */ | 
|---|