Ports are uint16_t.

This commit is contained in:
Bartosz Taudul 2020-10-02 18:51:54 +02:00
parent 0d82d6fe63
commit b6724bec3a
9 changed files with 38 additions and 37 deletions

View File

@ -1,4 +1,5 @@
#include <assert.h> #include <assert.h>
#include <inttypes.h>
#include <new> #include <new>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -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() ); assert( !IsValid() );
@ -159,7 +160,7 @@ bool Socket::Connect( const char* addr, int port )
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
char portbuf[32]; char portbuf[32];
sprintf( portbuf, "%i", port ); sprintf( portbuf, "%" PRIu16, port );
if( getaddrinfo( addr, portbuf, &hints, &res ) != 0 ) return false; if( getaddrinfo( addr, portbuf, &hints, &res ) != 0 ) return false;
int sock = 0; int sock = 0;
@ -218,7 +219,7 @@ bool Socket::Connect( const char* addr, int port )
return true; return true;
} }
bool Socket::ConnectBlocking( const char* addr, int port ) bool Socket::ConnectBlocking( const char* addr, uint16_t port )
{ {
assert( !IsValid() ); assert( !IsValid() );
assert( !m_ptr ); assert( !m_ptr );
@ -231,7 +232,7 @@ bool Socket::ConnectBlocking( const char* addr, int port )
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
char portbuf[32]; char portbuf[32];
sprintf( portbuf, "%i", port ); sprintf( portbuf, "%" PRIu16, port );
if( getaddrinfo( addr, portbuf, &hints, &res ) != 0 ) return false; if( getaddrinfo( addr, portbuf, &hints, &res ) != 0 ) return false;
int sock = 0; int sock = 0;
@ -446,7 +447,7 @@ ListenSocket::~ListenSocket()
if( m_sock != -1 ) Close(); 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; struct addrinfo hints;
memset( &hints, 0, sizeof( 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 #endif
char portbuf[32]; char portbuf[32];
sprintf( portbuf, "%i", port ); sprintf( portbuf, "%" PRIu16, port );
if( getaddrinfo( nullptr, portbuf, &hints, res ) != 0 ) return -1; if( getaddrinfo( nullptr, portbuf, &hints, res ) != 0 ) return -1;
int sock = socket( (*res)->ai_family, (*res)->ai_socktype, (*res)->ai_protocol ); int sock = socket( (*res)->ai_family, (*res)->ai_socktype, (*res)->ai_protocol );
if (sock == -1) freeaddrinfo( *res ); if (sock == -1) freeaddrinfo( *res );
return sock; return sock;
} }
bool ListenSocket::Listen( int port, int backlog ) bool ListenSocket::Listen( uint16_t port, int backlog )
{ {
assert( m_sock == -1 ); assert( m_sock == -1 );
@ -477,14 +478,14 @@ bool ListenSocket::Listen( int port, int backlog )
const char* onlyIPv4 = getenv( "TRACY_ONLY_IPV4" ); const char* onlyIPv4 = getenv( "TRACY_ONLY_IPV4" );
if( !onlyIPv4 || onlyIPv4[0] != '1' ) 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 #endif
if (m_sock == -1) if (m_sock == -1)
{ {
// IPV6 protocol may not be available/is disabled. Try to create a socket // IPV6 protocol may not be available/is disabled. Try to create a socket
// with the IPV4 protocol // 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( m_sock == -1 ) return false;
} }
#if defined _WIN32 || defined __CYGWIN__ #if defined _WIN32 || defined __CYGWIN__
@ -558,7 +559,7 @@ UdpBroadcast::~UdpBroadcast()
if( m_sock != -1 ) Close(); 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 ); assert( m_sock == -1 );
@ -570,7 +571,7 @@ bool UdpBroadcast::Open( const char* addr, int port )
hints.ai_socktype = SOCK_DGRAM; hints.ai_socktype = SOCK_DGRAM;
char portbuf[32]; char portbuf[32];
sprintf( portbuf, "%i", port ); sprintf( portbuf, "%" PRIu16, port );
if( getaddrinfo( addr, portbuf, &hints, &res ) != 0 ) return false; if( getaddrinfo( addr, portbuf, &hints, &res ) != 0 ) return false;
int sock = 0; int sock = 0;
@ -616,7 +617,7 @@ void UdpBroadcast::Close()
m_sock = -1; 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 ); assert( m_sock != -1 );
struct sockaddr_in addr; struct sockaddr_in addr;
@ -662,7 +663,7 @@ UdpListen::~UdpListen()
if( m_sock != -1 ) Close(); if( m_sock != -1 ) Close();
} }
bool UdpListen::Listen( int port ) bool UdpListen::Listen( uint16_t port )
{ {
assert( m_sock == -1 ); assert( m_sock == -1 );

View File

@ -23,8 +23,8 @@ public:
Socket( int sock ); Socket( int sock );
~Socket(); ~Socket();
bool Connect( const char* addr, int port ); bool Connect( const char* addr, uint16_t port );
bool ConnectBlocking( const char* addr, int port ); bool ConnectBlocking( const char* addr, uint16_t port );
void Close(); void Close();
int Send( const void* buf, int len ); int Send( const void* buf, int len );
@ -76,7 +76,7 @@ public:
ListenSocket(); ListenSocket();
~ListenSocket(); ~ListenSocket();
bool Listen( int port, int backlog ); bool Listen( uint16_t port, int backlog );
Socket* Accept(); Socket* Accept();
void Close(); void Close();
@ -95,10 +95,10 @@ public:
UdpBroadcast(); UdpBroadcast();
~UdpBroadcast(); ~UdpBroadcast();
bool Open( const char* addr, int port ); bool Open( const char* addr, uint16_t port );
void Close(); 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( const UdpBroadcast& ) = delete;
UdpBroadcast( UdpBroadcast&& ) = delete; UdpBroadcast( UdpBroadcast&& ) = delete;
@ -136,7 +136,7 @@ public:
UdpListen(); UdpListen();
~UdpListen(); ~UdpListen();
bool Listen( int port ); bool Listen( uint16_t port );
void Close(); void Close();
const char* Read( size_t& len, IpAddress& addr, int timeout ); const char* Read( size_t& len, IpAddress& addr, int timeout );

View File

@ -8,7 +8,7 @@
#include "ResolvService.hpp" #include "ResolvService.hpp"
ResolvService::ResolvService( int port ) ResolvService::ResolvService( uint16_t port )
: m_exit( false ) : m_exit( false )
, m_port( port ) , m_port( port )
, m_thread( [this] { Worker(); } ) , m_thread( [this] { Worker(); } )

View File

@ -19,7 +19,7 @@ class ResolvService
}; };
public: public:
ResolvService( int port ); ResolvService( uint16_t port );
~ResolvService(); ~ResolvService();
void Query( uint32_t ip, const std::function<void(std::string&&)>& callback ); void Query( uint32_t ip, const std::function<void(std::string&&)>& callback );
@ -31,7 +31,7 @@ private:
std::mutex m_lock; std::mutex m_lock;
std::condition_variable m_cv; std::condition_variable m_cv;
std::vector<QueueItem> m_queue; std::vector<QueueItem> m_queue;
int m_port; uint16_t m_port;
std::thread m_thread; std::thread m_thread;
}; };

