Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to non-deprecated asio functionality #10

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 16 additions & 32 deletions include/urdl/detail/connect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#ifndef URDL_DETAIL_CONNECT_HPP
#define URDL_DETAIL_CONNECT_HPP

#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/detail/string_view.hpp>
#include <sstream>
#include "urdl/detail/coroutine.hpp"

Expand All @@ -28,25 +30,22 @@ inline boost::system::error_code connect(
// Create a query that corresponds to the url.
std::ostringstream port_string;
port_string << u.port();
boost::asio::ip::tcp::resolver::query query(u.host(), port_string.str());

// Get a list of endpoints corresponding to the query.
boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query, ec);
boost::asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(u.host(), port_string.str(), ec);
if (ec)
return ec;

// Try each endpoint until we successfully establish a connection.
ec = boost::asio::error::host_not_found;
while (ec && iter != boost::asio::ip::tcp::resolver::iterator())
{
socket.close(ec);
socket.connect(*iter++, ec);
}
boost::asio::connect(socket, endpoints, ec);
if (ec)
return ec;

// Disable the Nagle algorithm on all sockets.
return socket.set_option(boost::asio::ip::tcp::no_delay(true), ec);
ec = boost::system::error_code();
socket.set_option(boost::asio::ip::tcp::no_delay(true), ec);
return ec;
}

template <typename Handler>
Expand All @@ -63,14 +62,15 @@ class connect_coro : coroutine
}

void operator()(boost::system::error_code ec,
boost::asio::ip::tcp::resolver::iterator iter)
boost::asio::ip::tcp::resolver::results_type iter)
{
iter_ = iter;
results_ = iter;
(*this)(ec);
}

