| 1 | /*! | 
|---|
| 2 | * @file network_socket.h | 
|---|
| 3 | *  Main interface for the network module. Manages all the modules | 
|---|
| 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 |  | 
|---|
| 52 | virtual bool writePacket(byte * data, int length); | 
|---|
| 53 |  | 
|---|
| 54 | virtual int readPacket(byte * data, int maxLength); | 
|---|
| 55 |  | 
|---|
| 56 | private: | 
|---|
| 57 | TCPsocket tcpSocket; | 
|---|
| 58 |  | 
|---|
| 59 | byte incomingBuffer[_INCOMING_BUFFER_SIZE]; | 
|---|
| 60 | #ifdef _USE_OUTGOING_BUFFER | 
|---|
| 61 | byte outgoingBuffer[_OUTGOING_BUFFER_SIZE]; | 
|---|
| 62 | #endif | 
|---|
| 63 | int incomingBufferLength; | 
|---|
| 64 | #ifdef _USE_OUTGOING_BUFFER | 
|---|
| 65 | int outgoingBufferLength; | 
|---|
| 66 | #endif | 
|---|
| 67 |  | 
|---|
| 68 | SDL_mutex * incomingBufferMutex; | 
|---|
| 69 | #ifdef _USE_OUTGOING_BUFFER | 
|---|
| 70 | SDL_mutex * outgoingBufferMutex; | 
|---|
| 71 | #endif | 
|---|
| 72 | SDL_mutex * socketMutex; | 
|---|
| 73 | bool terminateThread; | 
|---|
| 74 |  | 
|---|
| 75 | SDL_mutex* threadTerminationMutex; | 
|---|
| 76 | static int thread_read(void * data); | 
|---|
| 77 | bool thread_read_running; | 
|---|
| 78 | bool thread_write_running; | 
|---|
| 79 |  | 
|---|
| 80 | SDL_Thread*            readThread; | 
|---|
| 81 | SDL_Thread*            writeThread; | 
|---|
| 82 |  | 
|---|
| 83 | #ifdef _USE_OUTGOING_BUFFER | 
|---|
| 84 | static int thread_write(void * data); | 
|---|
| 85 | #endif | 
|---|
| 86 |  | 
|---|
| 87 | int writeBytes(byte * data, int length); | 
|---|
| 88 | int readBytes(byte * data, int length); | 
|---|
| 89 | int readBlock(byte * data, int length); | 
|---|
| 90 |  | 
|---|
| 91 | void init(); | 
|---|
| 92 |  | 
|---|
| 93 | }; | 
|---|
| 94 |  | 
|---|
| 95 |  | 
|---|
| 96 |  | 
|---|
| 97 | #endif /* _NETWORK_SOCKET */ | 
|---|