00001 /* Copyright (C) GridGain Systems. All Rights Reserved. */ 00002 00003 /* _________ _____ __________________ _____ 00004 * __ ____/___________(_)______ /__ ____/______ ____(_)_______ 00005 * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ 00006 * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / 00007 * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ 00008 */ 00009 00010 #ifndef GRID_CLIENT_CONNECTION_HPP_INCLUDED 00011 #define GRID_CLIENT_CONNECTION_HPP_INCLUDED 00012 00013 #include <boost/asio.hpp> 00014 #include <boost/bind.hpp> 00015 #include <boost/shared_ptr.hpp> 00016 #include <boost/asio/ssl.hpp> 00017 #include <boost/thread.hpp> 00018 #include <boost/lexical_cast.hpp> 00019 00020 #include "gridgain/gridclienttypedef.hpp" 00021 00023 class GridClientConnectionPool; 00024 00028 class GridClientConnection: private boost::noncopyable { 00029 public: 00035 GridClientConnection(); 00036 00043 GridClientConnection(boost::shared_ptr<boost::asio::ssl::context> ctx); 00044 00046 virtual ~GridClientConnection(); 00047 00053 boost::asio::io_service& getIoService(); 00054 00060 boost::asio::ip::tcp::socket& getSocket(); 00061 00067 const boost::asio::ip::tcp::socket& getSocket() const; 00068 00070 virtual void close(); 00071 00077 virtual bool isOpen() const { 00078 if (closed) return false; 00079 00080 boost::lock_guard<boost::mutex> guard(mux_); 00081 return getSocket().is_open(); 00082 } 00083 00091 virtual bool isConnected(const std::string& pHost, int pPort) { 00092 return isOpen() && (host == pHost) && (port == pPort); 00093 } 00094 00100 void setConnectionTimeout(unsigned int pConnTimeout) { 00101 connTimeout = pConnTimeout; 00102 } 00103 00109 void setReadTimeout(unsigned int pReadTimeout) { 00110 readTimeout = pReadTimeout; 00111 } 00112 00118 void setWriteTimeout(unsigned int pWriteTimeout) { 00119 writeTimeout = pWriteTimeout; 00120 } 00121 00127 void setSslContext(boost::shared_ptr<boost::asio::ssl::context> ctx) { 00128 assert(ctx.get() != NULL); 00129 00130 sslCtx = ctx; 00131 } 00132 00136 virtual void sendPing() = 0; 00137 00141 virtual std::string toString() const { 00142 return host + ":" + boost::lexical_cast<std::string>(port); 00143 } 00144 00145 protected: 00151 virtual bool verifyCertificate(bool preverified, boost::asio::ssl::verify_context& ctx); 00152 00158 bool isSslMode() const { 00159 return sslSock.get() == NULL; 00160 } 00161 00165 void resetSocket(); 00166 00168 std::string host; 00169 00171 int port; 00172 00174 boost::asio::io_service ioSrvc; 00175 00177 boost::shared_ptr<boost::asio::ip::tcp::socket> sock; 00178 00180 boost::shared_ptr<boost::asio::ssl::stream<boost::asio::ip::tcp::socket> > sslSock; 00181 00183 boost::shared_ptr<boost::asio::ssl::context> sslCtx; 00184 00186 std::string sessToken; 00187 00189 unsigned int connTimeout; 00190 00192 unsigned int readTimeout; 00193 00195 unsigned int writeTimeout; 00196 00198 mutable boost::mutex mux_; 00199 00201 TGridAtomicBool closed; 00202 }; 00203 #endif
1.7.1