blocxx
|
00001 /******************************************************************************* 00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved. 00003 * Copyright (C) 2006, Novell, Inc. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of 00014 * Vintela, Inc., 00015 * nor Novell, Inc., 00016 * nor the names of its contributors or employees may be used to 00017 * endorse or promote products derived from this software without 00018 * specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00030 * POSSIBILITY OF SUCH DAMAGE. 00031 *******************************************************************************/ 00032 00033 00039 #ifndef BLOCXX_SOCKET_HPP_INCLUDE_GUARD_ 00040 #define BLOCXX_SOCKET_HPP_INCLUDE_GUARD_ 00041 #include "blocxx/BLOCXX_config.h" 00042 #include "blocxx/CommonFwd.hpp" 00043 #include "blocxx/SelectableIFC.hpp" 00044 #include "blocxx/IOIFC.hpp" 00045 #include "blocxx/SocketBaseImpl.hpp" 00046 #include "blocxx/Types.hpp" 00047 #include "blocxx/UnnamedPipe.hpp" 00048 #include "blocxx/SocketFlags.hpp" 00049 #include "blocxx/NetworkTypes.hpp" 00050 #include "blocxx/SocketAddress.hpp" 00051 #include "blocxx/IntrusiveReference.hpp" 00052 #include "blocxx/Timeout.hpp" 00053 #include "blocxx/Exception.hpp" 00054 #include "blocxx/LazyGlobal.hpp" 00055 00056 #ifndef BLOCXX_NO_SSL 00057 #include <openssl/ssl.h> 00058 #endif 00059 00060 00061 namespace BLOCXX_NAMESPACE 00062 { 00063 00064 BLOCXX_DECLARE_APIEXCEPTION(SocketTimeout, BLOCXX_COMMON_API) 00065 class BLOCXX_COMMON_API Socket : public SelectableIFC, public IOIFC 00066 { 00067 public: 00071 Socket(); 00076 Socket(const SSLClientCtxRef& sslCtx); 00081 Socket(SocketFlags::ESSLFlag isSSL) BLOCXX_DEPRECATED; 00089 Socket(SocketHandle_t fd, SocketAddress::AddressType addrType, 00090 SocketFlags::ESSLFlag isSSL = SocketFlags::E_NOT_SSL); 00099 Socket(const SocketAddress& addr, SocketFlags::ESSLFlag isSSL = SocketFlags::E_NOT_SSL); 00105 void connect(const SocketAddress& addr) 00106 { m_impl->connect(addr); } 00110 void disconnect() { m_impl->disconnect(); } 00111 00112 static const int INFINITE_TIMEOUT BLOCXX_DEPRECATED = -1; 00117 BLOCXX_DEPRECATED void setReceiveTimeout(int seconds) { m_impl->setReceiveTimeout(Timeout::relative(seconds));} 00122 void setReceiveTimeout(const Timeout& timeout) { m_impl->setReceiveTimeout(timeout);} 00127 Timeout getReceiveTimeout() const { return m_impl->getReceiveTimeout(); } 00132 BLOCXX_DEPRECATED void setSendTimeout(int seconds) { m_impl->setSendTimeout(Timeout::relative(seconds)); } 00137 void setSendTimeout(const Timeout& timeout) { m_impl->setSendTimeout(timeout); } 00142 Timeout getSendTimeout() const { return m_impl->getSendTimeout(); } 00147 BLOCXX_DEPRECATED void setConnectTimeout(int seconds) { m_impl->setConnectTimeout(Timeout::relative(seconds)); } 00152 void setConnectTimeout(const Timeout& timeout) { m_impl->setConnectTimeout(timeout); } 00157 Timeout getConnectTimeout() const { return m_impl->getConnectTimeout(); } 00162 BLOCXX_DEPRECATED void setTimeouts(int seconds) { m_impl->setTimeouts(Timeout::relative(seconds)); } 00167 void setTimeouts(const Timeout& timeout) { m_impl->setTimeouts(timeout); } 00172 bool receiveTimeOutExpired() const { return m_impl->receiveTimeOutExpired(); } 00181 int write(const void* dataOut, int dataOutLen, ErrorAction errorAsException = E_RETURN_ON_ERROR) 00182 { return m_impl->write(dataOut, dataOutLen, errorAsException); } 00191 int read(void* dataIn, int dataInLen, ErrorAction errorAsException = E_RETURN_ON_ERROR) 00192 { return m_impl->read(dataIn, dataInLen, errorAsException); } 00193 00200 BLOCXX_DEPRECATED bool waitForInput(int timeOutSecs) 00201 { return m_impl->waitForInput(Timeout::relative(timeOutSecs)); } 00202 00209 bool waitForInput(const Timeout& timeout = Timeout::infinite) 00210 { return m_impl->waitForInput(timeout); } 00211 00218 BLOCXX_DEPRECATED bool waitForOutput(int timeOutSecs) 00219 { return m_impl->waitForOutput(Timeout::relative(timeOutSecs)); } 00220 00227 bool waitForOutput(const Timeout& timeout = Timeout::infinite) 00228 { return m_impl->waitForOutput(timeout); } 00229 00234 SocketAddress getLocalAddress() const { return m_impl->getLocalAddress(); } 00239 SocketAddress getPeerAddress() const { return m_impl->getPeerAddress(); } 00245 std::istream& getInputStream() // TODO: BLOCXX_DEPRECATED in 3.2.0 00246 { return m_impl->getInputStream(); } 00252 std::ostream& getOutputStream() // TODO: BLOCXX_DEPRECATED in 3.2.0 00253 { return m_impl->getOutputStream(); } 00259 BLOCXX_DEPRECATED std::iostream& getIOStream() // in 3.2.0 00260 { return m_impl->getIOStream(); } 00264 Select_t getSelectObj() const { return m_impl->getSelectObj(); } 00269 SocketHandle_t getfd() { return m_impl->getfd(); } 00270 00274 bool isConnected() const { return m_impl->isConnected(); } 00275 00276 static void createShutDownMechanism(); 00282 static void shutdownAllSockets(); 00287 static bool gotShutDown() BLOCXX_DEPRECATED; // deprecated in 3.1.0 00288 static void deleteShutDownMechanism(); 00289 00290 #if defined(BLOCXX_WIN32) 00291 typedef HANDLE ShutDownMechanism_t; 00292 #else 00293 typedef UnnamedPipeRef ShutDownMechanism_t; 00294 #endif 00295 00296 static ShutDownMechanism_t getShutDownMechanism() 00297 { 00298 return s_shutDownMechanism; 00299 } 00300 00301 #ifndef BLOCXX_NO_SSL 00302 00307 SSL* getSSL() const; 00308 00313 bool peerCertVerified() const; 00314 #endif 00315 00316 private: 00324 Socket(SocketHandle_t fd, SocketAddress::AddressType addrType, 00325 const SSLServerCtxRef& sslCtx); 00326 00327 #ifdef BLOCXX_WIN32 00328 #pragma warning (push) 00329 #pragma warning (disable: 4251) 00330 #endif 00331 00332 SocketBaseImplRef m_impl; 00333 00334 #ifdef BLOCXX_WIN32 00335 #pragma warning (pop) 00336 #endif 00337 00338 struct ShutDownMechanismFactory 00339 { 00340 static Socket::ShutDownMechanism_t* create(int initVal) 00341 { 00342 return new Socket::ShutDownMechanism_t(); 00343 } 00344 }; 00345 static LazyGlobal<Socket::ShutDownMechanism_t, int, ShutDownMechanismFactory> s_shutDownMechanism; 00346 00347 friend class ServerSocketImpl; 00348 00349 }; 00350 00351 } // end namespace BLOCXX_NAMESPACE 00352 00353 #endif