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();
|
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();
|
SteamAPI_Shutdown();
|
||||||
}
|
}
|
||||||
|
|
@ -36,7 +36,6 @@ MyGameSocket::process_request(std::string request, bool& quit) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case QUIT_GAME:
|
case QUIT_GAME:
|
||||||
SteamAPI_Shutdown();
|
|
||||||
quit = true;
|
quit = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@
|
||||||
|
|
||||||
MyServerSocket::MyServerSocket(AppId_t appid) : MySocket(appid)
|
MyServerSocket::MyServerSocket(AppId_t appid) : MySocket(appid)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (file_exists(m_socket_path))
|
if (file_exists(m_socket_path))
|
||||||
{
|
{
|
||||||
std::cerr << "It looks like the server before me did not shutdown properly." << std::endl;
|
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
|
void
|
||||||
MyServerSocket::run_server()
|
MyServerSocket::run_server()
|
||||||
{
|
{
|
||||||
|
|
@ -75,10 +62,7 @@ MyServerSocket::run_server()
|
||||||
if (quit)
|
if (quit)
|
||||||
{
|
{
|
||||||
std::cout << "Shutting down server safely." << std:: endl;
|
std::cout << "Shutting down server safely." << std:: endl;
|
||||||
send_message(data_socket, "SAM_ACK");
|
// destruction of this object will take care of shutdown
|
||||||
close(data_socket);
|
|
||||||
close(m_socket_fd);
|
|
||||||
unlink_file();
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,12 @@
|
||||||
|
|
||||||
class MyServerSocket : public MySocket
|
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:
|
public:
|
||||||
void run_server();
|
void run_server();
|
||||||
virtual std::string process_request(std::string request, bool& quit) = 0;
|
virtual std::string process_request(std::string request, bool& quit) = 0;
|
||||||
MyServerSocket(AppId_t appid);
|
MyServerSocket(AppId_t appid);
|
||||||
~MyServerSocket();
|
|
||||||
};
|
};
|
||||||
|
|
@ -12,7 +12,11 @@ MySocket::MySocket(AppId_t appid) : m_appid(appid), m_socket_fd(-1)
|
||||||
|
|
||||||
MySocket::~MySocket()
|
MySocket::~MySocket()
|
||||||
{
|
{
|
||||||
|
// Both server and client must unlink the socket file for it
|
||||||
|
// to be deleted on the filesystem.
|
||||||
close(m_socket_fd);
|
close(m_socket_fd);
|
||||||
|
unlink(m_socket_path.c_str());
|
||||||
|
std::cout << "destructor called" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user