I've been trying to use boost in order to asynchronously read from a serial_port until it puts incoming data into a buffer that I have specified. However, when I call:
boost::asio::async_read(*port_,
*readBuf_,
boost::bind(&Serial::async_read_handler,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
it calls async_read_handler immediately, which triggers the code inside my read_handler.
void Serial::async_read_handler(const boost::system::error_code &e,
std::size_t bytes_read)
{
namespace basio = boost::asio;
if(!e)
{
std::cout << e.message() << std::endl;
std::cout << bytes_read << std::endl;
if(bytes_read <= 0)
{
std::string read = *basio::buffer_cast<std::string*>(*readBuf_);
std::cout << read << std::endl;
basio::async_read(*port_,
*readBuf_,
boost::bind(&Serial::async_read_handler,
this,
basio::placeholders::error,
basio::placeholders::bytes_transferred));
}
else
{
std::string read = *basio::buffer_cast<std::string*>(*readBuf_);
std::cout << read << std::endl;
std::cout << "Bytes read: " << bytes_read << std::endl;
}
}
else
{
std::cerr << e.message() << std::endl;
}
}
I first call a thread to run async_read in my main
int main()
{
boost::asio::io_service io;
Serial::Serial serial(PORT, &io);
if(!serial.is_open())
{
serial.open(PORT);
}
serial.async_read();
io.run();
return 0;
}
I want the async_read call to wait until it has read data from the buffer to call the handler. How can I do this? Thanks.
Related
I'm trying to use serial_port of asio(standlone) to get data from a device. But I can't read even a byte.
I post a screenshoot of the example usage found from documentation website as below :
enter image description here
enter image description here
https://think-async.com/Asio/asio-1.24.0/doc/asio/reference/basic_serial_port/async_read_some.html
There are 3 question I have:
What is the specific type of the first parameter of async_read_some? std::vector<uint8_t>, for example?
How to bind a class member function as the callback/handler?
Is it right that we just make sure io_context.run() run on a background thread and call async_read_some just once?
I'll appreciate it if you guys could help me check out my code or give some advice.
class NmeaSource {
public:
explicit NmeaSource(std::shared_ptr<asio::io_context> io, const nlohmann::json& settings);
void parse();
void handle(const asio::error_code& error, // Result of operation.
std::size_t bytes_transferred // Number of bytes read.
);
private:
void open();
std::string m_port;
int m_baudrate;
std::shared_ptr<asio::io_context> m_io;
std::shared_ptr<asio::serial_port> m_serial;
unsigned char m_readBuffer[1024];
};
NmeaSource::NmeaSource(std::shared_ptr<asio::io_context> io, const nlohmann::json& settings)
:m_io(io)
{
try {
setPort(settings.at("port"));
setBaudrate(settings.at("baudrate"));
//LOG("");
std::cout << "port: " << m_port << ", baudrate: " << m_baudrate << std::endl;
}
catch (nlohmann::json::basic_json::type_error& e1) {
std::cout << "type is wrong " << e1.what() << std::endl;
throw e1;
}
catch (nlohmann::json::basic_json::out_of_range& e2) {
std::cout << "key is not found " << e2.what() << std::endl;
throw e2;
}
catch (...)
{
std::cout << "unknown error"<< std::endl;
exit(-1);
}
open();
}
void NmeaSource::open()
{
m_serial = std::make_shared<asio::serial_port>(*m_io);
asio::error_code ec;
m_serial->open(m_port, ec);
if (!ec) {
asio::serial_port_base::baud_rate baudrate(m_baudrate);
m_serial->set_option(baudrate);
std::cout << "successfully" << std::endl;
}
else {
std::cout << "failed " << ec.message() <<std::endl;
}
}
void NmeaSource::handle(const asio::error_code& error, // Result of operation.
std::size_t bytes_transferred // Number of bytes read.
)
{
if (!error) {
std::cout << bytes_transferred << " bytes read!" << std::endl;
}
else {
std::cout << error.message() << std::endl;
}
}
void NmeaSource::parse()
{
m_serial->async_read_some(
asio::buffer(m_readBuffer, 1024),
std::bind(&NmeaSource::handle, this,
std::placeholders::_1,
std::placeholders::_2)
);
}
int main()
{
auto io = std::make_shared<asio::io_context>();
//std::thread t(std::bind(static_cast<size_t(asio::io_service::*)()>(&asio::io_service::run), io.get()));
std::thread t([&io]() {io->run(); });
NmeaSource rtk(io, nlohmann::json{ {"port", "COM3"}, {"baudrate", 115200} });
rtk.parse();
for (;;)
{
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
}
The output
I am trying to write a simple server that allows multiple clients to connect. When I run my program, I get the following error as output:
[3730] Press [return] to exit.
Listening on: 127.0.0.1:7777
[5d0] Thread Start
Error: system:995 The I/O operation has been aborted because of either a
thread exit or an application request
I think this is because my Client object is falling out of scope, but I'm not entirely sure if this is actually the problem or if there's something else wrong with my code.
ClientNetwork::ClientNetwork(asio::io_service& ios, const asio::ip::tcp::endpoint& edp) : mIOService(ios), mAcceptor(ios, edp)
{
/*mAcceptor.open(edp.protocol());
mAcceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(false));*/
listen();
}
ClientNetwork::~ClientNetwork()
{
boost::system::error_code ec;
mAcceptor.close(ec);
mConnectionThreads.join_all();
mClients.clear();
std::cout << "KILLING NETWORK" << std::endl;
}
void ClientNetwork::listen()
{
mConnectionThreads.create_thread(boost::bind(&ClientNetwork::connectionThread, this));
Client* c = new Client(mIOService);
mAcceptor.listen(boost::asio::socket_base::max_connections);
mAcceptor.async_accept(*c->getSocket(), boost::bind(&ClientNetwork::handleAccept, this, _1, c));
}
void ClientNetwork::handleAccept(const boost::system::error_code& error, Client* client)
{
if (!error)
{
mClients.push_back(client);
mConnectionThreads.create_thread(boost::bind(&ClientNetwork::connectionThread, this));
std::cout << mConnectionThreads.size() << std::endl;
}
else
{
std::cout << "Error: " << error << " " << error.message() << std::endl;
}
}
This is my main (not sure it's entirely helpful but felt it would be good to include:
int main(int argc, char * argv[])
{
boost::shared_ptr< boost::asio::io_service > io_service(
new boost::asio::io_service
);
boost::shared_ptr< boost::asio::io_service::work > work(
new boost::asio::io_service::work(*io_service)
);
boost::shared_ptr< boost::asio::io_service::strand > strand(
new boost::asio::io_service::strand(*io_service)
);
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] Press [return] to exit." << std::endl;
global_stream_lock.unlock();
/*boost::thread_group worker_threads;
for (int x = 0; x < 2; ++x)
{
worker_threads.create_thread(boost::bind(&WorkerThread, io_service));
}*/
/*boost::shared_ptr< boost::asio::ip::tcp::acceptor > acceptor(
new boost::asio::ip::tcp::acceptor(*io_service)
);
boost::shared_ptr< boost::asio::ip::tcp::socket > sock(
new boost::asio::ip::tcp::socket(*io_service)
);*/
try
{
boost::asio::ip::tcp::resolver resolver(*io_service);
boost::asio::ip::tcp::resolver::query query(
"127.0.0.1",
boost::lexical_cast< std::string >(7777)
);
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
ClientNetwork clientNet(*io_service, endpoint);
global_stream_lock.lock();
std::cout << "Listening on: " << endpoint << std::endl;
global_stream_lock.unlock();
}
catch (std::exception & ex)
{
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] Exception: " << ex.what() << std::endl;
global_stream_lock.unlock();
}
std::cin.get();
//worker_threads.join_all();
return 0;
}
After creating ClientNetwork object and when first an asynchronous operation was started you should run io_service::run method to process handlers in events loop then your program runs until there is at least one asynchronous operation to be executed.
At the end of try scope put io_service->run():
ClientNetwork clientNet(*io_service, endpoint);
global_stream_lock.lock();
std::cout << "Listening on: " << endpoint << std::endl;
global_stream_lock.unlock();
io_service->run();
Without this the destructor of ClientNetwork class is called and mAcceptor is closed with printing this error message
void ClientNetwork::handleAccept (...) {
...
std::cout << "Error: " << error << " " << error.message() << std::endl;
}
The for loop in main.cpp, which calls a function that uses boost::mutex and that reads from a socket using read_until, only runs once, after that it's like it's blocked. I've tried putting a continue before the closing brackets and then it crashes. It's probably related to threading.
// MAIN.CPP
int main(int argc, char* argv[])
{
std::cout << "Enter port number: ";
std::string port;
std::getline(std::cin, port);
int tempPort = std::stoi(port);
Network * network = new Network(tempPort);
int it = 0;
boost::thread * t1;
t1 = new boost::thread([&network, &it]
{
while (true)
{
boost::asio::ip::tcp::socket * sock = new boost::asio::ip::tcp::socket(network->io_service);
network->accept(*sock);
if (network->socketList.size() > 0)
{
for (boost::asio::ip::tcp::socket * s : network->socketList)
{
if (s->remote_endpoint().address().to_string() == sock->remote_endpoint().address().to_string())
{
continue;
}
else {
network->socketList.push_back(sock);
std::cout << s->remote_endpoint().address().to_string() << " connected." << std::endl;
}
}
}
else {
network->socketList.push_back(sock);
std::cout << sock->remote_endpoint().address().to_string() << " connected." << std::endl;
}
}
});
while (true)
{
for (boost::asio::ip::tcp::socket * sock : network->socketList)
{
std::cout << "on range-based for loop" << std::endl;
network->readChatMessage(*(sock));
}
}
t1->join();
return 0;
}
// NETWORK.CPP
int Network::sendChatMessage(boost::asio::ip::tcp::socket & socket, ChatMessage & message)
{
try
{
boost::system::error_code err;
boost::asio::streambuf buf;
{
std::ostream out(&buf);
boost::archive::text_oarchive oa(out);
oa & message;
std::cout << std::string(message.text.begin(), message.text.end()) << std::endl;
}
m.lock();
write(socket, buf, err);
if (err)
{
std::cout << err.message() << std::endl;
}
m.unlock();
std::cout << "Mensagem enviada com sucesso!" << std::endl;
}
catch (std::exception& e)
{
std::cout << e.what() << std::endl;
}
return 0;
}
int Network::readChatMessage(boost::asio::ip::tcp::socket & socket)
{
std::cout << "in readChatMessage()" << std::endl;
boost::system::error_code err;
boost::asio::streambuf buf;
m.lock();
boost::asio::read_until(socket, buf, '\0', err);
if (err)
{
std::cout << err.message() << std::endl;
}
m.unlock();
std::istream in(&buf);
ChatMessage message;
boost::archive::text_iarchive ia(in);
ia & message;
std::cout << std::string(message.text.begin(), message.text.end()) << std::endl;
this->sendChatMessage(socket, message);
return 0;
}
I was able to fix the problem after debugging and editing the code a bit, then I was able to see there was an error happening: input stream error due to the serialization Input/Output. I handled the error properly, unlocking the mutex when the error happened and not letting the mutex go undone.
Snippet:
int Network::readChatMessage(boost::asio::ip::tcp::socket & socket)
{
std::cout << "in readChatMessage()" << std::endl;
boost::system::error_code err;
boost::asio::streambuf buf;
m.lock();
boost::asio::read_until(socket, buf, '\0', err);
if (err)
{
m.unlock();
std::cout << err.message() << std::endl;
return 0;
}
else {
m.unlock();
std::istream in(&buf);
ChatMessage message;
boost::archive::text_iarchive ia(in);
ia & message;
std::cout << std::string(message.text.begin(), message.text.end()) << std::endl;
this->sendChatMessage(socket, message);
return 0;
}
m.unlock();
return 0;
}
I am having a problem while creating a client program that sends requests. The request are using keep alive TCP HTTP connections. When a connection is closed(due to timeout or max being hit), I try and start a new connection if none are available, and resend the request. The connect works fine however, when I try and send the write, nothing is sent(according to Wireshark), but my error code for the write was a success. The receiving server does not receive any information either. Here is the main parts of my code:
void request_handler::send_1(std::vector<std::string> *bid_vector, std::string request_path, boost::mutex *bids_mutex)
{
try
{
boost::asio::streambuf request;
std::ostream request_stream(&request);
std::string reply_information;
request_stream << "GET /tests HTTP/1.1\r\n";
request_stream << "Host: 10.1.10.160\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: keep-alive\r\n\r\n";
server1_mutex_.lock();
if(server1_available_map_.size() == 0)
{
server1_mutex_.unlock();
persistent_connection *new_connection = new persistent_connection("10.1.10.160","80");
if(new_connection->send(request, reply_information))
{
server1_mutex_.lock();
server1_available_map_[new_connection->get_id()] = new_connection;
server1_mutex_.unlock();
}
}
else
{
persistent_connection *current_connection = (*(server1_available_map_.begin())).second;
server1_available_map_.erase(current_connection->get_id());
server1_mutex_.unlock();
int retry_counter = 20;
while(!current_connection->query_rtb(request, reply_information) && --retry_counter != 0)
{
delete current_connection;
server1_mutex_.lock();
if(server1_available_map_.size() == 0)
{
server1_mutex_.unlock();
current_connection = new persistent_connection("10.1.10.160","80");
}
else
{
current_connection = (*(server1_available_map_.begin())).second;
server1_available_map_.erase(current_connection->get_id());
server1_mutex_.unlock();
}
}
//Could not connect to 20 connections
if(retry_counter == 0)
{
Log::fatal("Could not connect in 20 tries");
delete current_connection;
return;
}
server1_mutex_.lock();
server1_available_map_[current_connection->get_id()] = current_connection;
server1_mutex_.unlock();
}
bids_mutex->lock();
bid_vector->push_back(reply_information);
bids_mutex->unlock();
}
catch(boost::thread_interrupted& e)
{
std::cout << "before cancel 1" << std::endl;
return;
}
catch(...)
{
std::cout << "blah blah blah" << std::endl;
}
}
And my persistent_connection class
persistent_connection::persistent_connection(std::string ip, std::string port):
io_service_(), socket_(io_service_), host_ip_(ip)
{
boost::uuids::uuid uuid = boost::uuids::random_generator()();
id_ = boost::lexical_cast<std::string>(uuid);
boost::asio::ip::tcp::resolver resolver(io_service_);
boost::asio::ip::tcp::resolver::query query(host_ip_,port);
boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
boost::asio::ip::tcp::endpoint endpoint = *iterator;
socket_.async_connect(endpoint, boost::bind(&persistent_connection::handler_connect, this, boost::asio::placeholders::error, iterator));
io_service_.run();
}
void persistent_connection::handler_connect(const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
{
if(ec)
{
std::cout << "Couldn't connect" << ec << std::endl;
return;
}
else
{
boost::asio::socket_base::keep_alive keep_option(true);
socket_.set_option(keep_option);
std::cout << "Connect handler" << std::endl;
}
}
bool persistent_connection::send(boost::asio::streambuf &request_information, std::string &reply_information)
{
std::cout << "DOING QUERY in " << id_ << std::endl;
boost::system::error_code write_ec, read_ec;
try
{
std::cout << "Before write" << std::endl;
boost::asio::write(socket_, request_information, write_ec);
std::cout << write_ec.message() << std::endl;
}catch(std::exception& e)
{
std::cout << "Write exception: " << e.what() << std::endl;
}
if(write_ec)
{
std::cout <<"Write error: " << write_ec.message() << std::endl;
return false;
}
boost::array<char,8192> buf;
buf.assign(0);
try
{
std::cout << "Before read" << std::endl;
boost::asio::read(socket_, boost::asio::buffer(buf), boost::asio::transfer_at_least(1), read_ec);
std::cout << read_ec.message() << std::endl;
}catch(std::exception& e)
{
std::cout << "Read exception: " << e.what() << std::endl;
}
if(read_ec)
{
std::cout << "Read error: " << read_ec.message() << std::endl;
return false;
}
reply_information = buf.data();
return true;
}
std::string persistent_connection::get_id()
{
return id_;
}
The path for this to happen is if server1_available_map_.size() > 0, and if the while executes, and fails. And then if the size == 0 on the second server1_available_map_.size();
The output for the call is:
DOING QUERY in 69a8f0ab-2a06-45b4-be26-37aea6d93ff2
Before write
Success
Before read
End of file
Read error: End of file
Connect handler
DOING QUERY in 4eacaa96-1040-4878-8bf5-c29b87fa1232
Before write
Success
Before read
Which shows that the first connection gets an end of file(connection closed by server on other end). The second connection connects fine(Connect handler message), and the query is executed in the second connection(different id), and the write is apparently successful, and the program hangs on the read(because there is nothing to read).
Does anyone have any idea why this would be happening? Is there something I seem to be doing wrong?
Thank you
It looks like you are passing the same boost::asio::streambuf to multiple write calls.
boost::asio::write(socket_, request_information, write_ec);
The contents of the buffer are consumed by the first call to boost::asio::write. This effectively empties the buffer so that there is nothing left to send. Pass a const string if you want to use the same buffer for multiple writes.
Here's the code I use:
class Server
{
.....
void Server::accepted()
{
std::cout << "Accepted!" << std::endl;
boost::array<char, 1> buf;
boost::asio::async_read(socket, boost::asio::buffer(buf),
boost::bind(&Server::handleRead, this, buf, boost::asio::placeholders::error));
}
void Server::handleRead(boost::array<char, 1> buf, const boost::system::error_code& error)
{
if(!error)
{
std::cout << "Message: " << buf.data() << std::endl;
}
else
{
std::cout << "Error occurred." << std::endl;
}
}
.....
}
The problem is that I always get the same data from the client: a specific char.
In my client I tried sending other char, but still the server shows the same char.
And when I try to read more than 1 bytes, I get an error that the buf variable is used before it's initialized.
You're using the local variable buf as the read buffer, which is dangerous and won't work. Also, you're just sending the original contents of that buffer to the handler. So instead, you need to use a buffer with a longer lifetime. Something like this:
class Server
{
.....
boost::array<char, 1> buf;
void Server::accepted()
{
std::cout << "Accepted!" << std::endl;
boost::asio::async_read(socket, boost::asio::buffer(buf),
boost::bind(&Server::handleRead, this, boost::asio::placeholders::error));
}
void Server::handleRead(const boost::system::error_code& error)
{
if(!error)
{
std::cout << "Message: " << buf.data() << std::endl;
}
else
{
std::cout << "Error occurred." << std::endl;
}
}
.....
}
edit: or alternatively, using a heap allocated buffer (not sure if the code is right, but you'll get the idea):
void Server::accepted()
{
std::cout << "Accepted!" << std::endl;
boost::shared_ptr<boost::array<char, 1>> buf(new boost::array<char, 1>);
boost::asio::async_read(socket, boost::asio::buffer(*buf),
boost::bind(&Server::handleRead, this, buf, boost::asio::placeholders::error));
}
void Server::handleRead(boost::shared_ptr<boost::array<char, 1>> buf, const boost::system::error_code& error)
{
if(!error)
{
std::cout << "Message: " << buf->data() << std::endl;
}
else
{
std::cout << "Error occurred." << std::endl;
}
}