Jump to content

Recommended Posts

Posted

Struggling with understanding how LUA receives data via export.lua. I can see the receive method thats being used, but its being called on the outbound socket?

 

So TCP i could understand, as its a connection, you just respond on the established connection. But for UDP that just doesnt work, unless youre communicating between distinct IP addresses. You cant communicate between two processes on the sames IP+Port endpoint as one would block the other. Boltz is using TCP, but Gadroc uses UDP....arrgghh, the lack of documentation here is driving me nuts.

 

Im toying with just instantiating a second connection object and receiving on that but id dearly love to know how Gadrocs doing it and others who have solved it.

 

And yeah there'll be another tutorial once i understand it so anyone else can follow it.

Posted

Just found this post..

 

http://forums.eagle.ru/showthread.php?t=45071&highlight=performclickableaction&page=12

 

Hey guys, just read through all the info in this particular thread. Please send me to another if it's been explained elsewhere.

 

I'm using a pokeys to capture key presses, switches etc. I've got that figured out and running in a C# program. My question is this:

 

I don't understand how I can use a UDP socket to get info back to the export.lua script. I understand that i create a listener socket on whatever port I choose and have my export.lua send stuff to it, but I don't get how my C# program can send stuff back? Forgive me if my socket-fu is not so strong, but I'm pretty sure that UDP is connectionless, and that I can't have a listener for the same socket in two different places on my PC. The example script I have (I think it might have been y2kiah?) has both incoming data (switches from my C# program) and outgoing data (updates from DCS) going on the same UDP socket. How does that work? Thanks for any wisdom!

 

So i know this has been asked before. Just cant find anyone whos answered it!!!

Posted (edited)

Ok assuming this is true, it confirms my suspicions

From http://w3.impa.br/~diego/software/luasocket/old/luasocket-2.0-alpha/udp.html

 

Note: UDP sockets are not bound to any address until the setsockname or the sendto method is called for the first time (in which case it is bound to an ephemeral port and the wild-card address).

 

For those who dont know what an ephemeral port is....

 

When you make a connection to a service over IP you connect to the destination services IP and port. Thats the same for TCP and UDP. However what you dont see is that your local IP network stack allocates a second port which is open on your local pc which the remote service returns its response to...

 

So you might open a webbrowser to http://www.google.com. Default http transport is port 80 over TCP. Your local webbrowser is waiting for a response on a randomly assigned port in this case say port 32456.... You send to port 80, google.com sends to port 32456.

 

DCS's lua engine is transmitting to whatever port you define in the export.lua but its ephemeral port can only be determined by examining the packet received by your UDP bridge.

 

Ill be writing a quick hack to prove that tonight, hopefully results later.

 

 

Heres a page on ephemeral ports....

 

http://en.wikipedia.org/wiki/Ephemeral_port

Edited by Devon Custard
Posted

As i thought,

 

udpport.png

 

The UDP connection is on this port (highlighted in yellow) so returning any input needs to come in on this port.

 

Simples.

 

I feel an entry on the wiki coming up

Posted

I don't have much time at the moment but I will try to write something.

 

UDP works for broadcasting which is why it is used for exporting from DCS. For importing data you really need to use TCP especially if you are only sending in changes like A2DCS does. I used a variety of documents from the web and a couple of books to learn the lua language and implementation with DCS. To do anything with imports and exports you have to dig into lua programming and set up sockets in DCS with exports and imports at the correct time.

 

Basically just use TCP :) I have no experience with UDP and DCS so I don't know how it works in any great detail but Helios and most other add on programs for DCS use UDP. Most likely because they are only reading exports and not sending anything back into DCS.

 

I also want to say that the lua socket information on the main DCS page is entirely wrong and I think it has been misleading to many people. My guess is that it is from an old beta test and has never been updated. Now it is not so simple to establish a connection since you have to code the connection into the export.lua.

Posted
I don't have much time at the moment but I will try to write something.

 

UDP works for broadcasting which is why it is used for exporting from DCS. For importing data you really need to use TCP especially if you are only sending in changes like A2DCS does. I used a variety of documents from the web and a couple of books to learn the lua language and implementation with DCS. To do anything with imports and exports you have to dig into lua programming and set up sockets in DCS with exports and imports at the correct time.

 

Basically just use TCP :) I have no experience with UDP and DCS so I don't know how it works in any great detail but Helios and most other add on programs for DCS use UDP. Most likely because they are only reading exports and not sending anything back into DCS.

 

