My Project
Loading...
Searching...
No Matches
CSocket.h
Go to the documentation of this file.
1//==============================================================================
2/*
3 Software License Agreement (BSD License)
4 Copyright (c) 2003-2024, CHAI3D
5 (www.chai3d.org)
6
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
11 are met:
12
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the following
18 disclaimer in the documentation and/or other materials provided
19 with the distribution.
20
21 * Neither the name of CHAI3D nor the names of its contributors may
22 be used to endorse or promote products derived from this software
23 without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
37
38 \author <http://www.chai3d.org>
39 \author Sebastien Grange
40 \version 3.3.0
41*/
42//==============================================================================
43
44//------------------------------------------------------------------------------
45#ifndef CSocketH
46#define CSocketH
47//------------------------------------------------------------------------------
48#include <string>
49//------------------------------------------------------------------------------
50
51//------------------------------------------------------------------------------
52namespace chai3d {
53//------------------------------------------------------------------------------
54
55//==============================================================================
63//==============================================================================
64
65//==============================================================================
73//==============================================================================
75{
76 //--------------------------------------------------------------------------
77 // CONSTRUCTOR & DESTRUCTOR:
78 //--------------------------------------------------------------------------
79
80public:
81
82 cSocket();
83 cSocket (int a_socket);
84 virtual ~cSocket ();
85
86
87 //--------------------------------------------------------------------------
88 // PUBLIC METHODS:
89 //--------------------------------------------------------------------------
90
91public:
92
94 static std::string getLocalIP();
95
97 int getLocalPort();
98
100 std::string getRemoteIP();
101
103 int getRemotePort();
104
106 int close();
107
109 int peek(char *a_output, unsigned int a_size);
110
112 int peek(std::string& a_output);
113
115 int read(char *a_output, unsigned int a_size);
116
118 int read(std::string& a_output);
119
121 int readLine(char *a_output, unsigned int a_size);
122
124 int readLine(std::string& a_output);
125
127 int send(const char *a_data, int a_length);
128
130 int send(std::string a_data);
131
133 double getTimeout();
134
136 int setTimeout(double a_timeout);
137
139 int getSendSize();
140
142 int setSendSize(int a_size);
143
145 int getRecvSize();
146
148 int setRecvSize(int a_size);
149
151 virtual unsigned char getAttributes() = 0;
152
154 virtual int setAttributes(unsigned char a_attributes) = 0;
155
156
157 //--------------------------------------------------------------------------
158 // PUBLIC MEMBERS:
159 //--------------------------------------------------------------------------
160
161public:
162
164 static const unsigned int BUFFERSIZE = 2048;
165
167 static const unsigned char BLOCKING = 0x01;
168
170 static const unsigned char REUSEADDR = 0x02;
171
173 static const unsigned char NODELAY = 0x04;
174
176 static const unsigned char DEFAULT_ATTR = 0x00 | BLOCKING | REUSEADDR | ~NODELAY;
177
179 static const int WOULDBLOCK = -2;
180
182 static const int SHORTLINE = -3;
183
185 static const int TIMEOUT = -4;
186
187
188 //--------------------------------------------------------------------------
189 // PROTECTED METHODS:
190 //--------------------------------------------------------------------------
191
192protected:
193
194
196 unsigned char getBaseAttributes();
197
199 int setBaseAttributes(unsigned char a_attributes);
200
201
202 //--------------------------------------------------------------------------
203 // PROTECTED MEMBERS:
204 //--------------------------------------------------------------------------
205
206protected:
207
210
212 unsigned char m_attributes;
213
216
218 unsigned long m_remoteIP;
219
221 unsigned int m_remotePort;
222
224 unsigned long m_localIP;
225
227 unsigned int m_localPort;
228
229
230 //--------------------------------------------------------------------------
231 // PRIVATE METHODS:
232 //--------------------------------------------------------------------------
233
234private:
235
237 static int initialize();
238
240 static int cleanup();
241
242
243 //--------------------------------------------------------------------------
244 // PRIVATE MEMBERS:
245 //--------------------------------------------------------------------------
246
247private:
248
250 static int m_socketCount;
251
253 static bool m_initialized;
254};
255
256
257
258//==============================================================================
266//==============================================================================
267class cSocketUDP : public cSocket
268{
269 //--------------------------------------------------------------------------
270 // CONSTRUCTOR & DESTRUCTOR:
271 //--------------------------------------------------------------------------
272
273public:
274
276 cSocketUDP();
277
279 virtual ~cSocketUDP();
280
281
282 //--------------------------------------------------------------------------
283 // PUBLIC METHODS:
284 //--------------------------------------------------------------------------
285
286public:
287
289 int listen(unsigned short a_lport, bool a_block = true, bool a_loopback = false);
290
292 int broadcast(unsigned short a_port, const char *a_data, int a_length);
293
295 int broadcast(unsigned short a_port, const std::string& a_data);
296
298 int setPeer(const char *a_address, unsigned short a_port, bool a_block = true);
299
301 int connect(const char *a_address, unsigned short a_port, unsigned short a_localport, bool a_block = true);
302
304 unsigned char getAttributes();
305
307 int setAttributes(unsigned char a_attributes);
308};
309
310
311
312//==============================================================================
320//==============================================================================
321class cSocketTCP : public cSocket
322{
323 //--------------------------------------------------------------------------
324 // CONSTRUCTOR & DESTRUCTOR:
325 //--------------------------------------------------------------------------
326
327public:
328
330 cSocketTCP();
331
333 cSocketTCP(int socket);
334
336 virtual ~cSocketTCP();
337
338
339 //--------------------------------------------------------------------------
340 // PUBLIC METHODS:
341 //--------------------------------------------------------------------------
342
343public:
344
346 int connect(const char *a_address, unsigned short a_port, bool a_block = true, double a_timeout = -1.0);
347
349 int listen(unsigned short a_port, bool a_block = true, bool a_loopback = false);
350
352 cSocketTCP* accept(bool a_block = true, double a_timeout = -1.0);
353
355 unsigned char getAttributes();
356
358 int setAttributes(unsigned char a_attributes);
359};
360
361//------------------------------------------------------------------------------
362} // namespace chai3d
363//------------------------------------------------------------------------------
364
365//------------------------------------------------------------------------------
366#endif
367//------------------------------------------------------------------------------
This class implements all methods shared by TCP and UDP sockets.
Definition CSocket.h:75
static std::string getLocalIP()
This method retrieves the local IP address.
Definition CSocket.cpp:125
int setSendSize(int a_size)
This method sets the socket outgoing buffer size.
Definition CSocket.cpp:665
int getSendSize()
This method retrieves the socket outgoing buffer size.
Definition CSocket.cpp:636
int readLine(char *a_output, unsigned int a_size)
This method reads an entire line of data (delimited by the '\n' character).
Definition CSocket.cpp:445
int m_socket
Unique socket identifier.
Definition CSocket.h:209
int send(const char *a_data, int a_length)
This method sends data through the socket.
Definition CSocket.cpp:546
virtual int setAttributes(unsigned char a_attributes)=0
This method sets the socket attributes.
int close()
This method closes the socket.
Definition CSocket.cpp:217
static const unsigned char REUSEADDR
This constant defines the attribute bit to hold the socket REUSEADDR attribute.
Definition CSocket.h:170
int peek(char *a_output, unsigned int a_size)
This method reads a copy of incoming data (without removing the data).
Definition CSocket.cpp:248
static const unsigned char BLOCKING
This constant defines the attribute bit to hold the socket BLOCKING attribute.
Definition CSocket.h:167
unsigned long m_remoteIP
Socket remote IP address.
Definition CSocket.h:218
int getRecvSize()
This method retrieves the socket incoming buffer size.
Definition CSocket.cpp:689
double getTimeout()
This method retrieves the socket timeout in (s).
Definition CSocket.cpp:606
std::string getRemoteIP()
This method retrieves the remote IP address that the socket is connected to.
Definition CSocket.cpp:185
unsigned long m_localIP
Socket local IP address.
Definition CSocket.h:224
unsigned int m_localPort
Socket local port number.
Definition CSocket.h:227
int getRemotePort()
This method returns the remote port number that the socket is connected to.
Definition CSocket.cpp:204
static const unsigned char NODELAY
This constant defines the attribute bit to hold the socket NODELAY attribute.
Definition CSocket.h:173
int setRecvSize(int a_size)
This method sets the socket incoming buffer size.
Definition CSocket.cpp:718
static const int SHORTLINE
This return constant is used to signify that a buffer is too small to hold a line.
Definition CSocket.h:182
unsigned char m_attributes
Socket attributes.
Definition CSocket.h:212
static const unsigned char DEFAULT_ATTR
This constant defines the default attributes for new sockets.
Definition CSocket.h:176
static const int TIMEOUT
This return constant is used to signify that a read operation on a socket has timed out.
Definition CSocket.h:185
unsigned char getBaseAttributes()
This method retrieves the socket attributes shared between TCP and UDP.
Definition CSocket.cpp:747
unsigned int m_remotePort
Socket remote port number.
Definition CSocket.h:221
int getLocalPort()
This method returns the local port number that the socket is connected to.
Definition CSocket.cpp:170
virtual ~cSocket()
Definition CSocket.cpp:107
char m_buffer[BUFFERSIZE]
Socket read buffer.
Definition CSocket.h:215
int setBaseAttributes(unsigned char a_attributes)
This method sets the socket attributes shared between TCP and UDP.
Definition CSocket.cpp:767
static const unsigned int BUFFERSIZE
This constant defines the default outgoing and incoming socket buffer sizes.
Definition CSocket.h:164
int read(char *a_output, unsigned int a_size)
This method reads a incoming data.
Definition CSocket.cpp:359
virtual unsigned char getAttributes()=0
This method retrieves the socket attributes.
cSocket()
Definition CSocket.cpp:67
int setTimeout(double a_timeout)
This method sets the socket timeout in (s).
Definition CSocket.cpp:622
static const int WOULDBLOCK
This return constant is used to signify that a non-blocking socket would block.
Definition CSocket.h:179
This class implements all methods specific to TCP sockets.
Definition CSocket.h:322
unsigned char getAttributes()
This method retrieves the socket attributes.
Definition CSocket.cpp:1575
cSocketTCP * accept(bool a_block=true, double a_timeout=-1.0)
This method accepts connections from incoming client connection requests.
Definition CSocket.cpp:1493
cSocketTCP()
Constructor of cSocketTCP.
Definition CSocket.cpp:1240
int listen(unsigned short a_port, bool a_block=true, bool a_loopback=false)
This method listens for incoming client connections.
Definition CSocket.cpp:1407
int connect(const char *a_address, unsigned short a_port, bool a_block=true, double a_timeout=-1.0)
This method connects to a remote server.
Definition CSocket.cpp:1288
int setAttributes(unsigned char a_attributes)
This method sets the socket attributes.
Definition CSocket.cpp:1598
virtual ~cSocketTCP()
Destructor of cSocketTCP.
Definition CSocket.cpp:1265
This class implements all methods specific to UDP sockets.
Definition CSocket.h:268
int listen(unsigned short a_lport, bool a_block=true, bool a_loopback=false)
This method listens for incoming packets on a given port.
Definition CSocket.cpp:905
int connect(const char *a_address, unsigned short a_port, unsigned short a_localport, bool a_block=true)
This method connects to a given remote socket.
Definition CSocket.cpp:1111
virtual ~cSocketUDP()
Destructor of cSocketUDP.
Definition CSocket.cpp:882
int broadcast(unsigned short a_port, const char *a_data, int a_length)
This method broadcast a packet to a given port.
Definition CSocket.cpp:983
unsigned char getAttributes()
This method retrieves the socket attributes.
Definition CSocket.cpp:1209
int setPeer(const char *a_address, unsigned short a_port, bool a_block=true)
This method assigns a peer (remote) socket to the current socket.
Definition CSocket.cpp:1055
int setAttributes(unsigned char a_attributes)
This method sets the socket attributes.
Definition CSocket.cpp:1229
cSocketUDP()
Constructor of cSocketUDP.
Definition CSocket.cpp:871
Definition CAudioBuffer.cpp:57