From 36edb22fd1e4392bd34536afd1119db0648f4947 Mon Sep 17 00:00:00 2001 From: rdb Date: Sun, 30 Jan 2022 00:23:55 +0100 Subject: [PATCH] pstats: Include client pid with hello message Useful for uniquely identifying the process in a situation where multiple clients on the same host connect with the same server. New version bump is not necessary, as old servers should just ignore the extra field in the datagram. --- panda/src/pstatclient/pStatClientControlMessage.cxx | 6 ++++++ panda/src/pstatclient/pStatClientControlMessage.h | 1 + panda/src/pstatclient/pStatClientImpl.cxx | 5 +++++ pandatool/src/pstatserver/pStatMonitor.I | 8 ++++++++ pandatool/src/pstatserver/pStatMonitor.cxx | 6 ++++-- pandatool/src/pstatserver/pStatMonitor.h | 6 +++++- pandatool/src/pstatserver/pStatReader.cxx | 4 +++- 7 files changed, 32 insertions(+), 4 deletions(-) diff --git a/panda/src/pstatclient/pStatClientControlMessage.cxx b/panda/src/pstatclient/pStatClientControlMessage.cxx index e7fd765547..5cbac136e4 100644 --- a/panda/src/pstatclient/pStatClientControlMessage.cxx +++ b/panda/src/pstatclient/pStatClientControlMessage.cxx @@ -39,6 +39,7 @@ encode(Datagram &datagram) const { datagram.add_string(_client_progname); datagram.add_uint16(_major_version); datagram.add_uint16(_minor_version); + datagram.add_uint32(_client_pid); break; case T_define_collectors: @@ -86,6 +87,11 @@ decode(const Datagram &datagram, PStatClientVersion *version) { _major_version = source.get_uint16(); _minor_version = source.get_uint16(); } + if (source.get_remaining_size() >= 4) { + _client_pid = source.get_uint32(); + } else { + _client_pid = -1; + } break; case T_define_collectors: diff --git a/panda/src/pstatclient/pStatClientControlMessage.h b/panda/src/pstatclient/pStatClientControlMessage.h index 97e7dede98..32a9f65f70 100644 --- a/panda/src/pstatclient/pStatClientControlMessage.h +++ b/panda/src/pstatclient/pStatClientControlMessage.h @@ -47,6 +47,7 @@ public: // Used for T_hello std::string _client_hostname; std::string _client_progname; + int _client_pid; int _major_version; int _minor_version; diff --git a/panda/src/pstatclient/pStatClientImpl.cxx b/panda/src/pstatclient/pStatClientImpl.cxx index f25b863b97..dcf0cdfef2 100644 --- a/panda/src/pstatclient/pStatClientImpl.cxx +++ b/panda/src/pstatclient/pStatClientImpl.cxx @@ -396,6 +396,11 @@ send_hello() { message._type = PStatClientControlMessage::T_hello; message._client_hostname = get_hostname(); message._client_progname = _client_name; +#ifdef _WIN32 + message._client_pid = GetCurrentProcessId(); +#else + message._client_pid = getpid(); +#endif message._major_version = get_current_pstat_major_version(); message._minor_version = get_current_pstat_minor_version(); diff --git a/pandatool/src/pstatserver/pStatMonitor.I b/pandatool/src/pstatserver/pStatMonitor.I index 761b7b9a79..ac9f2e46f9 100644 --- a/pandatool/src/pstatserver/pStatMonitor.I +++ b/pandatool/src/pstatserver/pStatMonitor.I @@ -69,3 +69,11 @@ INLINE std::string PStatMonitor:: get_client_progname() const { return _client_progname; } + +/** + * Returns the process id of the client, or -1 if it is not known. + */ +INLINE int PStatMonitor:: +get_client_pid() const { + return _client_pid; +} diff --git a/pandatool/src/pstatserver/pStatMonitor.cxx b/pandatool/src/pstatserver/pStatMonitor.cxx index 09e22b093a..20444352c1 100644 --- a/pandatool/src/pstatserver/pStatMonitor.cxx +++ b/pandatool/src/pstatserver/pStatMonitor.cxx @@ -38,10 +38,11 @@ PStatMonitor:: * indicates the client's reported hostname and program name. */ void PStatMonitor:: -hello_from(const string &hostname, const string &progname) { +hello_from(const string &hostname, const string &progname, int pid) { _client_known = true; _client_hostname = hostname; _client_progname = progname; + _client_pid = pid; got_hello(); } @@ -52,12 +53,13 @@ hello_from(const string &hostname, const string &progname) { * effect. */ void PStatMonitor:: -bad_version(const string &hostname, const string &progname, +bad_version(const string &hostname, const string &progname, int pid, int client_major, int client_minor, int server_major, int server_minor) { _client_known = true; _client_hostname = hostname; _client_progname = progname; + _client_pid = 0; got_bad_version(client_major, client_minor, server_major, server_minor); } diff --git a/pandatool/src/pstatserver/pStatMonitor.h b/pandatool/src/pstatserver/pStatMonitor.h index f7e92073c4..8c1259c9f0 100644 --- a/pandatool/src/pstatserver/pStatMonitor.h +++ b/pandatool/src/pstatserver/pStatMonitor.h @@ -43,8 +43,10 @@ public: PStatMonitor(PStatServer *server); virtual ~PStatMonitor(); - void hello_from(const std::string &hostname, const std::string &progname); + void hello_from(const std::string &hostname, const std::string &progname, + int pid); void bad_version(const std::string &hostname, const std::string &progname, + int pid, int client_major, int client_minor, int server_major, int server_minor); void set_client_data(PStatClientData *client_data); @@ -63,6 +65,7 @@ public: INLINE bool is_client_known() const; INLINE std::string get_client_hostname() const; INLINE std::string get_client_progname() const; + INLINE int get_client_pid() const; PStatView &get_view(int thread_index); PStatView &get_level_view(int collector_index, int thread_index); @@ -98,6 +101,7 @@ private: bool _client_known; std::string _client_hostname; std::string _client_progname; + int _client_pid; typedef pmap Views; Views _views; diff --git a/pandatool/src/pstatserver/pStatReader.cxx b/pandatool/src/pstatserver/pStatReader.cxx index 02b01f26a2..a2a1b4b7c9 100644 --- a/pandatool/src/pstatserver/pStatReader.cxx +++ b/pandatool/src/pstatserver/pStatReader.cxx @@ -195,11 +195,13 @@ handle_client_control_message(const PStatClientControlMessage &message) { (message._major_version == server_major_version && message._minor_version > server_minor_version)) { _monitor->bad_version(message._client_hostname, message._client_progname, + message._client_pid, message._major_version, message._minor_version, server_major_version, server_minor_version); _monitor->close(); } else { - _monitor->hello_from(message._client_hostname, message._client_progname); + _monitor->hello_from(message._client_hostname, message._client_progname, + message._client_pid); } } break;