Skip to content

Commit 5758673

Browse files
authored
Fix a bug with SSL on linux (#43)
1 parent b18b284 commit 5758673

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

trantor/net/inner/TcpConnectionImpl.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -693,13 +693,18 @@ void TcpConnectionImpl::sendFileInLoop(const BufferNodePtr &filePtr)
693693
}
694694
#endif
695695
lseek(filePtr->_sendFd, filePtr->_offset, SEEK_SET);
696+
if (!_fileBufferPtr)
697+
{
698+
_fileBufferPtr = std::make_unique<std::vector<char>>(16 * 1024);
699+
}
696700
while (filePtr->_fileBytesToSend > 0)
697701
{
698-
std::vector<char> buf(1024 * 1024);
699-
auto n = read(filePtr->_sendFd, &buf[0], buf.size());
702+
auto n = read(filePtr->_sendFd,
703+
&(*_fileBufferPtr)[0],
704+
_fileBufferPtr->size());
700705
if (n > 0)
701706
{
702-
auto nSend = writeInLoop(&buf[0], n);
707+
auto nSend = writeInLoop(&(*_fileBufferPtr)[0], n);
703708
if (nSend >= 0)
704709
{
705710
filePtr->_fileBytesToSend -= nSend;

trantor/net/inner/TcpConnectionImpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ class TcpConnectionImpl : public TcpConnection,
231231

232232
size_t _bytesSent = 0;
233233
size_t _bytesReceived = 0;
234+
235+
std::unique_ptr<std::vector<char>> _fileBufferPtr;
234236
};
235237

236238
typedef std::shared_ptr<TcpConnectionImpl> TcpConnectionImplPtr;

trantor/net/ssl/SSLConnection.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,15 @@ ssize_t SSLConnection::writeInLoop(const char *buffer, size_t length)
262262
}
263263

264264
// send directly
265+
ERR_clear_error();
265266
auto sendLen = SSL_write(_sslPtr->get(), buffer, length);
266267
if (sendLen <= 0)
267268
{
268269
int sslerr = SSL_get_error(_sslPtr->get(), sendLen);
269270
if (sslerr != SSL_ERROR_WANT_WRITE && sslerr != SSL_ERROR_WANT_READ)
270271
{
271272
LOG_ERROR << "ssl write error:" << sslerr;
273+
forceClose();
272274
return -1;
273275
}
274276
return 0;

0 commit comments

Comments
 (0)