| 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 1024 | 
|---|
| 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 |   public: | 
|---|
| 43 |     TcpSocket(); | 
|---|
| 44 |     TcpSocket( std::string host, int port ); | 
|---|
| 45 |     TcpSocket(TCPsocket sock); | 
|---|
| 46 |     virtual ~TcpSocket(); | 
|---|
| 47 |  | 
|---|
| 48 |     virtual void connectToServer( std::string host, int port ); | 
|---|
| 49 |  | 
|---|
| 50 |     virtual void disconnectServer(); | 
|---|
| 51 |     virtual void reconnectToServer( std::string host, int port); | 
|---|
| 52 |     virtual void reconnectToServerSoft( std::string host, int port); | 
|---|
| 53 |  | 
|---|
| 54 |     virtual bool writePacket(byte * data, int length); | 
|---|
| 55 |  | 
|---|
| 56 |     virtual int readPacket(byte * data, int maxLength); | 
|---|
| 57 |  | 
|---|
| 58 |   private: | 
|---|
| 59 |     TCPsocket tcpSocket; | 
|---|
| 60 |  | 
|---|
| 61 |     byte incomingBuffer[_INCOMING_BUFFER_SIZE]; | 
|---|
| 62 | #ifdef _USE_OUTGOING_BUFFER | 
|---|
| 63 |   byte outgoingBuffer[_OUTGOING_BUFFER_SIZE]; | 
|---|
| 64 | #endif | 
|---|
| 65 |   int incomingBufferLength; | 
|---|
| 66 | #ifdef _USE_OUTGOING_BUFFER | 
|---|
| 67 |   int outgoingBufferLength; | 
|---|
| 68 | #endif | 
|---|
| 69 |  | 
|---|
| 70 |   SDL_mutex * incomingBufferMutex; | 
|---|
| 71 | #ifdef _USE_OUTGOING_BUFFER | 
|---|
| 72 |   SDL_mutex * outgoingBufferMutex; | 
|---|
| 73 | #endif | 
|---|
| 74 |   SDL_mutex * socketMutex; | 
|---|
| 75 |   bool terminateThread; | 
|---|
| 76 |  | 
|---|
| 77 |   SDL_mutex* threadTerminationMutex; | 
|---|
| 78 |   static int thread_read(void * data); | 
|---|
| 79 |   bool thread_read_running; | 
|---|
| 80 |   bool thread_write_running; | 
|---|
| 81 |  | 
|---|
| 82 |   SDL_Thread*            readThread; | 
|---|
| 83 |   SDL_Thread*            writeThread; | 
|---|
| 84 |  | 
|---|
| 85 | #ifdef _USE_OUTGOING_BUFFER | 
|---|
| 86 |   static int thread_write(void * data); | 
|---|
| 87 | #endif | 
|---|
| 88 |  | 
|---|
| 89 |   int writeBytes(byte * data, int length); | 
|---|
| 90 |   int readBytes(byte * data, int length); | 
|---|
| 91 |   int readBlock(byte * data, int length); | 
|---|
| 92 |  | 
|---|
| 93 |   void init(); | 
|---|
| 94 |  | 
|---|
| 95 | }; | 
|---|
| 96 |  | 
|---|
| 97 |  | 
|---|
| 98 |  | 
|---|
| 99 | #endif /* _NETWORK_SOCKET */ | 
|---|