void luv_on_fs_event(uv_fs_event_t* handle, const char* filename, int events, int status) { /* load the lua state and the userdata */ luv_ref_t* ref = handle->data; lua_State *L = ref->L; int before = lua_gettop(L); lua_rawgeti(L, LUA_REGISTRYINDEX, ref->r); if (status == -1) { luv_push_async_error(L, uv_last_error(luv_get_loop(L)), "on_fs_event", NULL); luv_emit_event(L, "error", 1); } else { switch (events) { case UV_RENAME: lua_pushstring(L, "rename"); break; case UV_CHANGE: lua_pushstring(L, "change"); break; default: lua_pushnil(L); break; } if (filename) { lua_pushstring(L, filename); } else { lua_pushnil(L); } luv_emit_event(L, "change", 2); } assert(lua_gettop(L) == before); }
static void luv_on_udp_recv(uv_udp_t* handle, ssize_t nread, uv_buf_t buf, struct sockaddr* addr, unsigned flags) { int port; char ip[INET6_ADDRSTRLEN]; /* load the lua state and the userdata */ lua_State *L = luv_handle_get_lua(handle->data); /* perform some magic */ /* the base buffer is the offset of the slab block + sizeof(MemBlock) */ MemBlock *mb = (MemBlock *)(buf.base - sizeof(MemBlock)); printf("luv_on_read: %p pool=%p\n", mb, mb->pool); if (nread == 0) { return; } if (nread < 0) { uv_close((uv_handle_t *)handle, luv_on_close); luv_push_async_error(L, uv_last_error(luv_get_loop(L)), "on_recv", NULL); luv_emit_event(L, "error", 1); return; } lua_pushlstring(L, buf.base, nread); lua_newtable(L); if (addr->sa_family == AF_INET) { uv_inet_ntop(AF_INET, &(((struct sockaddr_in*)addr)->sin_addr), ip, INET6_ADDRSTRLEN); port = ntohs(((struct sockaddr_in*)addr)->sin_port); } else if (addr->sa_family == AF_INET6){ uv_inet_ntop(AF_INET6, &(((struct sockaddr_in6*)addr)->sin6_addr), ip, INET6_ADDRSTRLEN); port = ntohs(((struct sockaddr_in6*)addr)->sin6_port); } lua_pushstring(L, ip); lua_setfield(L, -2, "address"); lua_pushnumber(L, port); lua_setfield(L, -2, "port"); lua_pushboolean(L, flags == UV_UDP_PARTIAL); lua_setfield(L, -2, "partial"); lua_pushnumber(L, nread); lua_setfield(L, -2, "size"); luv_emit_event(L, "message", 2); lev_slab_decRef( mb ); /*free(buf.base);*/ }
static void disconnectCallback(const redisAsyncContext *redisAsyncContext, int status) { luv_ref_t *ref = redisAsyncContext->data; lua_State *L = ref->L; lua_rawgeti(L, LUA_REGISTRYINDEX, ref->r); if (status != REDIS_OK) { luv_push_async_hiredis_error(L, redisAsyncContext, "disconnectCallback"); luv_emit_event(L, "error", 1); } else { luv_emit_event(L, "disconnect", 0); } }
void onConnect(instanceReference * ref) { lua_State *L = ref->L; lua_rawgeti(L, LUA_REGISTRYINDEX, ref->r); luv_emit_event(L, "connect", 0); }
void luv_after_connect(uv_connect_t* req, int status) { // load the lua state and the userdata luv_connect_ref_t* ref = req->data; lua_State *L = ref->L; int before = lua_gettop(L); lua_rawgeti(L, LUA_REGISTRYINDEX, ref->r); if (status == -1) { luv_push_async_error(L, uv_last_error(uv_default_loop()), "after_connect", NULL); luv_emit_event(L, "error", 1); } else { luv_emit_event(L, "complete", 0); } assert(lua_gettop(L) == before); }
static void luv_on_udp_recv(uv_udp_t* handle, ssize_t nread, uv_buf_t buf, struct sockaddr* addr, unsigned flags) { int port; char ip[INET6_ADDRSTRLEN]; /* load the lua state and the userdata */ lua_State *L = luv_handle_get_lua(handle->data); if (nread == 0) { return; } if (nread < 0) { uv_close((uv_handle_t *)handle, luv_on_close); luv_push_async_error(L, uv_last_error(luv_get_loop(L)), "on_recv", NULL); luv_emit_event(L, "error", 1); return; } lua_pushlstring(L, buf.base, nread); lua_newtable(L); if (addr->sa_family == AF_INET) { uv_inet_ntop(AF_INET, &(((struct sockaddr_in*)addr)->sin_addr), ip, INET6_ADDRSTRLEN); port = ntohs(((struct sockaddr_in*)addr)->sin_port); } else if (addr->sa_family == AF_INET6){ uv_inet_ntop(AF_INET6, &(((struct sockaddr_in6*)addr)->sin6_addr), ip, INET6_ADDRSTRLEN); port = ntohs(((struct sockaddr_in6*)addr)->sin6_port); } lua_pushstring(L, ip); lua_setfield(L, -2, "address"); lua_pushnumber(L, port); lua_setfield(L, -2, "port"); lua_pushboolean(L, flags == UV_UDP_PARTIAL); lua_setfield(L, -2, "partial"); lua_pushnumber(L, nread); lua_setfield(L, -2, "size"); luv_emit_event(L, "message", 2); free(buf.base); buf.base = NULL; }
void luv_process_on_exit(uv_process_t* handle, int exit_status, int term_signal) { // load the lua state and the userdata luv_ref_t* ref = handle->data; lua_State *L = ref->L; int before = lua_gettop(L); lua_rawgeti(L, LUA_REGISTRYINDEX, ref->r); lua_pushinteger(L, exit_status); lua_pushinteger(L, term_signal); luv_emit_event(L, "exit", 2); assert(lua_gettop(L) == before); }
void luv_on_close(uv_handle_t* handle) { // load the lua state and the userdata luv_ref_t* ref = handle->data; lua_State *L = ref->L; int before = lua_gettop(L); lua_rawgeti(L, LUA_REGISTRYINDEX, ref->r); luv_emit_event(L, "closed", 0); // This handle is no longer valid, clean up memory luaL_unref(L, LUA_REGISTRYINDEX, ref->r); free(ref); assert(lua_gettop(L) == before); }
void luv_on_close(uv_handle_t* handle) { /* printf("on_close\tlhandle=%p handle=%p\n", handle->data, handle);*/ /* load the lua state and the userdata */ luv_handle_t* lhandle = handle->data; lua_State *L = lhandle->L; lua_rawgeti(L, LUA_REGISTRYINDEX, lhandle->ref); luv_emit_event(L, "close", 0); luv_handle_unref(L, handle->data); if (lhandle->ref != LUA_NOREF) { assert(lhandle->refCount); /* fprintf(stderr, "WARNING: closed %s with %d extra refs lhandle=%p handle=%p\n", lhandle->type, lhandle->refCount, handle->data, handle);*/ lhandle->refCount = 1; luv_handle_unref(L, handle->data); } assert(lhandle->ref == LUA_NOREF); /* This handle is no longer valid, clean up memory */ lhandle->handle = 0; free(handle); }
void luv_on_timer(uv_timer_t* handle) { /* load the lua state and put the userdata on the stack */ lua_State* L = luv_handle_get_lua(handle->data); luv_emit_event(L, "timeout", 0); }