View File

@ -107,7 +107,7 @@ struct ClientData
int64_t time; int64_t time;
uint32_t protocolVersion; uint32_t protocolVersion;
int32_t activeTime; int32_t activeTime;
uint32_t port; uint16_t port;
std::string procName; std::string procName;
std::string address; std::string address;
}; };
@ -117,7 +117,7 @@ enum class ViewShutdown { False, True, Join };
static tracy::unordered_flat_map<uint64_t, ClientData> clients; static tracy::unordered_flat_map<uint64_t, ClientData> clients;
static std::unique_ptr<tracy::View> view; static std::unique_ptr<tracy::View> view;
static tracy::BadVersionState badVer; static tracy::BadVersionState badVer;
static int port = 8086; static uint16_t port = 8086;
static const char* connectTo = nullptr; static const char* connectTo = nullptr;
static char title[128]; static char title[128];
static std::thread loadThread, updateThread, updateNotesThread; static std::thread loadThread, updateThread, updateNotesThread;
@ -357,7 +357,7 @@ int main( int argc, char** argv )
} }
else if( strcmp( argv[1], "-p" ) == 0 ) else if( strcmp( argv[1], "-p" ) == 0 )
{ {
port = atoi( argv[2] ); port = (uint16_t)atoi( argv[2] );
} }
else else
{ {
@ -475,7 +475,7 @@ static void DrawContents()
{ {
static bool reconnect = false; static bool reconnect = false;
static std::string reconnectAddr; static std::string reconnectAddr;
static int reconnectPort; static uint16_t reconnectPort;
static bool showFilter = false; static bool showFilter = false;
const ImVec4 clear_color = ImColor( 114, 144, 154 ); const ImVec4 clear_color = ImColor( 114, 144, 154 );
@ -712,7 +712,7 @@ static void DrawContents()
if( *ptr == ':' ) if( *ptr == ':' )
{ {
std::string addrPart = std::string( addr, 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<tracy::View>( RunOnMainThread, addrPart.c_str(), portPart, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative ); view = std::make_unique<tracy::View>( RunOnMainThread, addrPart.c_str(), portPart, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative );
} }
else else
@ -826,7 +826,7 @@ static void DrawContents()
if( portFilter.IsActive() ) if( portFilter.IsActive() )
{ {
char buf[32]; char buf[32];
sprintf( buf, "%" PRIu32, v.second.port ); sprintf( buf, "%" PRIu16, v.second.port );
if( !portFilter.PassFilter( buf ) ) continue; if( !portFilter.PassFilter( buf ) ) continue;
} }
if( progFilter.IsActive() && !progFilter.PassFilter( v.second.procName.c_str() ) ) continue; if( progFilter.IsActive() && !progFilter.PassFilter( v.second.procName.c_str() ) ) continue;
@ -838,7 +838,7 @@ static void DrawContents()
if( ImGui::IsItemHovered() ) if( ImGui::IsItemHovered() )
{ {
char portstr[32]; char portstr[32];
sprintf( portstr, "%" PRIu32, v.second.port ); sprintf( portstr, "%" PRIu16, v.second.port );
ImGui::BeginTooltip(); ImGui::BeginTooltip();
if( badProto ) if( badProto )
{ {
@ -853,7 +853,7 @@ static void DrawContents()
if( v.second.port != port ) if( v.second.port != port )
{ {
ImGui::SameLine(); ImGui::SameLine();
ImGui::TextDisabled( ":%" PRIu32, v.second.port ); ImGui::TextDisabled( ":%" PRIu16, v.second.port );
} }
if( selected && !loadThread.joinable() ) if( selected && !loadThread.joinable() )
{ {

View File

@ -129,7 +129,7 @@ enum { MinFrameSize = 5 };
static View* s_instance = nullptr; static View* s_instance = nullptr;
View::View( void(*cbMainThread)(std::function<void()>), const char* addr, int port, ImFont* fixedWidth, ImFont* smallFont, ImFont* bigFont, SetTitleCallback stcb, GetWindowCallback gwcb ) View::View( void(*cbMainThread)(std::function<void()>), const char* addr, uint16_t port, ImFont* fixedWidth, ImFont* smallFont, ImFont* bigFont, SetTitleCallback stcb, GetWindowCallback gwcb )
: m_worker( addr, port ) : m_worker( addr, port )
, m_staticView( false ) , m_staticView( false )
, m_viewMode( ViewMode::LastFrames ) , m_viewMode( ViewMode::LastFrames )

View File

@ -76,7 +76,7 @@ public:
using GetWindowCallback = void*(*)(); using GetWindowCallback = void*(*)();
View( void(*cbMainThread)(std::function<void()>), 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<void()>), 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<void()>), 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<void()>), 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<void()>), FileRead& f, ImFont* fixedWidth = nullptr, ImFont* smallFont = nullptr, ImFont* bigFont = nullptr, SetTitleCallback stcb = nullptr, GetWindowCallback gwcb = nullptr ); View( void(*cbMainThread)(std::function<void()>), FileRead& f, ImFont* fixedWidth = nullptr, ImFont* smallFont = nullptr, ImFont* bigFont = nullptr, SetTitleCallback stcb = nullptr, GetWindowCallback gwcb = nullptr );
~View(); ~View();
@ -89,7 +89,7 @@ public:
bool ReconnectRequested() const { return m_reconnectRequested; } bool ReconnectRequested() const { return m_reconnectRequested; }
std::string GetAddress() const { return m_worker.GetAddr(); } 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; const char* SourceSubstitution( const char* srcFile ) const;

View File

@ -234,7 +234,7 @@ static tracy_force_inline void UpdateLockRange( LockMap& lockmap, const LockEven
LoadProgress Worker::s_loadProgress; LoadProgress Worker::s_loadProgress;
Worker::Worker( const char* addr, int port ) Worker::Worker( const char* addr, uint16_t port )
: m_addr( addr ) : m_addr( addr )
, m_port( port ) , m_port( port )
, m_hasData( false ) , m_hasData( false )

View File

@ -392,13 +392,13 @@ public:
NUM_FAILURES NUM_FAILURES
}; };
Worker( const char* addr, int port ); Worker( const char* addr, uint16_t port );
Worker( const std::string& program, const std::vector<ImportEventTimeline>& timeline, const std::vector<ImportEventMessages>& messages, const std::vector<ImportEventPlots>& plots ); Worker( const std::string& program, const std::vector<ImportEventTimeline>& timeline, const std::vector<ImportEventMessages>& messages, const std::vector<ImportEventPlots>& plots );
Worker( FileRead& f, EventType::Type eventMask = EventType::All, bool bgTasks = true ); Worker( FileRead& f, EventType::Type eventMask = EventType::All, bool bgTasks = true );
~Worker(); ~Worker();
const std::string& GetAddr() const { return m_addr; } 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& GetCaptureName() const { return m_captureName; }
const std::string& GetCaptureProgram() const { return m_captureProgram; } const std::string& GetCaptureProgram() const { return m_captureProgram; }
uint64_t GetCaptureTime() const { return m_captureTime; } uint64_t GetCaptureTime() const { return m_captureTime; }
@ -816,7 +816,7 @@ private:
Socket m_sock; Socket m_sock;
std::string m_addr; std::string m_addr;
int m_port; uint16_t m_port;
std::thread m_thread; std::thread m_thread;
std::thread m_threadNet; std::thread m_threadNet;