Skip to content

Commit

Permalink
Netplay: Fixed a couple of crashes and the "select controller" menu
Browse files Browse the repository at this point in the history
-A client disconnecting could cause the server to crash
-A server stopping, then starting again, could cause the client to crash when reconnecting to the server after getting disconnected by the server stopped
-The select controller menu was not getting updated and was always empty
  • Loading branch information
SourMesen committed Aug 7, 2024
1 parent 1a98b3c commit f3b75db
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Core/Netplay/GameClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ bool GameClient::Connected()

void GameClient::Connect(ClientConnectionData &connectionData)
{
Disconnect();

_stop = false;
unique_ptr<Socket> socket(new Socket());
if(socket->Connect(connectionData.Host.c_str(), connectionData.Port)) {
Expand Down
2 changes: 2 additions & 0 deletions Core/Netplay/GameServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ void GameServer::UpdateConnections()
vector<unique_ptr<GameServerConnection>> connectionsToRemove;
for(int i = (int)_openConnections.size() - 1; i >= 0; i--) {
if(_openConnections[i]->ConnectionError()) {
//Pause emu thread to ensure nothing else modifies/accesses the _openConnections list while removing dead connections
auto lock = _emu->AcquireLock();
_openConnections.erase(_openConnections.begin() + i);
} else {
_openConnections[i]->ProcessMessages();
Expand Down
10 changes: 7 additions & 3 deletions UI/ViewModels/MainMenuViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,10 +1246,10 @@ private async void InstallHdPack(Window wnd)
}
}

public void UpdateNetplayMenu()
public bool UpdateNetplayMenu()
{
if(!NetplayApi.IsServerRunning() && !NetplayApi.IsConnected()) {
return;
return false;
}

List<object> controllerActions = new();
Expand All @@ -1276,7 +1276,9 @@ public void UpdateNetplayMenu()
playerIndex++;
}

controllerActions.Add(new ContextMenuSeparator());
if(controllerActions.Count > 0) {
controllerActions.Add(new ContextMenuSeparator());
}

controllerActions.Add(new MainMenuAction() {
ActionType = ActionType.Custom,
Expand All @@ -1286,6 +1288,8 @@ public void UpdateNetplayMenu()
});

_selectControllerAction.SubActions = controllerActions;

return true;
}
}
}
9 changes: 8 additions & 1 deletion UI/Views/MainMenuView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Avalonia.Markup.Xaml;
using Mesen.Utilities;
using Mesen.ViewModels;
using System.Collections;

namespace Mesen.Views
{
Expand Down Expand Up @@ -31,7 +32,13 @@ private void InitializeComponent()
private void mnuTools_Opened(object sender, RoutedEventArgs e)
{
if(DataContext is MainMenuViewModel model) {
model.UpdateNetplayMenu();
if(model.UpdateNetplayMenu() && e.Source is MenuItem item) {
//Force a refresh of the tools menu to ensure
//the "Select controller" submenu gets updated
IEnumerable? items = item.ItemsSource;
item.ItemsSource = null;
item.ItemsSource = items;
}
}
}
}
Expand Down

0 comments on commit f3b75db

Please sign in to comment.