[7540] | 1 | /*! |
---|
[9406] | 2 | * @file tcp_socket.h |
---|
| 3 | * tcp socket network interface |
---|
[7540] | 4 | |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | #ifndef _TCP_SOCKET |
---|
| 8 | #define _TCP_SOCKET |
---|
| 9 | |
---|
[7541] | 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 |
---|
[9406] | 18 | #define _LOCAL_BUFFER_SIZE 2048 |
---|
[7541] | 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 |
---|
[7540] | 33 | /* include this file, it contains some default definitions */ |
---|
| 34 | #include "netdefs.h" |
---|
| 35 | |
---|
| 36 | |
---|
[7541] | 37 | /* include network_socket.h since all classes are derived from this one */ |
---|
[7540] | 38 | #include "network_socket.h" |
---|
| 39 | |
---|
| 40 | class TcpSocket : public NetworkSocket |
---|
| 41 | { |
---|
[9869] | 42 | ObjectListDeclaration(TcpSocket); |
---|
[7540] | 43 | public: |
---|
| 44 | TcpSocket(); |
---|
| 45 | TcpSocket( std::string host, int port ); |
---|
[7541] | 46 | TcpSocket(TCPsocket sock); |
---|
[7540] | 47 | virtual ~TcpSocket(); |
---|
| 48 | |
---|
| 49 | virtual void connectToServer( std::string host, int port ); |
---|
[9406] | 50 | |
---|
[7540] | 51 | virtual void disconnectServer(); |
---|
[9406] | 52 | virtual void reconnectToServer( std::string host, int port); |
---|
| 53 | virtual void reconnectToServerSoft( std::string host, int port); |
---|
[7540] | 54 | |
---|
| 55 | virtual bool writePacket(byte * data, int length); |
---|
| 56 | |
---|
| 57 | virtual int readPacket(byte * data, int maxLength); |
---|
| 58 | |
---|
[7541] | 59 | private: |
---|
| 60 | TCPsocket tcpSocket; |
---|
[7540] | 61 | |
---|
[7541] | 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 | |
---|
[7540] | 96 | }; |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | #endif /* _NETWORK_SOCKET */ |
---|