From b6724bec3a63f96f60faadf830527092d817ae9c Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Fri, 2 Oct 2020 18:51:54 +0200 Subject: [PATCH] Ports are uint16_t. --- common/TracySocket.cpp | 27 ++++++++++++++------------- common/TracySocket.hpp | 12 ++++++------ profiler/src/ResolvService.cpp | 2 +- profiler/src/ResolvService.hpp | 4 ++-- profiler/src/main.cpp | 16 ++++++++-------- server/TracyView.cpp | 2 +- server/TracyView.hpp | 4 ++-- server/TracyWorker.cpp | 2 +- server/TracyWorker.hpp | 6 +++--- 9 files changed, 38 insertions(+), 37 deletions(-) diff --git a/common/TracySocket.cpp b/common/TracySocket.cpp index 0d666a84..c9196163 100644 --- a/common/TracySocket.cpp +++ b/common/TracySocket.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -106,7 +107,7 @@ Socket::~Socket() } } -bool Socket::Connect( const char* addr, int port ) +bool Socket::Connect( const char* addr, uint16_t port ) { assert( !IsValid() ); @@ -159,7 +160,7 @@ bool Socket::Connect( const char* addr, int port ) hints.ai_socktype = SOCK_STREAM; char portbuf[32]; - sprintf( portbuf, "%i", port ); + sprintf( portbuf, "%" PRIu16, port ); if( getaddrinfo( addr, portbuf, &hints, &res ) != 0 ) return false; int sock = 0; @@ -218,7 +219,7 @@ bool Socket::Connect( const char* addr, int port ) return true; } -bool Socket::ConnectBlocking( const char* addr, int port ) +bool Socket::ConnectBlocking( const char* addr, uint16_t port ) { assert( !IsValid() ); assert( !m_ptr ); @@ -231,7 +232,7 @@ bool Socket::ConnectBlocking( const char* addr, int port ) hints.ai_socktype = SOCK_STREAM; char portbuf[32]; - sprintf( portbuf, "%i", port ); + sprintf( portbuf, "%" PRIu16, port ); if( getaddrinfo( addr, portbuf, &hints, &res ) != 0 ) return false; int sock = 0; @@ -446,7 +447,7 @@ ListenSocket::~ListenSocket() if( m_sock != -1 ) Close(); } -static int addrinfo_and_socket_for_family(int port, int ai_family, struct addrinfo** res) +static int addrinfo_and_socket_for_family( uint16_t port, int ai_family, struct addrinfo** res ) { struct addrinfo hints; memset( &hints, 0, sizeof( hints ) ); @@ -460,14 +461,14 @@ static int addrinfo_and_socket_for_family(int port, int ai_family, struct addrin } #endif char portbuf[32]; - sprintf( portbuf, "%i", port ); + sprintf( portbuf, "%" PRIu16, port ); if( getaddrinfo( nullptr, portbuf, &hints, res ) != 0 ) return -1; int sock = socket( (*res)->ai_family, (*res)->ai_socktype, (*res)->ai_protocol ); if (sock == -1) freeaddrinfo( *res ); return sock; } -bool ListenSocket::Listen( int port, int backlog ) +bool ListenSocket::Listen( uint16_t port, int backlog ) { assert( m_sock == -1 ); @@ -477,14 +478,14 @@ bool ListenSocket::Listen( int port, int backlog ) const char* onlyIPv4 = getenv( "TRACY_ONLY_IPV4" ); if( !onlyIPv4 || onlyIPv4[0] != '1' ) { - m_sock = addrinfo_and_socket_for_family(port, AF_INET6, &res); + m_sock = addrinfo_and_socket_for_family( port, AF_INET6, &res ); } #endif if (m_sock == -1) { // IPV6 protocol may not be available/is disabled. Try to create a socket // with the IPV4 protocol - m_sock = addrinfo_and_socket_for_family(port, AF_INET, &res); + m_sock = addrinfo_and_socket_for_family( port, AF_INET, &res ); if( m_sock == -1 ) return false; } #if defined _WIN32 || defined __CYGWIN__ @@ -558,7 +559,7 @@ UdpBroadcast::~UdpBroadcast() if( m_sock != -1 ) Close(); } -bool UdpBroadcast::Open( const char* addr, int port ) +bool UdpBroadcast::Open( const char* addr, uint16_t port ) { assert( m_sock == -1 ); @@ -570,7 +571,7 @@ bool UdpBroadcast::Open( const char* addr, int port ) hints.ai_socktype = SOCK_DGRAM; char portbuf[32]; - sprintf( portbuf, "%i", port ); + sprintf( portbuf, "%" PRIu16, port ); if( getaddrinfo( addr, portbuf, &hints, &res ) != 0 ) return false; int sock = 0; @@ -616,7 +617,7 @@ void UdpBroadcast::Close() m_sock = -1; } -int UdpBroadcast::Send( int port, const void* data, int len ) +int UdpBroadcast::Send( uint16_t port, const void* data, int len ) { assert( m_sock != -1 ); struct sockaddr_in addr; @@ -662,7 +663,7 @@ UdpListen::~UdpListen() if( m_sock != -1 ) Close(); } -bool UdpListen::Listen( int port ) +bool UdpListen::Listen( uint16_t port ) { assert( m_sock == -1 ); diff --git a/common/TracySocket.hpp b/common/TracySocket.hpp index f9f618f2..e91f8ead 100644 --- a/common/TracySocket.hpp +++ b/common/TracySocket.hpp @@ -23,8 +23,8 @@ public: Socket( int sock ); ~Socket(); - bool Connect( const char* addr, int port ); - bool ConnectBlocking( const char* addr, int port ); + bool Connect( const char* addr, uint16_t port ); + bool ConnectBlocking( const char* addr, uint16_t port ); void Close(); int Send( const void* buf, int len ); @@ -76,7 +76,7 @@ public: ListenSocket(); ~ListenSocket(); - bool Listen( int port, int backlog ); + bool Listen( uint16_t port, int backlog ); Socket* Accept(); void Close(); @@ -95,10 +95,10 @@ public: UdpBroadcast(); ~UdpBroadcast(); - bool Open( const char* addr, int port ); + bool Open( const char* addr, uint16_t port ); void Close(); - int Send( int port, const void* data, int len ); + int Send( uint16_t port, const void* data, int len ); UdpBroadcast( const UdpBroadcast& ) = delete; UdpBroadcast( UdpBroadcast&& ) = delete; @@ -136,7 +136,7 @@ public: UdpListen(); ~UdpListen(); - bool Listen( int port ); + bool Listen( uint16_t port ); void Close(); const char* Read( size_t& len, IpAddress& addr, int timeout ); diff --git a/profiler/src/ResolvService.cpp b/profiler/src/ResolvService.cpp index 42efee7a..e8e46f5e 100644 --- a/profiler/src/ResolvService.cpp +++ b/profiler/src/ResolvService.cpp @@ -8,7 +8,7 @@ #include "ResolvService.hpp" -ResolvService::ResolvService( int port ) +ResolvService::ResolvService( uint16_t port ) : m_exit( false ) , m_port( port ) , m_thread( [this] { Worker(); } ) diff --git a/profiler/src/ResolvService.hpp b/profiler/src/ResolvService.hpp index a56e2462..c08932c2 100644 --- a/profiler/src/ResolvService.hpp +++ b/profiler/src/ResolvService.hpp @@ -19,7 +19,7 @@ class ResolvService }; public: - ResolvService( int port ); + ResolvService( uint16_t port ); ~ResolvService(); void Query( uint32_t ip, const std::function& callback ); @@ -31,7 +31,7 @@ private: std::mutex m_lock; std::condition_variable m_cv; std::vector m_queue; - int m_port; + uint16_t m_port; std::thread m_thread; }; diff --git a/profiler/src/main.cpp b/profiler/src/main.cpp index 43872d0e..f1a95b43 100644 --- a/profiler/src/main.cpp +++ b/profiler/src/main.cpp @@ -107,7 +107,7 @@ struct ClientData int64_t time; uint32_t protocolVersion; int32_t activeTime; - uint32_t port; + uint16_t port; std::string procName; std::string address; }; @@ -117,7 +117,7 @@ enum class ViewShutdown { False, True, Join }; static tracy::unordered_flat_map clients; static std::unique_ptr view; static tracy::BadVersionState badVer; -static int port = 8086; +static uint16_t port = 8086; static const char* connectTo = nullptr; static char title[128]; static std::thread loadThread, updateThread, updateNotesThread; @@ -357,7 +357,7 @@ int main( int argc, char** argv ) } else if( strcmp( argv[1], "-p" ) == 0 ) { - port = atoi( argv[2] ); + port = (uint16_t)atoi( argv[2] ); } else { @@ -475,7 +475,7 @@ static void DrawContents() { static bool reconnect = false; static std::string reconnectAddr; - static int reconnectPort; + static uint16_t reconnectPort; static bool showFilter = false; const ImVec4 clear_color = ImColor( 114, 144, 154 ); @@ -712,7 +712,7 @@ static void DrawContents() if( *ptr == ':' ) { std::string addrPart = std::string( addr, ptr ); - uint32_t portPart = atoi( ptr+1 ); + uint16_t portPart = (uint16_t)atoi( ptr+1 ); view = std::make_unique( RunOnMainThread, addrPart.c_str(), portPart, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative ); } else @@ -826,7 +826,7 @@ static void DrawContents() if( portFilter.IsActive() ) { char buf[32]; - sprintf( buf, "%" PRIu32, v.second.port ); + sprintf( buf, "%" PRIu16, v.second.port ); if( !portFilter.PassFilter( buf ) ) continue; } if( progFilter.IsActive() && !progFilter.PassFilter( v.second.procName.c_str() ) ) continue; @@ -838,7 +838,7 @@ static void DrawContents() if( ImGui::IsItemHovered() ) { char portstr[32]; - sprintf( portstr, "%" PRIu32, v.second.port ); + sprintf( portstr, "%" PRIu16, v.second.port ); ImGui::BeginTooltip(); if( badProto ) { @@ -853,7 +853,7 @@ static void DrawContents() if( v.second.port != port ) { ImGui::SameLine(); - ImGui::TextDisabled( ":%" PRIu32, v.second.port ); + ImGui::TextDisabled( ":%" PRIu16, v.second.port ); } if( selected && !loadThread.joinable() ) { diff --git a/server/TracyView.cpp b/server/TracyView.cpp index 15a472aa..14c9f57c 100644 --- a/server/TracyView.cpp +++ b/server/TracyView.cpp @@ -129,7 +129,7 @@ enum { MinFrameSize = 5 }; static View* s_instance = nullptr; -View::View( void(*cbMainThread)(std::function), const char* addr, int port, ImFont* fixedWidth, ImFont* smallFont, ImFont* bigFont, SetTitleCallback stcb, GetWindowCallback gwcb ) +View::View( void(*cbMainThread)(std::function), const char* addr, uint16_t port, ImFont* fixedWidth, ImFont* smallFont, ImFont* bigFont, SetTitleCallback stcb, GetWindowCallback gwcb ) : m_worker( addr, port ) , m_staticView( false ) , m_viewMode( ViewMode::LastFrames ) diff --git a/server/TracyView.hpp b/server/TracyView.hpp index 74a7b329..1b710f48 100644 --- a/server/TracyView.hpp +++ b/server/TracyView.hpp @@ -76,7 +76,7 @@ public: using GetWindowCallback = void*(*)(); View( void(*cbMainThread)(std::function), ImFont* fixedWidth = nullptr, ImFont* smallFont = nullptr, ImFont* bigFont = nullptr, SetTitleCallback stcb = nullptr, GetWindowCallback gwcb = nullptr ) : View( cbMainThread, "127.0.0.1", 8086, fixedWidth, smallFont, bigFont, stcb, gwcb ) {} - View( void(*cbMainThread)(std::function), const char* addr, int port, ImFont* fixedWidth = nullptr, ImFont* smallFont = nullptr, ImFont* bigFont = nullptr, SetTitleCallback stcb = nullptr, GetWindowCallback gwcb = nullptr ); + View( void(*cbMainThread)(std::function), const char* addr, uint16_t port, ImFont* fixedWidth = nullptr, ImFont* smallFont = nullptr, ImFont* bigFont = nullptr, SetTitleCallback stcb = nullptr, GetWindowCallback gwcb = nullptr ); View( void(*cbMainThread)(std::function), FileRead& f, ImFont* fixedWidth = nullptr, ImFont* smallFont = nullptr, ImFont* bigFont = nullptr, SetTitleCallback stcb = nullptr, GetWindowCallback gwcb = nullptr ); ~View(); @@ -89,7 +89,7 @@ public: bool ReconnectRequested() const { return m_reconnectRequested; } std::string GetAddress() const { return m_worker.GetAddr(); } - int GetPort() const { return m_worker.GetPort(); } + uint16_t GetPort() const { return m_worker.GetPort(); } const char* SourceSubstitution( const char* srcFile ) const; diff --git a/server/TracyWorker.cpp b/server/TracyWorker.cpp index 786b72f7..7d432879 100644 --- a/server/TracyWorker.cpp +++ b/server/TracyWorker.cpp @@ -234,7 +234,7 @@ static tracy_force_inline void UpdateLockRange( LockMap& lockmap, const LockEven LoadProgress Worker::s_loadProgress; -Worker::Worker( const char* addr, int port ) +Worker::Worker( const char* addr, uint16_t port ) : m_addr( addr ) , m_port( port ) , m_hasData( false ) diff --git a/server/TracyWorker.hpp b/server/TracyWorker.hpp index 98239c58..5b9409b8 100644 --- a/server/TracyWorker.hpp +++ b/server/TracyWorker.hpp @@ -392,13 +392,13 @@ public: NUM_FAILURES }; - Worker( const char* addr, int port ); + Worker( const char* addr, uint16_t port ); Worker( const std::string& program, const std::vector& timeline, const std::vector& messages, const std::vector& plots ); Worker( FileRead& f, EventType::Type eventMask = EventType::All, bool bgTasks = true ); ~Worker(); const std::string& GetAddr() const { return m_addr; } - int GetPort() const { return m_port; } + uint16_t GetPort() const { return m_port; } const std::string& GetCaptureName() const { return m_captureName; } const std::string& GetCaptureProgram() const { return m_captureProgram; } uint64_t GetCaptureTime() const { return m_captureTime; } @@ -816,7 +816,7 @@ private: Socket m_sock; std::string m_addr; - int m_port; + uint16_t m_port; std::thread m_thread; std::thread m_threadNet;