Example #1
0
static int
_trace_yield(lua_State *L) {
    struct trace_pool *p = lua_touserdata(L,1);
    struct trace_info * t = trace_yield(p);
    if (t) {
        lua_pushlightuserdata(L,t);
        return 1;
    }
    return 0;
}
static int
_cb(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
    struct stat *S = ud;
    lua_State *L = S->L;
    struct timespec ti;
    _stat_begin(S, &ti);
    int trace = 1;
    int r;
    int top = lua_gettop(L);
    if (top == 1) {
        lua_rawgetp(L, LUA_REGISTRYINDEX, _cb);
    }
    else {
        assert(top == 2);
        lua_pushvalue(L,2);
    }

    lua_pushinteger(L, type);
    lua_pushlightuserdata(L, (void *)msg);
    lua_pushinteger(L,sz);
    lua_pushinteger(L, session);
    lua_pushnumber(L, source);

    if (type == PTYPE_RESPONSE && session > 0) {
        double t = tqueue_pop(S->tq, session);
        if (t != 0) {
            t = current_time_tick(S) - t;
            lua_pushnumber(L, t);
            r = lua_pcall(L, 6, 0 , trace);
        } else {
            r = lua_pcall(L, 5, 0 , trace);
        }
    } else {
        r = lua_pcall(L, 5, 0 , trace);
    }

    _stat_end(S, &ti);

    struct trace_info *tti = trace_yield(S->trace);
    if (tti) {
        skynet_error(context, "Untraced time %f",  trace_delete(S->trace, tti));
    }

    if (r == LUA_OK) {
        if (S->lua->reload) {
            skynet_callback(context, NULL, 0);
            struct snlua * lua = S->lua;
            assert(lua->L == L);
            const char * cmd = lua->reload;
            lua->reload = NULL;
            lua->L = luaL_newstate();
            int err = lua->init(lua, context, cmd);
            if (err) {
                skynet_callback(context, S, _cb);
                skynet_error(context, "lua reload failed : %s", cmd);
                lua_close(lua->L);
                lua->L = L;
            } else {
                skynet_error(context, "lua reload %s", cmd);
                lua_close(L);
            }
        }
        return 0;
    }
    const char * self = skynet_command(context, "REG", NULL);
    switch (r) {
    case LUA_ERRRUN:
        skynet_error(context, "lua call [%x to %s : %d msgsz = %d] error : " KRED "%s" KNRM, source , self, session, sz, lua_tostring(L,-1));
        break;
    case LUA_ERRMEM:
        skynet_error(context, "lua memory error : [%x to %s : %d]", source , self, session);
        break;
    case LUA_ERRERR:
        skynet_error(context, "lua error in error : [%x to %s : %d]", source , self, session);
        break;
    case LUA_ERRGCMM:
        skynet_error(context, "lua gc error : [%x to %s : %d]", source , self, session);
        break;
    };

    lua_pop(L,1);

    return 0;
}