0

現在書いているアプリケーションでこのコード スニペットを取得しました。

#include "ClientSession.hpp"
void ClientSession::start(void)
{
    auto Self(shared_from_this());

    //boost::asio::read(this->_Socket, boost::asio::buffer(this->_ReadBuffer));

    //this->_Socket.get_io_service().post(boost::bind(&ClientSession::on_read, shared_from_this(), boost::system::error_code::error_code(), 0));

    boost::asio::async_read(this->_Socket, boost::asio::buffer(this->_ReadBuffer), boost::bind(&ClientSession::on_read, Self, _1, _2));
}

void ClientSession::on_read(const boost::system::error_code& Err_, size_t Bytes_)
{
    try 
    {
        if(Err_)
        {
            if(!(Err_ == boost::asio::error::eof || Err_ == boost::asio::error::connection_aborted))
            {
                throw std::runtime_error(Err_.message());
            }
            return;
        }

    }
    catch(std::exception& Ex_)
    {
        this->_OutputHandler.write_error(Ex_.what());
    }
}

ClientSession::start を呼び出すコードは次のとおりです。

void Application::start_accept()
{

    using namespace boost::asio;
    ip::tcp::endpoint Endpoint( ip::address::from_string(this->_Config.get_client_login_server().first), this->_Config.get_client_login_server().second );
    this->_ClientAcceptor.open(Endpoint.protocol());
    this->_ClientAcceptor.bind(Endpoint);
    this->_ClientAcceptor.listen();

    std::shared_ptr<ClientSession> Session = ClientSession::create_new(this->_AsioService, this->_OutputHandler);
    this->_ClientAcceptor.async_accept(Session->get_socket(), boost::bind(&Application::handle_accept, shared_from_this(), Session, _1));

}

コメントアウトされた行の 1 つを使用してアプリケーションを実行すると、正常に動作します (boost::asio::read バージョンでは、クライアントから送信されたメッセージが読み取りバッファーに書き込まれ、ポスト バージョン中にハンドラー関数が呼び出されます)。問題なく)、しかし、私のアプリケーションを async_read 部分で使用しようとすると、完全なハンドルを呼び出すことも!バイトを読み取ることさえせずに、アプリケーション全体が常にクラッシュします!少なくとも、asio が提供する出力からはそのように見えます。 :

@asio|1373633560.834957|0*1|socket@000000BD603D46C0.async_accept @asio|1373633573.324144|>1|ex=system:0 @asio|1373633573.324144|1*2|socket@000000BD603D46C0.async_receive_DBAB8.async_accept @asio|1373633573.324144|>1|ex=system:0

アプリケーションがboost::asio::detail::mutex::scoped_lockを取得しようとする「select_reactor.ipp」という名前のファイルにデバッグしましたが、そこで失敗しているように見えます。メソッドの完全なコードは次のとおりです。

void select_reactor::start_op(int op_type, socket_type descriptor,
select_reactor::per_descriptor_data&, reactor_op* op,
bool is_continuation, bool)
{
  boost::asio::detail::mutex::scoped_lock lock(mutex_);

  if (shutdown_)
  {
    post_immediate_completion(op, is_continuation);
    return;
  }

  bool first = op_queue_[op_type].enqueue_operation(descriptor, op);
  io_service_.work_started();
  if (first)
    interrupter_.interrupt();
}

ひどく間違ったことをしているのかもしれませんが、何がわからないのでしょうか。誰かがこのエラーを解決するために何ができるかのヒントを教えてくれることを願っています。

これまでに得たコメントに基づいて、より完全なコード スニペットを追加します。io_service は Application クラス内にあります。

class Application
    : public std::enable_shared_from_this<Application>
{
typedef boost::asio::io_service Service;
typedef boost::asio::ip::tcp::acceptor Acceptor;

public:
    Application(void) 
        : _ClientAcceptor(_AsioService), _ApplicationRunning(true)
    {
    }

    ~Application(void);

    /*
     * This is the main method which starts the actual program routine
     * called in main()
     */
    int run(void);

private:
    /*
     * Invokes the first accept and then invokes via async. handle_accept, which invokes itself repeatedly
     */
    void start_accept(void);
    /*
     * handle_accept calls NewClient_->start to invoke the communication with this client 
     */
    void handle_accept(std::shared_ptr<ClientSession> NewClient_, const boost::system::error_code& Err_);

    void loop_for_user_input(void);
    bool handle_user_input(const std::string& Msg_);

    /*
     * This method invokes the threads that call io_service::run()
     */
    static void start_service(Service& AsioService_) { try { AsioService_.run(); } catch(const std::exception& Ex_) { std::cout << Ex_.what() << std::endl; } }

private:
    Configuration _Config;
    Service _AsioService;
    Acceptor _ClientAcceptor;
    OutputHandler _OutputHandler;

    bool _ApplicationRunning;
};

そして実行方法:

int Application::run(void)
{
    try 
    {
        this->_Config.load();
        this->_OutputHandler.open(this->_Config.get_logs());

        this->start_accept();

        for(int i = 0; i < 4; i++)
        {
            std::thread t(start_service, std::ref(this->_AsioService));
            t.detach();
        }

        this->loop_for_user_input();

    }
    catch(const std::runtime_error& Ex_)
    {
        this->_OutputHandler.write_error(Ex_.what());
    }

    return 0;
}
4

1 に答える 1

1

エラーが見つかりました。これは、_WIN32_WINNT 0x0601 (Windows 7 の場合) に問題を解決した _WIN32_WINNT プリプロセッサ ディレクティブの設定の単なるタイプミスでした。

于 2013-07-12T21:14:48.750 に答える