diff --git a/src/MyGameServer.cpp b/src/MyGameServer.cpp index 54e135f..e6fa08d 100644 --- a/src/MyGameServer.cpp +++ b/src/MyGameServer.cpp @@ -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(); } \ No newline at end of file diff --git a/src/sockets/MyGameSocket.cpp b/src/sockets/MyGameSocket.cpp index f322fca..51bde97 100644 --- a/src/sockets/MyGameSocket.cpp +++ b/src/sockets/MyGameSocket.cpp @@ -36,7 +36,6 @@ MyGameSocket::process_request(std::string request, bool& quit) { break; case QUIT_GAME: - SteamAPI_Shutdown(); quit = true; break; diff --git a/src/sockets/MyServerSocket.cpp b/src/sockets/MyServerSocket.cpp index 4b40cf7..237119a 100644 --- a/src/sockets/MyServerSocket.cpp +++ b/src/sockets/MyServerSocket.cpp @@ -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; } diff --git a/src/sockets/MyServerSocket.h b/src/sockets/MyServerSocket.h index 9d8e024..2807653 100644 --- a/src/sockets/MyServerSocket.h +++ b/src/sockets/MyServerSocket.h @@ -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(); }; \ No newline at end of file diff --git a/src/sockets/MySocket.cpp b/src/sockets/MySocket.cpp index 206d5bc..918f5e9 100644 --- a/src/sockets/MySocket.cpp +++ b/src/sockets/MySocket.cpp @@ -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