fixes bug where priorities where lost when force-rechecking.
[libtorrent.git] / include / libtorrent / socket.hpp
blobd4cc9bdb01326595a925dc243c4e5565adffe3b8
1 /*
3 Copyright (c) 2003, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
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_SOCKET_HPP_INCLUDED
34 #define TORRENT_SOCKET_HPP_INCLUDED
36 #ifdef _MSC_VER
37 #pragma warning(push, 1)
38 #endif
40 // if building as Objective C++, asio's template
41 // parameters Protocol has to be renamed to avoid
42 // colliding with keywords
44 #ifdef __OBJC__
45 #define Protocol Protocol_
46 #endif
48 #include <boost/version.hpp>
50 #if BOOST_VERSION < 103500
51 #include <asio/ip/tcp.hpp>
52 #include <asio/ip/udp.hpp>
53 #include <asio/io_service.hpp>
54 #include <asio/deadline_timer.hpp>
55 #include <asio/write.hpp>
56 #include <asio/read.hpp>
57 #include <asio/time_traits.hpp>
58 #include <asio/basic_deadline_timer.hpp>
59 #else
60 #include <boost/asio/ip/tcp.hpp>
61 #include <boost/asio/ip/udp.hpp>
62 #include <boost/asio/io_service.hpp>
63 #include <boost/asio/deadline_timer.hpp>
64 #include <boost/asio/write.hpp>
65 #include <boost/asio/read.hpp>
66 #include <boost/asio/time_traits.hpp>
67 #include <boost/asio/basic_deadline_timer.hpp>
68 #endif
70 #ifdef __OBJC__
71 #undef Protocol
72 #endif
74 #include "libtorrent/io.hpp"
75 #include "libtorrent/time.hpp"
76 #include "libtorrent/error_code.hpp"
78 #ifdef _MSC_VER
79 #pragma warning(pop)
80 #endif
82 namespace libtorrent
85 #if BOOST_VERSION < 103500
86 using asio::ip::tcp;
87 using asio::ip::udp;
88 using asio::async_write;
89 using asio::async_read;
91 typedef asio::ip::tcp::socket stream_socket;
92 typedef asio::ip::address address;
93 typedef asio::ip::address_v4 address_v4;
94 typedef asio::ip::address_v6 address_v6;
95 typedef asio::ip::udp::socket datagram_socket;
96 typedef asio::ip::tcp::acceptor socket_acceptor;
97 typedef asio::io_service io_service;
99 namespace asio = ::asio;
100 typedef asio::basic_deadline_timer<libtorrent::ptime> deadline_timer;
101 #else
102 using boost::asio::ip::tcp;
103 using boost::asio::ip::udp;
104 using boost::asio::async_write;
105 using boost::asio::async_read;
107 typedef boost::asio::ip::tcp::socket stream_socket;
108 typedef boost::asio::ip::address address;
109 typedef boost::asio::ip::address_v4 address_v4;
110 typedef boost::asio::ip::address_v6 address_v6;
111 typedef boost::asio::ip::udp::socket datagram_socket;
112 typedef boost::asio::ip::tcp::acceptor socket_acceptor;
113 typedef boost::asio::io_service io_service;
115 namespace asio = boost::asio;
116 typedef boost::asio::basic_deadline_timer<libtorrent::ptime> deadline_timer;
117 #endif
119 inline std::ostream& print_address(std::ostream& os, address const& addr)
121 error_code ec;
122 std::string a = addr.to_string(ec);
123 if (ec) return os;
124 os << a;
125 return os;
128 inline std::ostream& print_endpoint(std::ostream& os, tcp::endpoint const& ep)
130 address const& addr = ep.address();
131 error_code ec;
132 std::string a = addr.to_string(ec);
133 if (ec) return os;
135 if (addr.is_v6())
136 os << "[" << a << "]:";
137 else
138 os << a << ":";
139 os << ep.port();
140 return os;
143 namespace detail
145 template<class OutIt>
146 void write_address(address const& a, OutIt& out)
148 if (a.is_v4())
150 write_uint32(a.to_v4().to_ulong(), out);
152 else if (a.is_v6())
154 address_v6::bytes_type bytes
155 = a.to_v6().to_bytes();
156 std::copy(bytes.begin(), bytes.end(), out);
160 template<class InIt>
161 address read_v4_address(InIt& in)
163 unsigned long ip = read_uint32(in);
164 return address_v4(ip);
167 template<class InIt>
168 address read_v6_address(InIt& in)
170 typedef address_v6::bytes_type bytes_t;
171 bytes_t bytes;
172 for (bytes_t::iterator i = bytes.begin()
173 , end(bytes.end()); i != end; ++i)
174 *i = read_uint8(in);
175 return address_v6(bytes);
178 template<class Endpoint, class OutIt>
179 void write_endpoint(Endpoint const& e, OutIt& out)
181 write_address(e.address(), out);
182 write_uint16(e.port(), out);
185 template<class Endpoint, class InIt>
186 Endpoint read_v4_endpoint(InIt& in)
188 address addr = read_v4_address(in);
189 int port = read_uint16(in);
190 return Endpoint(addr, port);
193 template<class Endpoint, class InIt>
194 Endpoint read_v6_endpoint(InIt& in)
196 address addr = read_v6_address(in);
197 int port = read_uint16(in);
198 return Endpoint(addr, port);
202 struct v6only
204 v6only(bool enable): m_value(enable) {}
205 template<class Protocol>
206 int level(Protocol const&) const { return IPPROTO_IPV6; }
207 template<class Protocol>
208 int name(Protocol const&) const { return IPV6_V6ONLY; }
209 template<class Protocol>
210 int const* data(Protocol const&) const { return &m_value; }
211 template<class Protocol>
212 size_t size(Protocol const&) const { return sizeof(m_value); }
213 int m_value;
216 struct type_of_service
218 type_of_service(char val): m_value(val) {}
219 template<class Protocol>
220 int level(Protocol const&) const { return IPPROTO_IP; }
221 template<class Protocol>
222 int name(Protocol const&) const { return IP_TOS; }
223 template<class Protocol>
224 char const* data(Protocol const&) const { return &m_value; }
225 template<class Protocol>
226 size_t size(Protocol const&) const { return sizeof(m_value); }
227 char m_value;
231 #endif // TORRENT_SOCKET_HPP_INCLUDED