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