为什么客户端在async_write上获得EOF?

时间:2019-10-06 10:40:56

标签: c++ boost boost-asio

我正在使用boost :: asio编写异步服务器。我将boost HTTP服务器示例更改为仅使用简单的protobuf。现在,当服务器尝试使用async_write发送消息时,客户端会收到libc++abi.dylib: terminating with uncaught exception of type boost::wrapexcept<boost::system::system_error>: read: End of file错误。

我尝试了同步和异步客户端,不同类型的缓冲区,但是问题仍然存在。一切顺利,直到async_write。

        void TConnection::DoRead() {
            std::cout << "Reading data" << std::endl;
            auto self(shared_from_this());
            Socket.async_read_some(boost::asio::buffer(Buffer),
                                    [this, self](boost::system::error_code ec, std::size_t bytes_transferred)
                                    {
                                        if (!ec) {
                                            TRequestParser::ResultType result;
                                            TMessage request;
                                            request.Parse(Buffer.data(), bytes_transferred);
                                            Reply = MasterPtr->HandleMessange(request);
                                            std::cout << "Replying: " << Reply.Content << std::endl;
                                            DoWrite();
                                        } else if (ec != boost::asio::error::operation_aborted) {
                                            ConnectionManager.Stop(shared_from_this());
                                        }
                                    });
        }

        void TConnection::DoWrite() {
            std::cout << "Writing data" << std::endl;
            auto self(shared_from_this());
            std::cout << "Replying2: " << Reply.Content << std::endl;
            Reply.ToStreamBuf(&Streambuf);
            std::cout << "Now I'm gonna write" << std::endl;
            std::cout << Streambuf.size() << std::endl;
            boost::asio::async_write(Socket, Streambuf,
                                     [this, self](boost::system::error_code ec, std::size_t)
                                     {
                                         std::cout << "Oh, handled" << std::endl;
                                         if (!ec)
                                         {
                                             // Initiate graceful connection closure.
                                             boost::system::error_code ignored_ec;
                                             Socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both,
                                                              ignored_ec);
                                         }
                                         if (ec != boost::asio::error::operation_aborted)
                                         {
                                             ConnectionManager.Stop(shared_from_this());
                                         }
                                     });
        }

我做错了什么?

0 个答案:

没有答案