http upgrade and connections use non blocking sockets

This commit is contained in:
Benjamin Sergeant
2018-12-09 14:07:40 -08:00
parent f77ececc92
commit 5bf1b91528
9 changed files with 255 additions and 110 deletions

View File

@ -231,14 +231,13 @@ namespace ix
if (_socket->send(const_cast<char*>(request.c_str()), requestSize) != requestSize)
{
return WebSocketInitResult(false, 0, std::string("Failed sending GET request to ") + _url);
}
char line[256];
int i;
for (i = 0; i < 2 || (i < 255 && line[i-2] != '\r' && line[i-1] != '\n'); ++i)
{
if (_socket->recv(line+i, 1) == 0)
if (!readByte(line+i))
{
return WebSocketInitResult(false, 0, std::string("Failed reading HTTP status line from ") + _url);
}
@ -282,7 +281,7 @@ namespace ix
i < 2 || (i < 255 && line[i-2] != '\r' && line[i-1] != '\n');
++i)
{
if (_socket->recv(line+i, 1) == 0)
if (!readByte(line+i))
{
return WebSocketInitResult(false, status, std::string("Failed reading response header from ") + _url);
}
@ -344,7 +343,6 @@ namespace ix
}
}
_socket->configure();
setReadyState(OPEN);
return WebSocketInitResult(true, status, "", headers);
@ -807,4 +805,33 @@ namespace ix
_socket->close();
}
// Used by init
bool WebSocketTransport::readByte(void* buffer)
{
while (true)
{
if (_readyState == CLOSING) return false;
int ret;
ret = _socket->recv(buffer, 1);
// We read one byte, as needed, all good.
if (ret == 1)
{
return true;
}
// There is possibly something to be read, try again
else if (ret < 0 && (_socket->getErrno() == EWOULDBLOCK ||
_socket->getErrno() == EAGAIN))
{
continue;
}
// There was an error during the read, abort
else
{
return false;
}
}
}
} // namespace ix