MCPcopy Create free account
hub / github.com/clementgallet/libTAS / initSocketProgram

Function initSocketProgram

src/shared/sockethelpers.cpp:65–106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

63
64#ifndef LIBTAS_LIBRARY
65bool initSocketProgram(pid_t fork_pid)
66{
67#ifdef __unix__
68 const struct sockaddr_un addr = { AF_UNIX, SOCKET_FILENAME };
69#elif defined(__APPLE__) && defined(__MACH__)
70 const struct sockaddr_un addr = { sizeof(struct sockaddr_un), AF_UNIX, SOCKET_FILENAME };
71#endif
72 socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
73
74 struct timespec tim = {0, 500L*1000L*1000L};
75
76 const int MAX_RETRIES = 10;
77 int retry = 0;
78
79 nanosleep(&tim, NULL);
80 while (connect(socket_fd, reinterpret_cast<const struct sockaddr*>(&addr),
81 sizeof(struct sockaddr_un))) {
82 std::cout << "Attempt " << retry + 1 << ": Couldn't connect to socket." << std::endl;
83 retry++;
84 if (retry < MAX_RETRIES) {
85 nanosleep(&tim, NULL);
86 } else {
87 return false;
88 }
89
90 /* Check if game is still running */
91 int ret = waitpid(fork_pid, nullptr, WNOHANG);
92 if (ret == fork_pid) {
93 std::cout << "Game couldn't start properly." << std::endl;
94 return false;
95 }
96
97 tim.tv_nsec *= 1.5;
98 if (tim.tv_nsec >= 1000000000) {
99 tim.tv_sec++;
100 tim.tv_nsec -= 1000000000;
101 }
102 }
103 std::cout << "Attempt " << retry + 1 << ": Connected." << std::endl;
104
105 return true;
106}
107
108#else
109

Callers 1

initProcessMessagesMethod · 0.85

Calls 2

socketFunction · 0.85
nanosleepFunction · 0.85

Tested by

no test coverage detected