mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-10 20:13:24 -08:00
[Server] Publish the dev server's port through a file
The smoke test parsed the listening port out of the dev server's stdout, but the server announces it from the main thread while the logger thread writes to the same stream. A log line can land inside the announcement — Arch CI caught it — leaving the digits on the next line, so the test read nothing and failed. Add --port-file, written through QSaveFile once the server listens, and read the port from there instead of from the log. The file is either absent or complete, so logging cannot corrupt the handshake.
This commit is contained in:
parent
16e6e8e3ae
commit
5f6b8b02a5
2 changed files with 56 additions and 24 deletions
|
|
@ -15,12 +15,14 @@
|
|||
#include "smtpclient.h"
|
||||
#include "version_string.h"
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QCommandLineOption>
|
||||
#include <QCommandLineParser>
|
||||
#include <QCoreApplication>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QMetaObject>
|
||||
#include <QSaveFile>
|
||||
#include <QString>
|
||||
#include <QThread>
|
||||
#include <cstdlib>
|
||||
|
|
@ -88,6 +90,23 @@ void dumpState(Servatrice &server, const Servatrice_InMemoryDatabaseInterface &d
|
|||
std::cout << "servatrice-dev: log entries=" << database.getLogEntries().size() << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Publishes the bound port the way a script can read it.
|
||||
*
|
||||
* The stdout announcement is written from this thread while the logger thread writes to the same
|
||||
* stream, so a reader can see a log line inside it. A file written through QSaveFile is either
|
||||
* absent or complete, which makes the port deterministic to consume.
|
||||
*/
|
||||
bool writeListeningPort(const QString &path, int port)
|
||||
{
|
||||
QSaveFile file(path);
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||
return false;
|
||||
}
|
||||
file.write(QByteArray::number(port) + '\n');
|
||||
return file.commit();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
|
|
@ -103,9 +122,13 @@ int main(int argc, char *argv[])
|
|||
parser.addVersionOption();
|
||||
|
||||
QCommandLineOption portOption({"p", "port"}, "TCP port to listen on; 0 picks an ephemeral port.", "port", "0");
|
||||
QCommandLineOption portFileOption("port-file",
|
||||
"Write the bound listening port to this file once the server listens.",
|
||||
"path");
|
||||
QCommandLineOption seedOption("seed", "Seed for the game RNG; random when omitted.", "seed");
|
||||
QCommandLineOption dumpStateOption("dump-state", "Print the server state after startup and keep serving.");
|
||||
parser.addOption(portOption);
|
||||
parser.addOption(portFileOption);
|
||||
parser.addOption(seedOption);
|
||||
parser.addOption(dumpStateOption);
|
||||
parser.process(app);
|
||||
|
|
@ -169,11 +192,18 @@ int main(int argc, char *argv[])
|
|||
std::cerr << "servatrice-dev: the server is not listening" << std::endl;
|
||||
retval = EXIT_FAILURE;
|
||||
} else {
|
||||
std::cout << "servatrice-dev: listening on port " << listeningPort << std::endl;
|
||||
if (parser.isSet(dumpStateOption)) {
|
||||
dumpState(*server, *server->getInMemoryDatabase());
|
||||
const QString portFilePath = parser.value(portFileOption);
|
||||
if (!portFilePath.isEmpty() && !writeListeningPort(portFilePath, listeningPort)) {
|
||||
std::cerr << "servatrice-dev: cannot write the listening port to " << portFilePath.toStdString()
|
||||
<< std::endl;
|
||||
retval = EXIT_FAILURE;
|
||||
} else {
|
||||
std::cout << "servatrice-dev: listening on port " << listeningPort << std::endl;
|
||||
if (parser.isSet(dumpStateOption)) {
|
||||
dumpState(*server, *server->getInMemoryDatabase());
|
||||
}
|
||||
retval = QCoreApplication::exec();
|
||||
}
|
||||
retval = QCoreApplication::exec();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue