From 607b7cc9ed5bd281eb286dabf3c7fc7998ab214e Mon Sep 17 00:00:00 2001 From: Taylor Richards Date: Thu, 17 Mar 2016 14:42:16 -0400 Subject: [PATCH] add test for existing instance of game (an error condition) --- CMakeLists.txt | 2 +- src/network/stand_gui.cpp | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b8f3c5..c642ed1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ find_package(OpenGL REQUIRED) find_package(OpenAL REQUIRED) find_package(LibWebSockets REQUIRED) -find_package(wxWidgets COMPONENTS core base gl) +find_package(wxWidgets COMPONENTS core base gl net) include(${wxWidgets_USE_FILE}) diff --git a/src/network/stand_gui.cpp b/src/network/stand_gui.cpp index 74f4f1c..b57b096 100644 --- a/src/network/stand_gui.cpp +++ b/src/network/stand_gui.cpp @@ -18,6 +18,7 @@ #include "wx/msgdlg.h" #include "wx/process.h" #include "wx/textfile.h" +#include "wx/socket.h" #include "SDL.h" @@ -72,7 +73,7 @@ bool StandaloneApp::OnInit() throw "Unable to initialize WebSocket"; } } catch (const char *err) { - wxMessageBox(err, "Error!"); + wxMessageBox(err, "Error!", wxOK|wxICON_ERROR|wxCENTRE|wxSTAY_ON_TOP); return false; } @@ -1165,6 +1166,24 @@ bool Standalone::startFreeSpace(int argc, wxCmdLineArgsArray &argv) } } + // test if socket is in use (in case exising instance is running on same port) + wxDatagramSocket *sock; + wxIPV4address addr; + + addr.AnyAddress(); + addr.Service( (unsigned short)fsport ); + + sock = new wxDatagramSocket(addr); + + bool isok = sock->IsOk(); + + sock->Destroy(); + + if ( !isok ) { + throw "Unable to start FreeSpace\n\nAn instance is already running"; + } + + // start game executable fspid = wxExecute(epath, wxEXEC_ASYNC | wxEXEC_MAKE_GROUP_LEADER | wxEXEC_HIDE_CONSOLE); return (fspid > 0); -- 2.39.2