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

发布于 2024-11-08 07:49:53 字数 3054 浏览 1 评论 0原文

我正在尝试转换函数的返回值 boost::asio::async_readint 以查看我是否收到了任何数据:

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, this,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::buffer((char*)buffer, 1000),
        boost::bind(&Connection::Receive, this, 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

我怎样才能完成我想要做的事情?另外,这些错误是什么意思以及如何修复它们?

I am attempting to convert the return value of the function boost::asio::async_read to an int in order to see if I've received any data:

int recvlen = boost::asio::async_read (
    socket_,
    boost::asio::buffer((char*)buffer, 1000),
    boost::bind(&Connection::Receive, this, boost::asio::placeholders::error)
);

Here is that statement in the context of the rest of my code, which does not compile:

.h file:

#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 file:

#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, this,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::buffer((char*)buffer, 1000),
        boost::bind(&Connection::Receive, this, boost::asio::placeholders::error)
    );

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

    this->OnReceived(buffer, recvlen);

    delete[] buffer;

    return true;
}

These are the errors this code produces in 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

How can I accomplish what I'm trying to do? In addition, what do these errors mean and how can I fix them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞美少年 2024-11-15 07:49:53

async_read 不返回读取的字节数。它在后台异步执行读取。字节数被传递给完成处理程序。当读取完成时,ASIO 会调用完成处理程序。您将完成处理程序传递给async_readasync_read 是一个接受任何函数对象作为处理程序的模板。在您的例子中,您传递了绑定语句的输出。

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_,boost::asio::buffer((char*)buffer, 1000),
          boost::bind(&Connection::HandleReceive, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));

您必须将 buffer 创建为一个类变量才能使异步示例正常工作,但您可能希望想出一种更好的方法来管理记忆。此外,您还必须采取一些措施来处理 Connection 类的生命周期问题。检查 ASIO 示例以获得更好的开发示例。

async_read does not return the number of bytes read. It performs the read in the background, asynchronously. The number of bytes is passed to the completion handler. The completion handler is invoked by ASIO when the read completes. You pass the completion handler into async_read. async_read is a template that accepts any function object as the handler. In your case, you passed the output of the bind statement.

There are good examples in the boost documentation, but here are two quick solutions.

You could use the synchronous boost::asio::read function instead of boost::asio::async_read.

int recvlen = boost::asio::read(socket_,boost::asio::buffer((char*)buffer, 1000));

Or, you could add a new function:

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!!!
}

And call async_read like

boost::asio::async_read(socket_,boost::asio::buffer((char*)buffer, 1000),
          boost::bind(&Connection::HandleReceive, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));

You will have to make buffer a class variable for the asynchronous example to work, but you may want to come up with a better way to manage the memory. Also, you will have to do something to deal with lifetime issues of the Connection class. Check the ASIO examples for a better developed example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文