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