I'm using asio tcp to receive some data from a device, the data will be sent from a device 25 times every 1 seconds (40Hz), and the data packet length varies from around 10 bytes to more than 200 bytes.
When I print some log file, I see the data is received with great jitters, like this:
[2020-03-03 17:16:15.162] [Can] [info] 32004
[2020-03-03 17:16:15.162] [Can] [info] 32005
[2020-03-03 17:16:15.163] [Can] [info] 32006 <-abnormal, interval should not be 1
[2020-03-03 17:16:15.163] [Can] [info] 32007
[2020-03-03 17:16:15.163] [Can] [info] 32008
[2020-03-03 17:16:15.164] [Can] [info] 32009
[2020-03-03 17:16:15.164] [Can] [info] 32010
[2020-03-03 17:16:15.165] [Can] [info] 32011
[2020-03-03 17:16:15.165] [Can] [info] 32012
[2020-03-03 17:16:15.165] [Can] [info] 32013
[2020-03-03 17:16:15.166] [Can] [info] 32014
[2020-03-03 17:16:15.166] [Can] [info] 32015
[2020-03-03 17:16:15.166] [Can] [info] 32016
[2020-03-03 17:16:15.167] [Can] [info] 32017
[2020-03-03 17:16:15.167] [Can] [info] 32018
[2020-03-03 17:16:15.167] [Can] [info] 32019
[2020-03-03 17:16:15.168] [Can] [info] 32020
[2020-03-03 17:16:15.168] [Can] [info] 32021
[2020-03-03 17:16:15.168] [Can] [info] 32022
[2020-03-03 17:16:15.169] [Can] [info] 32023
[2020-03-03 17:16:15.169] [Can] [info] 32024
[2020-03-03 17:16:15.570] [Can] [info] 32025 <-jitter here
[2020-03-03 17:16:15.771] [Can] [info] 32026
[2020-03-03 17:16:15.872] [Can] [info] 32027
[2020-03-03 17:16:15.872] [Can] [info] 32028
My code is a very standard asio tcp read, like this:
socket = make_unique<tcp::socket>(io_service);
socket->open(asio::ip::tcp::v4());
socket->set_option(tcp::no_delay(true));
please note that I set option to no_delay, but this made no difference.
asio::error_code error;
asio::streambuf len_buffer;
size_t n = asio::read(*socket, len_buffer, asio::transfer_exactly(byte), error);
if (error) {
cout << "receive failed: " << error.message() << endl;
return 0;
}
else
{
asio::streambuf::const_buffers_type buff = len_buffer.data();
data.assign(asio::buffers_begin(buff), asio::buffers_begin(buff) + len_buffer.size());
return n;
}
Basically I'm using asio::read(*socket, len_buffer, asio::transfer_exactly(byte), error);
, I know how many bytes I'm expecting, so I use read
to read synchronously.
I take for granted that asio is mature, but I'd like to see if there is any hidden setting to asio so that I can remove the jitter here. Is it related to transfer_exactly
?
Aucun commentaire:
Enregistrer un commentaire