MCPcopy Create free account
hub / github.com/Tencent/UnLua / inet_tryconnect

Function inet_tryconnect

Plugins/UnLuaExtensions/LuaSocket/Source/src/inet.cpp:392–433  ·  view source on GitHub ↗

-------------------------------------------------------------------------*\ * Tries to connect to remote address (address, port) \*-------------------------------------------------------------------------*/

Source from the content-addressed store, hash-verified

390* Tries to connect to remote address (address, port)
391\*-------------------------------------------------------------------------*/
392const char *inet_tryconnect(p_socket ps, int *family, const char *address,
393 const char *serv, p_timeout tm, struct addrinfo *connecthints)
394{
395 struct addrinfo *iterator = NULL, *resolved = NULL;
396 const char *err = NULL;
397 int current_family = *family;
398 /* try resolving */
399 err = socket_gaistrerror(getaddrinfo(address, serv,
400 connecthints, &resolved));
401 if (err != NULL) {
402 if (resolved) freeaddrinfo(resolved);
403 return err;
404 }
405 for (iterator = resolved; iterator; iterator = iterator->ai_next) {
406 timeout_markstart(tm);
407 /* create new socket if necessary. if there was no
408 * bind, we need to create one for every new family
409 * that shows up while iterating. if there was a
410 * bind, all families will be the same and we will
411 * not enter this branch. */
412 if (current_family != iterator->ai_family || *ps == SOCKET_INVALID) {
413 socket_destroy(ps);
414 err = inet_trycreate(ps, iterator->ai_family,
415 iterator->ai_socktype, iterator->ai_protocol);
416 if (err) continue;
417 current_family = iterator->ai_family;
418 /* set non-blocking before connect */
419 socket_setnonblocking(ps);
420 }
421 /* try connecting to remote address */
422 err = socket_strerror(socket_connect(ps, (SA *) iterator->ai_addr,
423 (socklen_t) iterator->ai_addrlen, tm));
424 /* if success or timeout is zero, break out of loop */
425 if (err == NULL || timeout_iszero(tm)) {
426 *family = current_family;
427 break;
428 }
429 }
430 freeaddrinfo(resolved);
431 /* here, if err is set, we failed */
432 return err;
433}
434
435/*-------------------------------------------------------------------------*\
436* Tries to accept a socket

Callers 3

meth_setpeernameFunction · 0.85
meth_connectFunction · 0.85
global_connectFunction · 0.85

Calls 7

timeout_markstartFunction · 0.85
inet_trycreateFunction · 0.85
socket_gaistrerrorFunction · 0.70
socket_destroyFunction · 0.70
socket_setnonblockingFunction · 0.70
socket_strerrorFunction · 0.70
socket_connectFunction · 0.70

Tested by

no test coverage detected