1919
2020#include " sockethelpers.h"
2121#include < sys/socket.h>
22- #include < sys/stat.h>
2322#include < cstdlib>
2423#include < unistd.h>
2524#include < sys/un.h>
2625#include < iostream>
2726#include < vector>
2827#include < mutex>
2928#include < errno.h>
29+ #include < fcntl.h>
3030
3131#ifdef SOCKET_LOG
3232#include " lcf.h"
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,80 +46,20 @@ static int socket_fd = 0;
4846
4947static std::mutex mutex;
5048
51- int removeSocket (void ) {
52- int ret = unlink (SOCKET_FILENAME);
53- if ((ret == -1 ) && (errno != ENOENT))
54- return errno;
55- return 0 ;
49+ void setSocket (int sock) {
50+ socket_fd = sock;
5651}
5752
58- bool initSocketProgram (void )
53+ bool initSocketGame (void )
5954{
60- #ifdef __unix__
61- const struct sockaddr_un addr = { AF_UNIX, SOCKET_FILENAME };
62- #elif defined(__APPLE__) && defined(__MACH__)
63- const struct sockaddr_un addr = { sizeof (struct sockaddr_un ), AF_UNIX, SOCKET_FILENAME };
64- #endif
65- socket_fd = socket (AF_UNIX, SOCK_STREAM, 0 );
55+ socket_fd = std::stoi (getenv (" LIBTAS_SOCKET_FD" ));
6656
67- struct timespec tim = {0 , 500L *1000L *1000L };
68-
69- const int MAX_RETRIES = 10 ;
70- int retry = 0 ;
71-
72- nanosleep (&tim, NULL );
73- while (connect (socket_fd, reinterpret_cast <const struct sockaddr *>(&addr),
74- sizeof (struct sockaddr_un ))) {
75- std::cout << " Attempt " << retry + 1 << " : Couldn't connect to socket." << std::endl;
76- retry++;
77- if (retry < MAX_RETRIES) {
78- nanosleep (&tim, NULL );
57+ if (fcntl (socket_fd, F_GETFD) == -1 ) {
58+ if (errno == EBADF) {
59+ perror (" LIBTAS_SOCKET_FD has been closed" );
7960 } else {
80- return false ;
81- }
82- tim.tv_nsec *= 1.5 ;
83- if (tim.tv_nsec >= 1000000000 ) {
84- tim.tv_sec ++;
85- tim.tv_nsec -= 1000000000 ;
61+ perror (" Unable to use game socket" );
8662 }
87- }
88- std::cout << " Attempt " << retry + 1 << " : Connected." << std::endl;
89-
90- return true ;
91- }
92-
93- bool initSocketGame (void )
94- {
95- /* Check if socket file already exists. If so, it is probably because
96- * the link is already done in another process of the game.
97- * In this case, we just return immediately.
98- */
99- struct stat st;
100- int result = stat (SOCKET_FILENAME, &st);
101- if (result == 0 )
102- return false ;
103-
104- #ifdef __unix__
105- const struct sockaddr_un addr = { AF_UNIX, SOCKET_FILENAME };
106- #elif defined(__APPLE__) && defined(__MACH__)
107- const struct sockaddr_un addr = { sizeof (struct sockaddr_un ), AF_UNIX, SOCKET_FILENAME };
108- #endif
109- const int tmp_fd = socket (AF_UNIX, SOCK_STREAM, 0 );
110- if (bind (tmp_fd, reinterpret_cast <const struct sockaddr *>(&addr), sizeof (struct sockaddr_un )))
111- {
112- std::cerr << " Couldn't bind client socket." << std::endl;
113- exit (-1 );
114- }
115-
116- if (listen (tmp_fd, 1 ))
117- {
118- std::cerr << " Couldn't listen on client socket." << std::endl;
119- exit (-1 );
120- }
121-
122- if ((socket_fd = accept (tmp_fd, NULL , NULL )) < 0 )
123- {
124- std::cerr << " Couldn't accept client connection." << std::endl;
12563 exit (-1 );
12664 }
12765
@@ -135,7 +73,6 @@ bool initSocketGame(void)
13573 }
13674#endif
13775
138- close (tmp_fd);
13976 return true ;
14077}
14178
0 commit comments