2016-10-17 2 views
0

입력을 제공하기 위해 클래스에서 시프트 연산자를 오버로드했습니다. 그 방법에서 동기화 asio::write()을 수행 한 후 즉시 비동기를 수행합니다. asio::async_read(). 내 문제는 전환 과부하가 내 수업의 친구가되어야한다는 것입니다.friend 메소드의 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, 
           socket, 
           std::placeholders::_1)); 
    } 

내가 오류를 얻을 : 나는 소켓에 대한 참조를 전달하는 경우

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

:

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)); 
    } 

내가 오류가 발생합니다 :

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; 
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

답변

2

소켓 복사본에 바인딩하는 중입니다. 불법입니다.

이 더 낫다 : (바인딩은 시대 착오적이기 때문에)

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

이 더 나은 것입니다 :

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