From e4ec6664799f0fcc23f9de26f3adef43981ae6dd Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Mon, 13 Apr 2020 14:14:36 +0200 Subject: [PATCH] Don't use std::function in sockets. --- common/TracySocket.cpp | 39 ++++++++++++++++----------------------- common/TracySocket.hpp | 19 +++++++++++++++++-- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/common/TracySocket.cpp b/common/TracySocket.cpp index a6bcb275..acbc9045 100644 --- a/common/TracySocket.cpp +++ b/common/TracySocket.cpp @@ -221,33 +221,26 @@ int Socket::Recv( void* _buf, int len, int timeout ) } } -bool Socket::Read( void* _buf, int len, int timeout, std::function exitCb ) +bool Socket::ReadImpl( char*& buf, int& len, int timeout ) { - auto buf = (char*)_buf; - - while( len > 0 ) + const auto sz = RecvBuffered( buf, len, timeout ); + switch( sz ) { - if( exitCb() ) return false; - const auto sz = RecvBuffered( buf, len, timeout ); - switch( sz ) - { - case 0: - return false; - case -1: + case 0: + return false; + case -1: #ifdef _WIN32 - { - auto err = WSAGetLastError(); - if( err == WSAECONNABORTED || err == WSAECONNRESET ) return false; - } -#endif - break; - default: - len -= sz; - buf += sz; - break; - } + { + auto err = WSAGetLastError(); + if( err == WSAECONNABORTED || err == WSAECONNRESET ) return false; + } +#endif + break; + default: + len -= sz; + buf += sz; + break; } - return true; } diff --git a/common/TracySocket.hpp b/common/TracySocket.hpp index 6716c0a7..4192596a 100644 --- a/common/TracySocket.hpp +++ b/common/TracySocket.hpp @@ -1,7 +1,9 @@ #ifndef __TRACYSOCKET_HPP__ #define __TRACYSOCKET_HPP__ -#include +#include + +#include "TracyForceInline.hpp" struct sockaddr; @@ -25,7 +27,18 @@ public: int Send( const void* buf, int len ); int GetSendBufSize(); - bool Read( void* buf, int len, int timeout, std::function exitCb ); + template + bool Read( void* buf, int len, int timeout, ShouldExit exitCb ) + { + auto cbuf = (char*)buf; + while( len > 0 ) + { + if( exitCb() ) return false; + if( !ReadImpl( cbuf, len, timeout ) ) return false; + } + return true; + } + bool ReadRaw( void* buf, int len, int timeout ); bool HasData(); bool IsValid() const; @@ -39,6 +52,8 @@ private: int RecvBuffered( void* buf, int len, int timeout ); int Recv( void* buf, int len, int timeout ); + bool ReadImpl( char*& buf, int& len, int timeout ); + char* m_buf; char* m_bufPtr; int m_sock;