Esempio n. 1
0
static void lluv_on_getnameinfo(uv_getnameinfo_t* arg, int status, const char* hostname, const char* service){
  lluv_req_t  *req  = lluv_req_byptr((uv_req_t*)arg);
  lluv_loop_t *loop = lluv_loop_byptr(arg->loop);
  lua_State   *L    = loop->L;

  LLUV_CHECK_LOOP_CB_INVARIANT(L);

  if(!IS_(loop, OPEN)){
    lluv_req_free(L, req);
    return;
  }

  lua_rawgeti(L, LLUV_LUA_REGISTRY, req->cb);
  lluv_req_free(L, req);
  assert(!lua_isnil(L, -1));

  lluv_loop_pushself(L, loop);
  lluv_push_status(L, status);
  if(hostname)lua_pushstring(L, hostname); else lua_pushnil(L);
  if(service) lua_pushstring(L, service);  else lua_pushnil(L);

  LLUV_LOOP_CALL_CB(L, loop, 4);

  LLUV_CHECK_LOOP_CB_INVARIANT(L);
}
Esempio n. 2
0
static void lluv_on_fs(uv_fs_t *arg){
  lluv_fs_request_t *req = arg->data;
  lua_State *L = LLUV_FCALLBACK_L(req);
  lluv_loop_t *loop = lluv_loop_byptr(req->req.loop);
  int argc;

  LLUV_CHECK_LOOP_CB_INVARIANT(L);

  lua_rawgeti(L, LLUV_LUA_REGISTRY, req->cb);

  argc = lluv_push_fs_result_object(L, req);

  if(arg->result < 0){
    lluv_error_create(L, LLUV_ERR_UV, arg->result, arg->path);
    ++argc;
  }
  else{
    lua_pushnil(L);
    argc += 1 + lluv_push_fs_result(L, req);
  }
  uv_fs_req_cleanup(&req->req);
  lluv_fs_request_free(L, req);

  LLUV_LOOP_CALL_CB(L, loop, argc);

  LLUV_CHECK_LOOP_CB_INVARIANT(L);
}
Esempio n. 3
0
static int lluv_push_fs_result_object(lua_State* L, lluv_fs_request_t* lreq) {
  uv_fs_t *req = &lreq->req;
  lluv_loop_t *loop = lluv_loop_byptr(req->loop);

  switch (req->fs_type) {
    case UV_FS_RENAME:
    case UV_FS_UNLINK:
    case UV_FS_RMDIR:
    case UV_FS_MKDIR:
    case UV_FS_MKDTEMP:
    case UV_FS_UTIME:
    case UV_FS_CHMOD:
    case UV_FS_LINK:
    case UV_FS_SYMLINK:
    case UV_FS_CHOWN:
    case UV_FS_READLINK:
    case UV_FS_SCANDIR:
    case UV_FS_STAT:
    case UV_FS_LSTAT:
    case UV_FS_ACCESS:
#if LLUV_UV_VER_GE(1,8,0)
    case UV_FS_REALPATH:
#endif
#if LLUV_UV_VER_GE(1,14,0)
    case UV_FS_COPYFILE:
#endif
      lua_pushvalue(L, LLUV_LOOP_INDEX);
      return 1;

    case UV_FS_OPEN:
      if(req->result < 0) lua_pushnil(L);
      else lluv_file_create(L, loop, (uv_file)req->result, 0);
      return 1;

    case UV_FS_CLOSE:
    case UV_FS_FTRUNCATE:
    case UV_FS_FSYNC:
    case UV_FS_FDATASYNC:
    case UV_FS_FUTIME:
    case UV_FS_FCHMOD:
    case UV_FS_FCHOWN:
    case UV_FS_FSTAT:
    case UV_FS_WRITE:
    case UV_FS_READ:
      lua_rawgeti(L, LLUV_LUA_REGISTRY, lreq->file_ref);
      return 1;

    case UV_FS_SENDFILE:
      lua_rawgeti(L, LLUV_LUA_REGISTRY, lreq->file_ref);
      return 1;

    default:
      fprintf(stderr, "UNKNOWN FS TYPE %d\n", req->fs_type);
      return 0;
  }
}
Esempio n. 4
0
static void lluv_on_getaddrinfo(uv_getaddrinfo_t* arg, int status, struct addrinfo* res){
  lluv_req_t  *req   = lluv_req_byptr((uv_req_t*)arg);
  lluv_loop_t *loop  = lluv_loop_byptr(arg->loop);
  lua_State   *L     = loop->L;
  struct addrinfo* a = res;
  int i = 0;

  LLUV_CHECK_LOOP_CB_INVARIANT(L);

  lua_rawgeti(L, LLUV_LUA_REGISTRY, req->cb);
  lluv_req_free(L, req);
  assert(!lua_isnil(L, -1));

  lluv_loop_pushself(L, loop);

  if(status < 0){
    uv_freeaddrinfo(res);
    lluv_error_create(L, LLUV_ERR_UV, (uv_errno_t)status, NULL);
    LLUV_LOOP_CALL_CB(L, loop, 2);
    LLUV_CHECK_LOOP_CB_INVARIANT(L);
    return;
  }

  lua_pushnil(L);
  lua_newtable(L);
  for(a = res; a; a = a->ai_next){
    char buf[INET6_ADDRSTRLEN + 1];
    int port;
    lua_newtable(L);

    switch (a->ai_family){
      case AF_INET:{
        struct sockaddr_in *sa = (struct sockaddr_in*)a->ai_addr;
        uv_ip4_name(sa, buf, sizeof(buf));
        lua_pushstring(L, buf);
        lua_setfield(L, -2, "address");
        if((port = ntohs(sa->sin_port))){
          lua_pushinteger(L, port);
          lua_setfield(L, -2, "port");
        }
        break;
      }

      case AF_INET6:{
        struct sockaddr_in6 *sa = (struct sockaddr_in6*)a->ai_addr;
        uv_ip6_name(sa, buf, sizeof(buf));
        lua_pushstring(L, buf);
        lua_setfield(L, -2, "address");
        if((port = ntohs(sa->sin6_port))){
          lua_pushinteger(L, port);
          lua_setfield(L, -2, "port");
        }
        break;
      }
    }

    if(a->ai_canonname){
      lua_pushstring(L, a->ai_canonname);
      lua_setfield(L, -2, "canonname");
    }

    lluv_push_ai_family(L, a->ai_family);
    lua_setfield(L, -2, "family");

    lluv_push_ai_stype(L, a->ai_socktype);
    lua_setfield(L, -2, "socktype");

    lluv_push_ai_proto(L, a->ai_protocol);
    lua_setfield(L, -2, "protocol");

    lua_rawseti(L, -2, ++i);
  }

  uv_freeaddrinfo(res);
  LLUV_LOOP_CALL_CB(L, loop, 3);

  LLUV_CHECK_LOOP_CB_INVARIANT(L);
}