OSDN Git Service

ソースツリー再構成中(ほぼOK?)
[ultramonkey-l7/ultramonkey-l7-v3.git] / l7vsd / include / tcp_ssl_socket.h
1 /*!
2  *      @file   tcp_ssl_socket.h
3  *      @brief  tcp ssl session socket class
4  *
5  * L7VSD: Linux Virtual Server for Layer7 Load Balancing
6  * Copyright (C) 2009  NTT COMWARE Corporation.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  **********************************************************************/
24
25 #ifndef TCP_SSL_SOCKET_H
26 #define TCP_SSL_SOCKET_H
27
28 #include <bitset>
29 #include <boost/asio.hpp>
30 #include <boost/asio/ssl.hpp>
31 #include <boost/thread/mutex.hpp>
32
33 #include "tcp_socket_option.h"
34 #include "wrlock.h"
35 #include "utility.h"
36 #include "logger.h"
37
38 typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket>  ssl_socket;
39
40 namespace l7vs{
41
42 //!     @class  tcp_ssl_socket
43 //! @brief      this class is tcp session object use socket.
44         class tcp_ssl_socket : private boost::noncopyable{
45                 public:
46                         //! construcor
47                         //! @param[in/out]      socket use io service object
48                         //! @param[in]          set socket option info 
49                         tcp_ssl_socket(boost::asio::io_service& io,
50                                        boost::asio::ssl::context& context)
51                                        :
52                                        my_socket(io, context),
53                                        open_flag(false)
54                         {
55                                 opt_info.nodelay_opt = false;
56                                 opt_info.cork_opt = false;
57                                 opt_info.quickack_opt = false;
58                                 if( unlikely( LOG_LV_DEBUG == Logger::getLogLevel( LOG_CAT_L7VSD_SESSION ) ) ){
59                                         Logger::putLogDebug( LOG_CAT_L7VSD_SESSION, 1, "tcp_ssl_socket::tcp_ssl_socket", __FILE__, __LINE__ );
60                                 }
61                         }
62                         //! construcor
63                         //! @param[in/out]      socket use io service object
64                         //! @param[in]          set socket option info 
65                         tcp_ssl_socket(boost::asio::io_service& io,
66                                        boost::asio::ssl::context& context,
67                                        const tcp_socket_option_info set_option)
68                                        :
69                                        my_socket(io, context),
70                                        open_flag(false),
71                                        opt_info(set_option)
72                         {
73                                 if( unlikely( LOG_LV_DEBUG == Logger::getLogLevel( LOG_CAT_L7VSD_SESSION ) ) ){
74                                         Logger::putLogDebug( LOG_CAT_L7VSD_SESSION, 1, "tcp_ssl_socket::tcp_ssl_socket", __FILE__, __LINE__ );
75                                 }
76                         }
77                         //! destructor
78                         ~tcp_ssl_socket(){
79                                 if( unlikely( LOG_LV_DEBUG == Logger::getLogLevel( LOG_CAT_L7VSD_SESSION ) ) ){
80                                         Logger::putLogDebug( LOG_CAT_L7VSD_SESSION, 2, "tcp_ssl_socket::~tcp_ssl_socket", __FILE__, __LINE__ );
81                                 }
82                         }
83                         
84                         //! get reference control socket
85                         //! @return                     reference control socket
86                         ssl_socket& get_socket(){
87                                 if( unlikely( LOG_LV_DEBUG == Logger::getLogLevel( LOG_CAT_L7VSD_SESSION ) ) ){
88                                         Logger::putLogDebug( LOG_CAT_L7VSD_SESSION, 3, "tcp_ssl_socket::get_socket", __FILE__, __LINE__ );
89                                 }
90                                 return my_socket;
91                         }
92
93                         //! connect socket
94                         //! @param[in]          connect_endpoint is connection endpoint
95                         //! @param[out]         ec is reference error code object
96                         bool connect(const boost::asio::ip::tcp::endpoint connect_endpoint,boost::system::error_code& ec);
97                         //! handshake socket
98                         //! @param[in]          handshake_type is handshaking as a server or client
99                         bool handshake(boost::asio::ssl::stream_base::handshake_type type);
100                         //! accept
101                         void accept();
102                         //! close socket
103                         //! @param[out]         ec is reference error code object
104                         //! @return             true is socket close
105                         //! @return             false is not open socket
106                         bool close(boost::system::error_code& ec);
107                         //! set non blocking mode of the socket 
108                         //! @return                     ec is reference error code object
109                         bool set_non_blocking_mode(boost::system::error_code& ec);
110                         //! write socket
111                         //! @param[in]          buffers is wite data buffer
112                         //! @param[out]         ec is reference error code object
113                         //! @return                     write data size
114                         std::size_t write_some(boost::asio::mutable_buffers_1 buffers, boost::system::error_code& ec);
115                         //! read socket
116                         //! @param[out]         buffers is read data buffer
117                         //! @param[out]         ec is reference error code object
118                         //! @return                     read data size
119                         std::size_t read_some(boost::asio::mutable_buffers_1 buffers, boost::system::error_code& ec);
120                         //! is open
121                         //! @return             true is open
122                         //! @return             false is close
123                         bool is_open(){
124                                 return open_flag;
125                         }
126
127                 protected:
128                         //! control socket
129                         ssl_socket my_socket;
130                         //! socket close mutex
131                         wr_mutex close_mutex;
132                         //! socket open flag
133                         bool open_flag;
134                         //! socket option 
135                         tcp_socket_option_info opt_info;
136         };// class tcp_ssl_socket
137 }// namespace l7vs
138
139 #endif//TCP_SSL_SOCKET_H
140