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