blocxx
Socket.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Vintela, Inc.,
15 * nor Novell, Inc.,
16 * nor the names of its contributors or employees may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *******************************************************************************/
32 
33 
39 #ifndef BLOCXX_SOCKET_HPP_INCLUDE_GUARD_
40 #define BLOCXX_SOCKET_HPP_INCLUDE_GUARD_
41 #include "blocxx/BLOCXX_config.h"
42 #include "blocxx/CommonFwd.hpp"
43 #include "blocxx/SelectableIFC.hpp"
44 #include "blocxx/IOIFC.hpp"
46 #include "blocxx/Types.hpp"
47 #include "blocxx/UnnamedPipe.hpp"
48 #include "blocxx/SocketFlags.hpp"
49 #include "blocxx/NetworkTypes.hpp"
50 #include "blocxx/SocketAddress.hpp"
52 #include "blocxx/Timeout.hpp"
53 #include "blocxx/Exception.hpp"
54 #include "blocxx/LazyGlobal.hpp"
55 
56 #ifndef BLOCXX_NO_SSL
57 #include <openssl/ssl.h>
58 #endif
59 
60 
61 namespace BLOCXX_NAMESPACE
62 {
63 
64 BLOCXX_DECLARE_APIEXCEPTION(SocketTimeout, BLOCXX_COMMON_API)
65 class BLOCXX_COMMON_API Socket : public SelectableIFC, public IOIFC
66 {
67 public:
71  Socket();
76  Socket(const SSLClientCtxRef& sslCtx);
105  void connect(const SocketAddress& addr)
106  { m_impl->connect(addr); }
110  void disconnect() { m_impl->disconnect(); }
111 
112  static const int INFINITE_TIMEOUT BLOCXX_DEPRECATED = -1;
117  BLOCXX_DEPRECATED void setReceiveTimeout(int seconds) { m_impl->setReceiveTimeout(Timeout::relative(seconds));}
122  void setReceiveTimeout(const Timeout& timeout) { m_impl->setReceiveTimeout(timeout);}
127  Timeout getReceiveTimeout() const { return m_impl->getReceiveTimeout(); }
132  BLOCXX_DEPRECATED void setSendTimeout(int seconds) { m_impl->setSendTimeout(Timeout::relative(seconds)); }
137  void setSendTimeout(const Timeout& timeout) { m_impl->setSendTimeout(timeout); }
142  Timeout getSendTimeout() const { return m_impl->getSendTimeout(); }
147  BLOCXX_DEPRECATED void setConnectTimeout(int seconds) { m_impl->setConnectTimeout(Timeout::relative(seconds)); }
152  void setConnectTimeout(const Timeout& timeout) { m_impl->setConnectTimeout(timeout); }
157  Timeout getConnectTimeout() const { return m_impl->getConnectTimeout(); }
162  BLOCXX_DEPRECATED void setTimeouts(int seconds) { m_impl->setTimeouts(Timeout::relative(seconds)); }
167  void setTimeouts(const Timeout& timeout) { m_impl->setTimeouts(timeout); }
172  bool receiveTimeOutExpired() const { return m_impl->receiveTimeOutExpired(); }
181  int write(const void* dataOut, int dataOutLen, ErrorAction errorAsException = E_RETURN_ON_ERROR)
182  { return m_impl->write(dataOut, dataOutLen, errorAsException); }
191  int read(void* dataIn, int dataInLen, ErrorAction errorAsException = E_RETURN_ON_ERROR)
192  { return m_impl->read(dataIn, dataInLen, errorAsException); }
193 
200  BLOCXX_DEPRECATED bool waitForInput(int timeOutSecs)
201  { return m_impl->waitForInput(Timeout::relative(timeOutSecs)); }
202 
209  bool waitForInput(const Timeout& timeout = Timeout::infinite)
210  { return m_impl->waitForInput(timeout); }
211 
218  BLOCXX_DEPRECATED bool waitForOutput(int timeOutSecs)
219  { return m_impl->waitForOutput(Timeout::relative(timeOutSecs)); }
220 
227  bool waitForOutput(const Timeout& timeout = Timeout::infinite)
228  { return m_impl->waitForOutput(timeout); }
229 
234  SocketAddress getLocalAddress() const { return m_impl->getLocalAddress(); }
239  SocketAddress getPeerAddress() const { return m_impl->getPeerAddress(); }
245  std::istream& getInputStream() // TODO: BLOCXX_DEPRECATED in 3.2.0
246  { return m_impl->getInputStream(); }
252  std::ostream& getOutputStream() // TODO: BLOCXX_DEPRECATED in 3.2.0
253  { return m_impl->getOutputStream(); }
259  BLOCXX_DEPRECATED std::iostream& getIOStream() // in 3.2.0
260  { return m_impl->getIOStream(); }
264  Select_t getSelectObj() const { return m_impl->getSelectObj(); }
269  SocketHandle_t getfd() { return m_impl->getfd(); }
270 
274  bool isConnected() const { return m_impl->isConnected(); }
275 
276  static void createShutDownMechanism();
282  static void shutdownAllSockets();
287  static bool gotShutDown() BLOCXX_DEPRECATED; // deprecated in 3.1.0
288  static void deleteShutDownMechanism();
289 
290 #if defined(BLOCXX_WIN32)
291  typedef HANDLE ShutDownMechanism_t;
292 #else
294 #endif
295 
296  static ShutDownMechanism_t getShutDownMechanism()
297  {
298  return s_shutDownMechanism;
299  }
300 
301 #ifndef BLOCXX_NO_SSL
302 
307  SSL* getSSL() const;
308 
313  bool peerCertVerified() const;
314 #endif
315 
316 private:
325  const SSLServerCtxRef& sslCtx);
326 
327 #ifdef BLOCXX_WIN32
328 #pragma warning (push)
329 #pragma warning (disable: 4251)
330 #endif
331 
333 
334 #ifdef BLOCXX_WIN32
335 #pragma warning (pop)
336 #endif
337 
339  {
340  static Socket::ShutDownMechanism_t* create(int initVal)
341  {
342  return new Socket::ShutDownMechanism_t();
343  }
344  };
346 
347  friend class ServerSocketImpl;
348 
349 };
350 
351 } // end namespace BLOCXX_NAMESPACE
352 
353 #endif
SocketAddress getPeerAddress() const
Get the peer address associated with the socket connection.
Definition: Socket.hpp:239
std::istream & getInputStream()
Get an istream to read from the socket.
Definition: Socket.hpp:245
Taken from RFC 1321.
Timeout getConnectTimeout() const
Get the connect timeout.
Definition: Socket.hpp:157
void connect(const SocketAddress &addr)
Connect to a peer node.
Definition: Socket.hpp:105
int write(const void *dataOut, int dataOutLen, ErrorAction errorAsException=E_RETURN_ON_ERROR)
Write some data to the socket.
Definition: Socket.hpp:181
bool isConnected() const
Get connected state.
Definition: Socket.hpp:274
SocketAddress getLocalAddress() const
Get the local address associated with the socket connection.
Definition: Socket.hpp:234
BLOCXX_DEPRECATED void setSendTimeout(int seconds)
Set the send timeout on the socket.
Definition: Socket.hpp:132
const UInt32 INFINITE_TIMEOUT BLOCXX_DEPRECATED
Value that means infinite timeout.
Definition: Select.hpp:71
BLOCXX_DEPRECATED void setTimeouts(int seconds)
Set all timeouts (send, receive, connect)
Definition: Socket.hpp:162
Select_t getSelectObj() const
Definition: Socket.hpp:264
static Socket::ShutDownMechanism_t * create(int initVal)
Definition: Socket.hpp:340
SocketBaseImplRef m_impl
Definition: Socket.hpp:332
bool waitForOutput(const Timeout &timeout=Timeout::infinite)
Wait for output on the socket for a specified length of time.
Definition: Socket.hpp:227
void setReceiveTimeout(const Timeout &timeout)
Set the receive timeout on the socket.
Definition: Socket.hpp:122
Timeout getSendTimeout() const
Get the send timeout.
Definition: Socket.hpp:142
int read(void *dataIn, int dataInLen, ErrorAction errorAsException=E_RETURN_ON_ERROR)
Read from the socket.
Definition: Socket.hpp:191
std::ostream & getOutputStream()
Get an ostream to write to the socket.
Definition: Socket.hpp:252
static Timeout relative(float seconds)
Definition: Timeout.cpp:58
void setSendTimeout(const Timeout &timeout)
Set the send timeout on the socket.
Definition: Socket.hpp:137
BLOCXX_DEPRECATED void setReceiveTimeout(int seconds)
Set the receive timeout on the socket.
Definition: Socket.hpp:117
BLOCXX_DEPRECATED bool waitForInput(int timeOutSecs)
Wait for input on the socket for a specified length of time.
Definition: Socket.hpp:200
A timeout can be absolute, which means that it will happen at the specified DateTime.
Definition: Timeout.hpp:55
static Timeout infinite
Definition: Timeout.hpp:62
void setTimeouts(const Timeout &timeout)
Set all timeouts (send, receive, connect)
Definition: Socket.hpp:167
static ShutDownMechanism_t getShutDownMechanism()
Definition: Socket.hpp:296
UnnamedPipeRef ShutDownMechanism_t
Definition: Socket.hpp:293
void setConnectTimeout(const Timeout &timeout)
Set the connect timeout on the socket.
Definition: Socket.hpp:152
BLOCXX_DEPRECATED void setConnectTimeout(int seconds)
Set the connect timeout on the socket.
Definition: Socket.hpp:147
SocketHandle_t getfd()
Get the socket handle for the socket.
Definition: Socket.hpp:269
BLOCXX_DEPRECATED std::iostream & getIOStream()
Get an iostream to read/write from/to the socket.
Definition: Socket.hpp:259
This class can be used to store a global variable that is lazily initialized in a thread safe manner...
Definition: LazyGlobal.hpp:114
void disconnect()
Disconnect the (presumably) open connection.
Definition: Socket.hpp:110
#define BLOCXX_DECLARE_APIEXCEPTION(NAME, LINKAGE_SPEC)
Declare a new exception class named <NAME>Exception that derives from Exception This macro is typical...
Definition: Exception.hpp:396
static LazyGlobal< Socket::ShutDownMechanism_t, int, ShutDownMechanismFactory > s_shutDownMechanism
Definition: Socket.hpp:345
bool receiveTimeOutExpired() const
Has the receive timeout expired?
Definition: Socket.hpp:172
Timeout getReceiveTimeout() const
Get the receive timeout.
Definition: Socket.hpp:127
bool waitForInput(const Timeout &timeout=Timeout::infinite)
Wait for input on the socket for a specified length of time.
Definition: Socket.hpp:209
ProcessImplRef m_impl
Definition: Process.cpp:317
BLOCXX_DEPRECATED bool waitForOutput(int timeOutSecs)
Wait for output on the socket for a specified length of time.
Definition: Socket.hpp:218