when trying to flush the send buffer, use select to wait until it is possible instead of using sleep to retry at a given frequency

This commit is contained in:
Benjamin Sergeant
2019-03-18 14:25:27 -07:00
parent e98634a277
commit d58798e36c
11 changed files with 126 additions and 53 deletions

View File

@ -1,18 +1,18 @@
/*
* The MIT License (MIT)
*
*
* Copyright (c) 2012, 2013 <dhbaird@gmail.com>
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -202,13 +202,20 @@ namespace ix
{
while (!isSendBufferEmpty() && !_requestInitCancellation)
{
sendOnSocket();
// Sleep 10ms between each send so that we dont busy loop
// A better strategy would be to select on the socket to
// check whether we can write to it without blocking
std::chrono::duration<double, std::micro> duration(10);
std::this_thread::sleep_for(duration);
// Wait with a 10ms timeout until the socket is ready to write.
// This way we are not busy looping
int timeoutMs = 10;
PollResultType result = _socket->isReadyToWrite(0, 10);
if (result == PollResultType_Error)
{
_socket->close();
setReadyState(CLOSED);
break;
}
else if (result == PollResultType_ReadyForWrite)
{
sendOnSocket();
}
}
}
else if (pollResult == PollResultType_ReadyForRead)