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