replace boost.asio with standalone asio.

This commit is contained in:
Chunting Gu
2020-02-20 10:43:46 +08:00
parent b72db3a048
commit 718392fa9e
490 changed files with 110256 additions and 144 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
# Don't use any deprecated definitions (e.g., io_service).
add_definitions(-DBOOST_ASIO_NO_DEPRECATED)
add_definitions(-DASIO_NO_DEPRECATED)
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
+4 -4
View File
@@ -58,7 +58,7 @@ void StringBody::InitPayload() {
Payload StringBody::NextPayload(bool /*free_previous*/) {
if (index_ == 0) {
index_ = 1;
return { boost::asio::buffer(data_) };
return { asio::buffer(data_) };
}
return {};
}
@@ -129,7 +129,7 @@ Payload FormBody::NextPayload(bool free_previous) {
}
void FormBody::AddBoundary(Payload* payload) {
using boost::asio::buffer;
using 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 boost::asio::buffer;
using 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 {
boost::asio::buffer(chunk_.data(), (std::size_t)ifstream_.gcount())
asio::buffer(chunk_.data(), (std::size_t)ifstream_.gcount())
};
}
return {};
+10 -10
View File
@@ -2,7 +2,7 @@
#include "webcc/logger.h"
using boost::asio::ip::tcp;
using asio::ip::tcp;
namespace webcc {
@@ -99,7 +99,7 @@ void Client::DoConnect(RequestPtr request, const std::string& default_port) {
port = default_port;
}
boost::system::error_code ec;
std::error_code ec;
auto endpoints = resolver.resolve(tcp::v4(), request->host(), port, ec);
if (ec) {
@@ -132,7 +132,7 @@ void Client::WriteRequest(RequestPtr request) {
// Use sync API directly since we don't need timeout control.
boost::system::error_code ec;
std::error_code ec;
if (socket_->Write(request->GetPayload(), &ec)) {
// Write request body.
@@ -166,18 +166,18 @@ void Client::ReadResponse() {
}
void Client::DoReadResponse() {
boost::system::error_code ec = boost::asio::error::would_block;
std::error_code ec = asio::error::would_block;
std::size_t length = 0;
// The read handler.
auto handler = [&ec, &length](boost::system::error_code inner_ec,
auto handler = [&ec, &length](std::error_code inner_ec,
std::size_t inner_length) {
ec = inner_ec;
length = inner_length;
};
while (true) {
ec = boost::asio::error::would_block;
ec = asio::error::would_block;
length = 0;
socket_->AsyncReadSome(std::move(handler), &buffer_);
@@ -188,7 +188,7 @@ void Client::DoReadResponse() {
// Block until the asynchronous operation has completed.
do {
io_context_.run_one();
} while (ec == boost::asio::error::would_block);
} while (ec == asio::error::would_block);
// Stop the timer.
CancelTimer();
@@ -235,11 +235,11 @@ void Client::DoWaitTimer() {
timer_.async_wait(std::bind(&Client::OnTimer, this, std::placeholders::_1));
}
void Client::OnTimer(boost::system::error_code ec) {
void Client::OnTimer(std::error_code ec) {
LOG_VERB("On timer.");
// timer_.cancel() was called.
if (ec == boost::asio::error::operation_aborted) {
if (ec == asio::error::operation_aborted) {
LOG_VERB("Timer canceled.");
return;
}
@@ -249,7 +249,7 @@ void Client::OnTimer(boost::system::error_code ec) {
return;
}
if (timer_.expiry() <= boost::asio::steady_timer::clock_type::now()) {
if (timer_.expiry() <= 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
View File
@@ -6,9 +6,9 @@
#include <string>
#include <vector>
#include "boost/asio/io_context.hpp"
#include "boost/asio/ip/tcp.hpp"
#include "boost/asio/steady_timer.hpp"
#include "asio/io_context.hpp"
#include "asio/ip/tcp.hpp"
#include "asio/steady_timer.hpp"
#include "webcc/globals.h"
#include "webcc/request.h"
@@ -82,13 +82,13 @@ private:
void DoReadResponse();
void DoWaitTimer();
void OnTimer(boost::system::error_code ec);
void OnTimer(std::error_code ec);
// Cancel any async-operations waiting on the timer.
void CancelTimer();
private:
boost::asio::io_context io_context_;
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.
boost::asio::steady_timer timer_;
asio::steady_timer timer_;
// The buffer for reading response.
std::vector<char> buffer_;
+1 -1
View File
@@ -214,7 +214,7 @@ FormPartPtr FormPart::NewFile(const std::string& name,
}
void FormPart::Prepare(Payload* payload) {
using boost::asio::buffer;
using asio::buffer;
if (data_.empty() && !path_.empty()) {
if (!utility::ReadFile(path_, &data_)) {
+15 -15
View File
@@ -2,12 +2,12 @@
#include <utility>
#include "boost/asio/write.hpp"
#include "asio/write.hpp"
#include "webcc/connection_pool.h"
#include "webcc/logger.h"
using boost::asio::ip::tcp;
using asio::ip::tcp;
namespace webcc {
@@ -20,7 +20,7 @@ Connection::Connection(tcp::socket socket, ConnectionPool* pool,
void Connection::Start() {
request_.reset(new Request{});
boost::system::error_code ec;
std::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
boost::system::error_code ec;
std::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(boost::asio::buffer(buffer_),
socket_.async_read_some(asio::buffer(buffer_),
std::bind(&Connection::OnRead, shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
}
void Connection::OnRead(boost::system::error_code ec, std::size_t length) {
void Connection::OnRead(std::error_code ec, std::size_t length) {
if (ec) {
if (ec == boost::asio::error::eof) {
if (ec == asio::error::eof) {
LOG_INFO("Socket read EOF (%s).", ec.message().c_str());
} else if (ec == boost::asio::error::operation_aborted) {
} else if (ec == 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(boost::system::error_code ec, std::size_t length) {
// Don't try to send any response back.
if (ec != boost::asio::error::operation_aborted) {
if (ec != 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.
boost::asio::async_write(socket_, response_->GetPayload(),
asio::async_write(socket_, response_->GetPayload(),
std::bind(&Connection::OnWriteHeaders,
shared_from_this(), std::placeholders::_1,
std::placeholders::_2));
}
void Connection::OnWriteHeaders(boost::system::error_code ec,
void Connection::OnWriteHeaders(std::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()) {
boost::asio::async_write(socket_, payload,
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(boost::system::error_code ec, std::size_t length) {
void Connection::OnWriteBody(std::error_code ec, std::size_t length) {
if (ec) {
OnWriteError(ec);
} else {
@@ -187,10 +187,10 @@ void Connection::OnWriteOK() {
}
}
void Connection::OnWriteError(boost::system::error_code ec) {
void Connection::OnWriteError(std::error_code ec) {
LOG_ERRO("Socket write error (%s).", ec.message().c_str());
if (ec != boost::asio::error::operation_aborted) {
if (ec != asio::error::operation_aborted) {
pool_->Close(shared_from_this());
}
}
+7 -7
View File
@@ -5,7 +5,7 @@
#include <string>
#include <vector>
#include "boost/asio/ip/tcp.hpp"
#include "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(boost::asio::ip::tcp::socket socket, ConnectionPool* pool,
Connection(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(boost::system::error_code ec, std::size_t length);
void OnRead(std::error_code ec, std::size_t length);
void DoWrite();
void OnWriteHeaders(boost::system::error_code ec, std::size_t length);
void OnWriteHeaders(std::error_code ec, std::size_t length);
void DoWriteBody();
void OnWriteBody(boost::system::error_code ec, std::size_t length);
void OnWriteBody(std::error_code ec, std::size_t length);
void OnWriteOK();
void OnWriteError(boost::system::error_code ec);
void OnWriteError(std::error_code ec);
// The socket for the connection.
boost::asio::ip::tcp::socket socket_;
asio::ip::tcp::socket socket_;
// The connection pool.
ConnectionPool* pool_;
+2 -2
View File
@@ -7,7 +7,7 @@
#include <string>
#include <vector>
#include "boost/asio/buffer.hpp" // for const_buffer
#include "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<boost::asio::const_buffer>;
using Payload = std::vector<asio::const_buffer>;
// -----------------------------------------------------------------------------
+1 -1
View File
@@ -95,7 +95,7 @@ void Message::SetContentType(const std::string& media_type,
}
Payload Message::GetPayload() const {
using boost::asio::buffer;
using asio::buffer;
Payload payload;
+8 -8
View File
@@ -12,7 +12,7 @@
namespace sfs = std::filesystem;
using tcp = boost::asio::ip::tcp;
using tcp = asio::ip::tcp;
namespace webcc {
@@ -51,7 +51,7 @@ void Server::Run(std::size_t workers, std::size_t loops) {
// Create worker threads.
for (std::size_t i = 0; i < workers; ++i) {
worker_threads_.emplace_back(std::bind(&Server::WorkerRoutine, this));
worker_threads_.emplace_back(&Server::WorkerRoutine, this);
}
}
@@ -64,12 +64,12 @@ void Server::Run(std::size_t workers, std::size_t loops) {
LOG_INFO("Loop is running in %u thread(s).", loops);
if (loops == 1) {
// Just run the loop in the current thread.
// Run the loop in current thread.
io_context_.run();
} else {
std::vector<std::thread> loop_threads;
for (std::size_t i = 0; i < loops; ++i) {
loop_threads.emplace_back(&boost::asio::io_context::run, &io_context_);
loop_threads.emplace_back(&asio::io_context::run, &io_context_);
}
// Join the threads for blocking.
for (std::size_t i = 0; i < loops; ++i) {
@@ -99,7 +99,7 @@ void Server::AddSignals() {
void Server::AsyncWaitSignals() {
signals_.async_wait(
[this](boost::system::error_code, int signo) {
[this](std::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.
@@ -110,7 +110,7 @@ void Server::AsyncWaitSignals() {
}
bool Server::Listen(std::uint16_t port) {
boost::system::error_code ec;
std::error_code ec;
tcp::endpoint endpoint(tcp::v4(), port);
@@ -139,7 +139,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(boost::asio::socket_base::max_listen_connections, ec);
acceptor_.listen(asio::socket_base::max_listen_connections, ec);
if (ec) {
LOG_ERRO("Acceptor listen error (%s).", ec.message().c_str());
return false;
@@ -150,7 +150,7 @@ bool Server::Listen(std::uint16_t port) {
void Server::AsyncAccept() {
acceptor_.async_accept(
[this](boost::system::error_code ec, tcp::socket socket) {
[this](std::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()) {
+6 -6
View File
@@ -6,9 +6,9 @@
#include <thread>
#include <vector>
#include "boost/asio/io_context.hpp"
#include "boost/asio/ip/tcp.hpp"
#include "boost/asio/signal_set.hpp"
#include "asio/io_context.hpp"
#include "asio/ip/tcp.hpp"
#include "asio/signal_set.hpp"
#include "webcc/connection.h"
#include "webcc/connection_pool.h"
@@ -111,16 +111,16 @@ private:
std::mutex state_mutex_;
// The io_context used to perform asynchronous operations.
boost::asio::io_context io_context_;
asio::io_context io_context_;
// Acceptor used to listen for incoming connections.
boost::asio::ip::tcp::acceptor acceptor_;
asio::ip::tcp::acceptor acceptor_;
// The connection pool which owns all live connections.
ConnectionPool pool_;
// The signals for processing termination notifications.
boost::asio::signal_set signals_;
asio::signal_set signals_;
// Worker threads.
std::vector<std::thread> worker_threads_;
+24 -24
View File
@@ -12,9 +12,9 @@
#endif // defined(_WIN32) || defined(_WIN64)
#endif // WEBCC_ENABLE_SSL
#include "boost/asio/connect.hpp"
#include "boost/asio/read.hpp"
#include "boost/asio/write.hpp"
#include "asio/connect.hpp"
#include "asio/read.hpp"
#include "asio/write.hpp"
#include "webcc/logger.h"
@@ -22,12 +22,12 @@ namespace webcc {
// -----------------------------------------------------------------------------
Socket::Socket(boost::asio::io_context& io_context) : socket_(io_context) {
Socket::Socket(asio::io_context& io_context) : socket_(io_context) {
}
bool Socket::Connect(const std::string& /*host*/, const Endpoints& endpoints) {
boost::system::error_code ec;
boost::asio::connect(socket_, endpoints, ec);
std::error_code ec;
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, boost::system::error_code* ec) {
boost::asio::write(socket_, payload, *ec);
bool Socket::Write(const Payload& payload, std::error_code* ec) {
asio::write(socket_, payload, *ec);
return !(*ec);
}
bool Socket::ReadSome(std::vector<char>* buffer, std::size_t* size,
boost::system::error_code* ec) {
*size = socket_.read_some(boost::asio::buffer(*buffer), *ec);
std::error_code* ec) {
*size = socket_.read_some(asio::buffer(*buffer), *ec);
return (*size != 0 && !(*ec));
}
void Socket::AsyncReadSome(ReadHandler&& handler, std::vector<char>* buffer) {
socket_.async_read_some(boost::asio::buffer(*buffer), std::move(handler));
socket_.async_read_some(asio::buffer(*buffer), std::move(handler));
}
bool Socket::Close() {
boost::system::error_code ec;
std::error_code ec;
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
socket_.shutdown(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 = boost::asio::ssl;
namespace ssl = asio::ssl;
SslSocket::SslSocket(boost::asio::io_context& io_context, bool ssl_verify)
SslSocket::SslSocket(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(boost::asio::io_context& io_context, bool ssl_verify)
}
bool SslSocket::Connect(const std::string& host, const Endpoints& endpoints) {
boost::system::error_code ec;
boost::asio::connect(ssl_socket_.lowest_layer(), endpoints, ec);
std::error_code ec;
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, boost::system::error_code* ec) {
boost::asio::write(ssl_socket_, payload, *ec);
bool SslSocket::Write(const Payload& payload, std::error_code* ec) {
asio::write(ssl_socket_, payload, *ec);
return !(*ec);
}
bool SslSocket::ReadSome(std::vector<char>* buffer, std::size_t* size,
boost::system::error_code* ec) {
*size = ssl_socket_.read_some(boost::asio::buffer(*buffer), *ec);
std::error_code* ec) {
*size = ssl_socket_.read_some(asio::buffer(*buffer), *ec);
return (*size != 0 && !(*ec));
}
void SslSocket::AsyncReadSome(ReadHandler&& handler,
std::vector<char>* buffer) {
ssl_socket_.async_read_some(boost::asio::buffer(*buffer), std::move(handler));
ssl_socket_.async_read_some(asio::buffer(*buffer), std::move(handler));
}
bool SslSocket::Close() {
boost::system::error_code ec;
std::error_code ec;
ssl_socket_.lowest_layer().close(ec);
return !ec;
}
@@ -185,7 +185,7 @@ bool SslSocket::Handshake(const std::string& host) {
ssl_socket_.set_verify_callback(ssl::rfc2818_verification(host));
// Use sync API directly since we don't need timeout control.
boost::system::error_code ec;
std::error_code ec;
ssl_socket_.handshake(ssl::stream_base::client, ec);
if (ec) {
+15 -15
View File
@@ -3,13 +3,13 @@
#include <vector>
#include "boost/asio/ip/tcp.hpp"
#include "asio/ip/tcp.hpp"
#include "webcc/config.h"
#include "webcc/request.h"
#if WEBCC_ENABLE_SSL
#include "boost/asio/ssl.hpp"
#include "asio/ssl.hpp"
#endif // WEBCC_ENABLE_SSL
namespace webcc {
@@ -20,18 +20,18 @@ class SocketBase {
public:
virtual ~SocketBase() = default;
using Endpoints = boost::asio::ip::tcp::resolver::results_type;
using Endpoints = asio::ip::tcp::resolver::results_type;
using ReadHandler =
std::function<void(boost::system::error_code, std::size_t)>;
std::function<void(std::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, boost::system::error_code* ec) = 0;
virtual bool Write(const Payload& payload, std::error_code* ec) = 0;
virtual bool ReadSome(std::vector<char>* buffer, std::size_t* size,
boost::system::error_code* ec) = 0;
std::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(boost::asio::io_context& io_context);
explicit Socket(asio::io_context& io_context);
bool Connect(const std::string& host, const Endpoints& endpoints) override;
bool Write(const Payload& payload, boost::system::error_code* ec) override;
bool Write(const Payload& payload, std::error_code* ec) override;
bool ReadSome(std::vector<char>* buffer, std::size_t* size,
boost::system::error_code* ec) override;
std::error_code* ec) override;
void AsyncReadSome(ReadHandler&& handler, std::vector<char>* buffer) override;
bool Close() override;
private:
boost::asio::ip::tcp::socket socket_;
asio::ip::tcp::socket socket_;
};
// -----------------------------------------------------------------------------
@@ -66,15 +66,15 @@ private:
class SslSocket : public SocketBase {
public:
explicit SslSocket(boost::asio::io_context& io_context,
explicit SslSocket(asio::io_context& io_context,
bool ssl_verify = true);
bool Connect(const std::string& host, const Endpoints& endpoints) override;
bool Write(const Payload& payload, boost::system::error_code* ec) override;
bool Write(const Payload& payload, std::error_code* ec) override;
bool ReadSome(std::vector<char>* buffer, std::size_t* size,
boost::system::error_code* ec) override;
std::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);
boost::asio::ssl::context ssl_context_;
asio::ssl::context ssl_context_;
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket_;
asio::ssl::stream<asio::ip::tcp::socket> ssl_socket_;
// Verify the certificate of the peer (remote server) or not.
bool ssl_verify_;