Skip to content

Commit

Permalink
fix magic numbers in tcpsocket, make Process::start more safe
Browse files Browse the repository at this point in the history
  • Loading branch information
w0lek committed Nov 16, 2023
1 parent 0f96872 commit 91e7db8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
14 changes: 8 additions & 6 deletions src/InteractivePathAnalysis/client/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Process::Process(const QString& name)
});
#endif

m_watcherTimer.setInterval(500);
m_watcherTimer.setInterval(PROCESS_WATCHER_INTERVAL_MS);
QObject::connect(&m_watcherTimer, &QTimer::timeout, this, &Process::checkEvent);
m_watcherTimer.start();

Expand All @@ -29,7 +29,7 @@ Process::~Process()
qInfo() << "~~~ ~Process() START";
m_watcherTimer.stop();
close();
if (!waitForFinished(5000)) {
if (!waitForFinished(PROCESS_FINISH_TIMOUT_MS)) {
kill();
waitForFinished();
}
Expand All @@ -39,12 +39,14 @@ Process::~Process()
void Process::start(const QString& fullCmd)
{
QList<QString> fragments = fullCmd.split(" ");
m_cmd = fragments[0];
if (!fragments.isEmpty()) {
m_cmd = fragments[0];

fragments.pop_front();
m_args = fragments;
fragments.pop_front();
m_args = fragments;

restart();
restart();
}
}

void Process::restart()
Expand Down
2 changes: 2 additions & 0 deletions src/InteractivePathAnalysis/client/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
class Process : public QProcess
{
Q_OBJECT
const int PROCESS_WATCHER_INTERVAL_MS = 500;
const int PROCESS_FINISH_TIMOUT_MS = 5000;
public:
Process(const QString& name);
~Process();
Expand Down
9 changes: 5 additions & 4 deletions src/InteractivePathAnalysis/client/tcpsocket.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "tcpsocket.h"
#include "keys.h"

const QString TcpSocket::LOCALHOST_IP_ADDR = "127.0.0.1";

TcpSocket::TcpSocket()
{
m_connectionWatcher.setInterval(1000);
m_connectionWatcher.setInterval(CONNECTION_WATCHER_INTERVAL_MS);
m_connectionWatcher.start();
QObject::connect(&m_connectionWatcher, &QTimer::timeout, this, [this](){
ensureConnected();
Expand Down Expand Up @@ -42,10 +44,9 @@ TcpSocket::~TcpSocket()

bool TcpSocket::connect()
{
static QString localIpAddr = "127.0.0.1";
m_socket.connectToHost(localIpAddr, PORT_NUM);
m_socket.connectToHost(LOCALHOST_IP_ADDR, PORT_NUM);
if (m_socket.waitForConnected()) {
qInfo() << "connected to host" << localIpAddr << PORT_NUM;
qInfo() << "connected to host" << LOCALHOST_IP_ADDR << PORT_NUM;
} else {
//qInfo() << "client connection failed!" << m_socket.errorString();
}
Expand Down
8 changes: 4 additions & 4 deletions src/InteractivePathAnalysis/client/tcpsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#include <QTcpSocket>
#include <QTimer>

#ifndef PORT_NUM
#define PORT_NUM 61555
#endif

class TcpSocket final: public ISocket {
static const int CONNECTION_WATCHER_INTERVAL_MS = 1000;
static const QString LOCALHOST_IP_ADDR;
static const int PORT_NUM = 61555;

public:
TcpSocket();
~TcpSocket();
Expand Down

0 comments on commit 91e7db8

Please sign in to comment.