Use per-message send buffer, rather than per connection
This commit is contained in:
committed by
Pieter Wuille
parent
967f24590b
commit
41b052ad87
57
src/net.cpp
57
src/net.cpp
@@ -715,26 +715,43 @@ int CNetMessage::readData(const char *pch, unsigned int nBytes)
|
||||
// requires LOCK(cs_vSend)
|
||||
void SocketSendData(CNode *pnode)
|
||||
{
|
||||
CDataStream& vSend = pnode->vSend;
|
||||
if (vSend.empty())
|
||||
return;
|
||||
std::deque<CSerializeData>::iterator it = pnode->vSendMsg.begin();
|
||||
|
||||
int nBytes = send(pnode->hSocket, &vSend[0], vSend.size(), MSG_NOSIGNAL | MSG_DONTWAIT);
|
||||
if (nBytes > 0)
|
||||
{
|
||||
vSend.erase(vSend.begin(), vSend.begin() + nBytes);
|
||||
pnode->nLastSend = GetTime();
|
||||
}
|
||||
else if (nBytes < 0)
|
||||
{
|
||||
// error
|
||||
int nErr = WSAGetLastError();
|
||||
if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
|
||||
{
|
||||
printf("socket send error %d\n", nErr);
|
||||
pnode->CloseSocketDisconnect();
|
||||
while (it != pnode->vSendMsg.end()) {
|
||||
const CSerializeData &data = *it;
|
||||
assert(data.size() > pnode->nSendOffset);
|
||||
int nBytes = send(pnode->hSocket, &data[pnode->nSendOffset], data.size() - pnode->nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
|
||||
if (nBytes > 0) {
|
||||
pnode->nLastSend = GetTime();
|
||||
pnode->nSendOffset += nBytes;
|
||||
if (pnode->nSendOffset == data.size()) {
|
||||
pnode->nSendOffset = 0;
|
||||
pnode->nSendSize -= data.size();
|
||||
it++;
|
||||
} else {
|
||||
// could not send full message; stop sending more
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (nBytes < 0) {
|
||||
// error
|
||||
int nErr = WSAGetLastError();
|
||||
if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
|
||||
{
|
||||
printf("socket send error %d\n", nErr);
|
||||
pnode->CloseSocketDisconnect();
|
||||
}
|
||||
}
|
||||
// couldn't send anything at all
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (it == pnode->vSendMsg.end()) {
|
||||
assert(pnode->nSendOffset == 0);
|
||||
assert(pnode->nSendSize == 0);
|
||||
}
|
||||
pnode->vSendMsg.erase(pnode->vSendMsg.begin(), it);
|
||||
}
|
||||
|
||||
void ThreadSocketHandler(void* parg)
|
||||
@@ -776,7 +793,7 @@ void ThreadSocketHandler2(void* parg)
|
||||
BOOST_FOREACH(CNode* pnode, vNodesCopy)
|
||||
{
|
||||
if (pnode->fDisconnect ||
|
||||
(pnode->GetRefCount() <= 0 && pnode->vRecvMsg.empty() && pnode->vSend.empty()))
|
||||
(pnode->GetRefCount() <= 0 && pnode->vRecvMsg.empty() && pnode->nSendSize == 0 && pnode->ssSend.empty()))
|
||||
{
|
||||
// remove from vNodes
|
||||
vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
|
||||
@@ -863,7 +880,7 @@ void ThreadSocketHandler2(void* parg)
|
||||
TRY_LOCK(pnode->cs_vSend, lockSend);
|
||||
if (lockSend) {
|
||||
// do not read, if draining write queue
|
||||
if (!pnode->vSend.empty())
|
||||
if (!pnode->vSendMsg.empty())
|
||||
FD_SET(pnode->hSocket, &fdsetSend);
|
||||
else
|
||||
FD_SET(pnode->hSocket, &fdsetRecv);
|
||||
@@ -1032,7 +1049,7 @@ void ThreadSocketHandler2(void* parg)
|
||||
//
|
||||
// Inactivity checking
|
||||
//
|
||||
if (pnode->vSend.empty())
|
||||
if (pnode->vSendMsg.empty())
|
||||
pnode->nLastSendEmpty = GetTime();
|
||||
if (GetTime() - pnode->nTimeConnected > 60)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user