Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sketch of an approach for ' Bind sockets to localhost #398 ' #399

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/AppDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,9 @@ bool AppDaemon::initialize()
QCoreApplication::translate("main", "Activate emulation mode, all Websocket API function return emulated string, useful if you want to try the API."));
parser.addOption(emulMode);

#ifndef Q_OS_MAC
QCommandLineOption anyAddressOption(QStringList() << "a" << "any-address",
QCoreApplication::translate("main", "Listen on any address. By default, it listens only on localhost."));
parser.addOption(anyAddressOption);
#endif

// An option with a value
QCommandLineOption debugHttpServer(QStringList() << "s" << "debug-http-server",
Expand All @@ -112,11 +110,7 @@ bool AppDaemon::initialize()

emulationMode = parser.isSet(emulMode);

#ifdef Q_OS_MAC
anyAddress = true;
#else
anyAddress = parser.isSet(anyAddressOption);
#endif

if (parser.isSet(debugHttpServer))
{
Expand Down Expand Up @@ -160,5 +154,5 @@ QHostAddress AppDaemon::getListenAddress()
if (anyAddress)
return QHostAddress::Any;

return QHostAddress::LocalHost;
return MOOLTICUTE_DAEMON_ADDR;
}
1 change: 1 addition & 0 deletions src/AppGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ bool AppGui::initialize()
needRestart = false;
}
});
qDebug() << "Started moolticutd(aemon)";

connect(daemonProcess, &QProcess::started, [=]()
{
Expand Down
1 change: 1 addition & 0 deletions src/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#define MP_DATA_HEADER_SIZE 4

#define MOOLTICUTE_DAEMON_PORT 30035
#define MOOLTICUTE_DAEMON_ADDR QHostAddress::LocalHostIPv6

//Max file size for standard data file
#define MP_MAX_FILE_SIZE 1024 * 10
Expand Down
17 changes: 14 additions & 3 deletions src/WSClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "WSClient.h"
#include "SystemNotifications/SystemNotification.h"

#define WS_URI "ws://localhost"
#define QUERY_RANDOM_NUMBER_TIME 10 * 60 * 1000 //10 min

WSClient::WSClient(QObject *parent):
Expand Down Expand Up @@ -56,8 +55,17 @@ void WSClient::openWebsocket()
this, SLOT(onWsError()));
}

QString url = QString("%1:%2").arg(WS_URI).arg(MOOLTICUTE_DAEMON_PORT);
wsocket->open(url);
// Unfortunately a nummeric IPv6 address needs some additional '[..]' brackets around
// it before putting it into a URL; whereas an IPv4 address does not. So we need to
// construct this through a URL - rather than just string concatenation.
//
QUrl qurl = QUrl();
qurl.setScheme("ws");
qurl.setHost(QHostAddress(MOOLTICUTE_DAEMON_ADDR).toString());
qurl.setPort(MOOLTICUTE_DAEMON_PORT);

wsocket->open(qurl);
qDebug() << "openWebsocket open(" << qurl.toString() << ")";
}

void WSClient::closeWebsocket()
Expand All @@ -83,8 +91,11 @@ void WSClient::onWsConnected()
{
qDebug() << "Websocket connected";
connect(wsocket, &QWebSocket::textMessageReceived, this, &WSClient::onTextMessageReceived);
qDebug() << "queryRandomNumbers()";
queryRandomNumbers();
qDebug() << "emit wsConnected()";
emit wsConnected();
qDebug() << "done onWsConnected()";
}

bool WSClient::isConnected() const
Expand Down