Added logic to allow netplay host to control pause state of clients.

This commit is contained in:
harry 2024-04-07 21:21:18 -04:00
parent e7d234136c
commit 9036dd07bd
5 changed files with 104 additions and 13 deletions

View File

@ -3601,6 +3601,9 @@ void consoleWin_t::consolePause(void)
fceuWrapperTogglePause(); fceuWrapperTogglePause();
FCEU_WRAPPER_UNLOCK(); FCEU_WRAPPER_UNLOCK();
bool isPaused = FCEUI_EmulationPaused() ? true : false;
emit pauseToggled( isPaused );
mainMenuEmuPauseSet = false; mainMenuEmuPauseSet = false;
return; return;
} }

View File

@ -331,6 +331,7 @@ class consoleWin_t : public QMainWindow
void romUnload(void); void romUnload(void);
void stateLoaded(void); void stateLoaded(void);
void nesResetOccurred(void); void nesResetOccurred(void);
void pauseToggled(bool state);
public slots: public slots:
void openDebugWindow(void); void openDebugWindow(void);

View File

@ -182,6 +182,7 @@ NetPlayServer::NetPlayServer(QObject *parent)
connect(consoleWindow, SIGNAL(romUnload(void)), this, SLOT(onRomUnload(void))); connect(consoleWindow, SIGNAL(romUnload(void)), this, SLOT(onRomUnload(void)));
connect(consoleWindow, SIGNAL(stateLoaded(void)), this, SLOT(onStateLoad(void))); connect(consoleWindow, SIGNAL(stateLoaded(void)), this, SLOT(onStateLoad(void)));
connect(consoleWindow, SIGNAL(nesResetOccurred(void)), this, SLOT(onNesReset(void))); connect(consoleWindow, SIGNAL(nesResetOccurred(void)), this, SLOT(onNesReset(void)));
connect(consoleWindow, SIGNAL(pauseToggled(bool)), this, SLOT(onPauseToggled(bool)));
FCEU_WRAPPER_LOCK(); FCEU_WRAPPER_LOCK();
inputFrameCount = static_cast<uint32_t>(currFrameCounter); inputFrameCount = static_cast<uint32_t>(currFrameCounter);
@ -373,7 +374,6 @@ int NetPlayServer::sendRomLoadReq( NetPlayClient *client )
Strlcpy( msg.fileName, GameInfo->filename, sizeof(msg.fileName) ); Strlcpy( msg.fileName, GameInfo->filename, sizeof(msg.fileName) );
printf("Sending ROM Load Request: %s %lu\n", filepath, fileSize ); printf("Sending ROM Load Request: %s %lu\n", filepath, fileSize );
FCEUI_SetEmulationPaused(EMULATIONPAUSED_PAUSED);
sendMsg( client, &msg, sizeof(netPlayLoadRomReq), [&msg]{ msg.toNetworkByteOrder(); } ); sendMsg( client, &msg, sizeof(netPlayLoadRomReq), [&msg]{ msg.toNetworkByteOrder(); } );
@ -431,7 +431,6 @@ int NetPlayServer::sendStateSyncReq( NetPlayClient *client )
} }
printf("Sending ROM Sync Request: %zu\n", em.size()); printf("Sending ROM Sync Request: %zu\n", em.size());
FCEUI_SetEmulationPaused(EMULATIONPAUSED_PAUSED);
sendMsg( client, &resp, sizeof(netPlayLoadStateResp), [&resp]{ resp.toNetworkByteOrder(); } ); sendMsg( client, &resp, sizeof(netPlayLoadStateResp), [&resp]{ resp.toNetworkByteOrder(); } );
sendMsg( client, em.buf(), em.size() ); sendMsg( client, em.buf(), em.size() );
@ -441,6 +440,44 @@ int NetPlayServer::sendStateSyncReq( NetPlayClient *client )
return 0; 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) void NetPlayServer::setRole(int _role)
{ {
role = _role; role = _role;
@ -507,6 +544,8 @@ void NetPlayServer::onRomLoad()
inputClear(); inputClear();
inputFrameCount = static_cast<uint32_t>(currFrameCounter); inputFrameCount = static_cast<uint32_t>(currFrameCounter);
sendPauseAll();
// New ROM has been loaded by server, signal clients to load and sync // New ROM has been loaded by server, signal clients to load and sync
for (auto& client : clientList ) for (auto& client : clientList )
{ {
@ -524,6 +563,8 @@ void NetPlayServer::onRomUnload()
unloadMsg.toNetworkByteOrder(); unloadMsg.toNetworkByteOrder();
sendPauseAll();
// New ROM has been loaded by server, signal clients to load and sync // New ROM has been loaded by server, signal clients to load and sync
for (auto& client : clientList ) for (auto& client : clientList )
{ {
@ -542,6 +583,8 @@ void NetPlayServer::onStateLoad()
inputClear(); inputClear();
inputFrameCount = static_cast<uint32_t>(currFrameCounter); inputFrameCount = static_cast<uint32_t>(currFrameCounter);
sendPauseAll();
// New State has been loaded by server, signal clients to load and sync // New State has been loaded by server, signal clients to load and sync
for (auto& client : clientList ) for (auto& client : clientList )
{ {
@ -562,6 +605,8 @@ void NetPlayServer::onNesReset()
inputClear(); inputClear();
inputFrameCount = static_cast<uint32_t>(currFrameCounter); inputFrameCount = static_cast<uint32_t>(currFrameCounter);
sendPauseAll();
// NES Reset has occurred on server, signal clients sync // NES Reset has occurred on server, signal clients sync
for (auto& client : clientList ) for (auto& client : clientList )
{ {
@ -571,6 +616,18 @@ void NetPlayServer::onNesReset()
FCEU_WRAPPER_UNLOCK(); FCEU_WRAPPER_UNLOCK();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void NetPlayServer::onPauseToggled( bool isPaused )
{
if (isPaused)
{
sendPauseAll();
}
else
{
sendUnpauseAll();
}
}
//-----------------------------------------------------------------------------
void NetPlayServer::resyncClient( NetPlayClient *client ) void NetPlayServer::resyncClient( NetPlayClient *client )
{ {
FCEU_WRAPPER_LOCK(); FCEU_WRAPPER_LOCK();
@ -667,6 +724,14 @@ void NetPlayServer::serverProcessMessage( NetPlayClient *client, void *msgBuf, s
FCEU_WRAPPER_LOCK(); FCEU_WRAPPER_LOCK();
resyncClient(client); resyncClient(client);
client->state = 1; client->state = 1;
if (FCEUI_EmulationPaused())
{
sendPauseAll();
}
else
{
sendUnpauseAll();
}
FCEU_WRAPPER_UNLOCK(); FCEU_WRAPPER_UNLOCK();
FCEU_DispMessage("%s Joined",0, client->userName.toLocal8Bit().constData()); FCEU_DispMessage("%s Joined",0, client->userName.toLocal8Bit().constData());
} }
@ -787,6 +852,13 @@ void NetPlayServer::serverProcessMessage( NetPlayClient *client, void *msgBuf, s
} }
} }
break; break;
case NETPLAY_SYNC_STATE_REQ:
{
FCEU_WRAPPER_LOCK();
resyncClient( client );
FCEU_WRAPPER_UNLOCK();
}
break;
case NETPLAY_SYNC_STATE_RESP: case NETPLAY_SYNC_STATE_RESP:
{ {
netPlayLoadStateResp* msg = static_cast<netPlayLoadStateResp*>(msgBuf); netPlayLoadStateResp* msg = static_cast<netPlayLoadStateResp*>(msgBuf);
@ -817,13 +889,6 @@ void NetPlayServer::serverProcessMessage( NetPlayClient *client, void *msgBuf, s
} }
} }
break; break;
case NETPLAY_CLIENT_SYNC_REQ:
{
FCEU_WRAPPER_LOCK();
resyncClient( client );
FCEU_WRAPPER_UNLOCK();
}
break;
default: default:
printf("Unknown Msg: %08X\n", msgId); printf("Unknown Msg: %08X\n", msgId);
break; break;
@ -878,6 +943,7 @@ void NetPlayServer::processClientRomLoadRequests(void)
FCEUI_SetEmulationPaused(EMULATIONPAUSED_PAUSED); FCEUI_SetEmulationPaused(EMULATIONPAUSED_PAUSED);
FCEU_WRAPPER_UNLOCK(); FCEU_WRAPPER_UNLOCK();
sendPauseAll();
resyncAllClients(); resyncAllClients();
} }
else else
@ -1394,7 +1460,6 @@ int NetPlayClient::requestRomLoad( const char *romPath )
Strlcpy( msg.fileName, fi.fileName().toLocal8Bit().constData(), sizeof(msg.fileName) ); Strlcpy( msg.fileName, fi.fileName().toLocal8Bit().constData(), sizeof(msg.fileName) );
printf("Sending ROM Load Request: %s %lu\n", romPath, fileSize ); printf("Sending ROM Load Request: %s %lu\n", romPath, fileSize );
FCEUI_SetEmulationPaused(EMULATIONPAUSED_PAUSED);
msg.toNetworkByteOrder(); msg.toNetworkByteOrder();
sock->write( reinterpret_cast<const char*>(&msg), sizeof(netPlayLoadRomReq) ); sock->write( reinterpret_cast<const char*>(&msg), sizeof(netPlayLoadRomReq) );
@ -1467,7 +1532,7 @@ int NetPlayClient::requestStateLoad(EMUFILE *is)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
int NetPlayClient::requestSync(void) int NetPlayClient::requestSync(void)
{ {
netPlayMsgHdr hdr(NETPLAY_CLIENT_SYNC_REQ); netPlayMsgHdr hdr(NETPLAY_SYNC_STATE_REQ);
hdr.toNetworkByteOrder(); hdr.toNetworkByteOrder();
sock->write( reinterpret_cast<const char*>(&hdr), sizeof(netPlayMsgHdr)); sock->write( reinterpret_cast<const char*>(&hdr), sizeof(netPlayMsgHdr));
@ -1717,7 +1782,6 @@ void NetPlayClient::clientProcessMessage( void *msgBuf, size_t msgSize )
FCEU_WRAPPER_LOCK(); FCEU_WRAPPER_LOCK();
LoadGame( filepath.toLocal8Bit().constData(), true, true ); LoadGame( filepath.toLocal8Bit().constData(), true, true );
FCEUI_SetEmulationPaused(EMULATIONPAUSED_PAUSED);
opsCrc32 = 0; opsCrc32 = 0;
netPlayFrameData.reset(); netPlayFrameData.reset();
@ -1817,6 +1881,22 @@ void NetPlayClient::clientProcessMessage( void *msgBuf, size_t msgSize )
sock->write( (const char*)&pong, sizeof(netPlayPingResp) ); sock->write( (const char*)&pong, sizeof(netPlayPingResp) );
} }
break; break;
case NETPLAY_CLIENT_PAUSE_REQ:
{
if (!FCEUI_EmulationPaused())
{
FCEUI_ToggleEmulationPause();
}
}
break;
case NETPLAY_CLIENT_UNPAUSE_REQ:
{
if (FCEUI_EmulationPaused())
{
FCEUI_ToggleEmulationPause();
}
}
break;
default: default:
printf("Unknown Msg: %08X\n", msgId); printf("Unknown Msg: %08X\n", msgId);
break; break;
@ -2480,6 +2560,7 @@ void NetPlayHostStatusDialog::resyncAllPlayers(void)
if (server != nullptr) if (server != nullptr)
{ {
server->sendPauseAll();
server->resyncAllClients(); server->resyncAllClients();
} }
} }

View File

@ -131,6 +131,10 @@ class NetPlayServer : public QTcpServer
int sendMsg( NetPlayClient *client, void *msg, size_t msgSize, std::function<void(void)> netByteOrderConvertFunc = []{}); int sendMsg( NetPlayClient *client, void *msg, size_t msgSize, std::function<void(void)> netByteOrderConvertFunc = []{});
int sendRomLoadReq( NetPlayClient *client ); int sendRomLoadReq( NetPlayClient *client );
int sendStateSyncReq( NetPlayClient *client ); int sendStateSyncReq( NetPlayClient *client );
int sendPause( NetPlayClient *client );
int sendUnpause( NetPlayClient *client );
int sendPauseAll(void);
int sendUnpauseAll(void);
void setRole(int _role); void setRole(int _role);
int getRole(void){ return role; } int getRole(void){ return role; }
bool claimRole(NetPlayClient* client, int _role); bool claimRole(NetPlayClient* client, int _role);
@ -181,6 +185,7 @@ class NetPlayServer : public QTcpServer
void onRomUnload(void); void onRomUnload(void);
void onStateLoad(void); void onStateLoad(void);
void onNesReset(void); void onNesReset(void);
void onPauseToggled(bool);
void processClientRomLoadRequests(void); void processClientRomLoadRequests(void);
void processClientStateLoadRequests(void); void processClientStateLoadRequests(void);
}; };

View File

@ -23,7 +23,8 @@ enum netPlayMsgType
NETPLAY_SYNC_STATE_RESP, NETPLAY_SYNC_STATE_RESP,
NETPLAY_RUN_FRAME_REQ = 30, NETPLAY_RUN_FRAME_REQ = 30,
NETPLAY_CLIENT_STATE = 40, NETPLAY_CLIENT_STATE = 40,
NETPLAY_CLIENT_SYNC_REQ, NETPLAY_CLIENT_PAUSE_REQ,
NETPLAY_CLIENT_UNPAUSE_REQ,
NETPLAY_INFO_MSG = 50, NETPLAY_INFO_MSG = 50,
NETPLAY_ERROR_MSG, NETPLAY_ERROR_MSG,
NETPLAY_CHAT_MSG, NETPLAY_CHAT_MSG,