Properly shut down game server

RTFM: all processes must let go of a file for unlink to delete it.
Don't call STEAM_API_SHUTDOWN twice on shutdown.
Make sure destructor is called and add some documentation.
This commit is contained in:
William Pierce 2019-10-21 21:10:22 -07:00
parent fdb809f0c0
commit cb08af20cf
5 changed files with 12 additions and 21 deletions

View File

@ -18,5 +18,8 @@ MyGameServer::run()
}
m_socket.run_server();
// Make sure to destruct this to make sure we unlink files nicely during
// destruction rather than when the child process is being terminated
m_socket.~MyGameSocket();
SteamAPI_Shutdown();
}

View File

@ -36,7 +36,6 @@ MyGameSocket::process_request(std::string request, bool& quit) {
break;
case QUIT_GAME:
SteamAPI_Shutdown();
quit = true;
break;

View File

@ -7,7 +7,6 @@
MyServerSocket::MyServerSocket(AppId_t appid) : MySocket(appid)
{
if (file_exists(m_socket_path))
{
std::cerr << "It looks like the server before me did not shutdown properly." << std::endl;
@ -42,18 +41,6 @@ MyServerSocket::MyServerSocket(AppId_t appid) : MySocket(appid)
}
MyServerSocket::~MyServerSocket()
{
close(m_socket_fd);
unlink_file();
}
void
MyServerSocket::unlink_file()
{
unlink(m_socket_path.c_str());
}
void
MyServerSocket::run_server()
{
@ -75,10 +62,7 @@ MyServerSocket::run_server()
if (quit)
{
std::cout << "Shutting down server safely." << std:: endl;
send_message(data_socket, "SAM_ACK");
close(data_socket);
close(m_socket_fd);
unlink_file();
// destruction of this object will take care of shutdown
break;
}

View File

@ -3,11 +3,12 @@
class MyServerSocket : public MySocket
{
protected:
void unlink_file();
/**
* The server socket constructor is responsible for creating
* the socket file both server and socket will communicate through.
*/
public:
void run_server();
virtual std::string process_request(std::string request, bool& quit) = 0;
MyServerSocket(AppId_t appid);
~MyServerSocket();
};

View File

@ -12,7 +12,11 @@ MySocket::MySocket(AppId_t appid) : m_appid(appid), m_socket_fd(-1)
MySocket::~MySocket()
{
// Both server and client must unlink the socket file for it
// to be deleted on the filesystem.
close(m_socket_fd);
unlink(m_socket_path.c_str());
std::cout << "destructor called" << std::endl;
}
std::string