コード例 #1
0
ファイル: lluv_dns.c プロジェクト: yinlei/lua-lluv
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);
}
コード例 #2
0
ファイル: lluv_fs.c プロジェクト: moteus/lua-lluv
static int lluv_file_pipe(lua_State *L){
  lluv_file_t *f = lluv_check_file(L, 1, LLUV_FLAG_OPEN);
  int ipc = lua_toboolean(L, 2);

  /*local ok, err = uv.pipe(loop, ipc)*/
  lua_pushvalue(L, LLUV_LUA_REGISTRY);
  lua_pushvalue(L, LLUV_LUA_HANDLES);
  lua_pushcclosure(L, IS_(f, RAISE_ERROR) ? lluv_pipe_create_unsafe : lluv_pipe_create_safe, 2);
  lluv_loop_pushself(L, f->loop);
  lua_pushboolean(L, ipc);
  lua_call(L, 2, 2);

  /*if not ok then return nil, err*/
  if(lua_isnil(L, -2)) return 2;
  lua_pop(L, 1);

  /*local ok, err = pipe:open(fd)*/
  lua_getfield(L, -1, "open");
  assert(lua_isfunction(L, -1));
  lua_pushvalue(L, -2);
  lutil_pushint64(L, f->handle);
  lua_call(L, 2, 2);

  /*if not ok then pipe:close() return nil, err*/
  if(lua_isnil(L, -2)){
    int top = lua_gettop(L);
    lua_getfield(L, -3, "close");
    assert(lua_isfunction(L, -1));
    lua_pushvalue(L, -4);
    lua_pcall(L, 0, 0, 0);
    lua_settop(L, top);
    return 2;
  }
  lua_pop(L, 2);

  UNSET_(f, OPEN);

  return 1;
}
コード例 #3
0
ファイル: lluv_dns.c プロジェクト: yinlei/lua-lluv
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);
}