How to access boost::asio::streambuf input as a string? - c++

I am writing a very simple HTTP server based on: http://www.boost.org/doc/libs/1_62_0/doc/html/boost_asio/example/cpp11/echo/async_tcp_echo_server.cpp
I have tried numerous techniques to extract the data from a boost::asio::streambuf in order to parse HTTP headers. The streambuf object does not appear to manage it's memory properly (or, more likely, I am mis-using it) & I end up getting a seg fault.
As you can see from the code, none of the techniques suggested here or here work. I suspect this is because I'm using boost::asio::async_read_until() to read all the headers, rather than just a single header at a time as most other coders appear to be doing.
Any advice/pointers would be appreciated.
/*
g++ -D TRY1 -ggdb3 -I $e/boost-1.62/include /tmp/streambuf.bug.cc $e/boost-1.62/lib/libboost_system.a -D TRY1
or
g++ -D TRY2 -ggdb3 -I $e/boost-1.62/include /tmp/streambuf.bug.cc $e/boost-1.62/lib/libboost_system.a -D TRY2
or
g++ -D TRY3 -ggdb3 -I $e/boost-1.62/include /tmp/streambuf.bug.cc $e/boost-1.62/lib/libboost_system.a -D TRY3
*/
#include <cstdlib>
#include <iostream>
#include <memory>
#include <utility>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
using boost::asio::ip::tcp;
class session : public std::enable_shared_from_this<session>
{
public:
session(tcp::socket socket) : socket_(std::move(socket)), dbg(true) {
assert(headers.empty());
memset(padding, 0, 10000);
std::cout << "buffer size: " << buffer.max_size() << '\n';
}
void start() { readHeaders(); }
private:
void readHeaders() {
if (dbg)
std::cout << "readHeaders() start\n";
//auto self(shared_from_this());
boost::asio::async_read_until(socket_, buffer, "\r\n\r\n", [this](const boost::system::error_code &ec, std::size_t length) {
if (dbg)
std::cout << "Read " << length << " bytes of headers in async_read_until()\n";
if (ec)
throw std::runtime_error("Error code in readHeaders()");
#ifdef TRY1
std::istream stream(&buffer);
std::string str;
assert(!corrupted("A1"));
while (std::getline(stream, str)) { // seg fault! (on 3rd invocation)
assert(!corrupted("A2"));
std::cout << "str=" << str << '\n';
assert(!corrupted("A3"));
}
#endif
#if 0
std::string str;
boost::asio::buffer_copy(boost::asio::buffer(str), buffer); // ugh, won't compile
#endif
#if 0
std::vector<unsigned char> v(buffer.size());
boost::asio::buffer_copy(boost::asio::buffer(v), buffer); // ugh, won't compile
const std::string str(v.begin(), v.end());
#endif
#ifdef TRY2
std::string str;
auto data = buffer.data();
assert(!corrupted("B1"));
for (auto it = data.begin(); it != data.end(); ++it) {
const auto buf = *it;
std::cout << "buf_size=" << boost::asio::buffer_size(buf) << '\n';
assert(!corrupted("B2"));
const char *tmp = boost::asio::buffer_cast<const char *>(buf);
assert(!corrupted("B3"));
str.append(tmp); // BUG!!
assert(!corrupted("B4")); // fails
}
#endif
#ifdef TRY3
auto data = buffer.data();
auto end = data.end();
std::string str;
assert(!corrupted("C1"));
for (auto it = data.begin(); it != end; ++it) {
assert(!corrupted("C2"));
std::vector<unsigned char> v(boost::asio::buffer_size(*it));
assert(!corrupted("C3"));
boost::asio::buffer_copy(boost::asio::buffer(v), *it); // BUG!!
assert(!corrupted("C4")); // fails
str.append(v.begin(), v.end());
assert(!corrupted("C5"));
}
#endif
#ifdef TRY4
assert(!corrupted("D1"));
const std::string str(boost::asio::buffers_begin(buffer.data()), boost::asio::buffers_end(buffer.data())); // BUG!!
assert(!corrupted("D2")); // fails
#endif
#ifdef TRY5
assert(!corrupted("E1"));
const std::string str((std::istreambuf_iterator<char>(&buffer)), std::istreambuf_iterator<char>()); // seg faults!
assert(!corrupted("E2"));
#endif
#ifdef TRY6
boost::asio::streambuf::const_buffers_type bufs = buffer.data();
assert(!corrupted("F1"));
std::string str(boost::asio::buffers_begin(bufs), boost::asio::buffers_begin(bufs) + buffer.size()); // BUG!!
assert(!corrupted("F2")); // fails
#endif
assert(!corrupted("Z1"));
std::cout << "str=" << str << "end of data\n";
std::istringstream input(str);
std::string line;
while (std::getline(input, line)) {
assert(!corrupted("Z2"));
if (line.size() == 1)
continue; // blank line
if (line.substr(0, 3) == "GET")
continue; // TODO: handle properly
const auto idx = line.find(':');
assert(idx != std::string::npos);
const std::string key(line.begin(), line.begin() + idx);
const std::string val(line.begin() + idx + 2, line.end());
// std::cout << "key=" << key << " val=" << val << '\n';
assert(!corrupted("Z3"));
headers[key] = val;
assert(!corrupted("Z4"));
}
assert(!corrupted("Z5"));
for (auto it3 : headers) {
std::cout << it3.first << '=' << it3.second << '\n';
}
const auto it2 = headers.find("Content Length");
contentLength = (it2 == headers.end() ? 0 : atoi(it2->second.c_str()));
if (contentLength > 0) {
const boost::system::error_code ec; // (boost::system::errc::success);
readBody(ec);
}
});
if (dbg)
std::cout << "readHeaders() end\n";
}
void readBody (const boost::system::error_code &ec) {
if (dbg)
std::cout << "readBody()\n";
if (ec)
throw std::runtime_error("Error code in readBody()");
boost::asio::streambuf::const_buffers_type bufs = buffer.data();
body.append(boost::asio::buffers_begin(bufs), boost::asio::buffers_begin(bufs) + buffer.size());
if (dbg)
std::cout << "body.size=" << body.size() << " content length=" << contentLength << '\n';
boost::asio::async_read(socket_,
buffer,
boost::asio::transfer_at_least(1),
boost::bind(&session::readBody, this, boost::asio::placeholders::error));
}
bool corrupted (const std::string s) const {
bool b = false;
if (strlen(padding) > 0) {
std::cout << "buffer overflow detected # " << s << "! padding is: " << padding << '\n';
std::cout.flush();
b = true;
}
if (headers.size() > 1000) {
std::cout << headers.size() << " headers!!\n";
b = true;
}
return b;
}
tcp::socket socket_;
boost::asio::streambuf buffer;
char padding[10000]; // $buffer appears not to manage it's memory properly. Add some padding to detect overflows.
std::map<std::string, std::string> headers;
uint contentLength;
std::string body;
const bool dbg;
};
class server
{
public:
server(boost::asio::io_service& io_service, short port) : acceptor_(io_service, tcp::endpoint(tcp::v4(), port)), socket_(io_service) {
do_accept();
}
private:
void do_accept() {
acceptor_.async_accept(socket_, [this](boost::system::error_code ec) {
if (!ec) {
std::cout << "Connection accepted\n";
std::make_shared<session>(std::move(socket_))->start();
}
do_accept();
});
}
tcp::acceptor acceptor_;
tcp::socket socket_;
};
int main(int argc, char* argv[])
{
try {
if (argc != 2) {
std::cerr << "Usage: async_tcp_echo_server <port>\n";
return 1;
}
boost::asio::io_service io_service;
server s(io_service, std::atoi(argv[1]));
io_service.run();
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
I am using boost v1.62, gcc v6.1 on Linux (Ubuntu 12.04).

You can read from the streambuf
manually (see documentation)
using std::istream:
boost::asio::streambuf buf;
std::istream is(&buf);
// usual extraction:
int i;
if (is >> i) {
// use `i`
}
// or usual line-wise extraction:
std::string line;
while (std::getline(is, line)) {
// do something with `line`
}
alternative use boost::asio::buffer_* functions (buffer_begin(), buffer_end() and buffer_copy) - How copy or reuse boost::asio::streambuf?

Copying the contents of a boost::asio::streambuf as a string has been answered in both the Copy a streambuf's contents to a string and How copy or reuse boost::asio::streambuf questions:
boost::asio::streambuf source;
...
std::string target{buffers_begin(source.data()), buffers_end(source.data())};
The problems being observed are the result of undefined behavior. The program fails to meet the lifetime requirement for async_read_until()'s b parameter, as the streambuf are being destroyed before the completion handler is invoked:
[...] Ownership of the streambuf is retained by the caller, which must guarantee that it remains valid until the handler is called.
In this case, streambuf is a data member of session, and session objects are managed by a shared pointer. The only shared pointer managing session is both created and destroyed in the following expression:
std::make_shared<session>(std::move(socket_))->start();
Within start(), an async_read_until() operation is initiated. However, upon returning form start(), the session's buffer is destroyed before the async_read_until()'s completion handler is invoked, violating the lifetime requirement.
The idiomatic solution used by the official Asio examples is to capture the results of shared_from_this() in the completion handler's lambda capture. This guarantees that the lifetime of the session will be at least as long as the completion handler.
auto self(shared_from_this());
async_read_until(socket_, buffer_, ...,
[this, self](boost::system::error_code& ec, std::size_t length)
{
// `self` keeps the `session` alive for the lifetime of the
// handler. If more async operations are initiated from within
// this handler, then the completion handlers should capture
// `self` as well.
...
});

The exact answer to your question gave sehe. Below is some pseudocode I am using currently for parsing headers.
// This is where to store headers.
_STL::map<_STL::string, _STL::string> m_headers;
// buffer is of type asio::streambuf and contains response from asio::async_read_until
_STL::istream response_stream_headers(&buffer);
// Some helper variables.
_STL::string header, header_name, header_value;
while (true) {
_STL::getline(response_stream_headers, header, '\r');
// Remove \n symbol from the stream.
response_stream_headers.get();
if (header == "") {
// We reached end of headers, there might be still some more data!!
break;
}
// Parse header to key->value
size_t separator_pos = header.find(':');
if (separator_pos != _STL::string::npos) {
header_name = header.substr(0, separator_pos);
if (separator_pos < header.length() - 1) {
header_value = header.substr(separator_pos + 1);
}
else {
header_value = "";
}
boost::trim_left(header_value);
m_headers[name] = value;
}
}
// Parsing is done, but some of the request response could have been reed by
// asio::async_read_until, so whe read response_stream_headers untill end.
// You should use body_response_start as the begining of your response.
std::string body_response_start(std::istreambuf_iterator<char>(response_stream_headers), {});

Related

Boost::Asio C++ Why I`m getting the garbage while reading buffer?

I have a client which read file and sends the data to server line by line. Server must count the number of lines sent. I'm using boost::asio::async_read_until to reach this result. But I'm getting the garbage (like this: Line: �68�) when trying to read data from buffer. Client sends the data only in ASCII encoding.
Client code fragment:
std::ifstream infile(argv[1]);
std::string line;
while (std::getline(infile, line)) {
boost::asio::write(s, boost::asio::buffer(line + '\n'));
std::cout << line << std::endl;
Server read function:
void handle_read(const boost::system::error_code& error, size_t bytes_trasferred, boost::asio::streambuf& buf)
{
if (!error)
{
if (!bytes_trasferred)
{
std::cout << "0 bytes trasferred\n";
return;
}
std::string data = boost::asio::buffer_cast<const char*>(buf.data());
std::istringstream is(data);
std::string line;
std::getline(is, line);
std::cout << "Line: " << line << std::endl;
}
else
std::cerr << "Error: " << error.message() << std::endl;
}
void do_read()
{
std::cout << "do_read\n";
auto self(shared_from_this());
boost::asio::streambuf buf;
buf.prepare(1048576);
std::cout << "ASYNC\n";
boost::asio::async_read_until(socket_, buf, "\n",
boost::bind(&TcpConnection::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, boost::ref(buf)));
}
How to resolve it? Help me, please!
I've tried to use 'self' 'this' instead. It follows to memory leak.
I've tried to add null terminator after getting data in handler function. ChatGPT said me about this way, but behavior still same.
Here:
std::string data = boost::asio::buffer_cast<const char*>(buf.data());
This is Undefined Behaviour as your are assuming that buf.data() points to a character sequence that is properly terminated with a NUL character, where you have absolutely no reason to assume this.
Besides you have UB because you pass a reference to buf, a local variable, which will by definition no longer be valid after do_read returns.
Thirdly, as Alan pointed out, you failed to copy the shared pointer (self) into the bound handler.
You're mixing many different ways to attack the problem of dealing with line-wise input. By far the easiest approach is to
use a dynamic buffer (like indeed, streambuf)
make it a member, because that's why you have shared_from_this in the first place
do not use prepare() because async_read_until knows how to do that (just like it does commit() for you)
do use consume() instead of doing getline /again/, even though async_read_until already tells you where the newline is.
Combining that:
struct TcpConnection : std::enable_shared_from_this<TcpConnection> {
TcpConnection(tcp::socket s) : socket_(std::move(s)) {}
void run() {
do_read();
}
private:
tcp::socket socket_;
asio::streambuf buf;
void handle_read(error_code ec, size_t n) {
std::cerr << "handle_read: " << ec.message() << std::endl;
if (!ec) {
std::string const line(boost::asio::buffer_cast<char const*>(buf.data()), n);
buf.consume(n);
std::cout << "Line: " << quoted(line) << std::endl;
do_read();
}
}
void do_read() {
async_read_until( //
socket_, buf, "\n",
boost::bind(&TcpConnection::handle_read, shared_from_this(), asio::placeholders::error,
asio::placeholders::bytes_transferred));
}
};
I'd probably simplify, avoiding copying and potentially allocations:
struct TcpConnection : std::enable_shared_from_this<TcpConnection> {
TcpConnection(tcp::socket s) : socket_(std::move(s)) {}
void run() {
buf_.reserve(8192); // optional, tune to taste
do_read();
}
private:
tcp::socket socket_;
std::string buf_;
void handle_read(error_code ec, size_t n) {
std::cerr << "handle_read: " << ec.message() << std::endl;
if (!ec) {
auto line = std::string_view(buf_).substr(0, n);
std::cout << "Line: " << quoted(line) << std::endl;
buf_.erase(0, n);
do_read();
}
}
void do_read() {
async_read_until( //
socket_, asio::dynamic_buffer(buf_), "\n",
boost::bind(&TcpConnection::handle_read, shared_from_this(), ph::error,
ph::bytes_transferred));
}
};
This still uses a dynamic buffer, but you don't have to do the extra hoops with istream which you apparently really don't need. For illustration purposes only, you could make the dynamic_string_buffer explicit:
void run() {
backing_.reserve(8192); // optional, tune to taste
do_read();
}
private:
tcp::socket socket_;
std::string backing_;
asio::dynamic_string_buffer<char, std::char_traits<char>, std::allocator<char>> buf_{backing_};
void handle_read(error_code ec, size_t n) {
std::cerr << "handle_read: " << ec.message() << std::endl;
if (!ec) {
auto line = std::string_view(backing_).substr(0, n);
std::cout << "Line: " << quoted(line) << std::endl;
buf_.consume(n); // look mom, it's a DynamiceBuffer all the same!
do_read();
}
}
void do_read() {
async_read_until( //
socket_, buf_, "\n",
boost::bind(&TcpConnection::handle_read, shared_from_this(), ph::error,
ph::bytes_transferred));
}
Note, I wouldn't do that, but it demonstrates you how both approaches use the same DynamicBuffer concept as you did with streambuf.
Live Demo
Live On Coliru
#include <boost/asio.hpp>
#include <boost/bind/bind.hpp>
#include <iomanip>
#include <iostream>
namespace asio = boost::asio;
namespace ph = asio::placeholders;
using asio::ip::tcp;
using boost::system::error_code;
struct TcpConnection : std::enable_shared_from_this<TcpConnection> {
TcpConnection(tcp::socket s) : socket_(std::move(s)) {}
void run() {
buf_.reserve(8192); // optional, tune to taste
do_read();
}
private:
tcp::socket socket_;
std::string buf_;
void handle_read(error_code ec, size_t n) {
if (n) {
auto line = std::string_view(buf_).substr(0, n - 1); // exclude '\n'
std::cout << socket_.remote_endpoint() << " Line: " << quoted(line) << std::endl;
buf_.erase(0, n);
}
if (!ec)
do_read();
else
std::cerr << "handle_read: n:" << n << " " << ec.message() << std::endl;
}
void do_read() {
async_read_until( //
socket_, asio::dynamic_buffer(buf_), "\n",
boost::bind(&TcpConnection::handle_read, shared_from_this(), ph::error,
ph::bytes_transferred));
}
};
struct Server {
Server(asio::any_io_executor ex) : acc(ex, {{}, 7878}) {}
void start() {
do_accept();
}
private:
tcp::acceptor acc;
void do_accept() {
acc.async_accept(make_strand(acc.get_executor()), [this](error_code ec, tcp::socket s) {
if (!ec) {
std::make_shared<TcpConnection>(std::move(s))->run();
do_accept();
}
});
}
};
int main() {
asio::io_context ioc;
Server srv(ioc.get_executor());
srv.start();
ioc.run();
}
With the client emulated by a very similar oneliner:
netcat 127.0.0.1 7878 -w0 < main.cpp
Prints:
127.0.0.1:54860 Line: "#include <boost/asio.hpp>"
127.0.0.1:54860 Line: "#include <boost/bind/bind.hpp>"
127.0.0.1:54860 Line: "#include <iomanip>"
127.0.0.1:54860 Line: "#include <iostream>"
127.0.0.1:54860 Line: "namespace asio = boost::asio;"
...
...
127.0.0.1:54860 Line: "int main() {"
127.0.0.1:54860 Line: " asio::io_context ioc;"
127.0.0.1:54860 Line: ""
127.0.0.1:54860 Line: " Server srv(ioc.get_executor());"
127.0.0.1:54860 Line: " srv.start();"
127.0.0.1:54860 Line: ""
127.0.0.1:54860 Line: " ioc.run();"
127.0.0.1:54860 Line: "}"
handle_read: n:0 End of file
Local demo showing multiple concurrent clients:

Asio async_read_until EOF Error in Asynchronous TCP Server

when I build it, and running server and then run client, that appear a error
error code = 2, error message = End of file
when I code synchronous tcp server it's work ok;
thanks
full client code
#include <boost/predef.h> // Tools to identify the os
#ifdef BOOST_OS_WINDOWS
#define _WIN32_WINNT 0x0501
#if _WIN32_WINNT <= 0x0502
#define BOOST_ASIO_DISABLE_TOCP
#define BOOST_ASIO_ENABLE_CANCELIO
#endif
#endif
#include <boost/asio.hpp>
#include <mutex>
#include <thread>
#include <memory>
#include <iostream>
#include <map>
using namespace boost;
typedef void(*Callback) (unsigned int request_id, const std::string& response, const system::error_code& ec);
struct Session{
Session(asio::io_service& ios, const std::string& raw_ip_address, unsigned short port_num, const std::string& request, unsigned int id, Callback callback) : m_sock(ios), m_ep(asio::ip::address::from_string(raw_ip_address),port_num), m_request(request), m_id(id), m_callback(callback), m_was_cancelled(false) {}
asio::ip::tcp::socket m_sock;
asio::ip::tcp::endpoint m_ep; // Remote endpoint
std::string m_request;
// streambuf where the response will be stored.
asio::streambuf m_response_buf;
std::string m_response; // Response represented as a string
system::error_code m_ec;
unsigned int m_id;
Callback m_callback;
bool m_was_cancelled;
std::mutex m_cancel_guard;
};
class AsyncTCPClient : public boost::asio::noncopyable {
public:
AsyncTCPClient(){
m_work.reset(new boost::asio::io_service::work(m_ios));
m_thread.reset(new std::thread([this](){
m_ios.run();
}));
}
void emulateLongComputationOp( unsigned int duration_sec, const std::string& raw_ip_address, unsigned short port_num, Callback callback, unsigned int request_id){
std::string request = "EMULATE_LONG_CALC_OP " + std::to_string(duration_sec) + "\n";
std::cout << "Request: " << request << std::endl;
std::shared_ptr<Session> session = std::shared_ptr<Session> (new Session(m_ios, raw_ip_address, port_num, request, request_id, callback));
session->m_sock.open(session->m_ep.protocol());
// active sessions list can be accessed from multiple thread, we guard it with a mutex to avoid data coruption
std::unique_lock<std::mutex> lock(m_active_sessions_guard);
m_active_sessions[request_id] = session;
lock.unlock();
session->m_sock.async_connect(session->m_ep, [this, session](const system::error_code& ec) {
if (ec.value() != 0) {
session->m_ec = ec;
onRequestComplete(session);
return;
}
std::unique_lock<std::mutex> cancel_lock(session->m_cancel_guard);
if (session->m_was_cancelled) {
onRequestComplete(session);
return;
}
asio::async_write(session->m_sock, asio::buffer(session->m_request), [this, session](const boost::system::error_code &ec, std::size_t bytes_transferred) {
if (ec.value() != 0) {
session->m_ec = ec;
onRequestComplete(session);
return;
}
std::unique_lock<std::mutex> cancel_lock(session->m_cancel_guard);
if (session->m_was_cancelled) {
onRequestComplete(session);
return;
}
asio::async_read_until(session->m_sock, session->m_response_buf, '\n',
[this, session](const boost::system::error_code &ec,
std::size_t bytes_transferred) {
if (ec.value() != 0) {
session->m_ec = ec;
} else {
std::istream strm(&session->m_response_buf);
std::getline(strm, session->m_response);
}
onRequestComplete(session);
});
});
});
};
// Cancels the request
void cancelRequest(unsigned int request_id){
std::unique_lock<std::mutex> lock(m_active_sessions_guard);
auto it = m_active_sessions.find(request_id);
if(it != m_active_sessions.end()){
std::unique_lock<std::mutex> cancel_lock(it->second->m_cancel_guard);
it->second->m_was_cancelled = true;
it->second->m_sock.cancel();
}
}
void close(){
// Destroy work object
m_work.reset(NULL);
// wait for the I/O thread tot exit
m_thread->join();
}
private:
void onRequestComplete(std::shared_ptr<Session> session){
// shutting down the connection, we don't care about the error code if function failed
boost::system::error_code ignored_ec;
session->m_sock.shutdown(asio::ip::tcp::socket::shutdown_both, ignored_ec);
// remove session from the map of active sessions
std::unique_lock<std::mutex> lock(m_active_sessions_guard);
auto it = m_active_sessions.find(session->m_id);
if(it != m_active_sessions.end()){
m_active_sessions.erase(it);
}
lock.unlock();
boost::system::error_code ec;
if(session->m_ec.value() == 0 && session->m_was_cancelled){
ec = asio::error::operation_aborted;
}else{
ec = session->m_ec;
}
session->m_callback(session->m_id, session->m_response, ec);
};
private:
asio::io_service m_ios;
std::map<int, std::shared_ptr<Session>> m_active_sessions;
std::mutex m_active_sessions_guard;
std::unique_ptr<boost::asio::io_service::work> m_work;
std::unique_ptr<std::thread> m_thread;
};
void handler(unsigned int request_id, const std::string& response, const system::error_code& ec){
if(ec.value() == 0){
std::cout << "Request #" << request_id << " has completed. Reponse: "<< response << std::endl;
}else if(ec == asio::error::operation_aborted){
std::cout << "Request #" << request_id << " has been cancelled by the user. " << std::endl;
}else{
std::cout << "Request #" << request_id << " failed! Error code = " << ec.value() << ". Error Message = " << ec.message() << std::endl;
}
return;
}
int main(){
try{
AsyncTCPClient client;
// emulate the user's behavior
client.emulateLongComputationOp(10, "127.0.0.1", 3333, handler, 1);
std::this_thread::sleep_for(std::chrono::seconds(60));
// another request with id 2
client.emulateLongComputationOp(11, "127.0.0.1", 3334, handler, 2);
// cancel request 1
client.cancelRequest(1);
std::this_thread::sleep_for(std::chrono::seconds(6));
// another request with id 3
client.emulateLongComputationOp(12, "127.0.0.1", 3335, handler, 3);
std::this_thread::sleep_for(std::chrono::seconds(15));
// exit the application
client.close();
}
catch(system::system_error &e){
std::cout << "Error occured! Error code = " << e.code() << ". Message: " << e.what();
return e.code().value();
}
return 0;
}
full server code
#include <boost/asio.hpp>
#include <thread>
#include <atomic>
#include <memory>
#include <iostream>
using namespace boost;
class Service {
public:
Service(std::shared_ptr<asio::ip::tcp::socket> sock) : m_sock(sock) {}
void StartHandling() {
asio::async_read_until(*m_sock.get(), m_request, '\n', [this](const boost::system::error_code& ec, std::size_t bytes_transferred){
onRequestReceived(ec, bytes_transferred);
});
std::istream is(&m_request);
std::string line;
std::getline(is, line);
std::cout << "m_request: " << line << std::endl;
}
private:
void onRequestReceived(const boost::system::error_code& ec, std::size_t bytes_transfered){
std::cout << "ec.value : " << ec.value() << std::endl;
if (ec.value() != 0){
std::cout << "Error occurred! Error code = " << ec.value() << ".Message: " << ec.message();
onFinish();
return;
}
// Process the request
asio::async_write(*m_sock.get(), asio::buffer(m_response), [this](const boost::system::error_code& ec, std::size_t bytes_transferred){
onResponseSent(ec, bytes_transferred);
});
}
void onResponseSent(const boost::system::error_code& ec, std::size_t bytes_transferred){
if(ec.value() != 0){
std::cout << "Error occurred! Error code = " << ec.value() << ". Message: " << ec.message();
}
onFinish();
}
// cleanup
void onFinish(){
delete this;
}
std::string ProcessingRequest(asio::streambuf& request){
// parse the request, process it and prepare the request
// Emulating CPU-consuming operations
int i = 0;
while (i != 1000){
i++;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
std::string response = "Response\n";
return response;
}
std::shared_ptr<asio::ip::tcp::socket> m_sock;
std::string m_response;
asio::streambuf m_request;
};
class Acceptor {
public:
Acceptor(asio::io_service& ios, unsigned short port_num) : m_ios(ios), m_acceptor(m_ios, asio::ip::tcp::endpoint(asio::ip::address_v4::any(), port_num)), m_isStopped(
false) {}
// Start accepting incoming connection request.
void Start(){
m_acceptor.listen();
InitAccept();
}
void Stop() {
m_isStopped.store(true);
}
private:
void InitAccept() {
std::shared_ptr<asio::ip::tcp::socket> sock(new asio::ip::tcp::socket(m_ios));
m_acceptor.async_accept(*sock.get(), [this, sock](const boost::system::error_code& error){
onAccept(error, sock);
});
}
void onAccept(const boost::system::error_code& ec, std::shared_ptr<asio::ip::tcp::socket> sock){
if(ec.value() == 0){
(new Service(sock))->StartHandling();
}else{
std::cout << "Error occurred! Error code = " << ec.value() << ". Message: " << ec.message();
}
// Init next accept operation if acceptor has not been stopped yet
if(!m_isStopped.load()){
InitAccept();
}else{
// free resources
m_acceptor.close();
}
}
private:
asio::io_service& m_ios;
asio::ip::tcp::acceptor m_acceptor;
std::atomic<bool> m_isStopped;
};
class Server{
public:
Server() {
m_work.reset(new asio::io_service::work(m_ios));
}
// Start the server
void Start(unsigned short port_num, unsigned int thread_pool_size){
assert(thread_pool_size > 0);
// Create and start Acceptor
acc.reset(new Acceptor(m_ios, port_num));
acc->Start();
// Create specified number of thread and add them to the pool
for(unsigned int i = 0; i < thread_pool_size; i++){
std::cout << "Thread " << i << " Running !";
std::unique_ptr<std::thread> th(new std::thread([this](){
m_ios.run();
}));
m_thread_pool.push_back(std::move(th));
}
}
// Stop the Server
void Stop(){
acc->Stop();
m_ios.stop();
for(auto& th : m_thread_pool){
th->join();
}
}
private:
asio::io_service m_ios;
std::unique_ptr<asio::io_service::work> m_work;
std::unique_ptr<Acceptor> acc;
std::vector<std::unique_ptr<std::thread>> m_thread_pool;
};
const unsigned int DEFAULT_THREAD_POOL_SIZE = 2;
int main(){
unsigned short port_num = 3333;
try{
Server srv;
unsigned int thread_pool_size = std::thread::hardware_concurrency() * 2;
if (thread_pool_size == 0){
thread_pool_size = DEFAULT_THREAD_POOL_SIZE;
}
srv.Start(port_num, thread_pool_size);
std::this_thread::sleep_for(std::chrono::seconds(60));
srv.Stop();
}
catch(system::system_error &e){
std::cout << "Error occurred! Error code = " << e.code() << ". Message: " << e.what();
}
return 0;
}
The server closes the connection after sending the (empty) response. That leads to EOF on the client, naturally. Just handle it.
There's loads of code smells
delete this; is an abomination, just make Service shared_from_this.
No need to use shared_ptrs other than that
When you use smart pointers, use them. Don't "convert to raw pointer" just to dereference (so *m_socket instead of *m_socket.get()).
In fact, there should be no need to use new, delete or get() in your code
You are accessing the m_request immediately after async_read_until which is too early,
it is a data race (so Undefined Behaviour)
it doesn't get the request, because async_read_until didn't complete yet.
So move that code into onRequestReceived at a minimum
It's pretty unnecessary to use an istream to read the line from the request when you already have bytes_transferred. I'd suggest
if (bytes_transferred) {
std::string line(m_request.data().data(), bytes_transferred - 1);
m_request.consume(bytes_transferred);
std::cout << "request: " << line << std::endl;
}
Or even:
std::cout << "request: ";
std::cout.write(asio::buffer_cast<char const*>(m_request.data()),
bytes_transferred - 1);
m_request.consume(bytes_transferred);
Or, if you indeed wanted to show the entire m_request, simply
std::cout << "m_request: " << &m_request << std::endl;
Note that read_until may read more than just including the delimiter; for your safety you might want to validate that no other data is trailing, or process it as well
Never switch on error_code::value(), that loses the error category, which is essential to interpret error codes.
Why unique_ptr for each thread? Just a deque<thread>:
while (thread_pool_size--)
m_thread_pool.emplace_back([this] { m_ios.run(); });
But see Should the exception thrown by boost::asio::io_service::run() be caught?
Why unique_ptr for acceptor?
Why a separate class for acceptor? It's not like the server allows more than 1
why a vector of threads anyways? Prefer boost::thread_group
why a manual thread pool? Prefer asio::thread_pool - which already uses the hardware_concurrency if available
In terms of review, the TCPAsyncClient looks like an attempt to implement async_result protocol. It misses the mark on many points. So I'll just point to something like how do i return the response back to caller asynchronously using a final callback dispatched from on_read handler? or How do I make this HTTPS connection persistent in Beast?. They have pretty similar interfaces (perhaps except for the cancellation, if I remember correctly).
Fixed/Return Demo
Here's the completed sample. It includes request parsing, so the server waits the actual amount of time requested.
I scaled all the times down 10x so it can complete online.
Client and server are in single source. Starting with:
./sotest&
./sotest client
wait
Completes both in 6 seconds (see screengrab below)
Live On Coliru
#include <boost/asio.hpp>
#include <boost/spirit/home/x3.hpp> // for request parsing
#include <iomanip>
#include <iostream>
#include <map>
#include <mutex>
#include <thread>
namespace asio = boost::asio;
using asio::ip::tcp;
using boost::system::error_code;
using namespace std::chrono_literals;
using std::this_thread::sleep_for;
/////// server //////////////////////////////////////////////////////////
struct Service : std::enable_shared_from_this<Service> {
Service(tcp::socket sock) : m_sock(std::move(sock)) {}
void StartHandling() {
async_read_until(
m_sock, asio::dynamic_buffer(m_request), '\n',
[this, self = shared_from_this()](error_code ec, size_t bytes) {
onRequestReceived(ec, bytes);
});
}
private:
void onRequestReceived(error_code ec, size_t /*bytes*/) {
std::cout << "onRequestReceived: " << ec.message() << std::endl;
if (ec)
return;
// Process the request
m_response = ProcessingRequest(m_request);
async_write(
m_sock, asio::buffer(m_response),
[this, self = shared_from_this()](error_code ec, size_t bytes) {
onResponseSent(ec, bytes);
});
}
void onResponseSent(error_code ec, size_t /*bytes*/) {
std::cout << "onResponseSent: " << ec.message() << std::endl;
}
std::string static ProcessingRequest(std::string request) {
std::cout << "request: " << request << std::endl;
// parse the request, process it and prepare the response
namespace x3 = boost::spirit::x3;
double value;
if (parse(request.begin(), request.end(),
"EMULATE_LONG_CALC_OP " >> x3::double_ >> "s" >> x3::eol >> x3::eoi,
value)) //
{
// Emulating time-consuming operation
sleep_for(1.0s * value);
return "Waited " + std::to_string(value) + "s\n";
}
return "Unknown request\n";
}
tcp::socket m_sock;
std::string m_request, m_response;
};
struct Server {
Server(asio::any_io_executor ex, uint16_t port_num)
: m_acceptor{ex, {{}, port_num}} {
m_acceptor.listen();
accept_loop();
}
void Stop() { m_acceptor.cancel(); }
private:
void accept_loop() {
m_acceptor.async_accept([this](error_code ec, tcp::socket sock) {
std::cout << "OnAccept: " << ec.message() << std::endl;
if (!ec) {
std::make_shared<Service>(std::move(sock))->StartHandling();
accept_loop();
}
//m_acceptor.close();
});
}
tcp::acceptor m_acceptor;
};
void server(uint16_t port) try {
asio::thread_pool io;
Server srv{io.get_executor(), port};
sleep_for(6s);
srv.Stop();
io.join();
} catch (std::exception const& e) {
std::cout << "Exception: " << e.what() << std::endl;
}
/////// client //////////////////////////////////////////////////////////
struct RequestOp : public std::enable_shared_from_this<RequestOp> {
using Callback = std::function<void( //
unsigned /*request_id*/, std::string_view /*response*/, error_code)>;
RequestOp(asio::any_io_executor ex, const std::string& raw_ip_address,
uint16_t port_num, std::string request, unsigned id,
Callback callback)
: m_ep(asio::ip::address::from_string(raw_ip_address), port_num)
, m_sock(ex, m_ep.protocol())
, m_request(std::move(request))
, m_id(id)
, m_callback(callback) {}
void Run() {
// assumed on logical strand
m_sock.async_connect(
m_ep, [this, self = shared_from_this()](error_code ec) {
if ((m_ec = ec) || m_was_cancelled)
return onComplete();
asio::async_write(m_sock, asio::buffer(m_request),
[this, self = shared_from_this()](
error_code ec, size_t /*bytes*/) {
onRequestWritten(ec);
});
});
}
void Cancel() {
m_was_cancelled = true;
dispatch(m_sock.get_executor(), [self=shared_from_this()]{ self->doCancel(); });
}
private:
void doCancel() {
m_sock.cancel();
}
void onRequestWritten(error_code ec) {
if ((m_ec = ec) || m_was_cancelled)
return onComplete();
asio::async_read_until(
m_sock, asio::dynamic_buffer(m_response), '\n',
[this, self = shared_from_this()](error_code ec, size_t bytes) {
onResponseReceived(ec, bytes);
});
}
void onResponseReceived(error_code ec, size_t /*bytes*/) {
if ((m_ec = ec) || m_was_cancelled)
return onComplete();
if (!m_response.empty())
m_response.resize(m_response.size() - 1); // drop '\n'
onComplete();
}
void onComplete() {
// shutting down the connection, we don't care about the error code
// if function failed
error_code ignored_ec;
m_sock.shutdown(tcp::socket::shutdown_both, ignored_ec);
if(!m_ec && m_was_cancelled){
m_ec = asio::error::operation_aborted;
}
m_callback(m_id, m_response, m_ec);
}
tcp::endpoint m_ep; // Remote endpoint
tcp::socket m_sock;
std::string m_request;
std::string m_response; // Response represented as a string
error_code m_ec;
unsigned m_id;
Callback m_callback;
std::atomic_bool m_was_cancelled{false};
};
class AsyncTCPClient {
public:
AsyncTCPClient(asio::any_io_executor ex) : m_executor(ex) {}
using Duration = std::chrono::steady_clock::duration;
size_t emulateLongCalcOp(Duration delay, std::string const& raw_ip_address,
uint16_t port_num, RequestOp::Callback callback) {
auto request =
"EMULATE_LONG_CALC_OP " + std::to_string(delay / 1.0s) + "s\n";
std::cout << "Request: " << request << std::flush;
auto const request_id = m_nextId++;
auto session = std::make_shared<RequestOp>(
make_strand(m_executor), //
raw_ip_address, port_num, request, request_id, callback);
{
// active sessions list can be accessed from multiple thread, we
// guard it with a mutex to avoid data coruption
std::unique_lock lock(m_active_sessions_guard);
auto [_,ok] = m_pending_ops.emplace(request_id, session);
assert(ok); // duplicate request_id?
// optionally: garbage collect completed sessions
std::erase_if(m_pending_ops,
[](auto& kv) { return kv.second.expired(); });
};
session->Run();
return request_id;
}
// Cancels the request
void cancelRequest(unsigned request_id) {
std::unique_lock lock(m_active_sessions_guard);
if (auto session = m_pending_ops[request_id].lock())
session->Cancel();
}
private:
using PendingOp = std::weak_ptr<RequestOp>;
asio::any_io_executor m_executor;
std::mutex m_active_sessions_guard;
size_t m_nextId = 1;
std::map<int, PendingOp> m_pending_ops;
};
void handler(unsigned request_id, std::string_view response, error_code ec) {
std::cout << "Request #" << request_id << " ";
if (!ec.failed())
std::cout << "Response: " << std::quoted(response) << std::endl;
else if (ec == asio::error::operation_aborted)
std::cout << "Cancelled" << std::endl;
else
std::cout << ec.message() << std::endl;
}
void client(uint16_t port) try {
asio::thread_pool io;
{
AsyncTCPClient client(io.get_executor());
auto id1 = client.emulateLongCalcOp(4s, "127.0.0.1", port, handler);
auto id2 = client.emulateLongCalcOp(1100ms, "127.0.0.1", port, handler);
auto id3 = client.emulateLongCalcOp(3500ms, "127.0.0.1", port, handler);
// cancel request 1
sleep_for(3s);
client.cancelRequest(id1);
sleep_for(1200ms);
client.cancelRequest(id2); // no effect, already completed
client.cancelRequest(id3); // no effect, already completed
// exit the application
}
io.join();
} catch (std::exception const& e) {
std::cout << "Exception: " << e.what() << std::endl;
}
/////// main //////////////////////////////////////////////////////////
int main(int argc, char**) {
if (argc > 1)
client(3333);
else
server(3333);
}
Prints client:
Request: EMULATE_LONG_CALC_OP 4.000000s
Request: EMULATE_LONG_CALC_OP 1.100000s
Request: EMULATE_LONG_CALC_OP 3.500000s
Request #2 Response: "Waited 1.100000s"
Request #1 Cancelled
Request #3 Response: "Waited 3.500000s"
Prints server:
OnAccept: Success
OnAccept: Success
onRequestReceived: Success
request: EMULATE_LONG_CALC_OP 1.100000s
onRequestReceived: Success
request: EMULATE_LONG_CALC_OP 4.000000s
OnAccept: Success
onRequestReceived: Success
request: EMULATE_LONG_CALC_OP 3.500000s
onResponseSent: Success
onResponseSent: Success
onResponseSent: Success
OnAccept: Operation canceled

Using Boost asio to receive commands and execute them

I'm trying to make a boost server, which will receive commands and do certain things. Now I would like to create a function, that will receive a file and save it to a specific location. The problem is with serialization. I don't know how can I recognize a command in a stream in an efficint way. I tried with boost::asio::read_until. And actually my code works. First file is being sent and received perfectly. But I am getting an error (The file handle supplied is not valid) when client sends second file. I would be very grateful for every advice. Thanks in an advance!
bool Sync::start_server() {
boost::asio::streambuf request_buf;
std::istream request_stream(&request_buf);
boost::system::error_code error;
try {
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), conf->def_port));
acceptor.accept(socket); //socket is a member of class Sync
while (true)
{
error.clear();
size_t siz = boost::asio::read_until(socket, request_buf, "\n\n");
std::cout << "request size:" << request_buf.size() << "\n";
string command;
string parameter;
size_t data_size = 0;
request_stream >> command;
request_stream >> parameter;
request_stream >> data_size;
request_buf.consume(siz);//And also this
//cut filename from path below
size_t pos = parameter.find_last_of('\\');
if (pos != std::string::npos)
parameter = parameter.substr(pos + 1);
//cut filename from path above
//command = "save";// constant until I make up other functions
//execute(command, parameter, data_size);
save(parameter,data_size);//parameter is filename
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
And function to save file to a hard drive:
bool Sync::save(string filename, size_t filesize) {
boost::array<char, 1024> buf;
cout << "filesize is" << filesize;
size_t data_size = 0;
boost::system::error_code error;
std::ofstream output_file(filename.c_str(), std::ios_base::binary);
if (!output_file)
{
std::cout << "failed to open " << filename << std::endl;
return __LINE__;
}
while (true) {
size_t len = socket.read_some(boost::asio::buffer(buf), error);
if (len>0)
output_file.write(buf.c_array(), (std::streamsize)len);
if (output_file.tellp() == (std::fstream::pos_type)(std::streamsize)filesize)
{
output_file.close();
buf.empty();
break; // file was received
}
if (error)
{
socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
socket.close(error);
output_file.close();
buf.empty();
break;//an error occured
}
}
}
read_until might read beyond the delimiter (therefore request_buf.size() can be more than siz). This is a conceptual problem when you implement save because you read data_size bytes from the socket, which ignores any data already in request_buf
These things are code smells:
if (output_file.tellp() == (std::fstream::pos_type)(std::streamsize)filesize) {
(never use C-style casts). And
return __LINE__; // huh? just `true` then
And
buf.empty();
(That has no effect whatsoever).
I present here three versions:
First Cleanup
Simplify (using tcp::iostream)
Simplify! (assuming more things about the request format)
First Cleanup
Here's a reasonable cleanup:
Live On Coliru
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <iostream>
#include <fstream>
namespace ba = boost::asio;
using ba::ip::tcp;
struct Conf {
int def_port = 6767;
} s_config;
struct Request {
std::string command;
std::string parameter;
std::size_t data_size = 0;
std::string get_filename() const {
// cut filename from path - TODO use boost::filesystem::path instead
return parameter.substr(parameter.find_last_of('\\') + 1);
}
friend std::istream& operator>>(std::istream& is, Request& req) {
return is >> req.command >> req.parameter >> req.data_size;
}
};
struct Sync {
bool start_server();
bool save(Request const& req, boost::asio::streambuf& request_buf);
ba::io_service& io_service;
tcp::socket socket{ io_service };
Conf const *conf = &s_config;
};
bool Sync::start_server() {
boost::asio::streambuf request_buf;
boost::system::error_code error;
try {
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), conf->def_port));
acceptor.accept(socket); // socket is a member of class Sync
while (true) {
error.clear();
std::string req_txt;
{
char const* delim = "\n\n";
size_t siz = boost::asio::read_until(socket, request_buf, delim, error);
// correct for actual request siz
auto b = buffers_begin(request_buf.data()),
e = buffers_end(request_buf.data());
auto where = std::search(b, e, delim, delim+strlen(delim));
siz = where==e
? std::distance(b,e)
: std::distance(b,where)+strlen(delim);
std::copy_n(b, siz, back_inserter(req_txt));
request_buf.consume(siz); // consume only the request text bits from the buffer
}
std::cout << "request size:" << req_txt.size() << "\n";
std::cout << "Request text: '" << req_txt << "'\n";
Request req;
{
std::istringstream request_stream(req_txt);
request_stream.exceptions(std::ios::failbit);
request_stream >> req;
}
save(req, request_buf); // parameter is filename
}
} catch (std::exception &e) {
std::cerr << "Error parsing request: " << e.what() << std::endl;
}
return false;
}
bool Sync::save(Request const& req, boost::asio::streambuf& request_buf) {
auto filesize = req.data_size;
std::cout << "filesize is: " << filesize << "\n";
{
std::ofstream output_file(req.get_filename(), std::ios::binary);
if (!output_file) {
std::cout << "failed to open " << req.get_filename() << std::endl;
return true;
}
// deplete request_buf
if (request_buf.size()) {
if (request_buf.size() < filesize)
{
filesize -= request_buf.size();
output_file << &request_buf;
}
else {
// copy only filesize already available bytes
std::copy_n(std::istreambuf_iterator<char>(&request_buf), filesize,
std::ostreambuf_iterator<char>(output_file));
filesize = 0;
}
}
while (filesize) {
boost::array<char, 1024> buf;
boost::system::error_code error;
std::streamsize len = socket.read_some(boost::asio::buffer(buf), error);
if (len > 0)
{
output_file.write(buf.c_array(), len);
filesize -= len;
}
if (error) {
socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, error); // ignore error
socket.close(error);
break; // an error occured
}
}
} // closes output_file
return false;
}
int main() {
ba::io_service svc;
Sync s{svc};
s.start_server();
svc.run();
}
Prints with a client like echo -ne "save test.txt 12\n\nHello world\n" | netcat 127.0.0.1 6767:
request size:18
Request text: 'save test.txt 12
'
filesize is: 12
request size:1
Request text: '
'
Error parsing request: basic_ios::clear: iostream error
SIMPLIFY
However, since everything is synchronous, why not just use tcp::iostream socket;. That would make start_server look like this:
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), conf->def_port));
acceptor.accept(*socket.rdbuf());
while (socket) {
std::string req_txt, line;
while (getline(socket, line) && !line.empty()) {
req_txt += line + "\n";
}
std::cout << "request size:" << req_txt.size() << "\n";
std::cout << "Request text: '" << req_txt << "'\n";
Request req;
if (std::istringstream(req_txt) >> req)
save(req);
}
And save even simpler:
void Sync::save(Request const& req) {
char buf[1024];
size_t remain = req.data_size, n = 0;
for (std::ofstream of(req.get_filename(), std::ios::binary);
socket.read(buf, std::min(sizeof(buf), remain)), (n = socket.gcount());
remain -= n)
{
if (!of.write(buf, n))
break;
}
}
See it Live On Coliru
When tested with
for f in test{a..z}.txt; do (echo -ne "save $f 12\n\nHello world\n"); done | netcat 127.0.0.1 6767
that prints:
request size:18
Request text: 'save testa.txt 12
'
request size:18
Request text: 'save testb.txt 12
'
[... snip ...]
request size:18
Request text: 'save testz.txt 12
'
request size:0
Request text: ''
Even Simpler
If you know that the request is a single line, or whitespace is not significant:
struct Sync {
void run_server();
void save(Request const& req);
private:
Conf const *conf = &s_config;
tcp::iostream socket;
};
void Sync::run_server() {
ba::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), conf->def_port));
acceptor.accept(*socket.rdbuf());
for (Request req; socket >> std::noskipws >> req; std::cout << req << " handled\n")
save(req);
}
void Sync::save(Request const& req) {
char buf[1024];
size_t remain = req.data_size, n = 0;
for (std::ofstream of(req.get_filename(), std::ios::binary);
socket.read(buf, std::min(sizeof(buf), remain)), (n = socket.gcount());
remain -= n)
{
if (!of.write(buf, n)) break;
}
}
int main() {
Sync().run_server();
}
That's the entire program in ~33 lines of code. See it Live On Coliru, printing:
Request {"save" "testa.txt"} handled
Request {"save" "testb.txt"} handled
Request {"save" "testc.txt"} handled
[... snip ...]
Request {"save" "testy.txt"} handled
Request {"save" "testz.txt"} handled

Using boost::asio stackless coroutines to download several files via HTTP

I translated the example from Programming in Lua by Roberto Ierusalimschy for downloading several files via HTTP using coroutines to C++ using boost::asio and stackful coroutines. Here is the code:
#include <iostream>
#include <chrono>
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
using namespace std;
using namespace boost::asio;
io_service ioService;
void download(const string& host, const string& file, yield_context& yield)
{
clog << "Downloading " << host << file << " ..." << endl;
size_t fileSize = 0;
boost::system::error_code ec;
ip::tcp::resolver resolver(ioService);
ip::tcp::resolver::query query(host, "80");
auto it = resolver.async_resolve(query, yield[ec]);
ip::tcp::socket socket(ioService);
socket.async_connect(*it, yield[ec]);
ostringstream req;
req << "GET " << file << " HTTP/1.0\r\n\r\n";
write(socket, buffer(req.str()));
while (true)
{
char data[8192];
size_t bytesRead = socket.async_read_some(buffer(data), yield[ec]);
if (0 == bytesRead) break;
fileSize += bytesRead;
}
socket.shutdown(ip::tcp::socket::shutdown_both);
socket.close();
clog << file << " size: " << fileSize << endl;
}
int main()
{
auto timeBegin = chrono::high_resolution_clock::now();
vector<pair<string, string>> resources =
{
{"www.w3.org", "/TR/html401/html40.txt"},
{"www.w3.org", "/TR/2002/REC-xhtml1-20020801/xhtml1.pdf"},
{"www.w3.org", "/TR/REC-html32.html"},
{"www.w3.org", "/TR/2000/REC-DOM-Level-2-Core-20001113/DOM2-Core.txt"},
};
for(const auto& res : resources)
{
spawn(ioService, [&res](yield_context yield)
{
download(res.first, res.second, yield);
});
}
ioService.run();
auto timeEnd = chrono::high_resolution_clock::now();
clog << "Time: " << chrono::duration_cast<chrono::milliseconds>(
timeEnd - timeBegin).count() << endl;
return 0;
}
Now I'm trying to translate the code to use stackless coroutines from boost::asio but the documentation is not enough for me to grok how to organize the code in such way to be able to do it. Can someone provide solution for this?
Here is a solution based on stackless coroutines as provided by Boost. Given that they are essentially a hack, I would not consider the solution particularly elegant. It could probably be done better with C++20, but I think that would be outside the scope of this question.
#include <functional>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/yield.hpp>
using boost::asio::async_write;
using boost::asio::buffer;
using boost::asio::error::eof;
using boost::system::error_code;
using std::placeholders::_1;
using std::placeholders::_2;
/**
* Stackless coroutine for downloading file from host.
*
* The lifetime of the object is limited to one () call. After that,
* the object will be copied and the old object is discarded. For this
* reason, the socket_ and resolver_ member are stored as shared_ptrs,
* so that they can live as long as there is a live copy. An alternative
* solution would be to manager these objects outside of the coroutine
* and to pass them here by reference.
*/
class downloader : boost::asio::coroutine {
using socket_t = boost::asio::ip::tcp::socket;
using resolver_t = boost::asio::ip::tcp::resolver;
public:
downloader(boost::asio::io_service &service, const std::string &host,
const std::string &file)
: socket_{std::make_shared<socket_t>(service)},
resolver_{std::make_shared<resolver_t>(service)}, file_{file},
host_{host} {}
void operator()(error_code ec = error_code(), std::size_t length = 0,
const resolver_t::results_type &results = {}) {
// Check if the last yield resulted in an error.
if (ec) {
if (ec != eof) {
throw boost::system::system_error{ec};
}
}
// Jump to after the previous yield.
reenter(this) {
yield {
resolver_t::query query{host_, "80"};
// Use bind to skip the length parameter not provided by async_resolve
auto result_func = std::bind(&downloader::operator(), this, _1, 0, _2);
resolver_->async_resolve(query, result_func);
}
yield socket_->async_connect(*results, *this);
yield {
std::ostringstream req;
req << "GET " << file_ << " HTTP/1.0\r\n\r\n";
async_write(*socket_, buffer(req.str()), *this);
}
while (true) {
yield {
char data[8192];
socket_->async_read_some(buffer(data), *this);
}
if (length == 0) {
break;
}
fileSize_ += length;
}
std::cout << file_ << " size: " << fileSize_ << std::endl;
socket_->shutdown(socket_t::shutdown_both);
socket_->close();
}
// Uncomment this to show progress and to demonstrace interleaving
// std::cout << file_ << " size: " << fileSize_ << std::endl;
}
private:
std::shared_ptr<socket_t> socket_;
std::shared_ptr<resolver_t> resolver_;
const std::string file_;
const std::string host_;
size_t fileSize_{};
};
int main() {
auto timeBegin = std::chrono::high_resolution_clock::now();
try {
boost::asio::io_service service;
std::vector<std::pair<std::string, std::string>> resources = {
{"www.w3.org", "/TR/html401/html40.txt"},
{"www.w3.org", "/TR/2002/REC-xhtml1-20020801/xhtml1.pdf"},
{"www.w3.org", "/TR/REC-html32.html"},
{"www.w3.org", "/TR/2000/REC-DOM-Level-2-Core-20001113/DOM2-Core.txt"},
};
std::vector<downloader> downloaders{};
std::transform(resources.begin(), resources.end(),
std::back_inserter(downloaders), [&](auto &x) {
return downloader{service, x.first, x.second};
});
std::for_each(downloaders.begin(), downloaders.end(),
[](auto &dl) { dl(); });
service.run();
} catch (std::exception &e) {
std::cerr << "exception: " << e.what() << "\n";
}
auto timeEnd = std::chrono::high_resolution_clock::now();
std::cout << "Time: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(timeEnd -
timeBegin)
.count()
<< std::endl;
return 0;
}
Compiled with Boost 1.72 and g++ -lboost_coroutine -lpthread test.cpp. Example output:
$ ./a.out
/TR/REC-html32.html size: 606
/TR/html401/html40.txt size: 629
/TR/2002/REC-xhtml1-20020801/xhtml1.pdf size: 115777
/TR/2000/REC-DOM-Level-2-Core-20001113/DOM2-Core.txt size: 229699
Time: 1644
The log line at the end of the () function can be uncommented to demonstrate the interleaving of the downloads.

std::string copy constructor seg fault

this happens when constructing a structure, my code:
http://wklej.org/hash/c42680a7f9d/txt/ at this line: http://wklej.org/hash/5fefcecc371/txt/ backtrace: http://wklej.org/id/451070/txt/
any help is apperciated
Sorry, i fail to copy the code here so i post it at any other site ;(
Use a debugger and get a stack trace.
The problem is almost certainly passing a bad C string into the std::string constructor. Perhaps the pointer is invalid or the C string does not terminate and the constructor reads off into protected memory.
But without more information I can't tell what the bug is. The debugger should point it out immediately.
Also, your Socket holds a pointer but only defines a constructor and destructor. You also need a copy constructor and an assignment operator. If those two operations aren't supposed to happen then define them as private with no implementation.
Also, I see from your backtrace that this is an old version of GCC. It's possible that this version does not have the fixes that make std::string safe to use in multi-threaded programs. I don't know when it was fixed but some older versions of the libstdc++ library didn't lock the reference counts on the string and could crash when different threads would free the string memory while also writing to it.
I've placed your code here in order to be able to edit it:
#ifdef _WIN32
#define _WIN32_WINNT 0x0501
#endif
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/noncopyable.hpp>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <sstream>
#include <string>
#ifdef assert
#undef assert
#endif
#define assert(x)\
if (!x) { \
std::clog << "[Error - __FUNCTION__] Asseration to: " << #x << " failed. line: " << __LINE__ << ", file: " << __FILE__ << "." << std::endl;\
std::exit(100);\
}
template<typename _Tp>
inline std::string toString(_Tp __p)
{
std::stringstream ss;
ss << __p;
std::string ret;
ss >> ret;
return ret;
}
struct Proxy;
typedef std::vector<Proxy*> ProxyVec;
struct Proxy
{
std::string name;
uint32_t port;
};
ProxyVec loadProxies(const std::string& fileName)
{
std::FILE *f = fopen(fileName.c_str(), "r");
if (!f) {
std::clog << "[Error - loadProxies] Cannot open: " << fileName << "." << std::endl;
delete f;
f = NULL;
return ProxyVec();
}
char buffer[1024];
ProxyVec ret;
int32_t __n = 0;
while (fgets(buffer, sizeof(buffer), f)) {
++__n;
std::string str(buffer);
if (str.find("\n") != std::string::npos)
str = str.substr(0, str.length()-1);
if (str.find("\r") != std::string::npos)
str = str.substr(0, str.length()-1);
size_t sep = str.rfind(":");
if (sep == std::string::npos) {
std::clog << "[Error - loadProxies] Cannot load proxy #" << __n << "." << std::endl;
continue;
}
std::string hostname = str.substr(0, sep);
uint32_t port = static_cast<uint32_t>(std::atoi(str.substr(sep+1, str.length()-sep).c_str()));
std::clog << "Loading proxy: " << hostname << ":" << port << "." << std::endl;
Proxy proxy;
proxy.name = hostname;
proxy.port = port;
ret.push_back(&proxy);
}
std::clog << "Loaded: " << __n << " proxies." << std::endl;
return ret;
}
class Socket
{
public:
Socket(boost::asio::io_service& service, const std::string& host,
const std::string& port, const std::string& targetHost, const std::string& targetPort);
~Socket();
void write(const std::string& message);
std::string receivedData() const;
bool connected() const;
void receive();
void handle();
private:
struct SocketData
{
SocketData(boost::asio::io_service& service, const std::string& _host, const std::string& _port,
const std::string& _targetHost, const std::string& _targetPort):
socket(service),
write(service),
read(service),
resolver(service),
buffers(),
host(_host),
port(_port),
targetHost(_targetHost),
targetPort(_targetPort),
connected(false)
{
}
boost::asio::ip::tcp::socket socket;
boost::asio::io_service::strand write, read;
boost::asio::ip::tcp::resolver resolver;
boost::array<char, 1024> buffers;
std::string host, port;
std::string targetHost, targetPort;
bool connected;
};
// FIXME: Use shared_ptr instead.
SocketData* d;
protected:
//handle resolve func
void handle_resolve(const boost::system::error_code&,
boost::asio::ip::tcp::resolver::iterator);
//handle connection func
void handle_connect(const boost::system::error_code&,
boost::asio::ip::tcp::resolver::iterator);
//handle write func
void handle_write(const boost::system::error_code&, size_t);
//handle read func
void handle_read(const boost::system::error_code&, size_t);
private:
void connectionThread();
};
Socket::Socket(boost::asio::io_service& service, const std::string& host, const std::string& port,
const std::string& targetHost, const std::string& targetPort)
: d(new SocketData(service, host, port, targetHost, targetPort))
{
boost::thread thread(boost::bind(&Socket::connectionThread, this));
// FIXME: This function is blocking. The constructur will never exit.
// Use thread_group.join_all() as last line in main() instead.
thread.join();
}
Socket::~Socket()
{
d->socket.close();
delete d;
d = NULL;
}
void Socket::connectionThread()
{
if (!d)
return;
if (d->connected)
return;
boost::asio::ip::tcp::resolver::query query(d->host, d->port);
d->resolver.async_resolve(query,
boost::bind(&Socket::handle_resolve, this,
boost::asio::placeholders::error,
boost::asio::placeholders::iterator));
}
void Socket::handle_resolve(const boost::system::error_code& e,
boost::asio::ip::tcp::resolver::iterator ep_iter)
{
if (!e) {
boost::asio::ip::tcp::endpoint iter = *ep_iter;
d->socket.async_connect(iter,
boost::bind(&Socket::handle_connect, this,
boost::asio::placeholders::error, ++ep_iter));
} else {
std::clog << "[Error - Socket::handle_resolve] " << e.message() << "." << std::endl;
}
}
void Socket::handle_connect(const boost::system::error_code& e,
boost::asio::ip::tcp::resolver::iterator ep_iter)
{
if (!e) {
std::cout << "[Notice - Socket::handle_connect] Connected to host." << std::endl;
d->connected = true;
write("CONNECT " + d->targetHost + ":" + d->targetPort + " HTTP/1.1\r\n\r\n");
receive();
} else if (ep_iter != boost::asio::ip::tcp::resolver::iterator()) {
d->socket.close();
boost::asio::ip::tcp::endpoint ep = *ep_iter;
d->socket.async_connect(ep,
boost::bind(&Socket::handle_connect, this,
boost::asio::placeholders::error, ++ep_iter));
} else {
std::clog << "[Error - Server::handle_connect] " << e.message() << "." << std::endl;
}
}
void Socket::handle_write(const boost::system::error_code& e,
size_t bytes)
{
assert(!e || bytes < 0);
}
void Socket::handle_read(const boost::system::error_code& e,
size_t bytes)
{
assert(!e || bytes < 0);
std::cout << receivedData() << std::endl;
receive();
}
void Socket::write(const std::string& message)
{
boost::asio::async_write(d->socket, boost::asio::buffer(message),
d->write.wrap(
boost::bind(&Socket::handle_write, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
}
std::string Socket::receivedData() const
{
return std::string(d->buffers.data());
}
void Socket::receive()
{
d->socket.async_read_some(boost::asio::buffer(d->buffers),
d->read.wrap(
boost::bind(&Socket::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
}
void Socket::handle()
{
assert(!d->targetHost.empty());
assert(!d->targetPort.empty());
std::string str(d->buffers.data());
std::clog << "Received: " << str << "." << std::endl;
if (str.substr(0, 4) == "PING")
write("PO" + str.substr(2) + "\r\n");
else if (str.find("MODE") != std::string::npos)
write("JOIN #OTland\r\n");
else if (str.find("JOIN") != std::string::npos)
write("PRIVMSG #OTland :Hello\r\n");
}
bool Socket::connected() const
{
return d->connected;
}
void handler(const std::string& fileName, const std::string& host, const std::string& port, uint32_t threads);
int main(int argc, char **argv)
{
if (argc < 5) {
std::clog << "[Error - main] Usage: " << argv[0] << " <proxy_file> <host> <port> <threads>" << std::endl;
return 1;
}
std::string file(argv[1]);
std::string host(argv[2]);
std::string port(argv[3]);
uint32_t threads = static_cast<uint32_t>(std::atoi(argv[4]));
if (!threads)
threads = 1;
for (uint32_t __i = 0; __i < threads; ++__i)
// FIXME: Use the thread.join() there.
handler(file, host, port, threads);
}
typedef std::vector<Socket*> SocketVec;
void handler(const std::string& fileName, const std::string& host,
const std::string& port, uint32_t threads)
{
assert(!fileName.empty());
assert(!host.empty());
assert(!port.empty());
ProxyVec proxies = loadProxies(fileName);
assert(proxies.size());
SocketVec sockets;
for (ProxyVec::const_iterator it = proxies.begin(); it != proxies.end(); ++it) {
boost::asio::io_service io;
Socket socket(io, (*it)->name, toString((*it)->port), host, port);
// FIXME: socket is a local variable and it's address is invalid outside
// this loop -> memory leak -> seg-fault.
sockets.push_back(&socket);
}
for (SocketVec::const_iterator it = sockets.begin(); it != sockets.end(); ++it) {
if (!(*it)->connected())
continue;
(*it)->handle();
}
// FIXME: I'm not sure if I understand this architecture. A new thread is
// started with this function as handler ? Who waits until this new created
// thread is finished ? Where is the join() ?
(void) new boost::thread(boost::bind(handler, fileName, host, port, threads));
}
I don't think this is your only problem, but in this snippet in handler, you're creating your Socket objects on the stack. Each Socket object that you create will be destroyed at the end of the for loop. This means that the objects in the sockets vector are invalid objects. Doing this may also corrupt the memory heap enough to produce the error that you're seeing.
SocketVec sockets;
for (ProxyVec::const_iterator it = proxies.begin(); it != proxies.end(); ++it) {
boost::asio::io_service io;
Socket socket(io, (*it)->name, toString((*it)->port), host, port);
sockets.push_back(&socket);
}
Change this to:
SocketVec sockets;
for (ProxyVec::const_iterator it = proxies.begin(); it != proxies.end(); ++it) {
boost::asio::io_service io;
Socket* socket = new Socket(io, (*it)->name, toString((*it)->port), host, port);
sockets.push_back(socket);
}
This answer was originally a comment to Dan's answer, but after looking at your code I felt compelled to give a full answer. You really need to take a closer look at the Boost.Asio examples and understand how they work. Pay special attention to the asynchronous examples, it doesn't look like you've grasped the concepts of object lifetime and how the handlers work. Particularly, you should master single threaded programs before jumping into multiple threads. When you've conquered that, you should use a thread pool invoking io_service::run instead of an io_service per thread. It will ultimately make your program's logic easier to understand.
You also should look into valgrind, there's a slew of errors in your code like this one:
==19853== Invalid read of size 4
==19853== at 0x10000D0E4: handler(std::string const&, std::string const&, std::string const&, unsigned int) (in ./a.out)
==19853== by 0x10000D5E6: main (in ./a.out)
==19853== Address 0x7fff5fbff398 is not stack'd, malloc'd or (recently) free'd