lundi 24 juin 2019

How to create a boost::asio::buffer with uint8_t* [Is it possible?]

This question is about boost::asio::buffer

I have a call to send data over a TCP socket. Following is the call:

std::vector<unsigned char> data = GetDataToSend();
boost::asio::async_write(tcp_socket, boost::asio::buffer(data), 
    [this](const boost::system::error_code & error, std::size_t bytes_sent) {
        if (!error && bytes_sent > 0) {
            // Success!
        }
    });

Above is great as long as GetDataToSend returns a std::vector<unsigned char>.

But, what if GetDataToSend returned a uint8_t*? Isn't it possible to put the uint8_t* data into boost::asio::buffer and do an async_write? Something like below..

uint8_t* data = GetDataToSend();
boost::asio::async_write(tcp_socket, boost::asio::buffer(data), 
    [this](const boost::system::error_code & error, std::size_t bytes_sent) {
        if (!error && bytes_sent > 0) {
            // Success!
        }
    });

Is it that boost::asio::buffer only operates on std::vector<unsigned char>s? How can I create a boost::asio::buffer object from uint8_t*? It will be great if I could do this without causing an extra copy if possible.

I am using boost 1.63.

Aucun commentaire:

Enregistrer un commentaire