c++ - asynchronous read write with device file -



c++ - asynchronous read write with device file -

i writing binary info device fie /dev/itun.

void ahaconnector::asyncwritedata(vector<uint8_t> packedmessage) { cout<<"\n async write info packed message"; devicestreamdescriptor.assign(device); boost::asio::write ( devicestreamdescriptor, boost::asio::buffer(packedmessage) ); readbuffer.resize(1024); devicestreamdescriptor.async_read_some(boost::asio::buffer(readbuffer), boost::bind(&ahaconnector::readheader, this, boost::asio::placeholders::error(), boost::asio::placeholders::bytes_transferred() )); io_service.run(); } void ahaconnector::readheader(const boost::system::error_code &ec, std::size_t bytes_transferred) { if(!ec) { std::cout<<"\n bytes transfereed :"<<bytes_transferred<<" "<<readbuffer.size(); devicestreamdescriptor.async_read_some(boost::asio::buffer(readbuffer), boost::bind(&ahaconnector::readheader, this, boost::asio::placeholders::error(), boost::asio::placeholders::bytes_transferred() )); callbacks callbacks; callbacks.onreceivingpackedmessage(); io_service.run(); } else { cout<<"\n scheme error code "<<ec; } }

the callback function readhandler getting executed successfully, not able transfer command callback function class.

is wrong design perspective. need handle message received callback function farther logic. should utilize thread here ?

looking @ code might want replace read(device,...) boost asio's back upwards posix streams:

#include <boost/asio.hpp> #include <boost/asio/posix/stream_descriptor.hpp> #include <boost/function.hpp> #include <iostream> static int device = 0; using namespace boost; int main() { boost::asio::io_service io_svc; boost::asio::posix::stream_descriptor iodevice(io_svc, device); char buffer[1024]; function<void(system::error_code const&, size_t)> callback; callback = [&](boost::system::error_code const& ec, size_t bytes_transferred) { if (ec) { std::cout << "error '" << ec.message() << "' during asynchronous operation\n"; } else { std::cout << "read " << bytes_transferred << " bytes\n"; std::cout << "data: '"; std::cout.write(buffer, bytes_transferred); std::cout << "'\n"; iodevice.async_read_some(asio::buffer(buffer), callback); } }; iodevice.async_read_some(asio::buffer(buffer), callback); io_svc.run(); }

see live on coliru.

sadly on coliru can't work because input redirected non-stream. if run interactively work , print first 10 characters entered.

c++ boost boost-asio

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

java - Parsing XML, skip certain tags -

c# - ASP.NET MVC Sequence contains no matching element -