Skip to content

Commit b0bfcf5

Browse files
committed
More clang-tidy fixes for TcpSocket
* Copy trivial type instead of moving * Use std::array for fixed-size buffers * Use correct ssize_t type for read/write operations
1 parent 85b8f11 commit b0bfcf5

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

src/KDNetwork/tcp_socket.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ TcpSocket::TcpSocket(TcpSocket &&other) noexcept
9898
, m_readBuffer(std::move(other.m_readBuffer)) // Move the read buffer
9999
, m_writeBuffer(std::move(other.m_writeBuffer)) // Move the write buffer
100100
, m_pendingConnection(std::move(other.m_pendingConnection)) // Move the pending connection info
101-
, m_peerAddress(std::move(other.m_peerAddress)) // Move the peer address
101+
, m_peerAddress(other.m_peerAddress) // Copy the peer address (trivially copyable)
102102
, m_peerPort(other.m_peerPort) // Copy the port
103103
{
104104
}
@@ -114,7 +114,7 @@ TcpSocket &TcpSocket::operator=(TcpSocket &&other) noexcept
114114
m_readBuffer = std::move(other.m_readBuffer); // Move the read buffer
115115
m_writeBuffer = std::move(other.m_writeBuffer); // Move the write buffer
116116
m_pendingConnection = std::move(other.m_pendingConnection); // Move the pending connection info
117-
m_peerAddress = std::move(other.m_peerAddress); // Move the peer address
117+
m_peerAddress = other.m_peerAddress; // Copy the peer address (trivially copyable)
118118
m_peerPort = other.m_peerPort; // Copy the port
119119
}
120120
return *this;
@@ -455,19 +455,19 @@ void TcpSocket::onReadReady()
455455

456456
// Read data in a loop as readiness notification is level-triggered
457457
constexpr int tempBufferSize = 4096; // Sensible chunk size
458-
std::uint8_t tempBuffer[tempBufferSize];
458+
std::array<std::uint8_t, tempBufferSize> tempBuffer;
459459
ssize_t bytesRead = 0;
460460

461461
while (isValid()) { // Loop while socket is valid
462462
#if defined(KD_PLATFORM_WIN32)
463-
bytesRead = ::recv(m_socketFd, reinterpret_cast<char *>(tempBuffer), tempBufferSize, 0);
463+
bytesRead = ::recv(m_socketFd, reinterpret_cast<char *>(tempBuffer.data()), tempBufferSize, 0);
464464
#else
465-
bytesRead = ::recv(m_socketFd, reinterpret_cast<char *>(tempBuffer), tempBufferSize, 0);
465+
bytesRead = ::recv(m_socketFd, reinterpret_cast<char *>(tempBuffer.data()), tempBufferSize, 0);
466466
#endif
467467

468468
if (bytesRead > 0) {
469469
// Successfully read some data
470-
processReceivedData(tempBuffer, bytesRead);
470+
processReceivedData(tempBuffer.data(), bytesRead);
471471
} else if (bytesRead == 0) {
472472
// Peer has performed an orderly shutdown (EOF)
473473
setError(SocketError::NoError); // This is not an application error
@@ -598,10 +598,10 @@ void TcpSocket::trySend()
598598
return;
599599
}
600600

601-
int bytesSentTotal = 0; // Track bytes sent in this call
601+
ssize_t bytesSentTotal = 0; // Track bytes sent in this call
602602

603603
while (!m_writeBuffer.isEmpty()) {
604-
int bytesSentNow = 0;
604+
ssize_t bytesSentNow = 0;
605605
#if defined(KD_PLATFORM_WIN32)
606606
bytesSentNow = ::send(m_socketFd, reinterpret_cast<const char *>(m_writeBuffer.constData()),
607607
static_cast<int>(std::min((int64_t)m_writeBuffer.size(), (int64_t)INT_MAX)), // Windows send takes int size

0 commit comments

Comments
 (0)