| 1 | /*! | 
|---|
| 2 |  * @file network_socket.h | 
|---|
| 3 |   *  Main interface for the network module. Manages all the modules | 
|---|
| 4 |  | 
|---|
| 5 | */ | 
|---|
| 6 |  | 
|---|
| 7 | #ifndef _NETWORK_SOCKET | 
|---|
| 8 | #define _NETWORK_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 base_object.h since all classes are derived from this one */ | 
|---|
| 38 | #include "base_object.h" | 
|---|
| 39 |  | 
|---|
| 40 | /* using namespace std is default, this needs to be here */ | 
|---|
| 41 | using namespace std; | 
|---|
| 42 |  | 
|---|
| 43 | class NetworkSocket : public BaseObject | 
|---|
| 44 | { | 
|---|
| 45 |  | 
|---|
| 46 | private: | 
|---|
| 47 | //  IPaddress serverAddress; | 
|---|
| 48 | //  unsigned int port; | 
|---|
| 49 |   TCPsocket tcpSocket; | 
|---|
| 50 | //  UDPsocket udpSocket; | 
|---|
| 51 |  | 
|---|
| 52 |   byte incomingBuffer[_INCOMING_BUFFER_SIZE]; | 
|---|
| 53 | #ifdef _USE_OUTGOING_BUFFER | 
|---|
| 54 |   byte outgoingBuffer[_OUTGOING_BUFFER_SIZE]; | 
|---|
| 55 | #endif | 
|---|
| 56 |   int incomingBufferLength; | 
|---|
| 57 | #ifdef _USE_OUTGOING_BUFFER | 
|---|
| 58 |   int outgoingBufferLength; | 
|---|
| 59 | #endif | 
|---|
| 60 |  | 
|---|
| 61 |   SDL_mutex * incomingBufferMutex; | 
|---|
| 62 | #ifdef _USE_OUTGOING_BUFFER | 
|---|
| 63 |   SDL_mutex * outgoingBufferMutex; | 
|---|
| 64 | #endif | 
|---|
| 65 |   SDL_mutex * socketMutex; | 
|---|
| 66 |   bool terminateThread; | 
|---|
| 67 |  | 
|---|
| 68 |   SDL_mutex* threadTerminationMutex; | 
|---|
| 69 |   static int thread_read(void * data); | 
|---|
| 70 |   bool thread_read_running; | 
|---|
| 71 |   bool thread_write_running; | 
|---|
| 72 |  | 
|---|
| 73 |   SDL_Thread*            readThread; | 
|---|
| 74 |   SDL_Thread*            writeThread; | 
|---|
| 75 |  | 
|---|
| 76 | #ifdef _USE_OUTGOING_BUFFER | 
|---|
| 77 |   static int thread_write(void * data); | 
|---|
| 78 | #endif | 
|---|
| 79 |  | 
|---|
| 80 |   int writeBytes(byte * data, int length); | 
|---|
| 81 |   int readBytes(byte * data, int length); | 
|---|
| 82 |   int readBlock(byte * data, int length); | 
|---|
| 83 |  | 
|---|
| 84 |   void init(); | 
|---|
| 85 |  | 
|---|
| 86 | public: | 
|---|
| 87 |  | 
|---|
| 88 |   NetworkSocket(); | 
|---|
| 89 |   virtual ~NetworkSocket(); | 
|---|
| 90 |   NetworkSocket(IPaddress ip); | 
|---|
| 91 |   NetworkSocket(TCPsocket sock); | 
|---|
| 92 |   void destroy() { terminateThread = true; }; | 
|---|
| 93 |  | 
|---|
| 94 |  | 
|---|
| 95 |   void connectToServer(IPaddress ip); | 
|---|
| 96 |   void disconnectServer(); | 
|---|
| 97 |  | 
|---|
| 98 |   bool writePacket(byte * data, int length); | 
|---|
| 99 |   int readPacket(byte * data, int maxLength); | 
|---|
| 100 |  | 
|---|
| 101 |   inline bool isOk() { return tcpSocket!=NULL; } | 
|---|
| 102 |  | 
|---|
| 103 | }; | 
|---|
| 104 |  | 
|---|
| 105 |  | 
|---|
| 106 |  | 
|---|
| 107 | #endif /* _NETWORK_SOCKET */ | 
|---|