Skip to content

Commit

Permalink
Added logic to allow netplay host to control pause state of clients.
Browse files Browse the repository at this point in the history
  • Loading branch information
thor2016 committed Apr 8, 2024
1 parent e7d2341 commit 9036dd0
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/drivers/Qt/ConsoleWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3601,6 +3601,9 @@ void consoleWin_t::consolePause(void)
fceuWrapperTogglePause();
FCEU_WRAPPER_UNLOCK();

bool isPaused = FCEUI_EmulationPaused() ? true : false;
emit pauseToggled( isPaused );

mainMenuEmuPauseSet = false;
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/drivers/Qt/ConsoleWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ class consoleWin_t : public QMainWindow
void romUnload(void);
void stateLoaded(void);
void nesResetOccurred(void);
void pauseToggled(bool state);

public slots:
void openDebugWindow(void);
Expand Down
105 changes: 93 additions & 12 deletions src/drivers/Qt/NetPlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ NetPlayServer::NetPlayServer(QObject *parent)
connect(consoleWindow, SIGNAL(romUnload(void)), this, SLOT(onRomUnload(void)));
connect(consoleWindow, SIGNAL(stateLoaded(void)), this, SLOT(onStateLoad(void)));
connect(consoleWindow, SIGNAL(nesResetOccurred(void)), this, SLOT(onNesReset(void)));
connect(consoleWindow, SIGNAL(pauseToggled(bool)), this, SLOT(onPauseToggled(bool)));

FCEU_WRAPPER_LOCK();
inputFrameCount = static_cast<uint32_t>(currFrameCounter);
Expand Down Expand Up @@ -373,7 +374,6 @@ int NetPlayServer::sendRomLoadReq( NetPlayClient *client )
Strlcpy( msg.fileName, GameInfo->filename, sizeof(msg.fileName) );

printf("Sending ROM Load Request: %s %lu\n", filepath, fileSize );
FCEUI_SetEmulationPaused(EMULATIONPAUSED_PAUSED);

sendMsg( client, &msg, sizeof(netPlayLoadRomReq), [&msg]{ msg.toNetworkByteOrder(); } );

Expand Down Expand Up @@ -431,7 +431,6 @@ int NetPlayServer::sendStateSyncReq( NetPlayClient *client )
}

printf("Sending ROM Sync Request: %zu\n", em.size());
FCEUI_SetEmulationPaused(EMULATIONPAUSED_PAUSED);

