Skip to content

Commit

Permalink
perf(mqtt): use mosquitto_strerror for ON_DISCONNECT
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Jan 5, 2024
1 parent f4397d3 commit 006c751
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 39 deletions.
46 changes: 8 additions & 38 deletions mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,14 @@ struct eco_mqtt_ctx {

static int mosq__pstatus(lua_State *L, int mosq_errno)
{
switch (mosq_errno) {
case MOSQ_ERR_SUCCESS:
lua_pushboolean(L, true);
return 1;
break;

case MOSQ_ERR_INVAL:
case MOSQ_ERR_NOMEM:
case MOSQ_ERR_PROTOCOL:
case MOSQ_ERR_NOT_SUPPORTED:
return luaL_error(L, mosquitto_strerror(mosq_errno));
break;

case MOSQ_ERR_NO_CONN:
case MOSQ_ERR_CONN_LOST:
case MOSQ_ERR_PAYLOAD_SIZE:
lua_pushnil(L);
lua_pushinteger(L, mosq_errno);
lua_pushstring(L, mosquitto_strerror(mosq_errno));
return 3;
break;

case MOSQ_ERR_ERRNO:
lua_pushnil(L);
lua_pushinteger(L, errno);
lua_pushstring(L, strerror(errno));
return 3;
break;
if (!mosq_errno) {
lua_pushboolean(L, true);
return 1;
}

return 0;
lua_pushnil(L);
lua_pushstring(L, mosquitto_strerror(mosq_errno));
return 2;
}

static int mosq_version(lua_State *L)
Expand Down Expand Up @@ -439,19 +416,12 @@ static void ctx_on_disconnect(struct mosquitto *mosq, void *obj, int rc)
{
struct eco_mqtt_ctx *ctx = obj;
lua_State *L = ctx->eco->L;
bool success = true;
char *str = "client-initiated disconnect";

if (rc) {
success = false;
str = "unexpected disconnect";
}

lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->on_disconnect);

lua_pushboolean(L, success);
lua_pushboolean(L, !rc);
lua_pushinteger(L, rc);
lua_pushstring(L, str);
lua_pushstring(L, rc ? mosquitto_strerror(rc) : "");

lua_call(L, 3, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion mqtt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ local function try_connect(con, address, port, keepalive)
s:close()

local ok
ok, _, err = __con:connect(address, port, keepalive)
ok, err = __con:connect(address, port, keepalive)
if not ok then
return false, err
end
Expand Down

0 comments on commit 006c751

Please sign in to comment.