Add non-blocking recv function to Socket class

This commit is contained in:
baldurk
2017-11-27 18:50:49 +00:00
parent b418a5db6a
commit 53ddb39e19
3 changed files with 65 additions and 0 deletions
+1
View File
@@ -147,6 +147,7 @@ public:
bool SendDataBlocking(const void *buf, uint32_t length);
bool RecvDataBlocking(void *data, uint32_t length);
bool RecvDataNonBlocking(void *data, uint32_t &length);
private:
ptrdiff_t socket;
+32
View File
@@ -183,6 +183,38 @@ bool Socket::IsRecvDataWaiting()
return ret > 0;
}
bool Socket::RecvDataNonBlocking(void *buf, uint32_t &length)
{
if(length == 0)
return true;
// socket is already blocking, don't have to change anything
int ret = recv(socket, (char *)buf, length, 0);
if(ret > 0)
{
length = (uint32_t)ret;
}
else
{
length = 0;
int err = errno;
if(err == EWOULDBLOCK || err == EAGAIN)
{
return true;
}
else
{
RDCWARN("recv: %d", err);
Shutdown();
return false;
}
}
return true;
}
bool Socket::RecvDataBlocking(void *buf, uint32_t length)
{
if(length == 0)
+32
View File
@@ -184,6 +184,38 @@ bool Socket::IsRecvDataWaiting()
return ret > 0;
}
bool Socket::RecvDataNonBlocking(void *buf, uint32_t &length)
{
if(length == 0)
return true;
// socket is already blocking, don't have to change anything
int ret = recv(socket, (char *)buf, length, 0);
if(ret > 0)
{
length = (uint32_t)ret;
}
else
{
length = 0;
int err = WSAGetLastError();
if(err == WSAEWOULDBLOCK)
{
return true;
}
else
{
RDCWARN("recv: %d", err);
Shutdown();
return false;
}
}
return true;
}
bool Socket::RecvDataBlocking(void *buf, uint32_t length)
{
if(length == 0)