I also want to say that the lua socket information on the main DCS page is entirely wrong and I think it has been misleading to many people. My guess is that it is from an old beta test and has never been updated. Now it is not so simple to establish a connection since you have to code the connection into the export.lua.

 

Helios does send data back, thats one of its many selling points.

 

Do take your point about TCP, but i damn well want to solve this!!! I can resolve the ephemeral port easily enough, just want to prove i can send data back :)

Posted

Right got it working.

 

I actually want a seperate udp port so i added these lines to LuaExportStart

 

udp=socket.udp()

udp:settimeout(0)

udp:setsockname('*',65001)

 

This sets up a server rather than a client which is precisely what i want.

 

Then i just do udp:receive....

 

 

 

 

I know im getting data into lua now, im dumping it into a file. Just need to do the performClickableAction and theres a fair amount of export.luas to crib from :)

Posted

And done.....

 

Easy once you know which values to use...

 

Like to know who figured out to add 3000 to the button ids. Would have had no idea about that....

 

Time for bed i think :)

Posted

I think your code seems this (with a base af Helios' Gadroc) :

 

With minimum functions

function LuaExportStart()

function LuaExportBeforeNextFrame()

function LuaExportStop()

 

Use : C,12,3001

Where 12 is device cockpit

Where 3001, is the button 1 + 3000

 

[b]function LuaExportStart()[/b]
   package.path  = package.path..";.\\LuaSocket\\?.lua"
   package.cpath = package.cpath..";.\\LuaSocket\\?.dll"

   socket = require("socket")

c = socket.udp()
c:setsockname("*", 65001)
c:setoption('broadcast', true)
   c:settimeout(.001) 

end

[b]function LuaExportBeforeNextFrame()[/b]
   local lInput = c:receive()
   local lCommand, lCommandArgs, lDevice, lArgument, lLastValue

   if lInput then

       lCommand = string.sub(lInput,1,1)

	if (lCommand == "C") then
		lCommandArgs = StrSplit(string.sub(lInput,2),",")
		lDevice = GetDevice(lCommandArgs[1])
		if type(lDevice) == "table" then
			lDevice:performClickableAction(lCommandArgs[2],lCommandArgs[3])
		end
	end
   end
end


[b]function LuaExportStop()[/b]
   c:close()
end

 

It is possible to get an error with using c:setsockname("*", 65001) ; if port 65001 is already used.

With c:setsockname("*", 0 ) , DCS will create a free port, but unkown in lua code.

You can use c:setsockname("*", 0 ) if you can get server udp port of DCS export.

For example, Send a frame to your "input progame" a text to get the server udp port.

 

Send "keep alive" frame every seconds to a owned "input program" at udp port 10410 :

function LuaExportAfterNextFrame()
local tsend = LoGetModelTime()

if ( tsend > (LOtm+1) ) then
	socket.try(c:sendto("KEEPALIVE\0", "127.0.0.1", 10410))
	tsend = LOtm
end
end

 

You can get udp port of the sender (iLUASourcePort) in this C code (after create a listener socket "Socket" on 10410 udp port) :

recvfrom ( Socket , (char *) szBuffer , 576 , 0 , (SOCKADDR *)&LUAClientSocket_in, &ptSocketBase->iSINsize);
iLUASourcePort = ntohs ( LUAClientSocket_in.sin_port );

UniversRadio for DCS : http://universradio.fr

Homepit on eagle.ru forum :http://forums.eagle.ru/showpost.php?p=1547848&postcount=1 (more details : http://www.tacnoworld.fr)

3rd-Wing.net/75th vFighter "Tiger Sharks"/S-01 Tacno (squadron commander)

Posted

Thanks Tacno, that just confirms my earlier discovery. Yup setting the socket might cause an error if that port was already in use, but im fairly used to setting ports up. So much easier to know what port is in use rather than having to code routines to detect a dynamic one.

 

Certainly its a better approach for those who are running firewalls etc on their home machines (and these days you'd be mad not to).

 

To be honest the most important outcome of this thread was to understand how the networking in LUA works. The available documentation from DCS is out of date and sadly lacking in explanation as Boltz already pointed out. The more people who share their experience here the better.

Posted
Thanks Tacno, that just confirms my earlier discovery. Yup setting the socket might cause an error if that port was already in use, but im fairly used to setting ports up. So much easier to know what port is in use rather than having to code routines to detect a dynamic one.

 

Certainly its a better approach for those who are running firewalls etc on their home machines (and these days you'd be mad not to).

 

To be honest the most important outcome of this thread was to understand how the networking in LUA works. The available documentation from DCS is out of date and sadly lacking in explanation as Boltz already pointed out. The more people who share their experience here the better.

 

Hmmmm rereading that it looks a bit ungracious, apols if i gave that impression. I really do appreciate the comments Tacno.

Posted
Hmmmm rereading that it looks a bit ungracious, apols if i gave that impression.

? I didn't read any words as ungracious ;)

 

Just to be sharp about udp/tcp IP ports :

 

To choice a fixed port is a good idea to configure a firewall as you wrote.

However, IP Windows clients seem take ports from the bottom 65535 to 1024 for connections. Often around 65000-40000 are used and released after, or not. The risk is here, because LAU script can't generate a direct alert for user, as a dialog box. This risk is very small of course.

To be sure to take a good server free port, no clients IP will take a UDP/TCP port below 1024.

 

Some are free yet... Also and generally ports near 1024-10000 are selected by unofficial server programs.

 

http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

UniversRadio for DCS : http://universradio.fr

Homepit on eagle.ru forum :http://forums.eagle.ru/showpost.php?p=1547848&postcount=1 (more details : http://www.tacnoworld.fr)

3rd-Wing.net/75th vFighter "Tiger Sharks"/S-01 Tacno (squadron commander)

Posted

I work in IT so I take all this for granted, but it's good to be reminded that others don't necessarily know this stuff.

 

I had to write a set of architectural policies about this very subject for my engineering team as part of our migration to Amazon Web Services.

 

I go to sleep counting CIDR blocks instead of sheep :)

 

Admittedly when I started this thread I was being lazy, I asked how to do it instead of just reading a LUA documentation site. 5 mins with Google and I had it cracked *winks @ Boltz*

Posted
...but it's good to be reminded that others don't necessarily know this stuff...

Of course. My reply is public, for sharing, not only and directly to you. And this can complete TigersharkBAS sticky thread.

UniversRadio for DCS : http://universradio.fr

Homepit on eagle.ru forum :http://forums.eagle.ru/showpost.php?p=1547848&postcount=1 (more details : http://www.tacnoworld.fr)

3rd-Wing.net/75th vFighter "Tiger Sharks"/S-01 Tacno (squadron commander)

  • 1 month later...
Posted

PoKeys for idiots ?

 

As I read through the thread and many other links, I assume your functioning with a Pokeys instead of the others..

 

Im LUA illiterate but not so much to understand the syntax.. My project hack started with a UDP screen dump of inbound connections to sort of understand and setup the Sockets. Im now Completely down to a very minimal time to work on this ( 1 hour a day) which is painfull..

 

So this is how I understand it, The PoKeys57E accepts UDP connections on a Pre-determined PORT, as it is a server in itself. Then we need to output from DCS Via the LUA Shell ( using the Export Script as our application ) however translate the data sent to the pokeys using the syntax that device understands ( wheather it be a Write or read state )

 

So if my understanding is correct, I may need to call a function to translate the requests between DCS and Pokeys by using an argument like IF THEN ELSE ? Again not fluent in luanese.. but I was almost thinking some Com application would have to be used as a Go between ( setting the localhost as a destination.. Well Set me straight here,

Posted

PoKeys for idiots ?

 

As I read through the thread and many other links, I assume your functioning with a Pokeys instead of the others..

 

Im LUA illiterate but not so much to understand the syntax.. My project hack started with a UDP screen dump of inbound connections to sort of understand and setup the Sockets. Im now Completely down to a very minimal time to work on this ( 1 hour a day) which is painfull..

 

So this is how I understand it, The PoKeys57E accepts UDP connections on a Pre-determined PORT, as it is a server in itself. Then we need to output from DCS Via the LUA Shell ( using the Export Script as our application ) however translate the data sent to the pokeys using the syntax that device understands ( wheather it be a Write or read state )

 

So if my understanding is correct, I may need to call a function to translate the requests between DCS and Pokeys by using an argument like IF THEN ELSE ? Again not fluent in luanese.. but I was almost thinking some Com application would have to be used as a Go between ( setting the localhost as a destination.. Well Set me straight here,

Posted
And done.....

 

Easy once you know which values to use...

 

Like to know who figured out to add 3000 to the button ids. Would have had no idea about that....

 

Time for bed i think :)

 

I'll post my C# program when I get home from work. Yes, I had to create two sockets in the export script, one for my POKEYS controller software and one for Helios. I haven't measured how this affects performance yet....

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...