mirror of
https://github.com/zebrajr/SamRewritten.git
synced 2025-12-06 12:19:51 +01:00
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:
parent
fdb809f0c0
commit
cb08af20cf
|
|
@ -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();
|
||||
}
|
||||
|
|
@ -36,7 +36,6 @@ MyGameSocket::process_request(std::string request, bool& quit) {
|
|||
break;
|
||||
|
||||
case QUIT_GAME:
|
||||
SteamAPI_Shutdown();
|
||||
quit = true;
|
||||
break;
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
};
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user