14.08.2017, 17:14
Hi Admin,
Thank you, I am trying to get the connection working with ssl, but the connection gets closed. I want to use copas since this gives more flexibility of building a non-blocking solution. I have the option also to run code in separate threads.
What am I doing wrong?
Thanks,
Roger
Thank you, I am trying to get the connection working with ssl, but the connection gets closed. I want to use copas since this gives more flexibility of building a non-blocking solution. I have the option also to run code in separate threads.
Code:
if not ready then
socket = require("socket")
copas = require("copas")
require('ssl')
ready=true
esys = {}
function checksum(str)
local sum = 0
for i = 1, #str do
sum = (sum + string.byte(str,i)) % 256
end
x = 256-sum
return string.format('%02X', 256 - sum)
end
function validchecksum(str)
calccs = checksum(str:sub(1, -3)) -- calculated checksum from message data
recvcs = str:sub(-2, -1) -- received checksum
return calccs == recvcs -- when they the same return true
end
--helper functions to round and increment values
function increment(n)
n = n + 1
return n
end
function parse(data)
alert('elk parsing: %s', data)
if(validchecksum(data)==true)then
local response = string.sub(data,3,-5)
alert('elk message is: %s',response)
end
end
function sendElk(command)
skt:send(command)
sleep(1)
end
function init()
alert('running init')
end
function fromKNX(command)
local telegram = string.split(command,',')
alert('from knx %s',command)
end
local server = socket.udp()
server:setsockname("127.0.0.1",33456)
function handler(skts)
skts = copas.wrap(skts)
alert("UDP connection handler")
while true do
local s, err
alert("UDP receiving..")
s, erro = skts:receive(2048)
if not s then
alert("UDP Receive error: %s", erro)
break
end
alert("UDP Received data, bytes: %s",s)
fromKNX(s)
end
end
copas.addserver(server, handler, 1)
--[[ co = copas.addthread(function()
while true do
copas.sleep(5) -- wait 5 seconds in case of other events
copas.sleep(-1) -- put the thread to sleep until wakeup is called
end
end)]]--
end
if not skt then
-- create tcp client
sslparams = {
mode = "client",
protocol = "sslv3",
key = "crt/ca-key.pem",
certificate = "crt/client.pem",
cafile = "crt/ca-cert.pem",
verify = "peer",
options = {"all", "sslv3"}
}
skt = copas.wrap(socket.tcp(), sslparams)
skt, err = socket.connect('192.168.0.11', 2601)
-- when theres no error connect ok, initialize
if(not err) then
skt:settimeout(0)
-- add receive thread
copas.addthread(function()
while true do
local resp,err = copas.receive(skt)
-- if theres no connection start a new connection
if not resp then
alert("[tcp-client] Receive error: %s", err)
copas.removeserver(skt)
skt = nil
break
end
if(string.find(resp,'^16XK')==false)then -- don't process timer messages
local fd,prtd = pcall(parse,resp)
if(fd==false)then
alert("Error with parsemsg %s ",prtd)
end
end
end
end)
if skt then
alert('[tcp-client] connection ok')
init()
-- error while connecting,
else
if warningfailed then alert('[tcp-client] connection failed (conn): %s', err) end
return
end
else
alert('[tcp-client] error connecting %s',err)
return
end
end
copas.loop()
What am I doing wrong?
Thanks,
Roger