Implement reading unknown lengths of data from socket.

This commit is contained in:
Bartosz Taudul 2020-09-10 21:39:58 +02:00
parent 72ce3ccf15
commit e2d69e7981
2 changed files with 19 additions and 0 deletions

View File

@ -351,6 +351,24 @@ int Socket::Recv( void* _buf, int len, int timeout )
} }
} }
int Socket::ReadUpTo( void* _buf, int len, int timeout )
{
const auto sock = m_sock.load( std::memory_order_relaxed );
auto buf = (char*)_buf;
int rd = 0;
while( len > 0 )
{
const auto res = recv( sock, buf, len, 0 );
if( res == 0 ) break;
if( res == -1 ) return -1;
len -= res;
rd += res;
buf += res;
}
return rd;
}
bool Socket::Read( void* buf, int len, int timeout ) bool Socket::Read( void* buf, int len, int timeout )
{ {
auto cbuf = (char*)buf; auto cbuf = (char*)buf;

View File

@ -30,6 +30,7 @@ public:
int Send( const void* buf, int len ); int Send( const void* buf, int len );
int GetSendBufSize(); int GetSendBufSize();
int ReadUpTo( void* buf, int len, int timeout );
bool Read( void* buf, int len, int timeout ); bool Read( void* buf, int len, int timeout );
template<typename ShouldExit> template<typename ShouldExit>