1

async_read_someに問題があります

async_write_someでコンパイルしたところ、うまくいきました。

しかし、私はasync_read_someでコンパイルしようとしました、

そして、正しい方法を見つけることができるという奇妙なエラーを思いついた。

私は世界を後押しするかなりの初心者です..Plzはこれらの問題で私を助けてくれます。

前もって感謝します。

Oppsコンパイラエラーはこのようなものです

エラー:'ws_async_tcp_connection :: async_recv_msg_some(boost :: asio :: mutable_buffers_1、boost :: _ bi :: bind_t、boost :: _ bi :: list3、boost :: arg <1>()()、 boost :: arg <2>()()>>)'/home/chris/proto/inc/ws_async_tcp_connection.h:119:注:候補は次のとおりです:void ws_async_tcp_connection :: async_recv_msg_some(T&、Handler)[with T = boost :: asio :: mutable_buffers_1、Handler = boost :: _ bi :: bind_t、boost :: _ bi :: list3、boost :: arg <1>()()、boost :: arg <2>()()>> ]

header 
-----------------------------------------------------------------------------------
class ws_async_tcp_connection{
    boost::asio::ip::tcp::socket socket_;
public:

    ws_async_tcp_connection(boost::asio::io_service& arg_io_service)
        : socket_(arg_io_service){}


    /*
    *   destructor
    */      
    ~ws_async_tcp_connection(){
        shutdown();
    }


    /*
    *   connect method
    *       : binding connection handler -> handle_connect
    */
    void connect(const std::string& host_addr, const std::string& port_no){
        boost::asio::ip::tcp::resolver resolver_(socket_.get_io_service());
        boost::asio::ip::tcp::resolver::query query_(boost::asio::ip::tcp::v4(), host_addr, port_no);
        boost::asio::ip::tcp::resolver::iterator end_point_iter = resolver_.resolve(query_);
        boost::asio::async_connect(socket_, end_point_iter, 
            boost::bind(&ws_async_tcp_connection::handle_connect, this, boost::asio::placeholders::error));
    }


    /*
    *   handle_connect
    *       : handle after the connect method called
    */
    void handle_connect(const boost::system::error_code& ec){
        if(!ec){
            // successfully established the connection - maybe print the remote ip, port
            /*  getting the remote_endpoint ip, port
            std::string connected_server_ip = socket_.remote_endpoint().address().to_string();
            unsigned short connected_server_port = socket_.remote_endpoint().port();
            */
        }
        else{
            // error handling - maybe use Logger class to print the error
            shutdown();
        }
    }


    /*
    *   set_option
    *       : set option for the tcp socket
    */
    template<typename SettableSocketOption>
    void set_option(const SettableSocketOption& option){
        // if performance is poor, then let's try no_delay option(true);
        /* keepalive option
            boost::asio::socket_base::keep_alive option(true);
            socket.set_option(option);
        */
        socket_.set_option(option);
    }


    /*
    *   is_open
    *       : socket is opened or not
    */
    bool is_open(){
        return socket_.is_open();
    }

    /*
    *   async_send_msg
    *       : send msg (warning! : will be blocked until the exact bytes you ask for are transmitted)
    */
    template <typename T, typename Handler> 
    void async_send_msg(const T& send_buf, Handler handler){
        boost::asio::async_write(socket_, send_buf, handler);
    }


    /*
    *   async_recv_msg
    *       : recv msg (warning! : will be blocked until the exact bytes you ask for are transmitted)
    */
    template <typename T, typename Handler> 
    void async_recv_msg(T& recv_buf, Handler handler){
        boost::asio::async_read(socket_, recv_buf, handler);
    }

    /*
    *   async_send_msg_some
    *       : send msg
    */  
    template <typename T, typename Handler> 
    void async_send_msg_some(const T& send_buf, Handler handler){
        socket_.async_write_some(send_buf, handler);
    }


    /*
    *   async_recv_msg_some
    *       : recv msg (Typically use this if you don't have to receive the exact numbers of bytes)
    */
    template <typename T, typename Handler> 
    void async_recv_msg_some(T& recv_buf, Handler handler){
        socket_.async_read_some(recv_buf, handler);
    }


    /*
    *   shutdown
    */
    void shutdown(){
        if(is_open()){
            boost::system::error_code ec;
            socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
            if(ec){
                // may print shutdown error
            }
            socket_.close();
        }
    }

    /*
    *   socket
    *       : returning the current holding socket
    */
    boost::asio::ip::tcp::socket& socket(){
        return socket_;
    }
};

typedef boost::shared_ptr<ws_async_tcp_connection> async_tcp_ptr;


-------------------------------------------------------------------------------
src
-------------------------------------------------------------------------------

