我是 OpenSSL 新手。我知道 BIO_write(BIO *b, const void *buf, int len) 需要在循环中调用,但我不完全确定我是否正确使用它。我写了一个这样的函数:
int32_t SendPacket(BIO * const pBio, const unsigned char * const pPacket, const int nPacketLength)
{
int32_t nPos = 0;
if (!pBio || !pPacket || !nPacketLength)
return -1;
while (nPos < nPacketLength)
{
int32_t nNumberOfBytesWritten = BIO_write(pBio, &pPacket[nPos], nPacketLength - nPos);
if (nNumberOfBytesWritten <= 0)
{
if (!BIO_should_retry(pBio))
return -1;
}
else
{
nPos += nNumberOfBytesWritten;
}
}
return nPos;
}
我正在考虑这样使用它:
if (SendPacket(pBio, pPacket, nPacketLength) == nPacketLength)
{
// Packet sent correctly.
}
else
{
// Error occurred.
}
该函数看起来正确吗?如有任何反馈,我们将不胜感激。
在我原来的帖子中,当重试 BIO_write() 时,我没有将“nNumberOfBytesWritten”重置为 0。
我最终做了这样的事情:
int32_t SendPacket(BIO * const pBio, const unsigned char * const pPacket, const int32_t nPacketLength)
{
int32_t nPos = 0;
int32_t nNumberOfBytesWritten = 0;
...
for (nPos = 0; nPos < nPacketLength; nPos += nNumberOfBytesWritten)
{
if ((nNumberOfBytesWritten = BIO_write(pBio, pPacket + nPos, nPacketLength - nPos)) <= 0)
{
if (BIO_should_retry(pBio))
{
nNumberOfBytesWritten = 0;
continue;
}
return -1;
}
}
return nPos;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)