SamRewritten/src/sockets/MyServerSocket.cpp
2020-02-17 19:57:24 +01:00

80 lines
2.4 KiB
C++

#include "MyServerSocket.h"
#include "../common/functions.h"
#include "../types/Actions.h"
#include <iostream>
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;
if(unlink(m_socket_path.c_str()) < 0) {
std::cout << "Unable to unlink previous socket. Exitting." << std::endl;
zenity("Unable to clean up after a previous failure. Please try running SamRewritten as the same user than last time you used it.");
exit(EXIT_FAILURE);
}
}
errno = 0;
if ((m_socket_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0)) == -1)
{
std::cerr << "Could not create the server socket. Exiting. Code: " << errno << std::endl;
zenity("An error occurred starting SamRewritten, please report it to Github with the following code to get directions: " + std::to_string(errno));
exit(EXIT_FAILURE);
}
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, m_socket_path.c_str(), sizeof(addr.sun_path) - 1);
if(bind(m_socket_fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) == -1) {
std::cerr << "Failed to bind server socket: " << m_socket_path << std::endl;
zenity();
exit(EXIT_FAILURE);
}
if (listen(m_socket_fd, 20) < 0)
{
std::cerr << "Unable to listen to the socket. Exiting." << std::endl;
zenity();
exit(EXIT_FAILURE);
}
}
void
MyServerSocket::run_server()
{
int data_socket;
bool quit = false;
for (;;) {
/* Wait for incoming connection. */
data_socket = accept(m_socket_fd, NULL, NULL);
if (data_socket == -1) {
std::cerr << "Server failed to accept. Exiting." << std::endl;
zenity();
exit(EXIT_FAILURE);
}
// Read all the client's request
std::string request = receive_message(data_socket);
send_message(data_socket, process_request(request, quit));
if (quit)
{
#ifdef DEBUG_CERR
std::cout << "Shutting down server safely." << std:: endl;
#endif
// destruction of this object will take care of shutdown
break;
}
/* Close socket. */
close(data_socket);
}
}