[5531] | 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 | |
---|
[6994] | 10 | //TODO HACK else gdb will not work on server |
---|
[6959] | 11 | #define DONTEXITTHREADS |
---|
| 12 | |
---|
[5630] | 13 | //if you want to use outgoing buffer define _USE_OUTGOING_BUFFER |
---|
| 14 | #define _USE_OUTGOING_BUFFER |
---|
| 15 | |
---|
[6959] | 16 | #define _INCOMING_BUFFER_SIZE 2024000 |
---|
[5631] | 17 | #define _OUTGOING_BUFFER_SIZE 2024000 |
---|
[5624] | 18 | #define _LOCAL_BUFFER_SIZE 1024 |
---|
[5630] | 19 | //sleep if incoming buffer is full |
---|
[5628] | 20 | #define _MSECONDS_SLEEP_FULL_BUFFER 10 |
---|
[5630] | 21 | //sleep if outgoing buffer is empty |
---|
| 22 | #define _MSECONDS_SLEEP_EMPTY_BUFFER 10 |
---|
[5624] | 23 | |
---|
[5996] | 24 | |
---|
[5624] | 25 | /* contains memmove and memcpy */ |
---|
| 26 | #include <string.h> |
---|
| 27 | |
---|
[5722] | 28 | #ifdef HAVE_SDL_H |
---|
[5822] | 29 | #include <SDL_thread.h> |
---|
[5732] | 30 | #else |
---|
[5822] | 31 | #include <SDL/SDL_thread.h> |
---|
[5722] | 32 | #endif |
---|
[5565] | 33 | /* include this file, it contains some default definitions */ |
---|
| 34 | #include "netdefs.h" |
---|
[5531] | 35 | |
---|
[5565] | 36 | |
---|
[5592] | 37 | /* include base_object.h since all classes are derived from this one */ |
---|
| 38 | #include "base_object.h" |
---|
[5588] | 39 | |
---|
[5565] | 40 | /* using namespace std is default, this needs to be here */ |
---|
| 41 | using namespace std; |
---|
| 42 | |
---|
[5592] | 43 | class NetworkSocket : public BaseObject |
---|
[5531] | 44 | { |
---|
| 45 | |
---|
[5565] | 46 | private: |
---|
[5624] | 47 | // IPaddress serverAddress; |
---|
| 48 | // unsigned int port; |
---|
[5592] | 49 | TCPsocket tcpSocket; |
---|
[5624] | 50 | // UDPsocket udpSocket; |
---|
[5565] | 51 | |
---|
[5630] | 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 |
---|
[5624] | 60 | |
---|
[5630] | 61 | SDL_mutex * incomingBufferMutex; |
---|
| 62 | #ifdef _USE_OUTGOING_BUFFER |
---|
| 63 | SDL_mutex * outgoingBufferMutex; |
---|
| 64 | #endif |
---|
| 65 | SDL_mutex * socketMutex; |
---|
[5624] | 66 | bool terminateThread; |
---|
| 67 | |
---|
[6139] | 68 | SDL_mutex* threadTerminationMutex; |
---|
[5624] | 69 | static int thread_read(void * data); |
---|
[6139] | 70 | bool thread_read_running; |
---|
| 71 | bool thread_write_running; |
---|
| 72 | |
---|
| 73 | SDL_Thread* readThread; |
---|
| 74 | SDL_Thread* writeThread; |
---|
| 75 | |
---|
[5630] | 76 | #ifdef _USE_OUTGOING_BUFFER |
---|
| 77 | static int thread_write(void * data); |
---|
| 78 | #endif |
---|
[5624] | 79 | |
---|
[5996] | 80 | int writeBytes(byte * data, int length); |
---|
| 81 | int readBytes(byte * data, int length); |
---|
| 82 | int readBlock(byte * data, int length); |
---|
[5630] | 83 | |
---|
[5996] | 84 | void init(); |
---|
| 85 | |
---|
[5531] | 86 | public: |
---|
| 87 | |
---|
| 88 | NetworkSocket(); |
---|
[6981] | 89 | virtual ~NetworkSocket(); |
---|
[5804] | 90 | NetworkSocket(IPaddress ip); |
---|
[5996] | 91 | NetworkSocket(TCPsocket sock); |
---|
[6139] | 92 | void destroy() { terminateThread = true; }; |
---|
[5533] | 93 | |
---|
[6139] | 94 | |
---|
[5804] | 95 | void connectToServer(IPaddress ip); |
---|
[5531] | 96 | void disconnectServer(); |
---|
[5533] | 97 | |
---|
[5996] | 98 | bool writePacket(byte * data, int length); |
---|
| 99 | int readPacket(byte * data, int maxLength); |
---|
[5808] | 100 | |
---|
[6139] | 101 | inline bool isOk() { return tcpSocket!=NULL; } |
---|
| 102 | |
---|
[5531] | 103 | }; |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | |
---|
[5532] | 107 | #endif /* _NETWORK_SOCKET */ |
---|