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