static void luv_on_udp_send(uv_udp_send_t* req, int status) { luv_udp_ref_t *ref; /* load the lua state and the userdata */ lua_State *L = luv_handle_get_lua(req->handle->data); lua_pop(L, 1); /* We don't need the userdata */ /* load the callback */ ref = req->data; lua_rawgeti(L, LUA_REGISTRYINDEX, ref->ref); luaL_unref(L, LUA_REGISTRYINDEX, ref->ref); free(ref); if (lua_isfunction(L, -1)) { if (status != 0) { luv_push_async_error(L, uv_last_error(luv_get_loop(L)), "on_udp_send", NULL); luv_acall(L, 1, 0, "on_udp_send"); } else { luv_acall(L, 0, 0, "on_udp_send"); } } else { lua_pop(L, 1); } luv_handle_unref(L, req->handle->data); free(req); }
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 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_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); }