ipc: switch to std::string, more readable code + fixes memleak

This commit is contained in:
Gauvain 'GovanifY' Roussel-Tarbouriech 2021-03-29 19:58:09 +02:00 committed by refractionpcsx2
parent 80cd65d347
commit 1d93cd25e5
2 changed files with 9 additions and 14 deletions

View File

@ -85,21 +85,15 @@ SocketIPC::SocketIPC(SysCoreThread* vm, unsigned int slot)
// fallback in case macOS or other OSes don't implement the XDG base // fallback in case macOS or other OSes don't implement the XDG base
// spec // spec
if (runtime_dir == nullptr) if (runtime_dir == nullptr)
m_socket_name = (char*)"/tmp/" IPC_EMULATOR_NAME ".sock"; m_socket_name = "/tmp/" IPC_EMULATOR_NAME ".sock";
else else
{ {
m_socket_name = new char[strlen(runtime_dir) + strlen("/" IPC_EMULATOR_NAME ".sock") + 1]; m_socket_name = runtime_dir;
strcpy(m_socket_name, runtime_dir); m_socket_name += "/" IPC_EMULATOR_NAME ".sock";
strcat(m_socket_name, "/" IPC_EMULATOR_NAME ".sock");
} }
if (slot != IPC_DEFAULT_SLOT) if (slot != IPC_DEFAULT_SLOT)
{ m_socket_name += std::to_string(slot);
// maximum size of .%u
char slot_ending[34];
sprintf(slot_ending, ".%u", slot);
m_socket_name = strcat(m_socket_name, slot_ending);
}
struct sockaddr_un server; struct sockaddr_un server;
@ -110,11 +104,11 @@ SocketIPC::SocketIPC(SysCoreThread* vm, unsigned int slot)
return; return;
} }
server.sun_family = AF_UNIX; server.sun_family = AF_UNIX;
strcpy(server.sun_path, m_socket_name); strcpy(server.sun_path, m_socket_name.c_str());
// we unlink the socket so that when releasing this thread the socket gets // we unlink the socket so that when releasing this thread the socket gets
// freed even if we didn't close correctly the loop // freed even if we didn't close correctly the loop
unlink(m_socket_name); unlink(m_socket_name.c_str());
if (bind(m_sock, (struct sockaddr*)&server, sizeof(struct sockaddr_un))) if (bind(m_sock, (struct sockaddr*)&server, sizeof(struct sockaddr_un)))
{ {
Console.WriteLn(Color_Red, "IPC: Error while binding to socket! Shutting down..."); Console.WriteLn(Color_Red, "IPC: Error while binding to socket! Shutting down...");
@ -250,7 +244,7 @@ SocketIPC::~SocketIPC()
#ifdef _WIN32 #ifdef _WIN32
WSACleanup(); WSACleanup();
#else #else
unlink(m_socket_name); unlink(m_socket_name.c_str());
#endif #endif
close_portable(m_sock); close_portable(m_sock);
close_portable(m_msgsock); close_portable(m_msgsock);

View File

@ -27,6 +27,7 @@
#include "Utilities/PersistentThread.h" #include "Utilities/PersistentThread.h"
#include "System/SysThreads.h" #include "System/SysThreads.h"
#include <string>
#ifdef _WIN32 #ifdef _WIN32
#include <WinSock2.h> #include <WinSock2.h>
#include <windows.h> #include <windows.h>
@ -48,7 +49,7 @@ protected:
SOCKET m_msgsock = INVALID_SOCKET; SOCKET m_msgsock = INVALID_SOCKET;
#else #else
// absolute path of the socket. Stored in XDG_RUNTIME_DIR, if unset /tmp // absolute path of the socket. Stored in XDG_RUNTIME_DIR, if unset /tmp
char* m_socket_name; std::string m_socket_name;
int m_sock = 0; int m_sock = 0;
// the message socket used in thread's accept(). // the message socket used in thread's accept().
int m_msgsock = 0; int m_msgsock = 0;