c++ - 我可以在两个不同的类实例中使用 boost asio 套接字引用吗?

标签 c++ sockets boost boost-asio move

我要std::move(my_asio_socket)到某个类的实例。如果我对其他类的实例做同样的事情怎么办?

我的目的是read_async在一个类实例中(假设类 A ),并执行 write_async在另一个(假设类 B )。由于某些功能实现,我应该这样做。

任何建议都会很好。

已更新

它没有像我预期的那样正常工作。
VS2015(vc14编译器)在调试时向我展示了一种异常:

Microsoft C++ exception: boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> > at memory location 0x02D8E3BC.

在我点击 continue 之后在 VS 中它告诉我:

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

正如我所说,我正在尝试 boost::move (或 std::move )到其他类实例的套接字。这是我正在做的代码片段:

boost::shared_ptr<connection> new_connection_;//it's a field in my class,
//that's why i should reset it later
new_connection_.reset(new connection(std::move(socket_), io_service_));
new_connection_->start();

现在这是我的 connection负责人:

    connection::connection(boost::asio::ip::tcp::socket sock, boost::asio::io_service& io_ptr)
    : socket_(boost::move(sock)),
      io_ptr_(io_ptr),
      remote_ep(socket_.remote_endpoint()),
      //request_handler_(new request_handler(boost::move(socket_))),
      work(new boost::asio::io_service::work(io_ptr))
    {
      boost::shared_ptr<request_handler> req_ptr(new request_handler(boost::move(socket_)));
      request_handler_.swap(req_ptr);
    }

如您所见,我已尝试在注释行中进行初始化,但它显示的结果相同。
这是 request_handler 的构造函数:

    request_handler::request_handler(boost::asio::ip::tcp::socket sock):
                                           socket_(boost::move(sock)){ }

这是我的方法connection::start检测到问题的地方:

void connection::start()
{
    boost::asio::ip::tcp::socket::keep_alive kl(true);
    boost::asio::ip::tcp::socket::enable_connection_aborted eca(true);
    // here it breaks, when trying to set an option
    socket_.set_option(eca);
    socket_.set_option(kl);

    socket_.async_read_some(boost::asio::buffer(buffer_),
        boost::bind(&connection::handle_read, shared_from_this(),
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
}

此问题仅在我使用 move socket时出现进入 request_handler 的新实例.如果我不是——那么一切都会好起来的。我不明白这可能是什么原因,但看起来像我的 socket_ (我在没有任何 &* 的地方声明它)来自 connection 的字段类丢失或有点。但是调试器没有向我显示 null 之类的东西,所以我不知道它是什么。

最佳答案

In general不能保证:

Move assignment operators typically "steal" the resources held by the argument (e.g. pointers to dynamically-allocated objects, file descriptors, TCP sockets, I/O streams, running threads, etc.), rather than make copies of them, and leave the argument in some valid but otherwise indeterminate state. For example, move-assigning from a std::string or from a std::vector may result in the argument being left empty. However, this behaviour should not be relied upon.

但根据 tcp::socket 的文档它保持相同的状态:

Following the move, the moved-from object is in the same state as if constructed using the basic_stream_socket(io_service&) constructor.

关于c++ - 我可以在两个不同的类实例中使用 boost asio 套接字引用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37444378/

相关文章:

c++ - 我如何在后台的 dll 中使用 muti boost 线程

c++ - T* 与 char* 指针算法

php - PHP的简单服务器/客户端(UDP,数据报)无法看到对方

python - 为什么在同一端口上但来自不同组的多播消息会合并在一起?

c - 如何在没有 while 循环的情况下使用 nanomsg 调查架构?

C++ 11 Thread 与 Boost Thread 有什么区别吗?

c++ - boost 源代码

c++ - 错误: Initialization with '{...}' expected for aggregate object

c++ - 在多线程程序中使用管道与子通信

c++ - 如何识别 C++ 中循环依赖的来源