switch asio from standalone to boost
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
# webcc
|
||||
|
||||
# Don't use any deprecated definitions (e.g., io_service).
|
||||
add_definitions(-DASIO_NO_DEPRECATED)
|
||||
add_definitions(-DBOOST_ASIO_NO_DEPRECATED)
|
||||
|
||||
if(MSVC)
|
||||
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
|
||||
|
||||
+4
-4
@@ -58,7 +58,7 @@ void StringBody::InitPayload() {
|
||||
Payload StringBody::NextPayload(bool /*free_previous*/) {
|
||||
if (index_ == 0) {
|
||||
index_ = 1;
|
||||
return { asio::buffer(data_) };
|
||||
return { boost::asio::buffer(data_) };
|
||||
}
|
||||
return {};
|
||||
}
|
||||
@@ -129,7 +129,7 @@ Payload FormBody::NextPayload(bool free_previous) {
|
||||
}
|
||||
|
||||
void FormBody::AddBoundary(Payload* payload) {
|
||||
using asio::buffer;
|
||||
using boost::asio::buffer;
|
||||
|
||||
payload->push_back(buffer(literal_buffers::DOUBLE_DASHES));
|
||||
payload->push_back(buffer(boundary_));
|
||||
@@ -137,7 +137,7 @@ void FormBody::AddBoundary(Payload* payload) {
|
||||
}
|
||||
|
||||
void FormBody::AddBoundaryEnd(Payload* payload) {
|
||||
using asio::buffer;
|
||||
using boost::asio::buffer;
|
||||
|
||||
payload->push_back(buffer(literal_buffers::DOUBLE_DASHES));
|
||||
payload->push_back(buffer(boundary_));
|
||||
@@ -195,7 +195,7 @@ void FileBody::InitPayload() {
|
||||
Payload FileBody::NextPayload(bool /*free_previous*/) {
|
||||
if (ifstream_.read(&chunk_[0], chunk_.size()).gcount() > 0) {
|
||||
return {
|
||||
asio::buffer(chunk_.data(), (std::size_t)ifstream_.gcount())
|
||||
boost::asio::buffer(chunk_.data(), (std::size_t)ifstream_.gcount())
|
||||
};
|
||||
}
|
||||
return {};
|
||||
|
||||
+10
-10
@@ -2,7 +2,7 @@
|
||||
|
||||
#include "webcc/logger.h"
|
||||
|
||||
using asio::ip::tcp;
|
||||
using boost::asio::ip::tcp;
|
||||
|
||||
namespace webcc {
|
||||
|
||||
@@ -101,7 +101,7 @@ void Client::DoConnect(RequestPtr request, const std::string& default_port) {
|
||||
|
||||
LOG_VERB("Resolve host (%s)...", request->host().c_str());
|
||||
|
||||
std::error_code ec;
|
||||
boost::system::error_code ec;
|
||||
|
||||
// The protocol depends on the `host`, both V4 and V6 are supported.
|
||||
auto endpoints = resolver.resolve(request->host(), port, ec);
|
||||
@@ -136,7 +136,7 @@ void Client::WriteRequest(RequestPtr request) {
|
||||
|
||||
// Use sync API directly since we don't need timeout control.
|
||||
|
||||
std::error_code ec;
|
||||
boost::system::error_code ec;
|
||||
|
||||
if (socket_->Write(request->GetPayload(), &ec)) {
|
||||
// Write request body.
|
||||
@@ -170,18 +170,18 @@ void Client::ReadResponse() {
|
||||
}
|
||||
|
||||
void Client::DoReadResponse() {
|
||||
std::error_code ec = asio::error::would_block;
|
||||
boost::system::error_code ec = boost::asio::error::would_block;
|
||||
std::size_t length = 0;
|
||||
|
||||
// The read handler.
|
||||
auto handler = [&ec, &length](std::error_code inner_ec,
|
||||
auto handler = [&ec, &length](boost::system::error_code inner_ec,
|
||||
std::size_t inner_length) {
|
||||
ec = inner_ec;
|
||||
length = inner_length;
|
||||
};
|
||||
|
||||
while (true) {
|
||||
ec = asio::error::would_block;
|
||||
ec = boost::asio::error::would_block;
|
||||
length = 0;
|
||||
|
||||
socket_->AsyncReadSome(std::move(handler), &buffer_);
|
||||
@@ -192,7 +192,7 @@ void Client::DoReadResponse() {
|
||||
// Block until the asynchronous operation has completed.
|
||||
do {
|
||||
io_context_.run_one();
|
||||
} while (ec == asio::error::would_block);
|
||||
} while (ec == boost::asio::error::would_block);
|
||||
|
||||
// Stop the timer.
|
||||
CancelTimer();
|
||||
@@ -239,11 +239,11 @@ void Client::DoWaitTimer() {
|
||||
timer_.async_wait(std::bind(&Client::OnTimer, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
void Client::OnTimer(std::error_code ec) {
|
||||
void Client::OnTimer(boost::system::error_code ec) {
|
||||
LOG_VERB("On timer.");
|
||||
|
||||
// timer_.cancel() was called.
|
||||
if (ec == asio::error::operation_aborted) {
|
||||
if (ec == boost::asio::error::operation_aborted) {
|
||||
LOG_VERB("Timer canceled.");
|
||||
return;
|
||||
}
|
||||
@@ -253,7 +253,7 @@ void Client::OnTimer(std::error_code ec) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (timer_.expiry() <= asio::steady_timer::clock_type::now()) {
|
||||
if (timer_.expiry() <= boost::asio::steady_timer::clock_type::now()) {
|
||||
// The deadline has passed. The socket is closed so that any outstanding
|
||||
// asynchronous operations are canceled.
|
||||
LOG_WARN("HTTP client timed out.");
|
||||
|
||||
+6
-6
@@ -6,9 +6,9 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "asio/io_context.hpp"
|
||||
#include "asio/ip/tcp.hpp"
|
||||
#include "asio/steady_timer.hpp"
|
||||
#include "boost/asio/io_context.hpp"
|
||||
#include "boost/asio/ip/tcp.hpp"
|
||||
#include "boost/asio/steady_timer.hpp"
|
||||
|
||||
#include "webcc/globals.h"
|
||||
#include "webcc/request.h"
|
||||
@@ -82,13 +82,13 @@ private:
|
||||
void DoReadResponse();
|
||||
|
||||
void DoWaitTimer();
|
||||
void OnTimer(std::error_code ec);
|
||||
void OnTimer(boost::system::error_code ec);
|
||||
|
||||
// Cancel any async-operations waiting on the timer.
|
||||
void CancelTimer();
|
||||
|
||||
private:
|
||||
asio::io_context io_context_;
|
||||
boost::asio::io_context io_context_;
|
||||
|
||||
// Socket connection.
|
||||
std::unique_ptr<SocketBase> socket_;
|
||||
@@ -97,7 +97,7 @@ private:
|
||||
ResponseParser response_parser_;
|
||||
|
||||
// Timer for the timeout control.
|
||||
asio::steady_timer timer_;
|
||||
boost::asio::steady_timer timer_;
|
||||
|
||||
// The buffer for reading response.
|
||||
std::vector<char> buffer_;
|
||||
|
||||
+1
-1
@@ -214,7 +214,7 @@ FormPartPtr FormPart::NewFile(const std::string& name,
|
||||
}
|
||||
|
||||
void FormPart::Prepare(Payload* payload) {
|
||||
using asio::buffer;
|
||||
using boost::asio::buffer;
|
||||
|
||||
if (data_.empty() && !path_.empty()) {
|
||||
if (!utility::ReadFile(path_, &data_)) {
|
||||
|
||||
+15
-15
@@ -2,12 +2,12 @@
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "asio/write.hpp"
|
||||
#include "boost/asio/write.hpp"
|
||||
|
||||
#include "webcc/connection_pool.h"
|
||||
#include "webcc/logger.h"
|
||||
|
||||
using asio::ip::tcp;
|
||||
using boost::asio::ip::tcp;
|
||||
|
||||
namespace webcc {
|
||||
|
||||
@@ -20,7 +20,7 @@ Connection::Connection(tcp::socket socket, ConnectionPool* pool,
|
||||
void Connection::Start() {
|
||||
request_.reset(new Request{});
|
||||
|
||||
std::error_code ec;
|
||||
boost::system::error_code ec;
|
||||
auto endpoint = socket_.remote_endpoint(ec);
|
||||
if (!ec) {
|
||||
request_->set_ip(endpoint.address().to_string());
|
||||
@@ -36,7 +36,7 @@ void Connection::Close() {
|
||||
// Initiate graceful connection closure.
|
||||
// Socket close VS. shutdown:
|
||||
// https://stackoverflow.com/questions/4160347/close-vs-shutdown-socket
|
||||
std::error_code ec;
|
||||
boost::system::error_code ec;
|
||||
socket_.shutdown(tcp::socket::shutdown_both, ec);
|
||||
|
||||
if (ec) {
|
||||
@@ -82,17 +82,17 @@ void Connection::SendResponse(Status status, bool no_keep_alive) {
|
||||
}
|
||||
|
||||
void Connection::DoRead() {
|
||||
socket_.async_read_some(asio::buffer(buffer_),
|
||||
socket_.async_read_some(boost::asio::buffer(buffer_),
|
||||
std::bind(&Connection::OnRead, shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
}
|
||||
|
||||
void Connection::OnRead(std::error_code ec, std::size_t length) {
|
||||
void Connection::OnRead(boost::system::error_code ec, std::size_t length) {
|
||||
if (ec) {
|
||||
if (ec == asio::error::eof) {
|
||||
if (ec == boost::asio::error::eof) {
|
||||
LOG_INFO("Socket read EOF (%s).", ec.message().c_str());
|
||||
} else if (ec == asio::error::operation_aborted) {
|
||||
} else if (ec == boost::asio::error::operation_aborted) {
|
||||
// The socket of this connection has been closed.
|
||||
// This happens, e.g., when the server was stopped by a signal (Ctrl-C).
|
||||
LOG_WARN("Socket operation aborted (%s).", ec.message().c_str());
|
||||
@@ -102,7 +102,7 @@ void Connection::OnRead(std::error_code ec, std::size_t length) {
|
||||
|
||||
// Don't try to send any response back.
|
||||
|
||||
if (ec != asio::error::operation_aborted) {
|
||||
if (ec != boost::asio::error::operation_aborted) {
|
||||
pool_->Close(shared_from_this());
|
||||
} // else: The socket of this connection has already been closed.
|
||||
|
||||
@@ -135,13 +135,13 @@ void Connection::DoWrite() {
|
||||
LOG_VERB("HTTP response:\n%s", response_->Dump().c_str());
|
||||
|
||||
// Firstly, write the headers.
|
||||
asio::async_write(socket_, response_->GetPayload(),
|
||||
boost::asio::async_write(socket_, response_->GetPayload(),
|
||||
std::bind(&Connection::OnWriteHeaders,
|
||||
shared_from_this(), std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
}
|
||||
|
||||
void Connection::OnWriteHeaders(std::error_code ec,
|
||||
void Connection::OnWriteHeaders(boost::system::error_code ec,
|
||||
std::size_t length) {
|
||||
if (ec) {
|
||||
OnWriteError(ec);
|
||||
@@ -156,7 +156,7 @@ void Connection::DoWriteBody() {
|
||||
auto payload = response_->body()->NextPayload();
|
||||
|
||||
if (!payload.empty()) {
|
||||
asio::async_write(socket_, payload,
|
||||
boost::asio::async_write(socket_, payload,
|
||||
std::bind(&Connection::OnWriteBody,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
@@ -167,7 +167,7 @@ void Connection::DoWriteBody() {
|
||||
}
|
||||
}
|
||||
|
||||
void Connection::OnWriteBody(std::error_code ec, std::size_t length) {
|
||||
void Connection::OnWriteBody(boost::system::error_code ec, std::size_t length) {
|
||||
if (ec) {
|
||||
OnWriteError(ec);
|
||||
} else {
|
||||
@@ -187,10 +187,10 @@ void Connection::OnWriteOK() {
|
||||
}
|
||||
}
|
||||
|
||||
void Connection::OnWriteError(std::error_code ec) {
|
||||
void Connection::OnWriteError(boost::system::error_code ec) {
|
||||
LOG_ERRO("Socket write error (%s).", ec.message().c_str());
|
||||
|
||||
if (ec != asio::error::operation_aborted) {
|
||||
if (ec != boost::asio::error::operation_aborted) {
|
||||
pool_->Close(shared_from_this());
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -5,7 +5,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "asio/ip/tcp.hpp"
|
||||
#include "boost/asio/ip/tcp.hpp"
|
||||
|
||||
#include "webcc/globals.h"
|
||||
#include "webcc/queue.h"
|
||||
@@ -23,7 +23,7 @@ using ConnectionPtr = std::shared_ptr<Connection>;
|
||||
|
||||
class Connection : public std::enable_shared_from_this<Connection> {
|
||||
public:
|
||||
Connection(asio::ip::tcp::socket socket, ConnectionPool* pool,
|
||||
Connection(boost::asio::ip::tcp::socket socket, ConnectionPool* pool,
|
||||
Queue<ConnectionPtr>* queue, ViewMatcher&& view_matcher);
|
||||
|
||||
~Connection() = default;
|
||||
@@ -53,17 +53,17 @@ public:
|
||||
|
||||
private:
|
||||
void DoRead();
|
||||
void OnRead(std::error_code ec, std::size_t length);
|
||||
void OnRead(boost::system::error_code ec, std::size_t length);
|
||||
|
||||
void DoWrite();
|
||||
void OnWriteHeaders(std::error_code ec, std::size_t length);
|
||||
void OnWriteHeaders(boost::system::error_code ec, std::size_t length);
|
||||
void DoWriteBody();
|
||||
void OnWriteBody(std::error_code ec, std::size_t length);
|
||||
void OnWriteBody(boost::system::error_code ec, std::size_t length);
|
||||
void OnWriteOK();
|
||||
void OnWriteError(std::error_code ec);
|
||||
void OnWriteError(boost::system::error_code ec);
|
||||
|
||||
// The socket for the connection.
|
||||
asio::ip::tcp::socket socket_;
|
||||
boost::asio::ip::tcp::socket socket_;
|
||||
|
||||
// The connection pool.
|
||||
ConnectionPool* pool_;
|
||||
|
||||
+2
-2
@@ -7,7 +7,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "asio/buffer.hpp" // for const_buffer
|
||||
#include "boost/asio/buffer.hpp" // for const_buffer
|
||||
|
||||
#include "webcc/config.h"
|
||||
|
||||
@@ -21,7 +21,7 @@ using Strings = std::vector<std::string>;
|
||||
// Could also be considered as arguments, so named as UrlArgs.
|
||||
using UrlArgs = std::vector<std::string>;
|
||||
|
||||
using Payload = std::vector<asio::const_buffer>;
|
||||
using Payload = std::vector<boost::asio::const_buffer>;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
+1
-1
@@ -95,7 +95,7 @@ void Message::SetContentType(const std::string& media_type,
|
||||
}
|
||||
|
||||
Payload Message::GetPayload() const {
|
||||
using asio::buffer;
|
||||
using boost::asio::buffer;
|
||||
|
||||
Payload payload;
|
||||
|
||||
|
||||
+7
-7
@@ -12,11 +12,11 @@
|
||||
|
||||
namespace sfs = std::filesystem;
|
||||
|
||||
using tcp = asio::ip::tcp;
|
||||
using tcp = boost::asio::ip::tcp;
|
||||
|
||||
namespace webcc {
|
||||
|
||||
Server::Server(asio::ip::tcp protocol, std::uint16_t port,
|
||||
Server::Server(boost::asio::ip::tcp protocol, std::uint16_t port,
|
||||
const sfs::path& doc_root)
|
||||
: protocol_(protocol),
|
||||
port_(port),
|
||||
@@ -75,7 +75,7 @@ void Server::Run(std::size_t workers, std::size_t loops) {
|
||||
} else {
|
||||
std::vector<std::thread> loop_threads;
|
||||
for (std::size_t i = 0; i < loops; ++i) {
|
||||
loop_threads.emplace_back(&asio::io_context::run, &io_context_);
|
||||
loop_threads.emplace_back(&boost::asio::io_context::run, &io_context_);
|
||||
}
|
||||
// Join the threads for blocking.
|
||||
for (std::size_t i = 0; i < loops; ++i) {
|
||||
@@ -105,7 +105,7 @@ void Server::AddSignals() {
|
||||
|
||||
void Server::AsyncWaitSignals() {
|
||||
signals_.async_wait(
|
||||
[this](std::error_code, int signo) {
|
||||
[this](boost::system::error_code, int signo) {
|
||||
// The server is stopped by canceling all outstanding asynchronous
|
||||
// operations. Once all operations have finished the io_context::run()
|
||||
// call will exit.
|
||||
@@ -116,7 +116,7 @@ void Server::AsyncWaitSignals() {
|
||||
}
|
||||
|
||||
bool Server::Listen(std::uint16_t port) {
|
||||
std::error_code ec;
|
||||
boost::system::error_code ec;
|
||||
|
||||
tcp::endpoint endpoint(protocol_, port);
|
||||
|
||||
@@ -145,7 +145,7 @@ bool Server::Listen(std::uint16_t port) {
|
||||
// Start listening for connections.
|
||||
// After listen, the client is able to connect to the server even the server
|
||||
// has not started to accept the connection yet.
|
||||
acceptor_.listen(asio::socket_base::max_listen_connections, ec);
|
||||
acceptor_.listen(boost::asio::socket_base::max_listen_connections, ec);
|
||||
if (ec) {
|
||||
LOG_ERRO("Acceptor listen error (%s).", ec.message().c_str());
|
||||
return false;
|
||||
@@ -156,7 +156,7 @@ bool Server::Listen(std::uint16_t port) {
|
||||
|
||||
void Server::AsyncAccept() {
|
||||
acceptor_.async_accept(
|
||||
[this](std::error_code ec, tcp::socket socket) {
|
||||
[this](boost::system::error_code ec, tcp::socket socket) {
|
||||
// Check whether the server was stopped by a signal before this
|
||||
// completion handler had a chance to run.
|
||||
if (!acceptor_.is_open()) {
|
||||
|
||||
+8
-8
@@ -6,9 +6,9 @@
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "asio/io_context.hpp"
|
||||
#include "asio/ip/tcp.hpp"
|
||||
#include "asio/signal_set.hpp"
|
||||
#include "boost/asio/io_context.hpp"
|
||||
#include "boost/asio/ip/tcp.hpp"
|
||||
#include "boost/asio/signal_set.hpp"
|
||||
|
||||
#include "webcc/connection.h"
|
||||
#include "webcc/connection_pool.h"
|
||||
@@ -20,7 +20,7 @@ namespace webcc {
|
||||
|
||||
class Server : public Router {
|
||||
public:
|
||||
Server(asio::ip::tcp protocol, std::uint16_t port,
|
||||
Server(boost::asio::ip::tcp protocol, std::uint16_t port,
|
||||
const std::filesystem::path& doc_root = {});
|
||||
|
||||
~Server() = default;
|
||||
@@ -95,7 +95,7 @@ private:
|
||||
|
||||
private:
|
||||
// tcp::v4() or tcp::v6()
|
||||
asio::ip::tcp protocol_;
|
||||
boost::asio::ip::tcp protocol_;
|
||||
|
||||
// Port number.
|
||||
std::uint16_t port_;
|
||||
@@ -114,16 +114,16 @@ private:
|
||||
std::mutex state_mutex_;
|
||||
|
||||
// The io_context used to perform asynchronous operations.
|
||||
asio::io_context io_context_;
|
||||
boost::asio::io_context io_context_;
|
||||
|
||||
// Acceptor used to listen for incoming connections.
|
||||
asio::ip::tcp::acceptor acceptor_;
|
||||
boost::asio::ip::tcp::acceptor acceptor_;
|
||||
|
||||
// The connection pool which owns all live connections.
|
||||
ConnectionPool pool_;
|
||||
|
||||
// The signals for processing termination notifications.
|
||||
asio::signal_set signals_;
|
||||
boost::asio::signal_set signals_;
|
||||
|
||||
// Worker threads.
|
||||
std::vector<std::thread> worker_threads_;
|
||||
|
||||
+30
-24
@@ -12,9 +12,9 @@
|
||||
#endif // defined(_WIN32) || defined(_WIN64)
|
||||
#endif // WEBCC_ENABLE_SSL
|
||||
|
||||
#include "asio/connect.hpp"
|
||||
#include "asio/read.hpp"
|
||||
#include "asio/write.hpp"
|
||||
#include "boost/asio/connect.hpp"
|
||||
#include "boost/asio/read.hpp"
|
||||
#include "boost/asio/write.hpp"
|
||||
|
||||
#include "webcc/logger.h"
|
||||
|
||||
@@ -22,12 +22,12 @@ namespace webcc {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
Socket::Socket(asio::io_context& io_context) : socket_(io_context) {
|
||||
Socket::Socket(boost::asio::io_context& io_context) : socket_(io_context) {
|
||||
}
|
||||
|
||||
bool Socket::Connect(const std::string& /*host*/, const Endpoints& endpoints) {
|
||||
std::error_code ec;
|
||||
asio::connect(socket_, endpoints, ec);
|
||||
boost::system::error_code ec;
|
||||
boost::asio::connect(socket_, endpoints, ec);
|
||||
|
||||
if (ec) {
|
||||
LOG_ERRO("Socket connect error (%s).", ec.message().c_str());
|
||||
@@ -37,25 +37,25 @@ bool Socket::Connect(const std::string& /*host*/, const Endpoints& endpoints) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Socket::Write(const Payload& payload, std::error_code* ec) {
|
||||
asio::write(socket_, payload, *ec);
|
||||
bool Socket::Write(const Payload& payload, boost::system::error_code* ec) {
|
||||
boost::asio::write(socket_, payload, *ec);
|
||||
return !(*ec);
|
||||
}
|
||||
|
||||
bool Socket::ReadSome(std::vector<char>* buffer, std::size_t* size,
|
||||
std::error_code* ec) {
|
||||
*size = socket_.read_some(asio::buffer(*buffer), *ec);
|
||||
boost::system::error_code* ec) {
|
||||
*size = socket_.read_some(boost::asio::buffer(*buffer), *ec);
|
||||
return (*size != 0 && !(*ec));
|
||||
}
|
||||
|
||||
void Socket::AsyncReadSome(ReadHandler&& handler, std::vector<char>* buffer) {
|
||||
socket_.async_read_some(asio::buffer(*buffer), std::move(handler));
|
||||
socket_.async_read_some(boost::asio::buffer(*buffer), std::move(handler));
|
||||
}
|
||||
|
||||
bool Socket::Close() {
|
||||
std::error_code ec;
|
||||
boost::system::error_code ec;
|
||||
|
||||
socket_.shutdown(asio::ip::tcp::socket::shutdown_both, ec);
|
||||
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
|
||||
|
||||
if (ec) {
|
||||
LOG_WARN("Socket shutdown error (%s).", ec.message().c_str());
|
||||
@@ -125,9 +125,9 @@ static bool UseSystemCertificateStore(SSL_CTX* ssl_ctx) {
|
||||
|
||||
#endif // defined(_WIN32) || defined(_WIN64)
|
||||
|
||||
namespace ssl = asio::ssl;
|
||||
namespace ssl = boost::asio::ssl;
|
||||
|
||||
SslSocket::SslSocket(asio::io_context& io_context, bool ssl_verify)
|
||||
SslSocket::SslSocket(boost::asio::io_context& io_context, bool ssl_verify)
|
||||
: ssl_context_(ssl::context::sslv23),
|
||||
ssl_socket_(io_context, ssl_context_),
|
||||
ssl_verify_(ssl_verify) {
|
||||
@@ -142,8 +142,8 @@ SslSocket::SslSocket(asio::io_context& io_context, bool ssl_verify)
|
||||
}
|
||||
|
||||
bool SslSocket::Connect(const std::string& host, const Endpoints& endpoints) {
|
||||
std::error_code ec;
|
||||
asio::connect(ssl_socket_.lowest_layer(), endpoints, ec);
|
||||
boost::system::error_code ec;
|
||||
boost::asio::connect(ssl_socket_.lowest_layer(), endpoints, ec);
|
||||
|
||||
if (ec) {
|
||||
LOG_ERRO("Socket connect error (%s).", ec.message().c_str());
|
||||
@@ -153,24 +153,24 @@ bool SslSocket::Connect(const std::string& host, const Endpoints& endpoints) {
|
||||
return Handshake(host);
|
||||
}
|
||||
|
||||
bool SslSocket::Write(const Payload& payload, std::error_code* ec) {
|
||||
asio::write(ssl_socket_, payload, *ec);
|
||||
bool SslSocket::Write(const Payload& payload, boost::system::error_code* ec) {
|
||||
boost::asio::write(ssl_socket_, payload, *ec);
|
||||
return !(*ec);
|
||||
}
|
||||
|
||||
bool SslSocket::ReadSome(std::vector<char>* buffer, std::size_t* size,
|
||||
std::error_code* ec) {
|
||||
*size = ssl_socket_.read_some(asio::buffer(*buffer), *ec);
|
||||
boost::system::error_code* ec) {
|
||||
*size = ssl_socket_.read_some(boost::asio::buffer(*buffer), *ec);
|
||||
return (*size != 0 && !(*ec));
|
||||
}
|
||||
|
||||
void SslSocket::AsyncReadSome(ReadHandler&& handler,
|
||||
std::vector<char>* buffer) {
|
||||
ssl_socket_.async_read_some(asio::buffer(*buffer), std::move(handler));
|
||||
ssl_socket_.async_read_some(boost::asio::buffer(*buffer), std::move(handler));
|
||||
}
|
||||
|
||||
bool SslSocket::Close() {
|
||||
std::error_code ec;
|
||||
boost::system::error_code ec;
|
||||
ssl_socket_.lowest_layer().close(ec);
|
||||
return !ec;
|
||||
}
|
||||
@@ -182,10 +182,16 @@ bool SslSocket::Handshake(const std::string& host) {
|
||||
ssl_socket_.set_verify_mode(ssl::verify_none);
|
||||
}
|
||||
|
||||
// ssl::host_name_verification has been added since Boost 1.73 to replace
|
||||
// ssl::rfc2818_verification.
|
||||
#if BOOST_VERSION < 107300
|
||||
ssl_socket_.set_verify_callback(ssl::rfc2818_verification(host));
|
||||
#else
|
||||
ssl_socket_.set_verify_callback(ssl::host_name_verification(host));
|
||||
#endif // BOOST_VERSION < 107300
|
||||
|
||||
// Use sync API directly since we don't need timeout control.
|
||||
std::error_code ec;
|
||||
boost::system::error_code ec;
|
||||
ssl_socket_.handshake(ssl::stream_base::client, ec);
|
||||
|
||||
if (ec) {
|
||||
|
||||
+15
-15
@@ -3,13 +3,13 @@
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "asio/ip/tcp.hpp"
|
||||
#include "boost/asio/ip/tcp.hpp"
|
||||
|
||||
#include "webcc/config.h"
|
||||
#include "webcc/request.h"
|
||||
|
||||
#if WEBCC_ENABLE_SSL
|
||||
#include "asio/ssl.hpp"
|
||||
#include "boost/asio/ssl.hpp"
|
||||
#endif // WEBCC_ENABLE_SSL
|
||||
|
||||
namespace webcc {
|
||||
@@ -20,18 +20,18 @@ class SocketBase {
|
||||
public:
|
||||
virtual ~SocketBase() = default;
|
||||
|
||||
using Endpoints = asio::ip::tcp::resolver::results_type;
|
||||
using Endpoints = boost::asio::ip::tcp::resolver::results_type;
|
||||
|
||||
using ReadHandler =
|
||||
std::function<void(std::error_code, std::size_t)>;
|
||||
std::function<void(boost::system::error_code, std::size_t)>;
|
||||
|
||||
// TODO: Remove |host|
|
||||
virtual bool Connect(const std::string& host, const Endpoints& endpoints) = 0;
|
||||
|
||||
virtual bool Write(const Payload& payload, std::error_code* ec) = 0;
|
||||
virtual bool Write(const Payload& payload, boost::system::error_code* ec) = 0;
|
||||
|
||||
virtual bool ReadSome(std::vector<char>* buffer, std::size_t* size,
|
||||
std::error_code* ec) = 0;
|
||||
boost::system::error_code* ec) = 0;
|
||||
|
||||
virtual void AsyncReadSome(ReadHandler&& handler,
|
||||
std::vector<char>* buffer) = 0;
|
||||
@@ -43,21 +43,21 @@ public:
|
||||
|
||||
class Socket : public SocketBase {
|
||||
public:
|
||||
explicit Socket(asio::io_context& io_context);
|
||||
explicit Socket(boost::asio::io_context& io_context);
|
||||
|
||||
bool Connect(const std::string& host, const Endpoints& endpoints) override;
|
||||
|
||||
bool Write(const Payload& payload, std::error_code* ec) override;
|
||||
bool Write(const Payload& payload, boost::system::error_code* ec) override;
|
||||
|
||||
bool ReadSome(std::vector<char>* buffer, std::size_t* size,
|
||||
std::error_code* ec) override;
|
||||
boost::system::error_code* ec) override;
|
||||
|
||||
void AsyncReadSome(ReadHandler&& handler, std::vector<char>* buffer) override;
|
||||
|
||||
bool Close() override;
|
||||
|
||||
private:
|
||||
asio::ip::tcp::socket socket_;
|
||||
boost::asio::ip::tcp::socket socket_;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -66,15 +66,15 @@ private:
|
||||
|
||||
class SslSocket : public SocketBase {
|
||||
public:
|
||||
explicit SslSocket(asio::io_context& io_context,
|
||||
explicit SslSocket(boost::asio::io_context& io_context,
|
||||
bool ssl_verify = true);
|
||||
|
||||
bool Connect(const std::string& host, const Endpoints& endpoints) override;
|
||||
|
||||
bool Write(const Payload& payload, std::error_code* ec) override;
|
||||
bool Write(const Payload& payload, boost::system::error_code* ec) override;
|
||||
|
||||
bool ReadSome(std::vector<char>* buffer, std::size_t* size,
|
||||
std::error_code* ec) override;
|
||||
boost::system::error_code* ec) override;
|
||||
|
||||
void AsyncReadSome(ReadHandler&& handler, std::vector<char>* buffer) override;
|
||||
|
||||
@@ -83,9 +83,9 @@ public:
|
||||
private:
|
||||
bool Handshake(const std::string& host);
|
||||
|
||||
asio::ssl::context ssl_context_;
|
||||
boost::asio::ssl::context ssl_context_;
|
||||
|
||||
asio::ssl::stream<asio::ip::tcp::socket> ssl_socket_;
|
||||
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket_;
|
||||
|
||||
// Verify the certificate of the peer (remote server) or not.
|
||||
bool ssl_verify_;
|
||||
|
||||
+4
-4
@@ -97,16 +97,16 @@ void DumpByLine(const std::string& data, std::ostream& os,
|
||||
}
|
||||
|
||||
void PrintEndpoint(std::ostream& ostream,
|
||||
const asio::ip::tcp::endpoint& endpoint) {
|
||||
const boost::asio::ip::tcp::endpoint& endpoint) {
|
||||
ostream << endpoint;
|
||||
if (endpoint.protocol() == asio::ip::tcp::v4()) {
|
||||
if (endpoint.protocol() == boost::asio::ip::tcp::v4()) {
|
||||
ostream << ", v4";
|
||||
} else if (endpoint.protocol() == asio::ip::tcp::v6()) {
|
||||
} else if (endpoint.protocol() == boost::asio::ip::tcp::v6()) {
|
||||
ostream << ", v6";
|
||||
}
|
||||
}
|
||||
|
||||
std::string EndpointToString(const asio::ip::tcp::endpoint& endpoint) {
|
||||
std::string EndpointToString(const boost::asio::ip::tcp::endpoint& endpoint) {
|
||||
std::stringstream ss;
|
||||
PrintEndpoint(ss, endpoint);
|
||||
return ss.str();
|
||||
|
||||
+3
-3
@@ -4,7 +4,7 @@
|
||||
#include <iosfwd>
|
||||
#include <string>
|
||||
|
||||
#include "asio/ip/tcp.hpp"
|
||||
#include "boost/asio/ip/tcp.hpp"
|
||||
|
||||
#include "webcc/globals.h"
|
||||
|
||||
@@ -41,10 +41,10 @@ void DumpByLine(const std::string& data, std::ostream& os,
|
||||
// Print TCP endpoint.
|
||||
// Usage: PrintEndpoint(std::cout, endpoint)
|
||||
void PrintEndpoint(std::ostream& ostream,
|
||||
const asio::ip::tcp::endpoint& endpoint);
|
||||
const boost::asio::ip::tcp::endpoint& endpoint);
|
||||
|
||||
// TCP endpoint to string.
|
||||
std::string EndpointToString(const asio::ip::tcp::endpoint& endpoint);
|
||||
std::string EndpointToString(const boost::asio::ip::tcp::endpoint& endpoint);
|
||||
|
||||
} // namespace utility
|
||||
} // namespace webcc
|
||||
|
||||
Reference in New Issue
Block a user