=========================================================================*\ * Library functions \*=========================================================================*/ -------------------------------------------------------------------------*\ * Creates a master tcp object \*-------------------------------------------------------------------------*/
| 385 | * Creates a master tcp object |
| 386 | \*-------------------------------------------------------------------------*/ |
| 387 | static int tcp_create(lua_State *L, int family) { |
| 388 | p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); |
| 389 | memset(tcp, 0, sizeof(t_tcp)); |
| 390 | /* set its type as master object */ |
| 391 | auxiliar_setclass(L, "tcp{master}", -1); |
| 392 | /* if family is AF_UNSPEC, we leave the socket invalid and |
| 393 | * store AF_UNSPEC into family. This will allow it to later be |
| 394 | * replaced with an AF_INET6 or AF_INET socket upon first use. */ |
| 395 | tcp->sock = SOCKET_INVALID; |
| 396 | tcp->family = family; |
| 397 | io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv, |
| 398 | (p_error) socket_ioerror, &tcp->sock); |
| 399 | timeout_init(&tcp->tm, -1, -1); |
| 400 | buffer_init(&tcp->buf, &tcp->io, &tcp->tm); |
| 401 | if (family != AF_UNSPEC) { |
| 402 | const char *err = inet_trycreate(&tcp->sock, family, SOCK_STREAM, 0); |
| 403 | if (err != NULL) { |
| 404 | lua_pushnil(L); |
| 405 | lua_pushstring(L, err); |
| 406 | return 2; |
| 407 | } |
| 408 | socket_setnonblocking(&tcp->sock); |
| 409 | } |
| 410 | return 1; |
| 411 | } |
| 412 | |
| 413 | static int global_create(lua_State *L) { |
| 414 | return tcp_create(L, AF_UNSPEC); |
no test coverage detected