3535#include < iostream>
3636#endif
3737
38- #define SOCKET_FILENAME " /tmp/libTAS.socket"
39-
4038#ifndef MSG_NOSIGNAL
4139#define MSG_NOSIGNAL 0
4240#endif
@@ -48,19 +46,21 @@ static int socket_fd = 0;
4846
4947static std::mutex mutex;
5048
51- int removeSocket (void ) {
52- int ret = unlink (SOCKET_FILENAME );
49+ int removeSocket (const std::string& socket_filename ) {
50+ int ret = unlink (socket_filename. c_str () );
5351 if ((ret == -1 ) && (errno != ENOENT))
5452 return errno;
5553 return 0 ;
5654}
5755
58- bool initSocketProgram (void )
56+ bool initSocketProgram (const std::string& socket_filename )
5957{
6058#ifdef __unix__
61- const struct sockaddr_un addr = { AF_UNIX, SOCKET_FILENAME };
59+ struct sockaddr_un addr = { AF_UNIX };
60+ strncpy (addr.sun_path , socket_filename.c_str (), 108 );
6261#elif defined(__APPLE__) && defined(__MACH__)
63- const struct sockaddr_un addr = { sizeof (struct sockaddr_un ), AF_UNIX, SOCKET_FILENAME };
62+ struct sockaddr_un addr = { sizeof (struct sockaddr_un ), AF_UNIX };
63+ strncpy (addr.sun_path , socket_filename.c_str (), 104 );
6464#endif
6565 socket_fd = socket (AF_UNIX, SOCK_STREAM, 0 );
6666
@@ -72,7 +72,7 @@ bool initSocketProgram(void)
7272 nanosleep (&tim, NULL );
7373 while (connect (socket_fd, reinterpret_cast <const struct sockaddr *>(&addr),
7474 sizeof (struct sockaddr_un ))) {
75- std::cout << " Attempt " << retry + 1 << " : Couldn't connect to socket." << std::endl;
75+ std::cout << " Attempt " << retry + 1 << " : No connection to socket." << std::endl;
7676 retry++;
7777 if (retry < MAX_RETRIES) {
7878 nanosleep (&tim, NULL );
@@ -85,21 +85,30 @@ bool initSocketProgram(void)
8585 return true ;
8686}
8787
88+ std::string getSocketFilenameGame (void )
89+ {
90+ return getenv (" LIBTAS_SOCKET" );
91+ }
92+
8893bool initSocketGame (void )
8994{
95+ std::string socket_filename = getSocketFilenameGame ();
96+
9097 /* Check if socket file already exists. If so, it is probably because
9198 * the link is already done in another process of the game.
9299 * In this case, we just return immediately.
93100 */
94101 struct stat st;
95- int result = stat (SOCKET_FILENAME , &st);
102+ int result = stat (socket_filename. c_str () , &st);
96103 if (result == 0 )
97104 return false ;
98105
99106#ifdef __unix__
100- const struct sockaddr_un addr = { AF_UNIX, SOCKET_FILENAME };
107+ struct sockaddr_un addr = { AF_UNIX };
108+ strncpy (addr.sun_path , socket_filename.c_str (), 108 );
101109#elif defined(__APPLE__) && defined(__MACH__)
102- const struct sockaddr_un addr = { sizeof (struct sockaddr_un ), AF_UNIX, SOCKET_FILENAME };
110+ struct sockaddr_un addr = { sizeof (struct sockaddr_un ), AF_UNIX };
111+ strncpy (addr.sun_path , socket_filename.c_str (), 104 );
103112#endif
104113 const int tmp_fd = socket (AF_UNIX, SOCK_STREAM, 0 );
105114 if (bind (tmp_fd, reinterpret_cast <const struct sockaddr *>(&addr), sizeof (struct sockaddr_un )))
0 commit comments