SSL 클라이언트 서버 모델에서 아래 코드를 사용하여 클라이언트 또는 서버 측 소켓의 데이터를 읽습니다.부스트 asio tcp 소켓 사용 가능한 바이트 수가 올바르지 않음을보고합니다.
사용할 수있는 데이터가있는 경우에만 데이터를 읽습니다. 사용할 수있는 데이터가 언제 있는지 알기 위해 asio::ssl::stream
의 lowest_layer()
에있는 available()
메소드를 확인합니다. 클라이언트에서 서버로 380 바이트를 보내고 서버에 read 메소드를 입력하면 다음과 같이 표시됩니다.
's'는 내가 제공 한 버퍼입니다.
'n'은 내가 제공 한 버퍼의 크기입니다.
'a1'은 읽기 전에 available()의 결과이며 458 바이트를보고합니다.
'r'은 실제로 읽은 바이트 수입니다. 그것은 올바른지 380을보고합니다.
'a2'는 읽기 후에 사용 가능한() 결과이며 0 바이트를보고합니다. 이것은 내 클라이언트가 380 바이트를 보낸 이후에 모두를 읽었 기 때문에 예상 한 것입니다.
왜 available()
에 대한 첫 번째 호출이 너무 많은 바이트를보고합니까?
유형 :
/**
* Type used as SSL Socket. Handles SSL and socket functionality.
*/
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> SslSocket;
/**
* A shared pointer version of the SSL Socket type.
*/
typedef boost::shared_ptr<SslSocket> ShpSslSocket;
회원 : 읽기 방법의
ShpSslSocket m_shpSecureSocket;
부 :
std::size_t a1 = 0;
if ((a1 = m_shpSecureSocket->lowest_layer().available()) > 0)
{
r += boost::asio::read(*m_shpSecureSocket,
boost::asio::buffer(s, n),
boost::asio::transfer_at_least(1));
}
std::size_t a2 = m_shpSecureSocket->lowest_layer().available();
추가 정보 :
그래서 boost :: asio :: ssl :: stream에서 읽을 수있는 데이터가 있는지 철저하게 확인하기 위해 read 메소드를 변경했습니다. 소켓 레벨에서 사용할 수있는 데이터가 있는지 확인할 필요가있을뿐만 아니라 OpenSSL 버퍼에 어딘가에 데이터가있을 수 있습니다. SSL_peek이 트릭을 수행합니다. 사용 가능한 데이터가 있는지 확인한 다음 TCP 포트 상태를 확인하고 시간 초과가없는 한이 작업을 수행합니다.
여기에 내가 작성한 boost :: iostreams :: device 클래스의 전체 읽기 메소드가 있습니다.
std::streamsize SslClientSocketDevice::read(char* s, std::streamsize n)
{
// Request from the stream/device to receive/read bytes.
std::streamsize r = 0;
LIB_PROCESS::TcpState eActualState = LIB_PROCESS::TCP_NOT_EXIST;
char chSslPeekBuf; // 1 byte peek buffer
// Check that there is data available. If not, wait for it.
// Check is on the lowest layer (tcp). In that layer the data is encrypted.
// The number of encrypted bytes is most often different than the number
// of unencrypted bytes that would be read from the secure socket.
// Also: Data may be read by OpenSSL from the socket and remain in an
// OpenSSL buffer somewhere. We also check that.
boost::posix_time::ptime start = BOOST_UTC_NOW;
int nSslPeek = 0;
std::size_t nAvailTcp = 0;
while ((*m_shpConnected) &&
(LIB_PROCESS::IpMonitor::CheckPortStatusEquals(GetLocalEndPoint(),
GetRemoteEndPoint(),
ms_ciAllowedStates,
eActualState)) &&
((nAvailTcp = m_shpSecureSocket->lowest_layer().available()) == 0) &&
((nSslPeek = SSL_peek(m_shpSecureSocket->native_handle(), &chSslPeekBuf, 1)) <= 0) && // May return error (<0) as well
((start + m_oReadTimeout) > BOOST_UTC_NOW))
{
boost::this_thread::sleep(boost::posix_time::millisec(10));
}
// Always read data when there is data available, even if the state is no longer valid.
// Data may be reported by the TCP socket (num encrypted bytes) or have already been read
// by SSL and not yet returned to us.
// Remote party can have sent data and have closed the socket immediately.
if ((nAvailTcp > 0) || (nSslPeek > 0))
{
r += boost::asio::read(*m_shpSecureSocket,
boost::asio::buffer(s, n),
boost::asio::transfer_at_least(1));
}
// Close socket when state is not valid.
if ((eActualState & ms_ciAllowedStates) == 0x00)
{
LOG4CXX_INFO(LOG4CXX_LOGGER, "TCP socket not/no longer connected. State is: " <<
LIB_PROCESS::IpMonitor::TcpStateToString(eActualState));
LOG4CXX_INFO(LOG4CXX_LOGGER, "Disconnecting socket.");
Disconnect();
}
if (! (*m_shpConnected))
{
if (r == 0)
{
r = -1; // Signal stream is closed if no data was retrieved.
ThrowExceptionStreamFFL("TCP socket not/no longer connected.");
}
}
return r;
}
이것은 지나치게 복잡해 보입니다. 왜 'async_read()'를 사용하지 말고'io_service' 핸들이 폴링을할까요? –