Skip to content

Commit

Permalink
Refactor: consistency fixes
Browse files Browse the repository at this point in the history
Turns out that the signal method I've been using this whole time must've been congested to the point that, at least with Flycast opting to do simultaneous outputs, it would cause QMamehook's input stream to be significantly delayed.
So, instead of only checking when the socket is *disconnected*, we cut the signal entirely: putting the blocking function on while the socket is *connected*, then just run straight through until the socket's buffer is exhausted. Lather, rinse, repeat ad-nauseum until it can no longer read, which then prompts the socket abort function.
This primarily fixes Flycast's outputs--namely Ninja Assault, if one decides to hook lamp7 to a gun ff device, but it should also make general QMamehook performance more consistent and feel less jittery overall, as this ensures no commands or read cycles are ever missed ever again.

So I guess, thanks Flycast for being so weird???
  • Loading branch information
SeongGino authored Feb 29, 2024
1 parent 2b5236d commit 279e88b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
37 changes: 20 additions & 17 deletions qhookermain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,38 @@ qhookerMain::qhookerMain(QObject *parent)
void qhookerMain::run()
{
//qDebug() << "Main app is running!";
tcpSocket = new QTcpSocket();
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readyRead()));
//connect(&tcpSocket, &QAbstractSocket::readyRead, this, &qhookerMain::ReadyRead);
//connect(&tcpSocket, &QAbstractSocket::errorOccurred, this, &qhookerMain::errorOccurred);

SerialInit();

qInfo() << "Waiting for MAME Network Output @ localhost:8000 ...";
qInfo() << "Waiting for MAME-compatible Network Output @ localhost:8000 ...";

for(;;) {
switch(tcpSocket->state()) { // oh, bite me QT designer--these two are the only ones I need. :/
switch(tcpSocket.state()) { // oh, bite me QT designer--these two are the only ones I need. :/
case QAbstractSocket::UnconnectedState:
tcpSocket->connectToHost("localhost", 8000);
if(tcpSocket->waitForConnected(5000)) {
qInfo() << "Connected to MAME instance!";
tcpSocket.connectToHost("localhost", 8000);
if(tcpSocket.waitForConnected(5000)) {
qInfo() << "Connected to output server instance!";
} else {
QThread::sleep(1);
}
break;
case QAbstractSocket::ConnectedState:
while(tcpSocket->state() == QAbstractSocket::ConnectedState) {
if(!tcpSocket->waitForReadyRead(-1)) {
// mame has disconnected, so exit out.
qInfo() << "MAME exiting, disconnecting...";
tcpSocket->abort();
// in case we exit without connecting to a game (*coughFLYCASTcough*)
while(tcpSocket.state() == QAbstractSocket::ConnectedState) {
if(tcpSocket.waitForReadyRead(-1)) {
while(!tcpSocket.atEnd()) {
ReadyRead();
}
} else {
qInfo() << "Server closing, disconnecting...";
tcpSocket.abort();
if(!gameName.isEmpty()) {
gameName.clear();
delete settings;
settingsMap.clear();
}
// in case we exit without connecting to a game (*coughFLYCASTcough*)
for(uint8_t i = 0; i < serialFoundList.count(); i++) {
if(serialPort[i].isOpen()) {
serialPort[i].write("E");
Expand All @@ -55,7 +58,6 @@ void qhookerMain::run()
}
}
}
break;
}
}
break;
Expand Down Expand Up @@ -282,16 +284,17 @@ void qhookerMain::GameStarted(QString input)
}


void qhookerMain::readyRead()
void qhookerMain::ReadyRead()
{
buffer.clear();
if(gameName.isEmpty()) {
GameSearching(tcpSocket->readLine());
GameSearching(tcpSocket.readLine());
} else {
GameStarted(tcpSocket->readLine());
GameStarted(tcpSocket.readLine());
}
}


void qhookerMain::LoadConfig(QString path)
{
settings = new QSettings(path, QSettings::IniFormat);
Expand Down
8 changes: 5 additions & 3 deletions qhookermain.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class qhookerMain : public QObject

QSettings *settings;

QTcpSocket *tcpSocket;
QTcpSocket tcpSocket;

QSerialPort *serialPort;

Expand All @@ -38,6 +38,8 @@ class qhookerMain : public QObject

void GameStarted(QString input);

void ReadyRead();

public:
explicit qhookerMain(QObject *parent = 0);

Expand All @@ -53,8 +55,8 @@ public slots:

void aboutToQuitApp();

private slots:
void readyRead();
//private slots:

};

#endif // QHOOKERMAIN_H

0 comments on commit 279e88b

Please # to comment.