• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2015 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #include "asio_client_session_tcp_impl.h"
26 
27 namespace nghttp2 {
28 namespace asio_http2 {
29 namespace client {
30 
session_tcp_impl(boost::asio::io_service & io_service,const std::string & host,const std::string & service,const boost::posix_time::time_duration & connect_timeout)31 session_tcp_impl::session_tcp_impl(
32     boost::asio::io_service &io_service, const std::string &host,
33     const std::string &service,
34     const boost::posix_time::time_duration &connect_timeout)
35     : session_impl(io_service, connect_timeout), socket_(io_service) {}
36 
session_tcp_impl(boost::asio::io_service & io_service,const boost::asio::ip::tcp::endpoint & local_endpoint,const std::string & host,const std::string & service,const boost::posix_time::time_duration & connect_timeout)37 session_tcp_impl::session_tcp_impl(
38     boost::asio::io_service &io_service,
39     const boost::asio::ip::tcp::endpoint &local_endpoint,
40     const std::string &host, const std::string &service,
41     const boost::posix_time::time_duration &connect_timeout)
42     : session_impl(io_service, connect_timeout), socket_(io_service) {
43   socket_.open(local_endpoint.protocol());
44   boost::asio::socket_base::reuse_address option(true);
45   socket_.set_option(option);
46   socket_.bind(local_endpoint);
47 }
48 
~session_tcp_impl()49 session_tcp_impl::~session_tcp_impl() {}
50 
start_connect(tcp::resolver::iterator endpoint_it)51 void session_tcp_impl::start_connect(tcp::resolver::iterator endpoint_it) {
52   auto self = shared_from_this();
53   socket_.async_connect(
54       *endpoint_it, [self, endpoint_it](const boost::system::error_code &ec) {
55         if (self->stopped()) {
56           return;
57         }
58 
59         if (ec) {
60           self->not_connected(ec);
61           return;
62         }
63 
64         self->connected(endpoint_it);
65       });
66 }
67 
socket()68 tcp::socket &session_tcp_impl::socket() { return socket_; }
69 
read_socket(std::function<void (const boost::system::error_code & ec,std::size_t n)> h)70 void session_tcp_impl::read_socket(
71     std::function<void(const boost::system::error_code &ec, std::size_t n)> h) {
72   socket_.async_read_some(boost::asio::buffer(rb_), h);
73 }
74 
write_socket(std::function<void (const boost::system::error_code & ec,std::size_t n)> h)75 void session_tcp_impl::write_socket(
76     std::function<void(const boost::system::error_code &ec, std::size_t n)> h) {
77   boost::asio::async_write(socket_, boost::asio::buffer(wb_, wblen_), h);
78 }
79 
shutdown_socket()80 void session_tcp_impl::shutdown_socket() {
81   boost::system::error_code ignored_ec;
82   socket_.close(ignored_ec);
83 }
84 
85 } // namespace client
86 } // namespace asio_http2
87 } // namespace nghttp2
88