获取boost :: asio :: async_read读取的字节数

问题描述

|| 我试图将函数
boost::asio::async_read
的返回值转换为
int
,以便查看是否已接收到任何数据:
int recvlen = boost::asio::async_read (
    socket_,boost::asio::buffer((char*)buffer,1000),boost::bind(&Connection::Receive,this,boost::asio::placeholders::error)
);
这是我的其余代码(未编译)中的该语句: .h文件:
#ifndef _CONNECTION_H_
#define _CONNECTION_H_

#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include <stdint.h>

class Connection 
{
public:
    Connection(boost::asio::io_service& io_service);
    ~Connection();
    boost::asio::ip::tcp::socket& socket() {return socket_;}
    void Send(uint8_t* buffer,int length);
    bool Receive();

protected:
    virtual void OnReceived(uint8_t* buffer,int len) = 0;

private:
    boost::asio::ip::tcp::socket socket_;
};

#endif
.cpp文件:
#include \"Connection.h\"

Connection::Connection(boost::asio::io_service& io_service)    
    : socket_(io_service) {}

Connection::~Connection() {}

void Connection::Send(uint8_t* buffer,int length) {
    boost::asio::async_write (
        socket_,boost::asio::buffer(buffer,length),boost::bind(&Connection::Send,boost::asio::placeholders::error)
    );
}

bool Connection::Receive(){
    uint8_t* buffer = new uint8_t[1000];

    //Conversion excerpt
    int recvlen = boost::asio::async_read (
        socket_,boost::asio::placeholders::error)
    );

    if (recvlen <= 0) {
        delete[] buffer;
        return false;
    }

    this->OnReceived(buffer,recvlen);

    delete[] buffer;

    return true;
}
这些是此代码在Visual C ++中产生的错误:
error C2825: \'F\': must be a class or namespace when followed by \'::\'    e:\\boost_1_46_1\\boost_1_46_1\\boost\\bind\\bind.hpp    69
error C2039: \'result_type\' : is not a member of \'`global namespace\'\'    e:\\boost_1_46_1\\boost_1_46_1\\boost\\bind\\bind.hpp    69
error C2146: syntax error : missing \';\' before identifier \'type\'    e:\\boost_1_46_1\\boost_1_46_1\\boost\\bind\\bind.hpp    69
error C2208: \'boost::_bi::type\' : no members defined using this type    e:\\boost_1_46_1\\boost_1_46_1\\boost\\bind\\bind.hpp    69
error C1903: unable to recover from previous error(s); stopping compilation e:\\boost_1_46_1\\boost_1_46_1\\boost\\bind\\bind.hpp    69
IntelliSense: a value of type \"void\" cannot be used to initialize an entity of type \"int\"   d:\\c++\\ugs\\common\\connection.cpp    18
IntelliSense: the #endif for this directive is missing  d:\\c++\\ugs\\common\\connection.h  1
IntelliSense: this declaration has no storage class or type specifier   d:\\c++\\ugs\\common\\connection.h  26
我该如何完成我想做的事情?此外,这些错误是什么意思,我该如何解决?     

解决方法

async_read
不返回读取的字节数。它在后台异步执行读取。字节数传递给完成处理程序。读取完成时,ASIO会调用完成处理程序。您将完成处理程序传递给
async_read
async_read
是接受任何函数对象作为处理程序的模板。在您的情况下,您传递了bind语句的输出。 boost文档中有很好的示例,但是这里有两个快速解决方案。 您可以使用同步boost :: asio :: read函数而不是boost :: asio :: async_read。
int recvlen = boost::asio::read(socket_,boost::asio::buffer((char*)buffer,1000));
或者,您可以添加一个新功能:
void HandleReceive(boost::system::error_code &error,std::size_t recvlen)
{
    if (!error && error != boost::asio::error::message_length) {
       this->OnReceived(buffer,recvlen);
       delete [] buffer;
    } // else ERROR!!!
}
并像这样调用async_read
boost::asio::async_read(socket_,1000),boost::bind(&Connection::HandleReceive,this,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
为了使异步示例正常工作,您必须使“ 12”成为类变量,但是您可能想提出一种更好的方法来管理内存。另外,您将需要采取一些措施来解决
Connection
类的终身问题。查看ASIO示例以获得更好的示例。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...