This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Copas - TCP status and Control
#1
Hi im using an example of Copas and wanted to add some additonal features:

1. Status of connection to object
    - i guess i can just write to an object at line " alert('[tcp-client] connection ok') " and line " alert('[tcp-client] connection failed (conn): ' .. err) " 
2. Object to control TCP connect, reconnect and disconnect
    - Aside from just disabling and enabling the script is there a method to call functions for connect, reconnect and disconnect from an object while the script is running? 
3. Detection of loss of TCP connection and attempt to reconnect
    - i tried adding a kep alive function that sends a string 'ping' and try to check if it fails to send, however this does not work. I disconnect the ethernet to the end device and the 'ping'         message keeps sending and copas/ tcp does not fail or recognise the end device is no longer connected?

Code:
if not ready then
  socket = require("socket")
  copas = require("copas")
  alert('not Ready')
  ready = true

  function parse(data)
    alert('parsing: ' .. data)
  end

  function fromKNX(command)
    alert('from KNX.. ' .. command)
  end

  local server = socket.udp()
  server:setsockname("127.0.0.1", 23456)

  function handler(skts)
    skts = copas.wrap(skts)
    alert("UDP connection handler")
    while true do
      local s, err
      alert("UDP receiving...")
      s, err = skts:receive(2048)
      if not s then
        alert("UDP Receive error: " .. err)
        break
      end
      alert("Received data, bytes: " .. s)
      fromKNX(s)
    end
  end

  copas.addserver(server, handler, 1)
end

-- Define the init function
function init()
  alert('Initialization function called')
  -- Add some initialization code here
end

function connect_tcp()
  alert('Attempting to connect to TCP server')
  local skt, err = socket.connect('x.x.x.x’, xxxx)
  if skt then
    skt:settimeout(0)
    alert('[tcp-client] connection ok')
    copas.addthread(function()
      while true do
        local resp, err = copas.receive(skt, '*l')
        if not resp then
          alert("Receive error: " .. err)
          copas.removeserver(skt)
          skt = nil
          break
        end
        parse(resp)
      end
      -- Attempt to reconnect after disconnect
      connect_tcp()
    end)

    -- Keep-alive mechanism: send ping every 5 seconds
    copas.addthread(function()
      while skt do
        local success, err = skt:send("ping\n")
        if not success then
          alert("Ping send error: " .. err)
          copas.removeserver(skt)
          skt:close()
          skt = nil
          break
        end
        copas.sleep(5)
      end
      -- Attempt to reconnect after ping failure
      connect_tcp()
    end)

    warningfailed = true
    init()
  else
    if warningfailed then
      alert('[tcp-client] connection failed (conn): ' .. err)
    end
    warningfailed = false
    -- Retry connection after a delay
    copas.addthread(function()
      copas.sleep(5)  -- Wait for 5 seconds before retrying
      connect_tcp()
    end)
  end
end

if not skt then
  alert('not socket')
  connect_tcp()
end

copas.loop()
Reply
#2
Please describe your task in more detail. Most likely this can be done without copas, just with normal sockets and correctly set timeout values.
Reply
#3
(20.05.2024, 07:24)admin Wrote: Please describe your task in more detail. Most likely this can be done without copas, just with normal sockets and correctly set timeout values.

Im using copas as the device being connecting to only accepts 1 TCP conenction on 1 port for send and recieve. local UDP server on the LM sends event driven messages via copas TCP conenction which also recieves and updates objects.

I'd like to periodically check the connection is still alive and update a status object. 

The device uses the same port for commisisoning, so i want to add some buttons to a page to disconnect, connect or do a reconnect ( refresh the connection ). So when commisisoning is required we can disconnect, and then reconnect when commisioning is complete.
Reply


Forum Jump: