Generic TCP Server for external requests - Printable Version +- Logic Machine Forum (https://forum.logicmachine.net) +-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1) +--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8) +--- Thread: Generic TCP Server for external requests (/showthread.php?tid=36) |
Generic TCP Server for external requests - Matt - 14.07.2015 I am not a big fan of the example : http://openrb.com/example-lm2-as-tcp-server-for-external-requests/ It uses the following principle : - the resident script is the TCP server : - manages TCP clients - listen on UDP for internal LM requests - the resident script do receive client request and process them - other LM scripts/events use UDP to communicate to the TCP server and transmit data to be sent For me, there is some limitations : - only 5 messages per second are read by TCP server on the UDP side - data can be lost with UDP As I need to send a lot of data, I did use the internal storage instead of UDP. I am sharing this as a generic TCP server that can be customized for every need. So the idea is the following : - the resident script is the TCP server : - manage TCP clients - read the internal storage for internal LM requests - the resident script do receive client request and process them - other LM scripts/events use internal storage to communicate to the TCP server and transmit data to be sent First, the User Library : Code: -- TCP Server user library Now the resident script with sleep interval 0 : Code: require('copas') Event base script : Code: value = knxdatatype.decode(event.datahex, dt.bool) Matthieu RE: Generic TCP Server for external requests - admin - 14.07.2015 Our example can be tuned for more messages per second if needed, but it will increase CPU load slightly. It's true that UDP packets can be lost over a network. In our example UDP communication happens locally over a virtual loopback interface, so it should be fine. You example has a race conditions between storage.get and storage.set calls which can lead to some messages being lost. Anyway, if you really need to process a large amount of data you should use some ready-made solutions like MQTT. RE: Generic TCP Server for external requests - Matt - 14.07.2015 Thanks for the feedback, in my case (used with RTI, my user interface) the risk to send 10 to 20 message at the exact same time. How will the UDP socket react if 10 packets come in one iteration ? So far this method works fine. If we could call a function of a residential script from an event script it would be perfect but I didn't find another way to do it. Basically, it's more a service/daemon than a script. RE: Generic TCP Server for external requests - admin - 14.07.2015 You can find an example of group monitoring script here: http://forum.logicmachine.net/showthread.php?tid=31 Sockets are buffered on OS level so queue must get really big in order for packets to get dropped. RE: Generic TCP Server for external requests - Matt - 15.07.2015 I am testing UDP again to compare. Matthieu I had issues with 10+ UDP requests per second with the following code : Code: while true do With this code, it's working better : Code: while true do It does process the UDP queue at each iteration instead of one by one. What do you think about it ? Matthieu RE: Generic TCP Server for external requests - admin - 16.07.2015 You can try setting UDP socket timeout to 0, so that receive function returns either new message at once or nil + "timeout" notification. This should speed things up for you. |