云大,请教一下,希望您能解答这个问题,我是一个新手,不胜感激
环境:
gcc——toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2
lua:5.1.5
skynet:1.5.0
local skynet = require("skynet")
local js = require("cjson.safe")
local mqtt = require("mqtt")
local blocking = require("blocking")
local ioloop = require("mqtt.ioloop") -- skynet/src/lualib/mqtt/ioloop.lua
local function start_mqtt_client_loop()
local mq_loop = ioloop.create {
sleep = 1,
timeout = 1,
sleep_function = skynet.sleep
}
local s, err = blocking.readfile(mq_cfg_path)
if err then
logger:error("readfile %s fail. %s", mq_cfg_path, err)
return false
end
local m = js.decode(s) -- m为table
if not (m and m.mwd_mqtt) then
logger:error("bad json in %s", mq_cfg_path)
return false
end
local cfg = m.mwd_mqtt
local host_str = cfg.product_id.."."..cfg.host
local conn_params = {
uri = string.format("%s:%s", host_str, mq_cfg.port),
id = mq_cfg.client_id,
username = mq_cfg.username,
password = mq_cfg.password,
keep_alive = mq_cfg.keeplive,
reconnect = 10,
clean = true
}
local topics = {}
for k, v in pairs(topic_key_map) do
--k: "sub_topic", v[1]: "config/ota/remote", v[2]: "pub_topic"
table.insert(topics, {topic = k, module = v[1], pub_topic = v[2]})
end
logger:info("MQTT client connect params: %s", js.encode(conn_params))
local client = mqtt.client(conn_params)
client:on{
connect = function(connack)
assert(not mqtt_client)
if not (connack and connack.rc == 0) then
logger:error("connection to mqtt fail: %s %s", connack and connack:reason_string() or "", connack)
return
end
logger:info("MQTT client connected server success!!!") -- successful connection
state_table.connect_ok = true
local response_count = 0
for _, item in ipairs(topics) do
local module, topic = item.module, item.topic
client:subscribe{
topic = topic,
qos = 0,
callback = function(suback, args)
response_count = response_count + 1
if suback.rc[1] ~= 0 then
client:disconnect(0)
logger:error("subscribe [%s %s] fail %s", module, topic, suback.rc[1])
return
end
logger:info("subscribe from cloud [%s, %s] ok", module, topic)
if response_count >= #topics then
mqtt_client = client
logger:info("All topic subscribes OK, MQTT client is ready!!!")
--Not support, upload software version with 5mins timer.
--[[
local mqtt_dispatcher = skynet.queryservice("mqtt_dispatcher")
skynet.send(mqtt_dispatcher, "lua", "post")
]]
end
end
}
end
end,
message = function(msg)
local err = client:acknowledge(msg)
if not err then
logger:error("received a error msg!")
return
end
table.insert(work_queue, {
msg.topic,
msg.payload
}) -- 如果有消息,则把它插入表
logger:info("Recv from cloud, topic: %s, payload: %s", msg.topic, msg.payload)
skynet.wakeup(work_loop_co) -- 唤醒消息工作线程
skynet.yield()
end,
error = function(err, args, connack)
logger:error("mqtt client err: %s, args: %s, connack: %s", err, args, connack)
if not connack then
logger:error("MQTT client error maybe unable to connect to network")
return
end
if connack.rc == 4 then
state_table.connect_ok = false
client.args.reconnect = client.args.reconnect + 5
end
end,
close = function()
state_table.connect_ok = false
mqtt_client = nil -- set to nil mqtt_client = nil -- set to nil
client = nil
logger:error("mqtt client is close, set to not ready!!!")
end
}
mq_loop:add(client)
client:start_connecting()
while true do
if not client then
break
end
mq_loop:iteration() ---------------------------------------------卡在了这里,最后追踪到函数client_mt:_io_iteration(),是recv()的地方卡住了
skynet.yield()
end
logger:debug("connect failed, ready to reconnect")
init_pri_key()
mq_loop = nil
client = nil
collectgarbage("collect")
return start_mqtt_client_loop()
end
这可能什么原因呢?