2011-05-20 154 views
2

我在嘗試,如果我收到任何數據功能boost::asio::async_read的返回值轉換爲int爲了看:獲得通過的boost :: ASIO :: async_read讀取的字節數

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 

我怎麼能做到我想要做什麼?另外,這些錯誤意味着什麼,我該如何解決它們?

+0

@Kiril基洛夫我不知道任何時候我雙擊這個錯誤就放在這裏bind.hpp'模板結構result_traits <不明,F> { 的typedef typename的˚F:: result_type的類型; };'但是我不是用F IDK的什麼是錯的!? – Abanoub 2011-05-20 21:25:21

+0

對不起,我幾乎立即刪除我的意見,因爲我錯過了一些東西。對不起,不能幫助你,我從來沒有使用過助力,不幸的是。 – 2011-05-20 21:30:23

回答

2

async_read不返回讀取的字節數。它以異步方式在後臺執行讀取操作。字節數傳遞給完成處理程序。完成處理程序由ASIO在讀取完成時調用。您將完成處理程序傳遞到async_readasync_read是一個接受任何函數對象作爲處理程序的模板。在你的情況下,你傳遞了bind語句的輸出。

有在升壓documentation很好的例子,但這裏有兩個快速的解決方案。

您可以使用同步升壓:: ASIO ::讀取功能,而不是升壓:: 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示例以獲得更好開發的示例。

+0

@Dan發佈的HandleReceive()+ async_read()調用對金錢是正確的,並且是讓某人獲得從asio的async_read()調用傳輸的字節數的正確方法。 – Sean 2011-05-21 21:10:28

相關問題