简体   繁体   English

从朋友方法使用async_read

[英]Using async_read from friend method

I've overloaded the shift operator in a class to provide input. 我在类中重载了shift运算符以提供输入。 I do a sync asio::write() in that method then do an async asio::async_read() immediately after. 我在该方法中执行async asio::write() ,然后立即执行async asio::async_read() My problem is that the shift overload needs to be a friend of my class. 我的问题是,超负荷工作需要成为我班上的朋友。

If I supply this to async_read: 如果我将此提供给async_read:

void operator>>(const vector<unsigned char> input, Socket &socket) {
      const size_t size = input.size();
      const size_t bytes = asio::write(socket.connection_socket, asio::buffer(input, size));
      if (bytes != size) {
        const std::error_code ec;
        throw std::system_error(ec, fmt::format("Tried to send {0} bytes but sent {1} instead.", size, bytes));
      }
      asio::async_read(socket.connection_socket,
                       asio::buffer(socket.read_buffer),
                       std::bind(&Socket::handle_async_read,
                                 this,
                                 std::placeholders::_1));
    }

I get the error: 我得到错误:

error: invalid use of 'this' outside of a non-static member function

If I pass the reference to socket: 如果我将引用传递给套接字:

void operator>>(const vector<unsigned char> input, Socket &socket) {
      const size_t size = input.size();
      const size_t bytes = asio::write(socket.connection_socket, asio::buffer(input, size));
      if (bytes != size) {
        const std::error_code ec;
        throw std::system_error(ec, fmt::format("Tried to send {0} bytes but sent {1} instead.", size, bytes));
      }
      asio::async_read(socket.connection_socket,
                       asio::buffer(socket.read_buffer),
                       std::bind(&Socket::handle_async_read,
                                 socket,
                                 std::placeholders::_1));
    }

I get the error: 我得到错误:

error: call to implicitly-deleted copy constructor of 'std::__1::__bind<void
      (databaseclient::internal::Socket::*)(std::__1::error_code &, unsigned long), databaseclient::internal::Socket &, std::__1::placeholders::__ph<1> &>'
  ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You're binding to a copy of the socket, which is illegal. 您绑定到套接字的副本,这是非法的。

this is better: 这个更好:

asio::async_read(socket.connection_socket,
                       asio::buffer(socket.read_buffer),
                       std::bind(&Socket::handle_async_read,
                                 std::ref(socket),
                                 std::placeholders::_1));

this is even better (since bind is anachronistic): 这甚至更好(因为绑定是不合时宜的):

asio::async_read(socket.connection_socket,
                       asio::buffer(socket.read_buffer),
                       [&socket](auto const& ec, auto transferred) 
                       {
                         handle_async_read(socket, ec);
                       });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM