Skip to content

Commit

Permalink
Added ROM loaded and NES reset signals to allow for net play to recon…
Browse files Browse the repository at this point in the history
…figure accordingly during those events.
  • Loading branch information
thor2016 committed Feb 26, 2024
1 parent 145fc16 commit d212b89
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/drivers/Qt/ConsoleWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ class consoleWin_t : public QMainWindow

QString findHelpFile(void);

public:
signals:
void romLoaded(void);
void nesResetOccurred(void);

public slots:
void openDebugWindow(void);
void openHexEditor(void);
Expand Down
43 changes: 33 additions & 10 deletions src/drivers/Qt/NetPlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ NetPlayServer::NetPlayServer(QObject *parent)
instance = this;

connect(this, SIGNAL(newConnection(void)), this, SLOT(newConnectionRdy(void)));

connect(consoleWindow, SIGNAL(romLoaded(void)), this, SLOT(onRomLoad(void)));
connect(consoleWindow, SIGNAL(nesResetOccurred(void)), this, SLOT(onNesReset(void)));
}


Expand Down Expand Up @@ -329,6 +332,7 @@ int NetPlayServer::sendStateSyncReq( NetPlayClient *client )
sendMsg( client, em.buf(), em.size() );

opsCrc32 = 0;
inputClear();

return 0;
}
Expand Down Expand Up @@ -366,6 +370,29 @@ bool NetPlayServer::claimRole(NetPlayClient* client, int _role)
return success;
}
//-----------------------------------------------------------------------------
void NetPlayServer::onRomLoad()
{
//printf("New ROM Loaded!\n");

// New ROM has been loaded by server, signal clients to load and sync
for (auto& client : clientList )
{
sendRomLoadReq( client );
sendStateSyncReq( client );
}
}
//-----------------------------------------------------------------------------
void NetPlayServer::onNesReset()
{
//printf("New ROM Loaded!\n");

// NES Reset has occurred on server, signal clients sync
for (auto& client : clientList )
{
sendStateSyncReq( client );
}
}
//-----------------------------------------------------------------------------
static void serverMessageCallback( void *userData, void *msgBuf, size_t msgSize )
{
NetPlayServer *server = NetPlayServer::GetInstance();
Expand Down Expand Up @@ -587,7 +614,8 @@ void NetPlayServer::update(void)
shouldRunFrame = (clientMinFrame != 0xFFFFFFFF) &&
(clientMinFrame >= lagFrame ) &&
(clientMaxFrame < leadFrame) &&
(currFrame > lastFrame) && (numClientsPaused == 0);
( (currFrame > lastFrame) || (lastFrame == 0) ) &&
(numClientsPaused == 0);

//printf("Client Frame: Min:%u Max:%u\n", clientMinFrame, clientMaxFrame);

Expand All @@ -613,10 +641,8 @@ void NetPlayServer::update(void)

runFrameReq.toNetworkByteOrder();

for (auto it = clientList.begin(); it != clientList.end(); it++)
for (auto& client : clientList )
{
NetPlayClient *client = *it;

if (client->state > 0)
{
sendMsg( client, &runFrameReq, sizeof(runFrameReq) );
Expand All @@ -635,10 +661,8 @@ void NetPlayServer::update(void)
ping.hostTimeStamp = ts.toMilliSeconds();
ping.toNetworkByteOrder();

for (auto it = clientList.begin(); it != clientList.end(); it++)
for (auto& client : clientList )
{
NetPlayClient *client = *it;

if (client->state > 0)
{
sendMsg( client, &ping, sizeof(ping) );
Expand All @@ -649,10 +673,8 @@ void NetPlayServer::update(void)
bool shouldFlushOutput = true;
if (shouldFlushOutput)
{
for (auto it = clientList.begin(); it != clientList.end(); it++)
for (auto& client : clientList )
{
NetPlayClient *client = *it;

client->flushData();
}
}
Expand Down Expand Up @@ -1051,6 +1073,7 @@ void NetPlayClient::clientProcessMessage( void *msgBuf, size_t msgSize )

opsCrc32 = 0;
netPlayFrameData.reset();
inputClear();
}
break;
case NETPLAY_RUN_FRAME_REQ:
Expand Down
14 changes: 14 additions & 0 deletions src/drivers/Qt/NetPlay.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ class NetPlayServer : public QTcpServer
return frame;
}

void inputClear()
{
FCEU::autoScopedLock alock(inputMtx);
input.clear();
}

int sendMsg( NetPlayClient *client, void *msg, size_t msgSize);
int sendRomLoadReq( NetPlayClient *client );
int sendStateSyncReq( NetPlayClient *client );
Expand Down Expand Up @@ -143,6 +149,8 @@ class NetPlayServer : public QTcpServer

public slots:
void newConnectionRdy(void);
void onRomLoad(void);
void onNesReset(void);
};

class NetPlayClient : public QObject
Expand Down Expand Up @@ -209,6 +217,12 @@ class NetPlayClient : public QObject
return frame;
}

void inputClear()
{
FCEU::autoScopedLock alock(inputMtx);
input.clear();
}

bool isAuthenticated();
bool isPlayerRole();
bool shouldDestroy(){ return needsDestroy; }
Expand Down
11 changes: 10 additions & 1 deletion src/drivers/Qt/fceuWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,11 @@ int LoadGame(const char *path, bool silent)
//}
isloaded = 1;

//FCEUD_NetworkConnect();
// Signal to listeners that a new ROM was loaded
if ( consoleWindow )
{
emit consoleWindow->romLoaded();
}
return 1;
}

Expand Down Expand Up @@ -560,6 +564,11 @@ int fceuWrapperSoftReset(void)
if ( isloaded )
{
ResetNES();

if (consoleWindow != nullptr)
{
emit consoleWindow->nesResetOccurred();
}
}
return 0;
}
Expand Down

0 comments on commit d212b89

Please sign in to comment.