我可以在两个不同的 class 实例中使用 boost asio 套接字引用吗?
Can i have boost asio socket references in two different class instances?
我想std::move(my_asio_socket)
到一些class的实例。如果我对其他 class 的实例做同样的事情会怎样?
我的目的是在一个 class 实例中 read_async
(比方说 class A
),并在另一个实例中执行 write_async
(比方说说 class 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.
我在 VS 中点击 continue
后,它显示:
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
)到其他 class 实例的套接字。这是我正在做的代码片段:
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
ctor:
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));
}
只有当我将 socket
移动到 request_handler
的新实例时才会出现此问题。如果我不是——那么一切都会好起来的。我不明白它可能是什么原因,但看起来像我的 socket_
(我到处声明它,没有任何 &
或 *
)字段来自 connection
class 丢失或种。但是调试器没有向我显示 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.
我想std::move(my_asio_socket)
到一些class的实例。如果我对其他 class 的实例做同样的事情会怎样?
我的目的是在一个 class 实例中 read_async
(比方说 class A
),并在另一个实例中执行 write_async
(比方说说 class 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.
我在 VS 中点击 continue
后,它显示:
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
)到其他 class 实例的套接字。这是我正在做的代码片段:
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
ctor:
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));
}
只有当我将 socket
移动到 request_handler
的新实例时才会出现此问题。如果我不是——那么一切都会好起来的。我不明白它可能是什么原因,但看起来像我的 socket_
(我到处声明它,没有任何 &
或 *
)字段来自 connection
class 丢失或种。但是调试器没有向我显示 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.