Discussion:
[asio-users] wrong data sequence using boost::asio::async_read()
e***@durr.com
2012-09-03 16:16:32 UTC
Permalink
Hello asio!

Use boost::asio::async_read() for communication with S7 PLC using TCP
It works without problems if messages are short enough and TCP doesn't have segmentation.
Have very strange results in case of segmentation.

Have 2 messages coming in 2 segments:

M1S1, M1S2, M2S1, M2S2 (MX for message X, SX for segmet X)

Here is the code:

char data_[2000];
---
boost::asio::async_read (socket_, boost::asio::buffer(data_, sizeof(data_)), &handle_read_message);


I get 2 handle_read_message() calls with data in data_, 2000 bytes each

But "data" are in the wrong sequence !

M2S2 M2S1 in the first
M1S2 M1S1 in the second

Would expect
M1S1 M1S2 in the first
M2S1 M2S2 in the second

Does somebody have an idea?
Would appreciate any help.


Best regards

Evgeni Rojkov
Marat Abrarov
2012-09-03 16:51:36 UTC
Permalink
Hi, Evgeni.
Post by e***@durr.com
Does somebody have an idea?
The most often (programmers') mistake (I'd read at this mailing list) related to such behavior is more than one
async_read(_some) simultaneous operations on the same socket.

http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/reference/async_read/overload1.html:
~~~~~~~~~
This operation is implemented in terms of zero or more calls to the stream's async_read_some function, and is known as a
composed operation. The program must ensure that the stream performs no other read operations (such as async_read, the
stream's async_read_some function, or any other composed operations that perform reads) until this operation completes.
~~~~~~~~~

Regards,
Marat Abrarov.
e***@durr.com
2012-09-04 07:35:43 UTC
Permalink
Hi Marat,
thank your for the answer.
Currenty doesn't see the how async_read(_some) could run simultaneous in my case.
Maybe by mistake or missunderstanding.

Have

boost::asio::async_read (
socket_, ...
&handle_read)
....

handle_read()
{

......
boost::asio::async_read (
socket_, ...
&handle_read)
}

Next async_read always starts in handle_read() after the previous finished.
Hope so :-)

Are there more details to care about?
Could the counterpart (actually not PC but Simens S7 PLC) have specific delivery policy or it's impossible with TCP ? (currently doesn't there answer)

Many thanks,
Evgeni




-----Ursprüngliche Nachricht-----
Von: Marat Abrarov [mailto:***@mail.ru]
Gesendet: Montag, 3. September 2012 18:52
An: asio-***@lists.sourceforge.net
Betreff: Re: [asio-users] wrong data sequence using boost::asio::async_read()

Hi, Evgeni.
Post by e***@durr.com
Does somebody have an idea?
The most often (programmers') mistake (I'd read at this mailing list) related to such behavior is more than one
async_read(_some) simultaneous operations on the same socket.

http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/reference/async_read/overload1.html:
~~~~~~~~~
This operation is implemented in terms of zero or more calls to the stream's async_read_some function, and is known as a composed operation. The program must ensure that the stream performs no other read operations (such as async_read, the stream's async_read_some function, or any other composed operations that perform reads) until this operation completes.
~~~~~~~~~

Regards,
Marat Abrarov.



------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Yuri Timenkov
2012-09-04 08:23:20 UTC
Permalink
You can check how data travel over network with WireShark. Just right-click
on packet and choose "Follow TCP stream". It shall open dialog with
reassembled session.

However by "concurrent" people usually mean that async_read called 2 times
before handler called. E.g. from write callback.
Post by e***@durr.com
Hi Marat,
thank your for the answer.
Currenty doesn't see the how async_read(_some) could run simultaneous in my case.
Maybe by mistake or missunderstanding.
Have
boost::asio::async_read (
socket_, ...
&handle_read)
....
handle_read()
{
......
boost::asio::async_read (
socket_, ...
&handle_read)
}
Next async_read always starts in handle_read() after the previous finished.
Hope so :-)
Are there more details to care about?
Could the counterpart (actually not PC but Simens S7 PLC) have specific
delivery policy or it's impossible with TCP ? (currently doesn't there
answer)
Many thanks,
Evgeni
-----UrsprÃŒngliche Nachricht-----
Gesendet: Montag, 3. September 2012 18:52
Betreff: Re: [asio-users] wrong data sequence using
boost::asio::async_read()
Hi, Evgeni.
Post by e***@durr.com
Does somebody have an idea?
The most often (programmers') mistake (I'd read at this mailing list)
related to such behavior is more than one
async_read(_some) simultaneous operations on the same socket.
http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/reference/async_read/overload1.html
~~~~~~~~~
This operation is implemented in terms of zero or more calls to the
stream's async_read_some function, and is known as a composed operation.
The program must ensure that the stream performs no other read operations
(such as async_read, the stream's async_read_some function, or any other
composed operations that perform reads) until this operation completes.
~~~~~~~~~
Regards,
Marat Abrarov.
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
e***@durr.com
2012-09-04 08:54:26 UTC
Permalink
Hi Yuri,
thank you, „write callback“ is the case!
Regards, Evgeni

