[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 | |
---|
| 65 | static int thread_read(void * data); |
---|
[5630] | 66 | #ifdef _USE_OUTGOING_BUFFER |
---|
| 67 | static int thread_write(void * data); |
---|
| 68 | #endif |
---|
[5624] | 69 | |
---|
[5996] | 70 | int writeBytes(byte * data, int length); |
---|
| 71 | int readBytes(byte * data, int length); |
---|
| 72 | int readBlock(byte * data, int length); |
---|
[5630] | 73 | |
---|
[5996] | 74 | void init(); |
---|
| 75 | |
---|
[5531] | 76 | public: |
---|
| 77 | |
---|
| 78 | NetworkSocket(); |
---|
[5804] | 79 | NetworkSocket(IPaddress ip); |
---|
[5996] | 80 | NetworkSocket(TCPsocket sock); |
---|
[5531] | 81 | ~NetworkSocket(); |
---|
[5533] | 82 | |
---|
[5804] | 83 | void connectToServer(IPaddress ip); |
---|
[5531] | 84 | void disconnectServer(); |
---|
[5533] | 85 | |
---|
[5996] | 86 | bool writePacket(byte * data, int length); |
---|
| 87 | int readPacket(byte * data, int maxLength); |
---|
[5808] | 88 | |
---|
[5531] | 89 | }; |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | |
---|
[5532] | 93 | #endif /* _NETWORK_SOCKET */ |
---|