lua sockets in spring

lua sockets in spring

Discuss the source code and development of Spring Engine in general from a technical point of view. Patches go here too.

Moderator: Moderators

abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

lua sockets in spring

Post by abma »

LuaSockets allow Lua Widgets use the socket interface. It allows to connect/bind a tcp/udp socket.

For details, see the wikipage:

http://springrts.com/wiki/LuaSocket
Last edited by abma on 18 Feb 2012, 05:47, edited 1 time in total.
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: lua sockets in spring

Post by abma »

...
Last edited by abma on 18 Feb 2012, 05:48, edited 1 time in total.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: lua sockets in spring

Post by knorke »

small question, in the example don't you risk "comparing nil with string blabla" by doing

Code: Select all

local readable, writeable, error = socket.select(set, set, 0)
if error=="timeout" then <- 
return
end
because http://w3.impa.br/~diego/software/luasocket/socket.html says error returned by socket.select can also be nil:
The error message is "timeout" if a timeout condition was met and nil otherwise.
So atm everything is unrestricted? (just asking so I do not wonder if I did something wrong or being blocked)
When the widget/lua ui is reloaded, are all sockets closed or could there be funny effects?
(i havent looked at it yet but might later when coming home)
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: lua sockets in spring

Post by abma »

knorke wrote:small question, in the example don't you risk "comparing nil with string blabla" by doing
yeah, that was wrong, i fixed it, thanks! the widget is still a proof of concept, it will stabelize over time...

knorke wrote: So atm everything is unrestricted? (just asking so I do not wonder if I did something wrong or being blocked)
When the widget/lua ui is reloaded, are all sockets closed or could there be funny effects?
(i havent looked at it yet but might later when coming home)
connections are restricted, see http://springrts.com/wiki/LuaSocket#config_settings . access to the sockets in general aren't restricted, thinks like a http-server should be possible.

if you want to connect to example.org port 80, you've to set LuaSocketEnabled=1 and TCPAllowConnect = example.org:80 in .springrc. i made that more clear in the example.

These values are also read-only, a lua-widget can't change them, and i hopefully already disabled most/all ways to override that, maybe see http://springrts.com/wiki/LuaSocket#Not ... s_.2F_TODO for that.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: lua sockets in spring

Post by knorke »

Image
wow its the captain himself.
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: lua sockets in spring

Post by abma »

nice! :-)
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: lua sockets in spring

Post by knorke »

btw, in spring.exe changing the new settings like LuaSocketEnabled=0/1 does not work. (enter value, click ok, value is not updated)
It works with springsettings.conf though.
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: lua sockets in spring

Post by abma »

