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

Function initSocketGame

src/shared/sockethelpers.cpp:110–159  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

108#else
109
110bool initSocketGame(void)
111{
112 GlobalNative gn;
113
114 /* Check if socket file already exists. If so, it is probably because
115 * the link is already done in another process of the game.
116 * In this case, we just return immediately.
117 */
118 struct stat st;
119 int result = stat(SOCKET_FILENAME, &st);
120 if (result == 0)
121 return false;
122
123#ifdef __unix__
124 const struct sockaddr_un addr = { AF_UNIX, SOCKET_FILENAME };
125#elif defined(__APPLE__) && defined(__MACH__)
126 const struct sockaddr_un addr = { sizeof(struct sockaddr_un), AF_UNIX, SOCKET_FILENAME };
127#endif
128 const int tmp_fd = socket(AF_UNIX, SOCK_STREAM, 0);
129 if (bind(tmp_fd, reinterpret_cast<const struct sockaddr*>(&addr), sizeof(struct sockaddr_un)))
130 {
131 LOG(LL_ERROR, LCF_SOCKET, "Couldn't bind client socket %s", strerror(errno));
132 exit(-1);
133 }
134
135 if (listen(tmp_fd, 1))
136 {
137 LOG(LL_ERROR, LCF_SOCKET, "Couldn't listen on client socket %s", strerror(errno));
138 exit(-1);
139 }
140
141 if ((socket_fd = accept(tmp_fd, NULL, NULL)) < 0)
142 {
143 LOG(LL_ERROR, LCF_SOCKET, "Couldn't accept client connection %s", strerror(errno));
144 exit(-1);
145 }
146
147 /* Don't generate SIGPIPE */
148#if defined(__APPLE__) && defined(__MACH__)
149 int option_value = 1;
150 if (setsockopt(socket_fd, SOL_SOCKET, SO_NOSIGPIPE, &option_value, sizeof(option_value)))
151 {
152 LOG(LL_ERROR, LCF_SOCKET, "Couldn't set SO_NOSIGPIPE option %s", strerror(errno));
153 exit(-1);
154 }
155#endif
156
157 close(tmp_fd);
158 return true;
159}
160
161#endif
162

Callers 1

initFunction · 0.85

Calls 3

socketFunction · 0.85
closeFunction · 0.85
statClass · 0.70

Tested by

no test coverage detected