libcommonc++  0.7
Socket.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_Socket_hxx
24 #define __ccxx_Socket_hxx
25 
26 #include <commonc++/Common.h++>
28 #include <commonc++/Network.h++>
30 
31 #ifdef CCXX_OS_WINDOWS
32 typedef int socklen_t;
33 typedef char FAR* sockbufptr_t;
34 #endif
35 
36 #ifdef CCXX_OS_POSIX
37 #include <netinet/in.h>
38 #include <sys/socket.h>
39 typedef void * sockbufptr_t;
40 #endif
41 
42 #ifndef MSG_NOSIGNAL
43 #define MSG_NOSIGNAL 0
44 #endif // MSG_NOSIGNAL
45 
46 namespace ccxx {
47 
48 #ifdef CCXX_OS_WINDOWS
49 typedef SOCKET SocketHandle;
50 #define INVALID_SOCKET_HANDLE INVALID_SOCKET
51 #else
52 typedef int SocketHandle;
53 #define INVALID_SOCKET_HANDLE (-1)
54 #endif
55 
62 {
63  friend class ServerSocket;
64  friend class SocketSelector;
65 
66  protected:
67 
73  Socket(NetProtocol type = ProtoTCP);
74 
75  public:
76 
78  virtual ~Socket();
79 
86  virtual void init();
87 
98  virtual void connect(const String& addr, uint16_t port);
99 
108  virtual void connect(const SocketAddress& addr);
109 
114  virtual void shutdown();
115 
123  void setTimeout(timespan_ms_t timeout);
124 
131  void setReceiveBufSize(size_t size);
132 
139  size_t getReceiveBufSize() const;
140 
147  void setSendBufSize(size_t size);
148 
155  size_t getSendBufSize() const;
156 
164  void setLingerTime(timespan_s_t timeout);
165 
173  timespan_s_t getLingerTime() const;
174 
185  void setReuseAddress(bool enable);
186 
193  bool getReuseAddress() const;
194 
202  void setKeepAlive(bool enable);
203 
210  bool getKeepAlive();
211 
219  void setTCPDelay(bool enable);
220 
228  bool getTCPDelay();
229 
231  inline bool isInitialized() const
232  { return(_socket != INVALID_SOCKET_HANDLE); }
233 
235  inline bool isConnected() const
236  { return(_connected); }
237 
239  inline NetProtocol getType() const
240  { return(_type); }
241 
243  inline const SocketAddress& getLocalAddress() const
244  { return(_laddr); }
245 
247  inline const SocketAddress& getRemoteAddress() const
248  { return(_raddr); }
249 
250  protected:
251 
252  enum IOWaitMode { WaitWrite, WaitRead };
253 
255  inline SocketHandle getSocketHandle() const
256  { return(_socket); }
257 
264  void waitForIO(IOWaitMode mode);
265 
269  SocketHandle _socket;
276 
278  bool _connected;
279  bool _reuseAddr;
282  private:
283 
285 };
286 
287 } // namespace ccxx
288 
289 #endif // __ccxx_Socket_hxx
TCP protocol.
Definition: Network.h++:44
bool isConnected() const
Determine if the socket is connected.
Definition: Socket.h++:235
SocketHandle getSocketHandle() const
Get the underlying socket handle for this socket.
Definition: Socket.h++:255
A server (listening) socket for StreamSocket (TCP) connections.
Definition: ServerSocket.h++:40
A base class for network sockets.
Definition: Socket.h++:61
NetProtocol getType() const
Get the socket type.
Definition: Socket.h++:239
const SocketAddress & getRemoteAddress() const
Get the address of the remote (peer) end of the socket.
Definition: Socket.h++:247
IOWaitMode
Definition: Socket.h++:252
SocketAddress _laddr
The local address.
Definition: Socket.h++:273
int SocketHandle
Definition: Socket.h++:52
A socket endpoint network address.
Definition: SocketAddress.h++:46
SocketAddress _raddr
The remote address.
Definition: Socket.h++:271
SocketHandle _socket
A handle to the socket itself.
Definition: Socket.h++:269
bool isInitialized() const
Determine if the socket has been initialized.
Definition: Socket.h++:231
A socket I/O selector.
Definition: SocketSelector.h++:351
#define COMMONCPP_API
Definition: Common.h++:126
NetProtocol
Network protocol types.
Definition: Network.h++:40
int timespan_s_t
A timespan expressed in seconds.
Definition: Integers.h++:91
Definition: Socket.h++:252
A flexible, reference counted, copy-on-write, thread-safe, nullable string.
Definition: String.h++:50
#define INVALID_SOCKET_HANDLE
Definition: Socket.h++:53
#define CCXX_COPY_DECLS(CLASS)
Inlines declarations of a copy constructor and assignment operator for the class CLASS.
Definition: Common.h++:295
int timespan_ms_t
A timespan expressed in milliseconds.
Definition: Integers.h++:104
int _sotimeout
The timeout value.
Definition: Socket.h++:275
NetProtocol _type
The socket type.
Definition: Socket.h++:267
Definition: AllocationMap.c++:25
const SocketAddress & getLocalAddress() const
Get the address of the local end of the socket.
Definition: Socket.h++:243