class async_tcp_test_server{
    enum{ buf_size = 1024 };
    boost::asio::ip::tcp::acceptor acceptor_;
    boost::array<char, buf_size> send_buf;
    boost::array<char, buf_size> recv_buf;
public:
    async_tcp_test_server(boost::asio::io_service& io_service, unsigned short port_no)
        : acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port_no)){

        // start an accept operation for a new connection
        async_tcp_ptr new_conn(new ws_async_tcp_connection(acceptor_.get_io_service()));
        acceptor_.async_accept(new_conn->socket(), boost::bind(&async_tcp_test_server::handle_accept, this,
            boost::asio::placeholders::error, new_conn));
    }

    void handle_accept(const boost::system::error_code& ec, async_tcp_ptr conn){

        if(!ec){
            send_buf.assign(0x00);
            string hello_str = "Welcome to connect Asynchronous TCP Test Server :)";
            memcpy(&send_buf[0], hello_str.c_str(), hello_str.length());
            conn->async_send_msg(boost::asio::buffer(send_buf), boost::bind(&async_tcp_test_server::handle_write, this, boost::asio::placeholders::error, conn));
            //conn->async_send_msg_some(boost::asio::buffer(send_buf), boost::bind(&async_tcp_test_server::handle_write, this, boost::asio::placeholders::error, 
                //boost::asio::placeholders::bytes_transferred, conn));
        }

        // start an accept operation for new connection
        async_tcp_ptr new_conn(new ws_async_tcp_connection(acceptor_.get_io_service()));
        acceptor_.async_accept(new_conn->socket(), boost::bind(&async_tcp_test_server::handle_accept, this,
            boost::asio::placeholders::error, new_conn));
    }


    void handle_write(const boost::system::error_code& ec, async_tcp_ptr conn){
        if(!ec){
            recv_buf.assign(0x00);
            conn->async_recv_msg_some(boost::asio::buffer(recv_buf), boost::bind(&async_tcp_test_server::handle_read, this, boost::asio::placeholders::error, 
                boost::asio::placeholders::bytes_transferred));
        }
    }


    void handle_read(const boost::system::error_code& ec, size_t num_recv_bytes){
        cout << recv_buf.data() << endl;
    }
};


int main(void)
{
    unsigned short port_no = 7807;
    boost::asio::io_service io_svr;
    async_tcp_test_server test_server(io_svr, port_no);
    io_svr.run();
}

エラーの出力は次のとおりです(T&-> const T&を変更した後)

