3 Copyright (c) 2007, Arvid Norberg
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #ifndef TORRENT_PROXY_BASE_HPP_INCLUDED
34 #define TORRENT_PROXY_BASE_HPP_INCLUDED
36 #include "libtorrent/io.hpp"
37 #include "libtorrent/socket.hpp"
38 #include <boost/bind.hpp>
39 #include <boost/lexical_cast.hpp>
40 #include <boost/function.hpp>
41 #if BOOST_VERSION < 103500
42 #include <asio/read.hpp>
43 #include <asio/write.hpp>
45 #include <boost/asio/read.hpp>
46 #include <boost/asio/write.hpp>
49 namespace libtorrent
{
51 class proxy_base
: boost::noncopyable
55 typedef stream_socket::lowest_layer_type lowest_layer_type
;
56 typedef stream_socket::endpoint_type endpoint_type
;
57 typedef stream_socket::protocol_type protocol_type
;
59 explicit proxy_base(io_service
& io_service
)
61 , m_resolver(io_service
)
64 void set_proxy(std::string hostname
, int port
)
66 m_hostname
= hostname
;
70 template <class Mutable_Buffers
, class Handler
>
71 void async_read_some(Mutable_Buffers
const& buffers
, Handler
const& handler
)
73 m_sock
.async_read_some(buffers
, handler
);
76 template <class Mutable_Buffers
>
77 std::size_t read_some(Mutable_Buffers
const& buffers
, error_code
& ec
)
79 return m_sock
.read_some(buffers
, ec
);
82 #ifndef BOOST_NO_EXCEPTIONS
83 template <class Mutable_Buffers
>
84 std::size_t read_some(Mutable_Buffers
const& buffers
)
86 return m_sock
.read_some(buffers
);
89 template <class IO_Control_Command
>
90 void io_control(IO_Control_Command
& ioc
)
92 m_sock
.io_control(ioc
);
96 template <class IO_Control_Command
>
97 void io_control(IO_Control_Command
& ioc
, error_code
& ec
)
99 m_sock
.io_control(ioc
, ec
);
102 template <class Const_Buffers
, class Handler
>
103 void async_write_some(Const_Buffers
const& buffers
, Handler
const& handler
)
105 m_sock
.async_write_some(buffers
, handler
);
108 #ifndef BOOST_NO_EXCEPTIONS
109 template <class SettableSocketOption
>
110 void set_option(SettableSocketOption
const& opt
)
112 m_sock
.set_option(opt
);
116 template <class SettableSocketOption
>
117 error_code
set_option(SettableSocketOption
const& opt
, error_code
& ec
)
119 return m_sock
.set_option(opt
, ec
);
122 #ifndef BOOST_NO_EXCEPTIONS
123 void bind(endpoint_type
const& endpoint
)
125 m_sock
.bind(endpoint
);
129 void bind(endpoint_type
const& endpoint
, error_code
& ec
)
131 m_sock
.bind(endpoint
, ec
);
134 #ifndef BOOST_NO_EXCEPTIONS
135 void open(protocol_type
const& p
)
141 void open(protocol_type
const& p
, error_code
& ec
)
146 #ifndef BOOST_NO_EXCEPTIONS
149 m_remote_endpoint
= endpoint_type();
155 void close(error_code
& ec
)
161 #ifndef BOOST_NO_EXCEPTIONS
162 endpoint_type
remote_endpoint() const
164 return m_remote_endpoint
;
168 endpoint_type
remote_endpoint(error_code
& ec
) const
170 return m_remote_endpoint
;
173 #ifndef BOOST_NO_EXCEPTIONS
174 endpoint_type
local_endpoint() const
176 return m_sock
.local_endpoint();
180 endpoint_type
local_endpoint(error_code
& ec
) const
182 return m_sock
.local_endpoint(ec
);
185 io_service
& get_io_service()
187 return m_sock
.get_io_service();
190 lowest_layer_type
& lowest_layer()
192 return m_sock
.lowest_layer();
195 bool is_open() const { return m_sock
.is_open(); }
199 stream_socket m_sock
;
200 std::string m_hostname
;
203 endpoint_type m_remote_endpoint
;
205 tcp::resolver m_resolver
;