Non-string recv() with LuaSocket...

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Non-string recv() with LuaSocket...

Matthew Percival
G'Day,

        I have used LuaSocket in several scripts over the past year, and
currently it works perfectly for all my purposes, but there is something
new I would like to do that it currently cannot handle elegantly.

        I am currently using sockets to access a couple of different daemons,
whereby I can send and receive strings between a C application and a Lua
script.  This works quite well, but I would like to write a new
application where I send strings from Lua and receive floats back.  I
could convert the floats to strings, pass them, then convert them back
on the Lua side, but this would be inefficient and inelegant, so I would
rather pass floats as floats.  The trouble is that LuaSocket's
socket_recv() function expects strings, and Lua does not have types to
begin with, so I am at a bit of a loss as to what to do.

        The best idea I could come up with was to give socket.receive() an
optional argument to specify whether you are receiving a string or a
float.  The problems with this are that it is not a very `Lua' solution
(nowhere else do you specify types), and it does not work well into
socket_recv(), which expects char* as the container for the received
data.  From what I can tell, a wrapper function would not work either.

        I was just wondering if I could get any advice on this issue, as to
what the best path to take would be.  I seem to have exhausted all my
ideas for how to handle this problem, and would appreciate any thoughts
others may have.

        -- Matthew

Reply | Threaded
Open this post in threaded view
|

Re: Non-string recv() with LuaSocket...

Diego Nehab-3
Hi,

> This works quite well, but I would like to write a new
> application where I send strings from Lua and receive floats back.

If you control both sides of the communication, i.e., the
daemon and the client, and if you know the floating-point
format used by them is the same, it is trivial to solve this
problem.

Just add a C function that converts a float to its binary
representation in a string, and another that does the
opposite. You can then send these strings back and forth.
Of course, the type of data you are sending back and forth
should be somehow implicit or explicit in your protocol.

Otherwise, I know of no better solution.

Regards,
Diego.

PS: It is unlikely receive() will support anything but
strings, since it wouldn't be portable enough to be widely used.
Reply | Threaded
Open this post in threaded view
|

Re: Non-string recv() with LuaSocket...

Jim Mellander
In reply to this post by Matthew Percival
If the float format is IEEE, you might try reading the data as a string
and converting via one of the unpacking libraries at
http://lua-users.org/wiki/LibrariesAndBindings - looks like there's a
couple of choices: lpack & struct

Matthew Percival wrote:

> G'Day,
>
> I have used LuaSocket in several scripts over the past year, and
> currently it works perfectly for all my purposes, but there is something
> new I would like to do that it currently cannot handle elegantly.
>
> I am currently using sockets to access a couple of different daemons,
> whereby I can send and receive strings between a C application and a Lua
> script.  This works quite well, but I would like to write a new
> application where I send strings from Lua and receive floats back.  I
> could convert the floats to strings, pass them, then convert them back
> on the Lua side, but this would be inefficient and inelegant, so I would
> rather pass floats as floats.  The trouble is that LuaSocket's
> socket_recv() function expects strings, and Lua does not have types to
> begin with, so I am at a bit of a loss as to what to do.
>
> The best idea I could come up with was to give socket.receive() an
> optional argument to specify whether you are receiving a string or a
> float.  The problems with this are that it is not a very `Lua' solution
> (nowhere else do you specify types), and it does not work well into
> socket_recv(), which expects char* as the container for the received
> data.  From what I can tell, a wrapper function would not work either.
>
> I was just wondering if I could get any advice on this issue, as to
> what the best path to take would be.  I seem to have exhausted all my
> ideas for how to handle this problem, and would appreciate any thoughts
> others may have.
>
> -- Matthew
>
>

--
Jim Mellander
Incident Response Manager
Computer Protection Program
Lawrence Berkeley National Laboratory
(510) 486-7204

Your fortune for today is:

Real programmers don't comment their code.  It was hard to write, it
should be hard to understand.