knorke wrote:btw, in spring.exe changing the new settings like LuaSocketEnabled=0/1 does not work. (enter value, click ok, value is not updated)
thanks for reporting. that was a feature ;-). now it should work (fixed by https://github.com/spring/spring/commit ... 43164a5a16 )
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: lua sockets in spring

Post by abma »

the luasocket branch is merged into spring develop, i also updated the wiki page:

http://springrts.com/wiki/LuaSocket

if you find bugs, please report!
Voidness
Posts: 3
Joined: 10 Nov 2011, 21:42

Re: lua sockets in spring

Post by Voidness »

Hello I am having trouble getting luasocket.lua example to work.

I am confused about this line right here.
--VFS.Include(LUAUI_DIRNAME .. "Widgets/socket/socket.lua")
Are we expected to uncomment this? When I leave it uncommented I

get this error:
Error in Initialize(): [string "LuaUI/Widgets/luasocket.lua"]:65: attempt to index upvalue 'socket' (a nil value)

I searched around and found socket.lua file and put it into Widgets/socket/
Failed to load: luasocket.lua (error = 2, LuaUI/Widgets/socket/socket.lua, [string "LuaUI/Widgets/socket/socket.lua"]:15: attempt to index global 'socket' (a nil value))

What am I doing wrong?
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: lua sockets in spring

Post by abma »

Are we expected to uncomment this?
no, this line can be removed (i already removed in locally the repo, will push soon).
Voidness wrote:Error in Initialize(): [string "LuaUI/Widgets/luasocket.lua"]:65: attempt to index upvalue 'socket' (a nil value)
are you using a development version of spring? the current stable version doesn't include luasocket support... spring 89 will contain it.
Voidness
Posts: 3
Joined: 10 Nov 2011, 21:42

Re: lua sockets in spring

Post by Voidness »

Fixed... What I needed to do was add this to my system.lua fie:
socket = socket,
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: lua sockets in spring

Post by abma »

aah, thanks. added to the wiki page.
Voidness
Posts: 3
Joined: 10 Nov 2011, 21:42

Re: lua sockets in spring

Post by Voidness »

Hello I am still having problems getting luasocket.lua example to work.
I am trying to connect to springrts.com:80
I get this error in :
[f=0000000] Error: Socket already closed
When I call SocketConnect
Any ideas?
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: lua sockets in spring

Post by abma »

thats strange. are you on windows?

i slightly changed luasocket.lua. now it gets the latest version of spring from springrts.com. I wonder why i did not make this as example before...

https://github.com/spring/spring/commit ... ccff942df8

to get it work, you've to set

Code: Select all

LuaSocketEnabled=1
TCPAllowConnect=springrts.com:80
in .springrc, then it should work... do you have a firewall that blocks spring maybe?

edit: your settings are fine, without it, you would get "Error: Access to 'springrts.com:80' denied". damnit, there is the error:
https://github.com/spring/spring/blob/d ... t.cpp#L222
this works on linux, but not on win32.. will need a bit to fix that.
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: lua sockets in spring

Post by abma »

bilhamil
Posts: 16
Joined: 12 Apr 2011, 10:00

Re: lua sockets in spring

Post by bilhamil »

Voidness (previous poster on this thread) and I developed a lua sockets widget that listens for tuio touch events.

Posted on http://springrts.com/phpbb/viewtopic.php?f=12&t=26900
evan
Posts: 11
Joined: 25 Jun 2012, 19:18

Re: lua sockets in spring

Post by evan »

I got some basic LuaSocket stuff running in a widget perfectly, but when I try it in a gadget I get the error "attempt to index global 'socket' (a nil value)". Is there a way I can get gadgets to support LuaSocket?
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: lua sockets in spring

Post by abma »

evan wrote:I got some basic LuaSocket stuff running in a widget perfectly, but when I try it in a gadget I get the error "attempt to index global 'socket' (a nil value)". Is there a way I can get gadgets to support LuaSocket?
no, that would cause a desync. you've to use something like this:
http://springrts.com/phpbb/viewtopic.ph ... 2&p=406930
and/or
http://springrts.com/wiki/Lua_sync_to_unsync

some more keywords:

SendToUnsynced
SendLuaRulesMsg

i hope thats a point to start..


what do you want to do? maybe it can be done within a widget?

(note: i'm bad in lua, maybe there are better solutions)

also imo thats a good question for here: http://answers.springlobby.info/ (something like "How can i send data from a widget to a gadget? (unsynced -> synced))
evan
Posts: 11
Joined: 25 Jun 2012, 19:18

Re: lua sockets in spring

Post by evan »

We're creating a game where each unit is represented by a real-life node. (For testing we're using an array of routers, but these will be remote control vehicles eventually.) Commands can only be issued to units in the game if their real-life counterparts are connected via an ad-hoc network. So the goal was to intercept a command (AllowCommand), send it out to the network as a UDP packet, and if/when the target node receives the command, send it back to the game and insert it into the command queue.

I've attached a diagram that illustrates our goal. The C&C node is a "home base" kind of unit where all commands are issued from. (You can ignore the difference between wifi and VIA connections.)
Attachments
Spring Remote Network.pdf
(16.2 KiB) Downloaded 59 times
Post Reply

Return to “Engine”