diff --git a/common/TracySocket.cpp b/common/TracySocket.cpp index 1dc4873b..5152635e 100644 --- a/common/TracySocket.cpp +++ b/common/TracySocket.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -47,6 +48,9 @@ static __wsinit InitWinSock() Socket::Socket() : m_sock( -1 ) + , m_buf( (char*)tracy_malloc( BufSize ) ) + , m_bufPtr( nullptr ) + , m_bufLeft( 0 ) { #ifdef _MSC_VER InitWinSock(); @@ -55,11 +59,15 @@ Socket::Socket() Socket::Socket( int sock ) : m_sock( sock ) + , m_buf( (char*)tracy_malloc( BufSize ) ) + , m_bufPtr( nullptr ) + , m_bufLeft( 0 ) { } Socket::~Socket() { + tracy_free( m_buf ); if( m_sock != -1 ) { Close(); @@ -130,6 +138,36 @@ int Socket::Send( const void* _buf, int len ) return int( buf - start ); } +int Socket::RecvBuffered( void* buf, int len, const timeval* tv ) +{ + if( len <= m_bufLeft ) + { + memcpy( buf, m_bufPtr, len ); + m_bufPtr += len; + m_bufLeft -= len; + return len; + } + + if( m_bufLeft > 0 ) + { + memcpy( buf, m_bufPtr, m_bufLeft ); + const auto ret = m_bufLeft; + m_bufLeft = 0; + return ret; + } + + if( len >= BufSize ) return Recv( buf, len, tv ); + + m_bufLeft = Recv( m_buf, BufSize, tv ); + if( m_bufLeft <= 0 ) return m_bufLeft; + + const auto sz = std::min( len, m_bufLeft ); + memcpy( buf, m_buf, sz ); + m_bufPtr = m_buf + sz; + m_bufLeft -= sz; + return sz; +} + int Socket::Recv( void* _buf, int len, const timeval* tv ) { auto buf = (char*)_buf; @@ -161,7 +199,7 @@ bool Socket::Read( void* _buf, int len, const timeval* tv, std::function while( len > 0 ) { if( exitCb() ) return false; - const auto sz = Recv( buf, len, tv ); + const auto sz = RecvBuffered( buf, len, tv ); switch( sz ) { case 0: diff --git a/common/TracySocket.hpp b/common/TracySocket.hpp index fa29274f..9f19924f 100644 --- a/common/TracySocket.hpp +++ b/common/TracySocket.hpp @@ -10,6 +10,8 @@ namespace tracy class Socket { + enum { BufSize = 128 * 1024 }; + public: Socket(); Socket( int sock ); @@ -19,7 +21,6 @@ public: void Close(); int Send( const void* buf, int len ); - int Recv( void* buf, int len, const timeval* tv ); bool Read( void* buf, int len, const timeval* tv, std::function exitCb ); bool HasData(); @@ -30,7 +31,14 @@ public: Socket& operator=( Socket&& ) = delete; private: + int RecvBuffered( void* buf, int len, const timeval* tv ); + int Recv( void* buf, int len, const timeval* tv ); + int m_sock; + + char* m_buf; + char* m_bufPtr; + int m_bufLeft; }; class ListenSocket