MCPcopy Create free account
hub / github.com/assaultcube/AC / connectwithtimeout

Function connectwithtimeout

source/src/serverbrowser.cpp:237–280  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

235#define CONNLIMIT 20000
236
237int connectwithtimeout(ENetSocket sock, const char *hostname, ENetAddress &address)
238{
239 if(isdedicated)
240 {
241 int result = enet_socket_connect(sock, &address);
242 if(result<0) enet_socket_destroy(sock);
243 return result;
244 }
245
246 defformatstring(text)("connecting to %s:%d... (esc to abort)", hostname, address.port);
247 show_out_of_renderloop_progress(0, text);
248
249 if(!connmutex) connmutex = SDL_CreateMutex();
250 if(!conncond) conncond = SDL_CreateCond();
251 SDL_LockMutex(connmutex);
252 connectdata cd = { sock, address, -1 };
253 connthread = SDL_CreateThread(connectthread, "ConnectThread", &cd);
254
255 int starttime = SDL_GetTicks(), timeout = 0;
256 for(;;)
257 {
258 if(!SDL_CondWaitTimeout(conncond, connmutex, 250))
259 {
260 if(cd.result<0) enet_socket_destroy(sock);
261 break;
262 }
263 timeout = SDL_GetTicks() - starttime;
264 show_out_of_renderloop_progress(min(float(timeout)/CONNLIMIT, 1.0f), text);
265 SDL_Event event;
266 while(SDL_PollEvent(&event))
267 {
268 if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) timeout = CONNLIMIT + 1;
269 }
270 if(timeout > CONNLIMIT) break;
271 }
272
273 /* thread will actually timeout eventually if its still trying to connect
274 * so just leave it (and let it destroy socket) instead of causing problems on some platforms by killing it
275 */
276 connthread = NULL;
277 SDL_UnlockMutex(connmutex);
278
279 return cd.result;
280}
281
282vector<serverinfo *> servers;
283ENetSocket pingsock = ENET_SOCKET_NULL;

Callers

nothing calls this directly

Calls 4

minFunction · 0.85
enet_socket_connectFunction · 0.50
enet_socket_destroyFunction · 0.50

Tested by

no test coverage detected