lua-users home
lua-l archive

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


I had trouble trying to figure out luasocket from its documentation without resorting to digging through the source.  I understand that certain choices were made to unify the differences between how Linux and Windows error (as they use different constant names and values), but I found it frustrating figuring out what things like :receive() might return.  Maybe I'm just bad at navigating the docs, but I could not find a list of possible return values (the error names to compare against). :(

My reasoning for trying to match C structs is that there are a plethora of C networking tutorials and man pages detailing what to expect so it makes it very easy for others to figure out what values are in the table under what key names -- even if the table seems a bit wonky with how to how one would represent that data in Lua.  There are less Lua networking tutorials...

Your getaddrinfo() does look much nicer :]  I just couldn't decide on how to make it friendly -- how I would return a table showing which sock types and protocols the remote host supports.  Maybe people only really use getaddrinfo() to resolve domain names as they already know they want to establish a TCP connection.  My other reasoning was that because my bindings are pretty direct, it still leaves the option of layering over it a friendlier interface while not keeping things like getaddrinfo() from the user.

With luasocket, I found a lot of the time I needed to break user-friendly abstractions.  Especially if I wanted to use something other than select() or a library expecting a socket:getfd().  I also found the number of socket options you can toggle/set a bit limited, though I understand the portability behind which options made it in.


On Mon, Nov 18, 2013 at 11:54 PM, Sean Conner <sean@conman.org> wrote:
It was thus said that the Great Sir Pogsalot once stated:
> I've felt a bit ridiculous trying to mirror some of the C structs when
> "exporting" them to Lua as tables, but I wanted people to be able to follow
> along with a C socket tutorial and expect the same member fields.

  I've explicitely rejected trying to slavisly follow a C API when writing a
wrapper for Lua, because if I wanted to write in C, I'd write in C.  I want
to write Lua in Lua, if that makes any sense.  And thus, I try to adapt a
Lua interface to be more Luaish, or, at least what makes it easier for *me*
to write Lua code.

  As an example, I could have just wrapped setrlimit() [1]

        process.setrlimit(process.RLIMIT_CPU,{ rlim_cur = 4 , rlim_max = 4})

but like I said, if I wanted C, I have C.  Instead, I spent the time writing
the interface so I could do:

        process.limits.soft.cpu = 4

  Yes, behind the scenes, it calls setrlimit(), but I don't have to both
with that in Lua.  In fact, I can also do:

        print(process.limits.soft.cpu) -- call getrlimit()

> Mostly I've been kept up nights about the tables I return from my
> getaddrinfo(). I really have to keep myself from molding it into a
> friendlier table, but why I am unhappy with luasocket is because I have so
> much trouble figuring out what to expect from its functions.  The
> documentation only goes so far...

  Why are you afraid of molding the results of getaddrinfo() into something
friendlier (or easier)?  Is it because you want people using a C tutorial
for networking to follow along in Lua?  To me, that seems rather odd.  I
just went ahead and made the results in Lua friendlier for my own usage.  My
own interface [2] is called thusly:

        results = net.address2('www.google.com','any','tcp','www')
        for i = 1 , #results do print(results[i]) end

        > ip:74.125.229.208:80
        > ip:74.125.229.209:80
        > ip:74.125.229.210:80
        > ip:74.125.229.211:80
        > ip:74.125.229.212:80
        > ip6:[2607:f8b0:4008:801::]:80

  Also, the results have already been converted into struct sockaddr so they
can be used directly by socket:connect().
  It's perhaps a bit shorter than what I wrote [2] so I don't consider it
that monstrous.  But we might be operating under different definitions of
"monstrous."

  -spc (Embrace Lua; if you want C, you know where to find it ... )

[1]     https://github.com/spc476/lua-conmanorg/blob/master/src/process.c#L985

[2]     https://github.com/spc476/lua-conmanorg/blob/master/src/net.c#L531