/home/chris/proto/boost/boost/bind/bind_template.hpp:15:'boost :: _ bi :: bind_t、boost :: arg <1>()()、boost :: arg <2>()()、boost :: _ bi :: value >>>'async_tcp_test_server.cpp:46:ここからインスタンス化/home/chris/proto/boost/boost/bind/bind.hpp:69:エラー:' void(async_tcp_test_server: :*)(const boost :: system :: error_code&、size_t)'は、クラス、構造体、または共用体タイプではありません/home/chris/proto/boost/boost/asio/basic_stream_socket.hpp:メンバー関数内' void boost: :asio :: basic_stream_socket :: async_read_some(const MutableBufferSequence&、const ReadHandler&)[with MutableBufferSequence = boost :: asio :: mutable_buffers_1、ReadHandler = boost :: _ bi :: bind_t、boost :: arg <1>()()、boost :: arg <2>()()、boost :: _ bi :: value >>>、Protocol = boost :: asio :: ip :: tcp、StreamSocketService = boost :: asio :: stream_socket_service]':/ home / chris / proto / inc / ws_async_tcp_connection .h:120:'void ws_async_tcp_connection :: async_recv_msg_some(const T&、Handler)[with T = boost :: asio :: mutable_buffers_1、Handler = boost :: _ bi :: bind_t、boost :: arg <1>()からインスタンス化()、boost :: arg <2>()()、boost :: _ bi :: value >>>]'async_tcp_test_server.cpp:46:ここからインスタンス化/ home / chris / proto / boost / boost / asio / basic_stream_socket .hpp:785:エラー:'(boost :: _ bi :: bind_t、boost :: arg <1>()()、boost :: arg <2>()()、boost :: _ bi :: value >>>)(const boost :: system :: error_code&、const long unsigned int&)'/home/chris/proto/boost/boost/asio/detail/bind_handler.hpp:メンバー関数'voidboost :: asio :: detail :: binder2 :: operator()()[with Handler = boost :: _ bi :: bind_t、boost :: arg <1>()()、boost :: arg <2>()()、boost :: _ bi :: value >>>、Arg1 = boost :: system :: error_code、Arg2 = long unsigned int]':/ home / chris / proto / boost / boost / asio / handler_invoke_hook.hpp:64:'void boost :: asio :: asio_handler_invoke(Function、...)[with Function = boost :: asio :: detail :: binder2、boost :: arg <1>()()からインスタンス化、boost :: arg <2>()()、boost :: _ bi :: value >>>、boost :: system :: error_code、long unsigned int>]'/home/chris/proto/boost/boost/asio/detail/handler_invoke_helpers.hpp:39: 'void boost_asio_handler_invoke_helpers :: invoke(Function&、Context&)[with Function = boost :: asio :: detail :: binary2、boost :: arg <1>()()、boost :: arg <2>()( )、boost :: _ bi :: value >>>、boost :: system :: error_code、long unsigned int>、Context = boost :: _ bi :: bind_t、boost :: arg <1>()()、boost: :arg <2>()()、boost :: _ bi :: value >>>]'/home/chris/proto/boost/boost/asio/detail/reactive_socket_recv_op.hpp:110:' static void boost :: asio :: detail:からインスタンス化:reactive_socket_recv_op :: do_complete(boost :: asio :: detail :: io_service_impl *、boost :: asio :: detail :: operation *、const boost :: system :: error_code&、size_t)[with MutableBufferSequence = boost :: asio: :mutable_buffers_1、Handler = boost :: _ bi :: bind_t、boost :: arg <1>()()、boost :: arg <2>()()、boost :: _ bi :: value >>>]'/home/chris/proto/boost/boost/asio/detail/reactive_socket_recv_op.hpp:80:' boost :: asio :: detail::reactive_socket_recv_opからインスタンス化:: react_socket_recv_op(boost :: asio :: detail :: socket_type、boost :: asio :: detail :: socket_ops :: state_type、const MutableBufferSequence&、int、Handler&)[with MutableBufferSequence = boost :: asio :: mutable_buffers_1、Handler = boost :: _ bi :: bind_t、boost :: arg <1>()()、boost :: arg <2>()()、boost :: _ bi :: value >>>]'/home/chris/proto/boost/boost/asio/detail/reactive_socket_service_base.hpp:273:' void boost :: asio ::detail::からインスタンス化react_socket_service_base :: async_receive(boost :: asio :: detail :: react_socket_service_base :: base_implementation_type&、const MutableBufferSequence&、int、Handler)[with MutableBufferSequence = boost :: asio :: mutable_buffers_1、Handler = boost :: _ bi :: bin_ :arg <1>()()、boost :: arg <2>()()、boost :: _ bi :: value >>>]'/home/chris/proto/boost/boost/asio/stream_socket_service.hpp:318:' void boost :: asio :: stream_socket_service :: async_receive( typename boost :: asio :: detail :: react_socket_service ::implementation_type&、const MutableBufferSequence&、int、const ReadHandler&)[with MutableBufferSequence = boost :: asio :: mutable_buffers_1、ReadHandler = boost :: _ bi :: bind_t、boost :: arg < 1>()()、boost :: arg <2>()()、boost :: _ bi :: value >>>、Protocol = boost :: asio :: ip :: tcp]'/ home / chris / proto /boost/boost/asio/basic_stream_socket.hpp:787:'void boost :: asio :: basic_stream_socket :: async_read_some(const MutableBufferSequence&、const ReadHandler&)[with MutableBufferSequence = boost :: asio :: mutable_buffers_1、ReadHandler = boost :: _ bi :: bind_t、boost :: arg <1>()()、boost :: arg <2>()()、boost :: _ bi :: value >>>、Protocol = boost :: asio :: ip :: tcp、StreamSocketService = boost :: asio :: stream_socket_service] '/home/chris/proto/inc/ws_async_tcp_connection.h:120:からインスタンス化' void ws_async_tcp_connection :: async_recv_msg_some(const T&、Handler)[with T = boost :: asio :: mutable_buffers_1、Handler = boost :: _ bi :: bind_t、boost :: arg <1>()()、boost :: arg <2>()()、boost :: _ bi :: value >>>]'async_tcp_test_server.cpp:46:ここからインスタンス化/ home / chris / proto / boost / boost / asio / detail / bind_handler.hpp:118:エラー:'(boost :: _ bi :: bind_t、boost :: arg <1>()()、boost :: arg <2>()()、boost :: _ bi :: value

4

1 に答える 1

2

コンパイラが言ったように、あなたが呼び出している関数は、バッファへの非定数参照を期待しています: async_recv_msg_some(T&, ... [with T = boost::asio::mutable_buffers_1

バッファを値で渡しているasync_recv_msg_some(boost::asio::mutable_buffers_1,のは、右辺値式を使用して一時オブジェクトとしてバッファを作成したためですboost::asio::buffer(recv_buf)

T関数のシグネチャをtakeまたはに変更する必要がありますconst T&(これが必要const T&なのでasync_read_some、関数はそれに転送するだけです)

于 2012-12-21T02:14:42.733 に答える