SSLクライアントサーバーモデルでは、以下のコードを使用してクライアントまたはサーバー側のソケットからデータを読み取ります。利用可能なBoost asio tcpソケットが不正なバイト数を報告します
利用可能なデータがある場合にのみデータを読み取ります。利用可能なデータがあるかどうかを知るため、asio::ssl::stream
のlowest_layer()
にあるavailable()
メソッドを確認します。 クライアントからサーバーに380バイトを送り、サーバーにreadメソッドを入力すると、次のように表示されます。
's'は私が提供したバッファです。
'n'は、指定したバッファのサイズです。
'a1'は読み取りの前にavailable()の結果であり、458バイトを報告します。
'r'は実際に読み取られたバイト数です。それは正しいと報告するでしょう380。
'a2'は読み取り後にavailable()の結果であり、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;
メンバー:readメソッドの
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();
追加情報:
私はreadメソッドを変更して、まだboost :: asio :: ssl :: streamから読み込めるデータがあるかどうかをより完全にチェックしました。ソケットレベルで利用可能なデータがあるかどうかを確認する必要があるだけでなく、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'ハンドルがポーリングするのでしょうか? –