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_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);*/ }
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; }
int luv_process_fs_result(lua_State* L, uv_fs_t* req) { luv_fs_ref_t* ref = req->data; int argc = 0; if (req->result == -1) { uv_err_t err; memset(&err, 0, sizeof err); err.code = (uv_err_code)req->errorno; luv_push_async_error(L, err, "after_fs", req->path); } else { lua_pushnil(L); switch (req->fs_type) { case UV_FS_CLOSE: case UV_FS_RENAME: case UV_FS_UNLINK: case UV_FS_RMDIR: case UV_FS_MKDIR: case UV_FS_FTRUNCATE: case UV_FS_FSYNC: case UV_FS_FDATASYNC: case UV_FS_LINK: case UV_FS_SYMLINK: case UV_FS_CHMOD: case UV_FS_FCHMOD: case UV_FS_CHOWN: case UV_FS_FCHOWN: case UV_FS_UTIME: case UV_FS_FUTIME: argc = 0; break; case UV_FS_OPEN: case UV_FS_SENDFILE: case UV_FS_WRITE: argc = 1; lua_pushinteger(L, req->result); break; case UV_FS_STAT: case UV_FS_LSTAT: case UV_FS_FSTAT: argc = 1; luv_push_stats_table(L, (struct stat*)req->ptr); break; case UV_FS_READLINK: argc = 1; lua_pushstring(L, (char*)req->ptr); break; case UV_FS_READ: argc = 2; lua_pushlstring(L, ref->buf, req->result); lua_pushinteger(L, req->result); free(ref->buf); break; case UV_FS_READDIR: { int i; char* namebuf = (char*)req->ptr; int nnames = req->result; argc = 1; lua_createtable(L, nnames, 0); for (i = 0; i < nnames; i++) { lua_pushstring(L, namebuf); lua_rawseti(L, -2, i + 1); namebuf += strlen(namebuf); assert(*namebuf == '\0'); namebuf += 1; } } break; default: assert(0 && "Unhandled eio response"); } } return argc; }
int luv_process_fs_result(lua_State* L, uv_fs_t* req) { luv_fs_ref_t* ref = req->data; int argc = 0; if (req->result < 0) { luv_push_async_error(L, req->result, "after_fs", req->path); } else { lua_pushnil(L); switch (req->fs_type) { case UV_FS_CLOSE: case UV_FS_RENAME: case UV_FS_UNLINK: case UV_FS_RMDIR: case UV_FS_MKDIR: case UV_FS_FTRUNCATE: case UV_FS_FSYNC: case UV_FS_FDATASYNC: case UV_FS_LINK: case UV_FS_SYMLINK: case UV_FS_CHMOD: case UV_FS_FCHMOD: case UV_FS_CHOWN: case UV_FS_FCHOWN: case UV_FS_UTIME: case UV_FS_FUTIME: argc = 0; break; case UV_FS_OPEN: case UV_FS_SENDFILE: case UV_FS_WRITE: argc = 1; lua_pushinteger(L, req->result); break; case UV_FS_STAT: case UV_FS_LSTAT: case UV_FS_FSTAT: argc = 1; luv_push_stats_table(L, &req->statbuf); break; case UV_FS_READLINK: argc = 1; lua_pushstring(L, (char*)req->ptr); break; case UV_FS_READ: argc = 2; lua_pushlstring(L, ref->buf, req->result); lua_pushinteger(L, req->result); free(ref->buf); break; case UV_FS_SCANDIR: { uv_dirent_t ent; int err = uv_fs_scandir_next(req, &ent); argc = 1; lua_createtable(L, 0, 0); int i = 0; while(err != UV_EOF) { lua_createtable(L, 2, 0); lua_pushstring(L, "name"); lua_pushstring(L, ent.name); lua_settable(L, -3); lua_pushstring(L, "type"); switch(ent.type) { case UV_DIRENT_FILE: lua_pushstring(L, "FILE"); break; case UV_DIRENT_DIR: lua_pushstring(L, "DIR"); break; case UV_DIRENT_LINK: lua_pushstring(L, "LINK"); break; case UV_DIRENT_FIFO: lua_pushstring(L, "FIFO"); break; case UV_DIRENT_SOCKET: lua_pushstring(L, "SOCKET"); break; case UV_DIRENT_CHAR: lua_pushstring(L, "CHAR"); break; case UV_DIRENT_BLOCK: lua_pushstring(L, "BLOCK"); break; default: case UV_DIRENT_UNKNOWN: lua_pushstring(L, "UNKOWN"); break; } lua_settable(L, -3); lua_rawseti(L, -2, ++i); err = uv_fs_scandir_next(req, &ent); } } break; default: assert(0 && "Unhandled eio response"); } } return argc; }