UltraMonkey-L7 V3(multi-thread implementation)
Revisão | 19ec24e8bcb55273a09c8f36c10807ec6670e278 (tree) |
---|---|
Hora | 2011-12-27 10:40:07 |
Autor | Michiro HIBARI <hibari@user...> |
Commiter | Michiro HIBARI |
Delete needless files.
@@ -1,40 +0,0 @@ | ||
1 | -/*! | |
2 | - * @file tcp_data.h | |
3 | - * @brief tcp session processing data 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_DATA_H | |
26 | -#define TCP_DATA_H | |
27 | - | |
28 | -#include "data_buff_base.h" | |
29 | - | |
30 | -namespace l7vs | |
31 | -{ | |
32 | - | |
33 | -//! @class tcp_data | |
34 | -//! @brief this class is tcp session object use data for processing. | |
35 | -typedef data_buff_base<boost::asio::ip::tcp> tcp_data; | |
36 | - | |
37 | -}// namespace l7vs | |
38 | - | |
39 | -#endif//TCP_DATA_H | |
40 | - |
@@ -1,74 +0,0 @@ | ||
1 | -/*! | |
2 | - * @file tcp_realserver_connect_socket_list.h | |
3 | - * @brief tcp session connect realserver socket list 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_REALSERVER_CONNECT_SOCKET_LIST_H | |
26 | -#define TCP_REALSERVER_CONNECT_SOCKET_LIST_H | |
27 | - | |
28 | -#include <cstdlib> | |
29 | -#include <boost/asio.hpp> | |
30 | -#include <boost/thread/mutex.hpp> | |
31 | - | |
32 | -#include "tcp_socket.h" | |
33 | - | |
34 | -#include "wrlock.h" | |
35 | - | |
36 | -namespace l7vs | |
37 | -{ | |
38 | - | |
39 | -//! @class tcp_realserver_connect_socket_list | |
40 | -//! @brief this class is connected realserver list by up thread. | |
41 | -class tcp_realserver_connect_socket_list : private boost::noncopyable | |
42 | -{ | |
43 | -public: | |
44 | - //! socket pointer type | |
45 | - typedef boost::shared_ptr<tcp_socket> tcp_socket_ptr; | |
46 | - //! realserver list element type first : connection endpoint second : socket pointer | |
47 | - typedef std::pair<boost::asio::ip::tcp::endpoint, tcp_socket_ptr > list_element; | |
48 | - | |
49 | - //! constructor | |
50 | - tcp_realserver_connect_socket_list(); | |
51 | - //! destructor | |
52 | - ~tcp_realserver_connect_socket_list(); | |
53 | - //! push back list | |
54 | - //! @param[in/out] realserver_socket is realserver list push back element | |
55 | - void push_back(list_element realserver_socket); | |
56 | - //! get socket list | |
57 | - //! @return realserver list element | |
58 | - list_element get_socket(); | |
59 | - //! check empty list | |
60 | - bool empty(); | |
61 | - //! clear list | |
62 | - void clear(); | |
63 | - | |
64 | -protected: | |
65 | - //! realserver list | |
66 | - std::list< list_element > connect_list; | |
67 | - //! list access mutex | |
68 | - wr_mutex list_mutex; | |
69 | -};// class tcp_realserver_connect_socket_list | |
70 | -}// namespace l7vs | |
71 | - | |
72 | -#endif//TCP_REALSERVER_CONNECT_SOCKET_LIST_H | |
73 | - | |
74 | - |
@@ -1,77 +0,0 @@ | ||
1 | -/*! | |
2 | - * @file tcp_realserver_connect_socket_list.cpp | |
3 | - * @brief tcp session connect realserver socket list 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 | -#include "tcp_realserver_connect_socket_list.h" | |
26 | -#include "logger.h" | |
27 | - | |
28 | -namespace l7vs | |
29 | -{ | |
30 | - | |
31 | -//! constructor | |
32 | -tcp_realserver_connect_socket_list::tcp_realserver_connect_socket_list() | |
33 | -{ | |
34 | -} | |
35 | -//! destructor | |
36 | -tcp_realserver_connect_socket_list::~tcp_realserver_connect_socket_list() | |
37 | -{ | |
38 | -} | |
39 | - | |
40 | -//! push back list | |
41 | -//! @param[in/out] realserver_socket is realserver list push back element | |
42 | -void tcp_realserver_connect_socket_list::push_back(list_element realserver_socket) | |
43 | -{ | |
44 | - rw_scoped_lock scope_lock(list_mutex); | |
45 | - | |
46 | - connect_list.push_back(realserver_socket); | |
47 | -} | |
48 | - | |
49 | -//! get socket list | |
50 | -//! @return realserver list element | |
51 | -tcp_realserver_connect_socket_list::list_element tcp_realserver_connect_socket_list::get_socket() | |
52 | -{ | |
53 | - rw_scoped_lock scope_lock(list_mutex); | |
54 | - | |
55 | - list_element sock; | |
56 | - if (!connect_list.empty()) { | |
57 | - sock = *(connect_list.begin()); | |
58 | - connect_list.pop_front(); | |
59 | - } | |
60 | - return sock; | |
61 | -} | |
62 | - | |
63 | -//! check empty list | |
64 | -bool tcp_realserver_connect_socket_list::empty() | |
65 | -{ | |
66 | - return connect_list.empty(); | |
67 | -} | |
68 | - | |
69 | -//! clear list | |
70 | -void tcp_realserver_connect_socket_list::clear() | |
71 | -{ | |
72 | - rw_scoped_lock scope_lock(list_mutex); | |
73 | - connect_list.clear(); | |
74 | -} | |
75 | - | |
76 | -}// namespace l7vs | |
77 | - |
@@ -1,300 +0,0 @@ | ||
1 | -/*! | |
2 | - * @file tcp_socket.cpp | |
3 | - * @brief tcp 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 | -#include <boost/thread/thread.hpp> | |
26 | - | |
27 | -#include "tcp_socket.h" | |
28 | - | |
29 | -namespace l7vs | |
30 | -{ | |
31 | - | |
32 | -//! connect socket | |
33 | -//! @param[in] connect_endpoint is connection endpoint | |
34 | -//! @param[out] ec is reference error code object | |
35 | -//! @return true is connect | |
36 | -//! @return false is connect failure | |
37 | -bool tcp_socket::connect(boost::asio::ip::tcp::endpoint connect_endpoint, | |
38 | - boost::system::error_code &ec) | |
39 | -{ | |
40 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
41 | - LOG_CAT_L7VSD_SESSION))) { | |
42 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 4, | |
43 | - "in_function : tcp_socket::connect", __FILE__, __LINE__); | |
44 | - } | |
45 | - | |
46 | - rw_scoped_lock scope_lock(close_mutex); | |
47 | - | |
48 | - if (likely(!open_flag)) { | |
49 | - my_socket.connect(connect_endpoint, ec); | |
50 | - if (unlikely(!ec)) { | |
51 | - open_flag = true; | |
52 | - //----Debug log------------------------------------------------ | |
53 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
54 | - LOG_CAT_L7VSD_SESSION))) { | |
55 | - std::stringstream buf; | |
56 | - buf << "Thread ID["; | |
57 | - buf << boost::this_thread::get_id(); | |
58 | - buf << "] tcp_socket::connect ["; | |
59 | - buf << connect_endpoint; | |
60 | - buf << "]"; | |
61 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 5, buf.str(), | |
62 | - __FILE__, __LINE__); | |
63 | - } | |
64 | - //----Debug log------------------------------------------------ | |
65 | - | |
66 | - //set TCP_NODELAY | |
67 | - if (opt_info.nodelay_opt) { | |
68 | - boost::asio::ip::tcp::no_delay set_option( | |
69 | - opt_info.nodelay_val); | |
70 | - my_socket.set_option(set_option, ec); | |
71 | - if (unlikely(ec)) { | |
72 | - //ERROR | |
73 | - Logger::putLogError(LOG_CAT_L7VSD_SESSION, 100, | |
74 | - "socket option(TCP_NODELAY) set failed" , | |
75 | - __FILE__, __LINE__); | |
76 | - } | |
77 | - } | |
78 | - | |
79 | - //set TCP_CORK | |
80 | - if (opt_info.cork_opt) { | |
81 | - int val = opt_info.cork_val; | |
82 | - size_t len = sizeof(val); | |
83 | - int err = ::setsockopt(my_socket.native(), IPPROTO_TCP, TCP_CORK, &val, len); | |
84 | - if (unlikely(err)) { | |
85 | - //ERROR | |
86 | - Logger::putLogError(LOG_CAT_L7VSD_SESSION, 101, | |
87 | - "socket option(TCP_CORK) set failed" , | |
88 | - __FILE__, __LINE__); | |
89 | - } | |
90 | - } | |
91 | - } else { | |
92 | - open_flag = false; | |
93 | - } | |
94 | - } | |
95 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
96 | - LOG_CAT_L7VSD_SESSION))) { | |
97 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 50, | |
98 | - "out_function : tcp_socket::connect", __FILE__, __LINE__); | |
99 | - } | |
100 | - return open_flag; | |
101 | -} | |
102 | - | |
103 | -//! accept socket | |
104 | -void tcp_socket::accept() | |
105 | -{ | |
106 | - rw_scoped_lock scope_lock(close_mutex); | |
107 | - | |
108 | - open_flag = true; | |
109 | - //----Debug log-------------------------------------------------------- | |
110 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
111 | - LOG_CAT_L7VSD_SESSION))) { | |
112 | - boost::system::error_code ec; | |
113 | - std::stringstream buf; | |
114 | - buf << "Thread ID["; | |
115 | - buf << boost::this_thread::get_id(); | |
116 | - buf << "] tcp_socket::accept ["; | |
117 | - buf << my_socket.remote_endpoint(ec); | |
118 | - buf << "]"; | |
119 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 6, buf.str(), | |
120 | - __FILE__, __LINE__); | |
121 | - } | |
122 | - //----Debug log-------------------------------------------------------- | |
123 | - | |
124 | - //set TCP_NODELAY | |
125 | - if (opt_info.nodelay_opt) { | |
126 | - boost::system::error_code ec; | |
127 | - boost::asio::ip::tcp::no_delay set_option(opt_info.nodelay_val); | |
128 | - my_socket.set_option(set_option, ec); | |
129 | - if (unlikely(ec)) { | |
130 | - //ERROR | |
131 | - Logger::putLogError(LOG_CAT_L7VSD_SESSION, 102, | |
132 | - "socket option(TCP_NODELAY) set failed" , | |
133 | - __FILE__, __LINE__); | |
134 | - } | |
135 | - } | |
136 | - | |
137 | - //set TCP_CORK | |
138 | - if (opt_info.cork_opt) { | |
139 | - boost::system::error_code ec; | |
140 | - int val = opt_info.cork_val; | |
141 | - size_t len = sizeof(val); | |
142 | - int err = ::setsockopt(my_socket.native(), IPPROTO_TCP, TCP_CORK, &val, len); | |
143 | - if (unlikely(err)) { | |
144 | - //ERROR | |
145 | - Logger::putLogError(LOG_CAT_L7VSD_SESSION, 103, | |
146 | - "socket option(TCP_CORK) set failed" , | |
147 | - __FILE__, __LINE__); | |
148 | - } | |
149 | - } | |
150 | -} | |
151 | - | |
152 | -//! close socket | |
153 | -//! @param[out] ec is reference error code object | |
154 | -//! @return true is socket close | |
155 | -//! @return false is not open socket | |
156 | -bool tcp_socket::close(boost::system::error_code &ec) | |
157 | -{ | |
158 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
159 | - LOG_CAT_L7VSD_SESSION))) { | |
160 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 7, | |
161 | - "in_function : tcp_socket::close", __FILE__, __LINE__); | |
162 | - } | |
163 | - | |
164 | - rw_scoped_lock scope_lock(close_mutex); | |
165 | - | |
166 | - //----Debug log-------------------------------------------------------- | |
167 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
168 | - LOG_CAT_L7VSD_SESSION))) { | |
169 | - if (open_flag) { | |
170 | - boost::system::error_code ec; | |
171 | - std::stringstream buf; | |
172 | - buf << "Thread ID["; | |
173 | - buf << boost::this_thread::get_id(); | |
174 | - buf << "] tcp_socket::close ["; | |
175 | - buf << my_socket.remote_endpoint(ec); | |
176 | - buf << "]"; | |
177 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 8, buf.str(), | |
178 | - __FILE__, __LINE__); | |
179 | - } | |
180 | - } | |
181 | - //----Debug log-------------------------------------------------------- | |
182 | - bool bres = false; | |
183 | - if (likely(open_flag)) { | |
184 | - open_flag = false; | |
185 | - bres = true; | |
186 | - } | |
187 | - my_socket.close(ec); | |
188 | - non_blocking_flag = false; | |
189 | - | |
190 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
191 | - LOG_CAT_L7VSD_SESSION))) { | |
192 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 55, | |
193 | - "out_function : tcp_socket::close", __FILE__, __LINE__); | |
194 | - } | |
195 | - return bres; | |
196 | -} | |
197 | - | |
198 | -//! set non blocking mode of the socket | |
199 | -//! @param[out] ec is reference error code object | |
200 | -//! @return true is set non blocking mode | |
201 | -//! @return false is set non blocking mode failure | |
202 | -bool tcp_socket::set_non_blocking_mode(boost::system::error_code &ec) | |
203 | -{ | |
204 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
205 | - LOG_CAT_L7VSD_SESSION))) { | |
206 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 9, | |
207 | - "in_function : tcp_socket::set_non_blocking_mode", | |
208 | - __FILE__, __LINE__); | |
209 | - } | |
210 | - | |
211 | - rd_scoped_lock scope_lock(close_mutex); | |
212 | - bool bres = false; | |
213 | - boost::asio::socket_base::non_blocking_io cmd(true); | |
214 | - my_socket.io_control(cmd, ec); | |
215 | - if (likely(!ec)) { | |
216 | - // OK | |
217 | - bres = true; | |
218 | - non_blocking_flag = true; | |
219 | - } | |
220 | - | |
221 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
222 | - LOG_CAT_L7VSD_SESSION))) { | |
223 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 59, | |
224 | - "out_function : tcp_socket::set_non_blocking_mode", | |
225 | - __FILE__, __LINE__); | |
226 | - } | |
227 | - return bres; | |
228 | -} | |
229 | - | |
230 | -//! write socket | |
231 | -//! @param[in] buffers is write data buffer | |
232 | -//! @param[out] ec is reference error code object | |
233 | -//! @return write data size | |
234 | -std::size_t tcp_socket::write_some( | |
235 | - boost::asio::mutable_buffers_1 buffers, | |
236 | - boost::system::error_code &ec) | |
237 | -{ | |
238 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
239 | - LOG_CAT_L7VSD_SESSION))) { | |
240 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 10, | |
241 | - "in_function : tcp_socket::write_some", | |
242 | - __FILE__, __LINE__); | |
243 | - } | |
244 | - | |
245 | - rd_scoped_lock scope_lock(close_mutex); | |
246 | - std::size_t res_size = 0; | |
247 | - if (likely(non_blocking_flag)) { | |
248 | - res_size = my_socket.write_some(buffers, ec); | |
249 | - if (unlikely(ec)) { | |
250 | - if (likely(!open_flag)) { | |
251 | - res_size = 0; | |
252 | - ec.clear(); | |
253 | - } | |
254 | - } | |
255 | - } | |
256 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
257 | - LOG_CAT_L7VSD_SESSION))) { | |
258 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 62, | |
259 | - "out_function : tcp_socket::write_some", | |
260 | - __FILE__, __LINE__); | |
261 | - } | |
262 | - return res_size; | |
263 | -} | |
264 | - | |
265 | -//! read socket | |
266 | -//! @param[out] buffers is read data buffer | |
267 | -//! @param[out] ec is reference error code object | |
268 | -//! @return read data size | |
269 | -std::size_t tcp_socket::read_some( | |
270 | - boost::asio::mutable_buffers_1 buffers, | |
271 | - boost::system::error_code &ec) | |
272 | -{ | |
273 | - | |
274 | - rd_scoped_lock scope_lock(close_mutex); | |
275 | - std::size_t res_size = 0; | |
276 | - if (unlikely(open_flag && non_blocking_flag)) { | |
277 | - //set TCP_QUICKACK | |
278 | - if (opt_info.quickack_opt) { | |
279 | - int val = opt_info.quickack_val; | |
280 | - size_t len = sizeof(val); | |
281 | - int err = ::setsockopt(my_socket.native(), IPPROTO_TCP, TCP_QUICKACK, &val, len); | |
282 | - if (unlikely(err)) { | |
283 | - //ERROR | |
284 | - std::stringstream buf; | |
285 | - buf << "Thread ID["; | |
286 | - buf << boost::this_thread::get_id(); | |
287 | - buf << "] socket option(TCP_QUICKACK) set failed : "; | |
288 | - buf << ec.message(); | |
289 | - Logger::putLogError(LOG_CAT_L7VSD_SESSION, 104, | |
290 | - buf.str() , __FILE__, __LINE__); | |
291 | - } | |
292 | - } | |
293 | - boost::this_thread::yield(); | |
294 | - res_size = my_socket.read_some(buffers, ec); | |
295 | - } | |
296 | - return res_size; | |
297 | -} | |
298 | - | |
299 | -}// namespace l7vs | |
300 | - |
@@ -1,263 +0,0 @@ | ||
1 | -/*! | |
2 | - * @file tcp_ssl_socket.cpp | |
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 | -#include <boost/thread/thread.hpp> | |
26 | - | |
27 | -#include "tcp_ssl_socket.h" | |
28 | - | |
29 | -namespace l7vs | |
30 | -{ | |
31 | - | |
32 | -//! handshake socket | |
33 | -//! @param[in] handshake_type is handshaking as a server or client | |
34 | -//! @return true is handshake success | |
35 | -//! @return false is handshake failure | |
36 | -bool tcp_ssl_socket::handshake(boost::system::error_code &ec) | |
37 | -{ | |
38 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
39 | - LOG_CAT_L7VSD_SESSION))) { | |
40 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 74, | |
41 | - "in_function : tcp_ssl_socket::handshake", | |
42 | - __FILE__, __LINE__); | |
43 | - } | |
44 | - | |
45 | - rw_scoped_lock scope_lock(close_mutex); | |
46 | - | |
47 | - bool bres = false; | |
48 | - my_socket->handshake(boost::asio::ssl::stream_base::server, ec); | |
49 | - if (!ec) { | |
50 | - bres = true; | |
51 | - } else if (ec != boost::asio::error::try_again) { | |
52 | - handshake_error_flag = true; | |
53 | - } | |
54 | - | |
55 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
56 | - LOG_CAT_L7VSD_SESSION))) { | |
57 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 78, | |
58 | - "out_function : tcp_ssl_socket::handshake", | |
59 | - __FILE__, __LINE__); | |
60 | - } | |
61 | - return bres; | |
62 | -} | |
63 | - | |
64 | -//! accept socket | |
65 | -void tcp_ssl_socket::accept() | |
66 | -{ | |
67 | - rw_scoped_lock scope_lock(close_mutex); | |
68 | - | |
69 | - open_flag = true; | |
70 | - //----Debug log-------------------------------------------------------- | |
71 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
72 | - LOG_CAT_L7VSD_SESSION))) { | |
73 | - boost::system::error_code ec; | |
74 | - std::stringstream buf; | |
75 | - buf << "Thread ID["; | |
76 | - buf << boost::this_thread::get_id(); | |
77 | - buf << "] tcp_ssl_socket::accept ["; | |
78 | - buf << my_socket->lowest_layer().remote_endpoint(ec); | |
79 | - buf << "]"; | |
80 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 54, buf.str(), | |
81 | - __FILE__, __LINE__); | |
82 | - } | |
83 | - //----Debug log-------------------------------------------------------- | |
84 | - | |
85 | - //set TCP_NODELAY | |
86 | - if (opt_info.nodelay_opt) { | |
87 | - boost::system::error_code ec; | |
88 | - boost::asio::ip::tcp::no_delay set_option(opt_info.nodelay_val); | |
89 | - my_socket->lowest_layer().set_option(set_option, ec); | |
90 | - if (unlikely(ec)) { | |
91 | - //ERROR | |
92 | - Logger::putLogError(LOG_CAT_L7VSD_SESSION, 107, | |
93 | - "socket option(TCP_NODELAY) set failed" , | |
94 | - __FILE__, __LINE__); | |
95 | - } | |
96 | - } | |
97 | - | |
98 | - //set TCP_CORK | |
99 | - if (opt_info.cork_opt) { | |
100 | - boost::system::error_code ec; | |
101 | - int val = opt_info.cork_val; | |
102 | - size_t len = sizeof(val); | |
103 | - int err = ::setsockopt(my_socket->lowest_layer().native(), IPPROTO_TCP, | |
104 | - TCP_CORK, &val, len); | |
105 | - if (unlikely(err)) { | |
106 | - //ERROR | |
107 | - Logger::putLogError(LOG_CAT_L7VSD_SESSION, 108, | |
108 | - "socket option(TCP_CORK) set failed" , | |
109 | - __FILE__, __LINE__); | |
110 | - } | |
111 | - } | |
112 | -} | |
113 | - | |
114 | -//! close socket | |
115 | -//! @param[out] ec is reference error code object | |
116 | -//! @return true is socket close | |
117 | -//! @return false is not open socket | |
118 | -bool tcp_ssl_socket::close(boost::system::error_code &ec) | |
119 | -{ | |
120 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
121 | - LOG_CAT_L7VSD_SESSION))) { | |
122 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 56, | |
123 | - "in_function : tcp_ssl_socket::close", __FILE__, __LINE__); | |
124 | - } | |
125 | - | |
126 | - rw_scoped_lock scope_lock(close_mutex); | |
127 | - | |
128 | - //----Debug log-------------------------------------------------------- | |
129 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
130 | - LOG_CAT_L7VSD_SESSION))) { | |
131 | - if (open_flag) { | |
132 | - boost::system::error_code ec; | |
133 | - std::stringstream buf; | |
134 | - buf << "Thread ID["; | |
135 | - buf << boost::this_thread::get_id(); | |
136 | - buf << "] tcp_ssl_socket::close ["; | |
137 | - buf << my_socket->lowest_layer().remote_endpoint(ec); | |
138 | - buf << "]"; | |
139 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 58, buf.str(), | |
140 | - __FILE__, __LINE__); | |
141 | - } | |
142 | - } | |
143 | - //----Debug log-------------------------------------------------------- | |
144 | - bool bres = false; | |
145 | - if (likely(open_flag)) { | |
146 | - open_flag = false; | |
147 | - bres = true; | |
148 | - } | |
149 | - my_socket->shutdown(ec); | |
150 | - my_socket->lowest_layer().close(ec); | |
151 | - | |
152 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
153 | - LOG_CAT_L7VSD_SESSION))) { | |
154 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 57, | |
155 | - "out_function : tcp_ssl_socket::close", __FILE__, __LINE__); | |
156 | - } | |
157 | - return bres; | |
158 | -} | |
159 | - | |
160 | -//! set non blocking mode of the socket | |
161 | -//! @param[out] ec is reference error code object | |
162 | -//! @return true is set non blocking mode | |
163 | -//! @return false is set non blocking mode failure | |
164 | -bool tcp_ssl_socket::set_non_blocking_mode(boost::system::error_code &ec) | |
165 | -{ | |
166 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
167 | - LOG_CAT_L7VSD_SESSION))) { | |
168 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 60, | |
169 | - "in_function : tcp_ssl_socket::set_non_blocking_mode", | |
170 | - __FILE__, __LINE__); | |
171 | - } | |
172 | - | |
173 | - rd_scoped_lock scope_lock(close_mutex); | |
174 | - bool bres = false; | |
175 | - boost::asio::socket_base::non_blocking_io cmd(true); | |
176 | - my_socket->lowest_layer().io_control(cmd, ec); | |
177 | - if (likely(!ec)) { | |
178 | - // OK | |
179 | - bres = true; | |
180 | - non_blocking_flag = true; | |
181 | - } | |
182 | - | |
183 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
184 | - LOG_CAT_L7VSD_SESSION))) { | |
185 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 61, | |
186 | - "out_function : tcp_ssl_socket::set_non_blocking_mode", | |
187 | - __FILE__, __LINE__); | |
188 | - } | |
189 | - return bres; | |
190 | -} | |
191 | - | |
192 | -//! write socket | |
193 | -//! @param[in] buffers is write data buffer | |
194 | -//! @param[out] ec is reference error code object | |
195 | -//! @return write data size | |
196 | -std::size_t tcp_ssl_socket::write_some( | |
197 | - boost::asio::mutable_buffers_1 buffers, | |
198 | - boost::system::error_code &ec) | |
199 | -{ | |
200 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
201 | - LOG_CAT_L7VSD_SESSION))) { | |
202 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 63, | |
203 | - "in_function : tcp_ssl_socket::write_some", | |
204 | - __FILE__, __LINE__); | |
205 | - } | |
206 | - | |
207 | - rw_scoped_lock scope_lock(close_mutex); | |
208 | - std::size_t res_size = 0; | |
209 | - if (likely(non_blocking_flag)) { | |
210 | - res_size = my_socket->write_some(buffers, ec); | |
211 | - if (unlikely(ec)) { | |
212 | - if (likely(!open_flag)) { | |
213 | - res_size = 0; | |
214 | - ec.clear(); | |
215 | - } | |
216 | - } | |
217 | - } | |
218 | - if (unlikely(LOG_LV_DEBUG == Logger::getLogLevel( | |
219 | - LOG_CAT_L7VSD_SESSION))) { | |
220 | - Logger::putLogDebug(LOG_CAT_L7VSD_SESSION, 64, | |
221 | - "out_function : tcp_ssl_socket::write_some", | |
222 | - __FILE__, __LINE__); | |
223 | - } | |
224 | - return res_size; | |
225 | -} | |
226 | - | |
227 | -//! read socket | |
228 | -//! @param[out] buffers is read data buffer | |
229 | -//! @param[out] ec is reference error code object | |
230 | -//! @return read data size | |
231 | -std::size_t tcp_ssl_socket::read_some( | |
232 | - boost::asio::mutable_buffers_1 buffers, | |
233 | - boost::system::error_code &ec) | |
234 | -{ | |
235 | - | |
236 | - rw_scoped_lock scope_lock(close_mutex); | |
237 | - std::size_t res_size = 0; | |
238 | - if (unlikely(open_flag && non_blocking_flag)) { | |
239 | - //set TCP_QUICKACK | |
240 | - if (opt_info.quickack_opt) { | |
241 | - int val = opt_info.quickack_val; | |
242 | - std::size_t len = sizeof(val); | |
243 | - int err = ::setsockopt(my_socket->lowest_layer().native(), IPPROTO_TCP, | |
244 | - TCP_QUICKACK, &val, len); | |
245 | - if (unlikely(err)) { | |
246 | - //ERROR | |
247 | - std::stringstream buf; | |
248 | - buf << "Thread ID["; | |
249 | - buf << boost::this_thread::get_id(); | |
250 | - buf << "] socket option(TCP_QUICKACK) set failed : "; | |
251 | - buf << ec.message(); | |
252 | - Logger::putLogError(LOG_CAT_L7VSD_SESSION, 109, | |
253 | - buf.str() , __FILE__, __LINE__); | |
254 | - } | |
255 | - } | |
256 | - boost::this_thread::yield(); | |
257 | - res_size = my_socket->read_some(buffers, ec); | |
258 | - } | |
259 | - return res_size; | |
260 | -} | |
261 | - | |
262 | -}// namespace l7vs | |
263 | - |