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


Messages In This Thread
Copas - TCP status and Control - by Diggerz - 17.05.2024, 22:08
RE: Copas - TCP status and Control - by admin - 20.05.2024, 07:24
RE: Copas - TCP status and Control - by admin - 15.07.2024, 06:30
RE: Copas - TCP status and Control - by admin - 15.07.2024, 10:57

Forum Jump: