[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 | |
---|
| 21 | /* contains memmove and memcpy */ |
---|
| 22 | #include <string.h> |
---|
| 23 | |
---|
[5722] | 24 | #ifdef HAVE_SDL_H |
---|
| 25 | #include <SDL_Thread.h> |
---|
[5732] | 26 | #else |
---|
[5722] | 27 | #include <SDL/SDL_thread.h> |
---|
| 28 | #endif |
---|
[5565] | 29 | /* include this file, it contains some default definitions */ |
---|
| 30 | #include "netdefs.h" |
---|
[5531] | 31 | |
---|
[5565] | 32 | |
---|
[5592] | 33 | /* include base_object.h since all classes are derived from this one */ |
---|
| 34 | #include "base_object.h" |
---|
[5588] | 35 | |
---|
[5565] | 36 | /* using namespace std is default, this needs to be here */ |
---|
| 37 | using namespace std; |
---|
| 38 | |
---|
[5592] | 39 | class NetworkSocket : public BaseObject |
---|
[5531] | 40 | { |
---|
| 41 | |
---|
[5565] | 42 | private: |
---|
[5624] | 43 | // IPaddress serverAddress; |
---|
| 44 | // unsigned int port; |
---|
[5592] | 45 | TCPsocket tcpSocket; |
---|
[5624] | 46 | // UDPsocket udpSocket; |
---|
[5565] | 47 | |
---|
[5630] | 48 | byte incomingBuffer[_INCOMING_BUFFER_SIZE]; |
---|
| 49 | #ifdef _USE_OUTGOING_BUFFER |
---|
| 50 | byte outgoingBuffer[_OUTGOING_BUFFER_SIZE]; |
---|
| 51 | #endif |
---|
| 52 | int incomingBufferLength; |
---|
| 53 | #ifdef _USE_OUTGOING_BUFFER |
---|
| 54 | int outgoingBufferLength; |
---|
| 55 | #endif |
---|
[5624] | 56 | |
---|
[5630] | 57 | SDL_mutex * incomingBufferMutex; |
---|
| 58 | #ifdef _USE_OUTGOING_BUFFER |
---|
| 59 | SDL_mutex * outgoingBufferMutex; |
---|
| 60 | #endif |
---|
| 61 | SDL_mutex * socketMutex; |
---|
[5624] | 62 | bool terminateThread; |
---|
| 63 | |
---|
| 64 | static int thread_listen(void * data); |
---|
| 65 | static int thread_read(void * data); |
---|
[5630] | 66 | #ifdef _USE_OUTGOING_BUFFER |
---|
| 67 | static int thread_write(void * data); |
---|
| 68 | #endif |
---|
[5624] | 69 | |
---|
[5630] | 70 | bool _isListening; |
---|
| 71 | |
---|
[5531] | 72 | public: |
---|
| 73 | |
---|
| 74 | NetworkSocket(); |
---|
[5733] | 75 | NetworkSocket(IPaddress ip, unsigned int port); |
---|
[5531] | 76 | ~NetworkSocket(); |
---|
[5533] | 77 | |
---|
[5565] | 78 | void connectToServer(IPaddress ip, unsigned int port); |
---|
| 79 | void listen(unsigned int port); |
---|
[5531] | 80 | void disconnectServer(); |
---|
[5614] | 81 | int writeBytes(byte * data, int length); |
---|
| 82 | int readBytes(byte * data, int length); |
---|
[5729] | 83 | int readBlock(byte * data, int length); |
---|
[5533] | 84 | |
---|
[5531] | 85 | }; |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | |
---|
[5532] | 89 | #endif /* _NETWORK_SOCKET */ |
---|