Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In boost::asio, why are there no socket member functions for read/write?

I working on a client/server application using boost asio, specifically boost::asio::ip::tcp::socket to connect and transfer data. Right now I am using boost::asio::async_read to recive a certain amount of bytes. Until now, in all cases I know how many bytes I want to receive, before calling my handler. Therefore I don't see a reason to use the sockets meber function read_some. But I wonder why there is no "async_read" member function of boost::asio::ip::tcp::socket but only the free one.

So my question is: Are there conceptual or technical reasons why there is a read_some member function but no readmember function, or did "Boost just forgot to implement it" ?

like image 247
Haatschii Avatar asked Feb 17 '23 20:02

Haatschii


1 Answers

All of the streaming interfaces in Asio provide both read_some and async_read_some moethods. This is true for the TCP sockets, SSL streams, and Serial ports. The implementation of read, read_until, and their async cousins all have the same implementations, based on using the read_some method. The read function is written as a generic template, that can use the read_some method on its first argument to perform the call as you requested.

There are some C++ advocates that recommend using non-friend non-member functions whenever possible, so as to minimize the changes when a class implementation changes. read_some is the interface, and read is just a wrapper that adds blocking in the case of partial reads for a variety of different sources.

like image 124
Dave S Avatar answered Feb 20 '23 11:02

Dave S