| 338 | // create_and_connect(host, port, timeout) |
| 339 | extern double __gettime(); |
| 340 | static 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) |
| 370 | static int l_socket_connect(lua_State *L) { |
nothing calls this directly
no test coverage detected