void operator()(boost::system::error_code ec,
const boost::asio::ip::tcp::resolver::query* query = 0)
boost::asio::string_view host = {},
boost::asio::string_view port = {})
{
URDL_CORO_BEGIN;

Expand All @@ -79,14 +79,14 @@ class connect_coro : coroutine
socket_.open(boost::asio::ip::tcp::v4(), ec);
if (ec)
{
URDL_CORO_YIELD(socket_.get_io_service().post(
URDL_CORO_YIELD(boost::asio::post(
boost::asio::detail::bind_handler(*this, ec)));
handler_(ec);
return;
}

// Get a list of endpoints corresponding to the host name.
URDL_CORO_YIELD(resolver_.async_resolve(*query, *this));
URDL_CORO_YIELD(resolver_.async_resolve(host, port, *this));
if (ec)
{
handler_(ec);
Expand All @@ -95,21 +95,7 @@ class connect_coro : coroutine

// Try each endpoint until we successfully establish a connection.
ec = boost::asio::error::host_not_found;
while (ec && iter_ != boost::asio::ip::tcp::resolver::iterator())
{
// Check whether the operation has been cancelled.
if (!socket_.is_open())
{
ec = boost::asio::error::operation_aborted;
handler_(ec);
return;
}

// Try next endpoint.
socket_.close(ec);
endpoint_ = *iter_++;
URDL_CORO_YIELD(socket_.async_connect(endpoint_, *this));
}
URDL_CORO_YIELD(boost::asio::connect(socket_, results_, ec));
if (ec)
{
handler_(ec);
Expand Down Expand Up @@ -166,8 +152,7 @@ class connect_coro : coroutine
Handler handler_;
boost::asio::ip::tcp::socket::lowest_layer_type& socket_;
boost::asio::ip::tcp::resolver& resolver_;
boost::asio::ip::tcp::resolver::iterator iter_;
boost::asio::ip::tcp::endpoint endpoint_;
boost::asio::ip::tcp::resolver::results_type results_;
};

template <typename Handler>
Expand All @@ -176,9 +161,8 @@ void async_connect(boost::asio::ip::tcp::socket::lowest_layer_type& socket,
{
std::ostringstream port_string;
port_string << u.port();
boost::asio::ip::tcp::resolver::query query(u.host(), port_string.str());
connect_coro<Handler>(handler, socket, resolver)(
boost::system::error_code(), &query);
boost::system::error_code(), u.host(), port_string.str());
}

} // namespace detail
Expand Down
34 changes: 14 additions & 20 deletions include/urdl/detail/file_read_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define URDL_DETAIL_FILE_READ_STREAM_HPP

#include <boost/config.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/detail/bind_handler.hpp>
#include <cctype>
Expand All @@ -28,9 +28,9 @@ namespace detail {
class file_read_stream
{
public:
explicit file_read_stream(boost::asio::io_service& io_service,
explicit file_read_stream(boost::asio::io_context& io_context,
option_set& options)
: io_service_(io_service),
: io_context_(io_context),
options_(options)
{
}
Expand Down Expand Up @@ -59,7 +59,7 @@ class file_read_stream
{
boost::system::error_code ec;
open(u, ec);
io_service_.post(boost::asio::detail::bind_handler(handler, ec));
boost::asio::post(boost::asio::detail::bind_handler(handler, ec));
}

boost::system::error_code close(boost::system::error_code& ec)
Expand All @@ -77,7 +77,7 @@ class file_read_stream
}

template <typename MutableBufferSequence>
std::size_t read_some(const MutableBufferSequence& buffers,
std::size_t read_some(const MutableBufferSequence& buffer,
boost::system::error_code& ec)
{
if (!file_)
Expand All @@ -86,20 +86,14 @@ class file_read_stream
return 0;
}

typename MutableBufferSequence::const_iterator iter = buffers.begin();
typename MutableBufferSequence::const_iterator end = buffers.end();
for (; iter != end; ++iter)
size_t length = boost::asio::buffer_size(buffer);
if (length > 0)
{
boost::asio::mutable_buffer buffer(*iter);
size_t length = boost::asio::buffer_size(buffer);
if (length > 0)
{
file_.read(boost::asio::buffer_cast<char*>(buffer), length);
length = file_.gcount();
if (length == 0 && !file_)
ec = boost::asio::error::eof;
return length;
}
file_.read(static_cast<char*>(buffer.data()), length);
length = file_.gcount();
if (length == 0 && !file_)
ec = boost::asio::error::eof;
return length;
}

ec = boost::system::error_code();
Expand All @@ -111,12 +105,12 @@ class file_read_stream
{
boost::system::error_code ec;
std::size_t bytes_transferred = read_some(buffers, ec);
io_service_.post(boost::asio::detail::bind_handler(
boost::asio::post(boost::asio::detail::bind_handler(
handler, ec, bytes_transferred));
}

private:
boost::asio::io_service& io_service_;
boost::asio::io_context& io_context_;
option_set& options_;
std::ifstream file_;
};
Expand Down
12 changes: 6 additions & 6 deletions include/urdl/detail/handshake.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void async_handshake(boost::asio::ip::tcp::socket& socket,
const std::string& /*host*/, Handler handler)
{
boost::system::error_code ec;
socket.get_io_service().post(boost::asio::detail::bind_handler(handler, ec));
boost::asio::post(socket.get_executor(), boost::asio::detail::bind_handler(handler, ec));
}

#if !defined(URDL_DISABLE_SSL)
Expand Down Expand Up @@ -81,7 +81,7 @@ inline bool certificate_matches_host(X509* cert, const std::string& host)
// to look for an IP address in the certificate rather than a host name.
boost::system::error_code ec;
boost::asio::ip::address address
= boost::asio::ip::address::from_string(host, ec);
= boost::asio::ip::make_address(host, ec);
bool is_address = !ec;

// Go through the alternate names in the certificate looking for matching DNS
Expand Down Expand Up @@ -167,9 +167,9 @@ inline boost::system::error_code handshake(
return ec;

// Verify the certificate returned by the host.
if (X509* cert = SSL_get_peer_certificate(socket.impl()->ssl))
if (X509* cert = SSL_get_peer_certificate(socket.native_handle()))
{
if (SSL_get_verify_result(socket.impl()->ssl) == X509_V_OK)
if (SSL_get_verify_result(socket.native_handle()) == X509_V_OK)
{
if (certificate_matches_host(cert, host))
ec = boost::system::error_code();
Expand Down Expand Up @@ -213,9 +213,9 @@ class handshake_coro : coroutine
}

// Verify the certificate returned by the host.
if (X509* cert = SSL_get_peer_certificate(socket_.impl()->ssl))
if (X509* cert = SSL_get_peer_certificate(socket_.native_handle()))
{
if (SSL_get_verify_result(socket_.impl()->ssl) == X509_V_OK)
if (SSL_get_verify_result(socket_.native_handle()) == X509_V_OK)
{
if (certificate_matches_host(cert, host_))
ec = boost::system::error_code();
Expand Down
40 changes: 18 additions & 22 deletions include/urdl/detail/http_read_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define URDL_DETAIL_HTTP_READ_STREAM_HPP

#include <boost/asio/buffer.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/streambuf.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/read_until.hpp>
Expand All @@ -39,20 +39,20 @@ template <typename Stream>
class http_read_stream
{
public:
explicit http_read_stream(boost::asio::io_service& io_service,
explicit http_read_stream(boost::asio::io_context& io_context,
option_set& options)
: resolver_(io_service),
socket_(io_service),
: resolver_(io_context),
socket_(io_context),
options_(options),
content_length_(0)
{
}

template <typename Arg>
http_read_stream(boost::asio::io_service& io_service,
http_read_stream(boost::asio::io_context& io_context,
option_set& options, Arg& arg)
: resolver_(io_service),
socket_(io_service, arg),
: resolver_(io_context),
socket_(io_context, arg),
options_(options),
content_length_(0)
{
Expand Down Expand Up @@ -203,7 +203,7 @@ class http_read_stream
if (socket_.lowest_layer().is_open())
{
ec = boost::asio::error::already_open;
URDL_CORO_YIELD(socket_.get_io_service().post(
URDL_CORO_YIELD(boost::asio::post(
boost::asio::detail::bind_handler(*this, ec)));
handler_(ec);
return;
Expand Down Expand Up @@ -394,7 +394,8 @@ class http_read_stream
boost::system::error_code close(boost::system::error_code& ec)
{
resolver_.cancel();
if (!socket_.lowest_layer().close(ec))
socket_.lowest_layer().close(ec);
if (!ec)
{
request_buffer_.consume(request_buffer_.size());
reply_buffer_.consume(reply_buffer_.size());
Expand Down Expand Up @@ -432,31 +433,26 @@ class http_read_stream
}

template <typename MutableBufferSequence>
std::size_t read_some(const MutableBufferSequence& buffers,
std::size_t read_some(const MutableBufferSequence& buffer,
boost::system::error_code& ec)
{
// If we have any data in the reply_buffer_, return that first.
if (reply_buffer_.size() > 0)
{
std::size_t bytes_transferred = 0;
typename MutableBufferSequence::const_iterator iter = buffers.begin();
typename MutableBufferSequence::const_iterator end = buffers.end();
for (; iter != end && reply_buffer_.size() > 0; ++iter)
size_t length = boost::asio::buffer_size(buffer);
if (length > 0)
{
boost::asio::mutable_buffer buffer(*iter);
size_t length = boost::asio::buffer_size(buffer);
if (length > 0)
{
bytes_transferred += reply_buffer_.sgetn(
boost::asio::buffer_cast<char*>(buffer), length);
}
bytes_transferred += reply_buffer_.sgetn(
static_cast<char*>(buffer.data()), length);
}

ec = boost::system::error_code();
return bytes_transferred;
}

// Otherwise we forward the call to the underlying socket.
std::size_t bytes_transferred = socket_.read_some(buffers, ec);
std::size_t bytes_transferred = socket_.read_some(buffer, ec);
if (ec == boost::asio::error::shut_down)
ec = boost::asio::error::eof;
return bytes_transferred;
Expand Down Expand Up @@ -520,7 +516,7 @@ class http_read_stream
{
boost::system::error_code ec;
std::size_t bytes_transferred = read_some(buffers, ec);
socket_.get_io_service().post(boost::asio::detail::bind_handler(
boost::asio::post(boost::asio::detail::bind_handler(
handler, ec, bytes_transferred));
return;
}
Expand Down
16 changes: 8 additions & 8 deletions include/urdl/impl/istreambuf.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define URDL_IMPL_ISTREAMBUF_IPP

#include <boost/array.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/deadline_timer.hpp>
#include <boost/system/system_error.hpp>
#include <boost/throw_exception.hpp>
Expand All @@ -28,15 +28,15 @@ struct istreambuf::body
enum { buffer_size = 512 };

body()
: read_stream_(io_service_),
timer_(io_service_),
: read_stream_(io_context_),
timer_(io_context_),
open_timeout_(300 * 1000),
read_timeout_(300 * 1000)
{
}

boost::array<char, buffer_size> get_buffer_;
boost::asio::io_service io_service_;
boost::asio::io_context io_context_;
boost::system::error_code error_;
read_stream read_stream_;
boost::asio::deadline_timer timer_;
Expand Down Expand Up @@ -125,8 +125,8 @@ istreambuf* istreambuf::open(const url& u)
boost::posix_time::milliseconds(body_->open_timeout_));
body_->timer_.async_wait(th);

body_->io_service_.reset();
body_->io_service_.run();
body_->io_context_.restart();
body_->io_context_.run();

if (!body_->read_stream_.is_open())
body_->error_ = make_error_code(boost::system::errc::timed_out);
Expand Down Expand Up @@ -205,8 +205,8 @@ std::streambuf::int_type istreambuf::underflow()
boost::posix_time::milliseconds(body_->read_timeout_));
body_->timer_.async_wait(th);

body_->io_service_.reset();
body_->io_service_.run();
body_->io_context_.restart();
body_->io_context_.run();

if (!body_->read_stream_.is_open())
body_->error_ = make_error_code(boost::system::errc::timed_out);
Expand Down
Loading