Unable to call callback

I use Boost.Asio for network operations, they should (and, in fact, cannot create complex data structures or anything else), remain quite low, because I cannot afford the luxury of serialization overhead (and libs I found that offered good enough performance, it seemed to be bad for my case).

The problem is the async record that I am making from the client (in QT, but that should probably be irrelevant here). The callback specified in async_writeis not called, ever, and I absolutely do not understand why. The code:

void SpikingMatrixClient::addMatrix() {
    std::cout << "entered add matrix" << std::endl;
    int action = protocol::Actions::AddMatrix;
    int matrixSize = this->ui->editNetworkSize->text().toInt();
    std::ostream out(&buf);
    out.write(reinterpret_cast<const char*>(&action), sizeof(action));
    out.write(reinterpret_cast<const char*>(&matrixSize), sizeof(matrixSize));
    boost::asio::async_write(*connection.socket(), buf.data(),
                             boost::bind(&SpikingMatrixClient::onAddMatrix, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}

which calls the first record. Callback

void SpikingMatrixClient::onAddMatrix(const boost::system::error_code& error, size_t bytes_transferred) {
    std::cout << "entered onAddMatrix" << std::endl;
    if (!error) {
        buf.consume(bytes_transferred);
        requestMatrixList();
    } else {
        QString message = QString::fromStdString(error.message());
        this->ui->statusBar->showMessage(message, 15000);
    }
}

, . - - , ?

P.S. , , , . , .

+5
2

, , ( ).

QT, , IO . .

:

  • GUI , IO.
  • /.
  • , / , ( 1.).

Boost.Asio,

io_service:: run(), , . , , io_service, , io_service .

, io_service.run() , io_service .

, io_service.run() .

void SpikingMatrixClient::connect() {
    Ui::ConnectDialog ui;
    QDialog *dialog = new QDialog;
    ui.setupUi(dialog);
    if (dialog->exec()) {
        QString host = ui.lineEditHost->text();
        QString port = ui.lineEditPort->text();
        connection = TcpConnection::create(io);
        boost::system::error_code error = connection->connect(host, port);
        if (!error) {
            io = boost::shared_ptr<boost::asio::io_service>(new boost::asio::io_service);
            work = boost::shared_ptr<boost::asio::io_service::work>(new boost::asio::io_service::work(*io));
            io_threads.create_thread(boost::bind(&SpikingMatrixClient::runIo, this, io));
        }
        QString message = QString::fromStdString(error.message());
        this->ui->statusBar->showMessage(message, 15000);
    }
}

IO, :

  • work boost::shared_ptr boost::asio::io_service::work, ,
  • io boost::shared_ptr boost::asio::io_service,
  • connection - boost::shared_ptr , connect() .. ,
  • io_threads boost::thread_group.

, typedef.

TcpConnection - , , , , . , ...

:

void SpikingMatrixClient::disconnect() {
    work.reset();
    io_threads.join_all();
    boost::system::error_code error = connection->disconnect();
    if (!error) {
        connection.reset();
    }
    QString message = QString::fromStdString(error.message());
    this->ui->statusBar->showMessage(message, 15000);
}
  • , io_service ,
  • , , , ,
  • disconnect() shutdown() close() , .

, , , ( C), disconnect() .

+2

( ), (io_service , ). , , -.

async_connect(), io_service.run(), , .

async_connect() on_connect_handler(), , , async_write().

on_write_complete_handler() , .

, , on_connect_handler(). , , async_write(), , on_connect_handler() . , , io_service , , . ( , , io_service async_x())

, .

+1

All Articles