MCPcopy Create free account
hub / github.com/apache/thrift / l_socket_create_and_connect

Function l_socket_create_and_connect

lib/lua/src/luasocket.c:340–367  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

338// create_and_connect(host, port, timeout)
339extern double __gettime();
340static int l_socket_create_and_connect(lua_State *L) {
341 const char* err = NULL;
342 double end;
343 t_socket sock;
344 const char *host = luaL_checkstring(L, 1);
345 unsigned short port = luaL_checknumber(L, 2);
346 int timeout = luaL_checknumber(L, 3);
347
348 // Create and connect loop for timeout milliseconds
349 end = __gettime() + timeout/1000;
350 do {
351 // Create and connect the socket
352 err = tcp_create_and_connect(&sock, host, port, timeout);
353 if (err) {
354 tcp_destroy(&sock);
355 usleep(100000); // sleep for 100ms
356 } else {
357 p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
358 settype(L, -2, SOCKET_CLIENT);
359 socket_setnonblocking(&sock);
360 tcp->sock = sock;
361 tcp->timeout = timeout;
362 return 1; // Return userdata
363 }
364 } while (err && __gettime() < end);
365
366 LUA_CHECK_RETURN(L, err);
367}
368
369// connect(host, port)
370static int l_socket_connect(lua_State *L) {

Callers

nothing calls this directly

Calls 5

__gettimeFunction · 0.85
tcp_create_and_connectFunction · 0.85
tcp_destroyFunction · 0.85
settypeFunction · 0.85
socket_setnonblockingFunction · 0.85

Tested by

no test coverage detected