lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 16 March 2012 09:28, Egil Hjelmeland <privat@egil-hjelmeland.no> wrote:
> Den 2012-03-16 01:55, skrev Sam Roberts:
>
>> On Thu, Mar 15, 2012 at 3:59 PM, Jay Carlson<nop@nop.com>  wrote:
>>>>
>>>> This is the most basic usage of the BSD socket API, and while not
>>>> directly supported by luasocket, you can do it like this:
>>>>
>>>> https://github.com/sam-github/swirl/blob/master/lua/sockext.lua, see
>>>> line 39.
>>>
>>> This works because elsewhere sockets are set to have a timeout of 0.
>>
>> Of course, if you don't set a timeout, you're going to block.
>>
>>
> That is one of the things I don't like about LuaSockets: AFAIK there is no
> way from Lua to make the socket nonblocking. By nonblocking I mean in C
> "fcntl(socket_fd, F_SETFL, flags|O_NONBLOCK)". When I want a nonblocking
> socket, I want to say in Lua "sock:setnonblocking()", and it stays
> nonblocking.

LuaSocket makes all sockets non-blocking internally by default. It
mimics blocking behaviour with timeouts by using select()/poll(). All
you need to get a non-blocking socket all the way up to Lua is call
conn:settimeout(0) and LuaSocket will not use this waiting code.

> The second thing is that all error codes should be returned so they can be
> dealt with.

LuaSocket uses fixed strings for the common socket error codes:

const char *socket_strerror(int err) {
    if (err <= 0) return io_strerror(err);
    switch (err) {
        case EADDRINUSE: return "address already in use";
        case EISCONN: return "already connected";
        case EACCES: return "permission denied";
        case ECONNREFUSED: return "connection refused";
        case ECONNABORTED: return "closed";
        case ECONNRESET: return "closed";
        case ETIMEDOUT: return "timeout";
        default: return strerror(errno);
    }
}

Regards,
Matthew