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?
	
	
	
	
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() 
 

 

 tart_server()
tart_server()