diff --git a/profiler/build/win32/Tracy.vcxproj b/profiler/build/win32/Tracy.vcxproj index 193f897d..4213e4e2 100644 --- a/profiler/build/win32/Tracy.vcxproj +++ b/profiler/build/win32/Tracy.vcxproj @@ -156,6 +156,7 @@ + @@ -260,6 +261,7 @@ + diff --git a/profiler/build/win32/Tracy.vcxproj.filters b/profiler/build/win32/Tracy.vcxproj.filters index 4e534a10..a891d9ed 100644 --- a/profiler/build/win32/Tracy.vcxproj.filters +++ b/profiler/build/win32/Tracy.vcxproj.filters @@ -207,6 +207,9 @@ server + + src + @@ -503,6 +506,9 @@ src + + src + diff --git a/profiler/src/HttpRequest.cpp b/profiler/src/HttpRequest.cpp new file mode 100644 index 00000000..38f162d0 --- /dev/null +++ b/profiler/src/HttpRequest.cpp @@ -0,0 +1,56 @@ +#include +#include +#include + +#include "../common/TracySocket.hpp" +#include "../server/TracyVersion.hpp" +#include "HttpRequest.hpp" + +static constexpr char CRLF[2] = { '\r', '\n' }; + +void HttpRequest( const char* server, const char* resource, int port, std::function cb ) +{ + tracy::Socket sock; + if( !sock.ConnectBlocking( server, port ) ) return; + char request[4096]; + const auto len = sprintf( request, "GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: Tracy Profiler %i.%i.%i\r\nConnection: close\r\nCache-Control: no-cache, no-store, must-revalidate\r\n\r\n", resource, server, tracy::Version::Major, tracy::Version::Minor, tracy::Version::Patch ); + sock.Send( request, len ); + char response[4096]; + const auto sz = sock.ReadUpTo( response, 4096, 15 ); + if( sz < 13 ) return; + if( memcmp( response, "HTTP/1.1 200", 12 ) != 0 ) return; + auto hdr = response + 13; + int contentLength = 0; + for(;;) + { + while( memcmp( hdr, CRLF, 2 ) != 0 ) hdr++; + hdr += 2; + if( memcmp( hdr, "Content-Length: ", 16 ) == 0 ) + { + hdr += 16; + contentLength = atoi( hdr ); + break; + } + } + assert( contentLength != 0 ); + for(;;) + { + while( memcmp( hdr, CRLF, 2 ) != 0 ) hdr++; + hdr += 2; + if( memcmp( hdr, CRLF, 2 ) == 0 ) + { + hdr += 2; + break; + } + hdr += 2; + } + + const auto hdrSize = hdr - response; + const auto partSize = sz - hdrSize; + char* data = new char[contentLength]; + memcpy( data, hdr, partSize ); + auto remaining = contentLength - partSize; + if( remaining > 0 ) sock.Read( data + partSize, remaining, 15 ); + + cb( contentLength, data ); +} diff --git a/profiler/src/HttpRequest.hpp b/profiler/src/HttpRequest.hpp new file mode 100644 index 00000000..7aa86c5c --- /dev/null +++ b/profiler/src/HttpRequest.hpp @@ -0,0 +1,8 @@ +#ifndef __HTTPREQUEST_HPP__ +#define __HTTPREQUEST_HPP__ + +#include + +void HttpRequest( const char* server, const char* resource, int port, std::function cb ); + +#endif diff --git a/profiler/src/main.cpp b/profiler/src/main.cpp index 021ceb71..155b1643 100644 --- a/profiler/src/main.cpp +++ b/profiler/src/main.cpp @@ -51,6 +51,7 @@ #include "icon.hpp" #include "ResolvService.hpp" #include "NativeWindow.hpp" +#include "HttpRequest.hpp" static void glfw_error_callback(int error, const char* description) { @@ -227,18 +228,15 @@ int main( int argc, char** argv ) mainThread = std::this_thread::get_id(); updateThread = std::thread( [] { - tracy::Socket sock; - if( !sock.ConnectBlocking( "51.89.23.220", 8099 ) ) return; - char request[1024]; - const auto len = sprintf( request, "GET /tracy/version HTTP/1.1\r\nHost: 51.89.23.220\r\nUser-Agent: Tracy Profiler %i.%i.%i\r\nConnection: close\r\nCache-Control: no-cache, no-store, must-revalidate\r\n\r\n", tracy::Version::Major, tracy::Version::Minor, tracy::Version::Patch ); - sock.Send( request, len ); - char response[1024]; - const auto sz = sock.ReadUpTo( response, 1024, 15 ); - if( sz < 13 ) return; - if( memcmp( response, "HTTP/1.1 200", 12 ) != 0 ) return; - uint32_t ver; - memcpy( &ver, response + sz - 4, 4 ); - RunOnMainThread( [ver] { updateVersion = ver; } ); + HttpRequest( "51.89.23.220", "/tracy/version", 8099, [] ( int size, char* data ) { + if( size == 4 ) + { + uint32_t ver; + memcpy( &ver, data, 4 ); + RunOnMainThread( [ver] { updateVersion = ver; } ); + } + delete[] data; + } ); } ); // Setup window