Von: Yuri Timenkov [mailto:***@timenkov.ru]
Gesendet: Dienstag, 4. September 2012 10:23
An: asio-***@lists.sourceforge.net
Betreff: Re: [asio-users] wrong data sequence using boost::asio::async_read()

You can check how data travel over network with WireShark. Just right-click on packet and choose "Follow TCP stream". It shall open dialog with reassembled session.

However by "concurrent" people usually mean that async_read called 2 times before handler called. E.g. from write callback.
On Tue, Sep 4, 2012 at 11:35 AM, <***@durr.com<mailto:***@durr.com>> wrote:
Hi Marat,
thank your for the answer.
Currenty doesn't see the how async_read(_some) could run simultaneous in my case.
Maybe by mistake or missunderstanding.

Have

boost::asio::async_read (
socket_, ...
&handle_read)
....

handle_read()
{

......
boost::asio::async_read (
socket_, ...
&handle_read)
}

Next async_read always starts in handle_read() after the previous finished.
Hope so :-)

Are there more details to care about?
Could the counterpart (actually not PC but Simens S7 PLC) have specific delivery policy or it's impossible with TCP ? (currently doesn't there answer)

Many thanks,
Evgeni




-----UrsprÃŒngliche Nachricht-----
Von: Marat Abrarov [mailto:***@mail.ru<mailto:***@mail.ru>]
Gesendet: Montag, 3. September 2012 18:52
An: asio-***@lists.sourceforge.net<mailto:asio-***@lists.sourceforge.net>
Betreff: Re: [asio-users] wrong data sequence using boost::asio::async_read()

Hi, Evgeni.
Post by e***@durr.com
Does somebody have an idea?
The most often (programmers') mistake (I'd read at this mailing list) related to such behavior is more than one
async_read(_some) simultaneous operations on the same socket.

http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/reference/async_read/overload1.html:
~~~~~~~~~
This operation is implemented in terms of zero or more calls to the stream's async_read_some function, and is known as a composed operation. The program must ensure that the stream performs no other read operations (such as async_read, the stream's async_read_some function, or any other composed operations that perform reads) until this operation completes.
~~~~~~~~~

Regards,
Marat Abrarov.



------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net<mailto:asio-***@lists.sourceforge.net>
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net<mailto:asio-***@lists.sourceforge.net>
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio

Marat Abrarov
2012-09-04 08:38:41 UTC
Permalink
Hi, Evgeni.
Post by e***@durr.com
Currenty doesn't see the how async_read(_some) could run simultaneous in my case.
Maybe by mistake or missunderstanding.
Have
boost::asio::async_read (
socket_, ...
&handle_read)
....
handle_read()
{
......
boost::asio::async_read (
socket_, ...
&handle_read)
}
Next async_read always starts in handle_read() after the previous finished.
Hope so :-)
It seems that your code is right.

As Yuri suggested:
1. Try WireShark or any other sniffer to check TCP stream itself.
2. Review all points where async_read(_some) is called. May be "Handler Tracking"
(http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/overview/core/handler_tracking.html) can help.

Regards,
Marat Abrarov.
e***@durr.com
2012-09-04 08:50:08 UTC
Permalink
Hello Marat,
you are right :-)
I really have simultaneous _reads :-(
Many thanks!
Evgeni


-----Ursprüngliche Nachricht-----
Von: Marat Abrarov [mailto:***@mail.ru]
Gesendet: Dienstag, 4. September 2012 10:39
An: asio-***@lists.sourceforge.net
Betreff: Re: [asio-users] wrong data sequence using boost::asio::async_read()

Hi, Evgeni.
Post by e***@durr.com
Currenty doesn't see the how async_read(_some) could run simultaneous in my case.
Maybe by mistake or missunderstanding.
Have
boost::asio::async_read (
socket_, ...
&handle_read)
....
handle_read()
{
......
boost::asio::async_read (
socket_, ...
&handle_read)
}
Next async_read always starts in handle_read() after the previous finished.
Hope so :-)
It seems that your code is right.

As Yuri suggested:
1. Try WireShark or any other sniffer to check TCP stream itself.
2. Review all points where async_read(_some) is called. May be "Handler Tracking"
(http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/overview/core/handler_tracking.html) can help.

Regards,
Marat Abrarov.



------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Loading...