binmodules/luasocket/src/tcp.c
/*-------------------------------------------------------------------------*\
* Waits for and returns a client object attempting connection to the
* server object
\*-------------------------------------------------------------------------*/
\
Waits for and returns a client object attempting connection to the
server object
\
\ Waits for and returns a client object attempting connection to the server object \
static int meth_accept(lua_State *L)
{
    p_tcp server = (p_tcp) auxiliar_checkclass(L, "tcp{server}", 1);
    p_timeout tm = timeout_markstart(&server->tm);
    t_socket sock;
    const char *err = inet_tryaccept(&server->sock, server->family, &sock, tm);
    if (err == NULL) {
        p_tcp clnt = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
        auxiliar_setclass(L, "tcp{client}", -1);
        memset(clnt, 0, sizeof(t_tcp));
        socket_setnonblocking(&sock);
        clnt->sock = sock;
        io_init(&clnt->io, (p_send) socket_send, (p_recv) socket_recv,
                (p_error) socket_ioerror, &clnt->sock);
        timeout_init(&clnt->tm, -1, -1);
        buffer_init(&clnt->buf, &clnt->io, &clnt->tm);
        clnt->family = server->family;
        return 1;
    } else {
        lua_pushnil(L);
        lua_pushstring(L, err);
        return 2;
    }
}
static int meth_accept ( lua_State * L ) { p_tcp server = ( p_tcp ) auxiliar_checkclass ( L , " " , 1 ) ; p_timeout tm = timeout_markstart ( & server -> tm ) ; t_socket sock ; const char * err = inet_tryaccept ( & server -> sock , server -> family , & sock , tm ) ; if ( err == NULL ) { p_tcp clnt = ( p_tcp ) lua_newuserdata ( L , sizeof ( t_tcp ) ) ; auxiliar_setclass ( L , " " , -1 ) ; memset ( clnt , 0 , sizeof ( t_tcp ) ) ; socket_setnonblocking ( & sock ) ; clnt -> sock = sock ; io_init ( & clnt -> io , ( p_send ) socket_send , ( p_recv ) socket_recv , ( p_error ) socket_ioerror , & clnt -> sock ) ; timeout_init ( & clnt -> tm , -1 , -1 ) ; buffer_init ( & clnt -> buf , & clnt -> io , & clnt -> tm ) ; clnt -> family = server -> family ; return 1 ; } else { lua_pushnil ( L ) ; lua_pushstring ( L , err ) ; return 2 ; } }
\
Waits for and returns a client object attempting connection to the
server object
\
\ Waits for and returns a client object attempting connection to the server object \
/* if successful, push client socket */ /* initialize structure fields */