sendMsg( client, &resp, sizeof(netPlayLoadStateResp), [&resp]{ resp.toNetworkByteOrder(); } );
sendMsg( client, em.buf(), em.size() );
Expand All @@ -441,6 +440,44 @@ int NetPlayServer::sendStateSyncReq( NetPlayClient *client )
return 0;
}
//-----------------------------------------------------------------------------
int NetPlayServer::sendPause( NetPlayClient *client )
{
netPlayMsgHdr msg(NETPLAY_CLIENT_PAUSE_REQ);

sendMsg( client, &msg, sizeof(netPlayMsgHdr), [&msg]{ msg.toNetworkByteOrder(); } );

return 0;
}
//-----------------------------------------------------------------------------
int NetPlayServer::sendUnpause( NetPlayClient *client )
{
netPlayMsgHdr msg(NETPLAY_CLIENT_UNPAUSE_REQ);

sendMsg( client, &msg, sizeof(netPlayMsgHdr), [&msg]{ msg.toNetworkByteOrder(); } );

return 0;
}
//-----------------------------------------------------------------------------
int NetPlayServer::sendPauseAll()
{
int ret = 0;
for (auto& client : clientList )
{
ret |= sendPause( client );
}
return ret;
}
//-----------------------------------------------------------------------------
int NetPlayServer::sendUnpauseAll()
{
int ret = 0;
for (auto& client : clientList )
{
ret |= sendUnpause( client );
}
return ret;
}
//-----------------------------------------------------------------------------
void NetPlayServer::setRole(int _role)
{
role = _role;
Expand Down Expand Up @@ -507,6 +544,8 @@ void NetPlayServer::onRomLoad()
inputClear();
inputFrameCount = static_cast<uint32_t>(currFrameCounter);

sendPauseAll();

// New ROM has been loaded by server, signal clients to load and sync
for (auto& client : clientList )
{
Expand All @@ -524,6 +563,8 @@ void NetPlayServer::onRomUnload()

unloadMsg.toNetworkByteOrder();

sendPauseAll();

// New ROM has been loaded by server, signal clients to load and sync
for (auto& client : clientList )
{
Expand All @@ -542,6 +583,8 @@ void NetPlayServer::onStateLoad()
inputClear();
inputFrameCount = static_cast<uint32_t>(currFrameCounter);

sendPauseAll();

// New State has been loaded by server, signal clients to load and sync
for (auto& client : clientList )
{
Expand All @@ -562,6 +605,8 @@ void NetPlayServer::onNesReset()
inputClear();
inputFrameCount = static_cast<uint32_t>(currFrameCounter);

sendPauseAll();

// NES Reset has occurred on server, signal clients sync
for (auto& client : clientList )
{
Expand All @@ -571,6 +616,18 @@ void NetPlayServer::onNesReset()
FCEU_WRAPPER_UNLOCK();
}
//-----------------------------------------------------------------------------
void NetPlayServer::onPauseToggled( bool isPaused )
{
if (isPaused)
{
sendPauseAll();
}
else
{
sendUnpauseAll();
}
}
//-----------------------------------------------------------------------------
void NetPlayServer::resyncClient( NetPlayClient *client )
{
FCEU_WRAPPER_LOCK();
Expand Down Expand Up @@ -667,6 +724,14 @@ void NetPlayServer::serverProcessMessage( NetPlayClient *client, void *msgBuf, s
FCEU_WRAPPER_LOCK();
resyncClient(client);
client->state = 1;
if (FCEUI_EmulationPaused())
{
sendPauseAll();
}
else
{
sendUnpauseAll();
}
FCEU_WRAPPER_UNLOCK();
FCEU_DispMessage("%s Joined",0, client->userName.toLocal8Bit().constData());
}
Expand Down Expand Up @@ -787,6 +852,13 @@ void NetPlayServer::serverProcessMessage( NetPlayClient *client, void *msgBuf, s
}
}
break;
case NETPLAY_SYNC_STATE_REQ:
{
FCEU_WRAPPER_LOCK();
resyncClient( client );
FCEU_WRAPPER_UNLOCK();
}
break;
case NETPLAY_SYNC_STATE_RESP:
{
netPlayLoadStateResp* msg = static_cast<netPlayLoadStateResp*>(msgBuf);
Expand Down Expand Up @@ -817,13 +889,6 @@ void NetPlayServer::serverProcessMessage( NetPlayClient *client, void *msgBuf, s
}
}
break;
case NETPLAY_CLIENT_SYNC_REQ:
{
FCEU_WRAPPER_LOCK();
resyncClient( client );
FCEU_WRAPPER_UNLOCK();
}
break;
default:
printf("Unknown Msg: %08X\n", msgId);
break;
Expand Down Expand Up @@ -878,6 +943,7 @@ void NetPlayServer::processClientRomLoadRequests(void)
FCEUI_SetEmulationPaused(EMULATIONPAUSED_PAUSED);
FCEU_WRAPPER_UNLOCK();

sendPauseAll();
resyncAllClients();
}
else
Expand Down Expand Up @@ -1394,7 +1460,6 @@ int NetPlayClient::requestRomLoad( const char *romPath )
Strlcpy( msg.fileName, fi.fileName().toLocal8Bit().constData(), sizeof(msg.fileName) );

printf("Sending ROM Load Request: %s %lu\n", romPath, fileSize );
FCEUI_SetEmulationPaused(EMULATIONPAUSED_PAUSED);

msg.toNetworkByteOrder();
sock->write( reinterpret_cast<const char*>(&msg), sizeof(netPlayLoadRomReq) );
Expand Down Expand Up @@ -1467,7 +1532,7 @@ int NetPlayClient::requestStateLoad(EMUFILE *is)
//-----------------------------------------------------------------------------
int NetPlayClient::requestSync(void)
{
netPlayMsgHdr hdr(NETPLAY_CLIENT_SYNC_REQ);
netPlayMsgHdr hdr(NETPLAY_SYNC_STATE_REQ);

hdr.toNetworkByteOrder();
sock->write( reinterpret_cast<const char*>(&hdr), sizeof(netPlayMsgHdr));
Expand Down Expand Up @@ -1717,7 +1782,6 @@ void NetPlayClient::clientProcessMessage( void *msgBuf, size_t msgSize )

FCEU_WRAPPER_LOCK();
LoadGame( filepath.toLocal8Bit().constData(), true, true );
FCEUI_SetEmulationPaused(EMULATIONPAUSED_PAUSED);

opsCrc32 = 0;
netPlayFrameData.reset();
Expand Down Expand Up @@ -1817,6 +1881,22 @@ void NetPlayClient::clientProcessMessage( void *msgBuf, size_t msgSize )
sock->write( (const char*)&pong, sizeof(netPlayPingResp) );
}
break;
case NETPLAY_CLIENT_PAUSE_REQ:
{
if (!FCEUI_EmulationPaused())
{
FCEUI_ToggleEmulationPause();
}
}
break;
case NETPLAY_CLIENT_UNPAUSE_REQ:
{
if (FCEUI_EmulationPaused())
{
FCEUI_ToggleEmulationPause();
}
}
break;
default:
printf("Unknown Msg: %08X\n", msgId);
break;
Expand Down Expand Up @@ -2480,6 +2560,7 @@ void NetPlayHostStatusDialog::resyncAllPlayers(void)

if (server != nullptr)
{
server->sendPauseAll();
server->resyncAllClients();
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/drivers/Qt/NetPlay.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ class NetPlayServer : public QTcpServer
int sendMsg( NetPlayClient *client, void *msg, size_t msgSize, std::function<void(void)> netByteOrderConvertFunc = []{});
int sendRomLoadReq( NetPlayClient *client );
int sendStateSyncReq( NetPlayClient *client );
int sendPause( NetPlayClient *client );
int sendUnpause( NetPlayClient *client );
int sendPauseAll(void);
int sendUnpauseAll(void);
void setRole(int _role);
int getRole(void){ return role; }
bool claimRole(NetPlayClient* client, int _role);
Expand Down Expand Up @@ -181,6 +185,7 @@ class NetPlayServer : public QTcpServer
void onRomUnload(void);
void onStateLoad(void);
void onNesReset(void);
void onPauseToggled(bool);
void processClientRomLoadRequests(void);
void processClientStateLoadRequests(void);
};
Expand Down
3 changes: 2 additions & 1 deletion src/drivers/Qt/NetPlayMsgDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ enum netPlayMsgType
NETPLAY_SYNC_STATE_RESP,
NETPLAY_RUN_FRAME_REQ = 30,
NETPLAY_CLIENT_STATE = 40,
NETPLAY_CLIENT_SYNC_REQ,
NETPLAY_CLIENT_PAUSE_REQ,
NETPLAY_CLIENT_UNPAUSE_REQ,
NETPLAY_INFO_MSG = 50,
NETPLAY_ERROR_MSG,
NETPLAY_CHAT_MSG,
Expand Down

0 comments on commit 9036dd0

Please sign in to comment.