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