Esempio n. 1
0
static int lluv_udp_try_send(lua_State *L) {
    lluv_handle_t *handle = lluv_check_udp(L, 1, LLUV_FLAG_OPEN);
    struct sockaddr_storage sa;
    int err = lluv_check_addr(L, 2, &sa);
    size_t len;
    const char *str = luaL_checklstring(L, 4, &len);
    uv_buf_t buf = uv_buf_init((char*)str, len);

    if(err < 0) {
        lua_settop(L, 3);
        lua_pushliteral(L, ":");
        lua_insert(L, -2);
        lua_concat(L, 3);
        return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, lua_tostring(L, -1));
    }

    lluv_check_none(L, 3);

    err = uv_udp_try_send(LLUV_H(handle, uv_udp_t), &buf, 1, (struct sockaddr*)&sa);
    if(err < 0) {
        return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
    }

    lua_pushinteger(L, err);
    return 1;
}
Esempio n. 2
0
static int lluv_udp_send(lua_State *L){
  lluv_handle_t  *handle = lluv_check_udp(L, 1, LLUV_FLAG_OPEN);
  struct sockaddr_storage sa; int err = lluv_check_addr(L, 2, &sa);

  if(err < 0){
    int top = lua_gettop(L);
    if(top > 4) lua_settop(L, top = 5);

    if(lua_isfunction(L, top)){
      lua_pushvalue(L, 1); /*self*/
      /*host:port*/
      lua_pushvalue(L, 2); lua_pushliteral(L, ":"); lua_pushvalue(L, 3); lua_concat(L, 3);
      lluv_error_create(L, LLUV_ERR_UV, err, lua_tostring(L, -1));
      lua_remove(L, -2);
      lluv_loop_defer_call(L, lluv_loop_by_handle(&handle->handle), 2);
      lua_settop(L, 1);
      return 1;
    }
  
    lua_settop(L, 3);
    lua_pushliteral(L, ":");lua_insert(L, -2);lua_concat(L, 3);
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, lua_tostring(L, -1));
  }

  if(lua_type(L, 4) == LUA_TTABLE){
    return lluv_udp_send_t(L, handle, (struct sockaddr*)&sa);
  }
  else{
    size_t len; const char *str = luaL_checklstring(L, 4, &len);
    uv_buf_t buf = uv_buf_init((char*)str, len);
    return lluv_udp_send_(L, handle, (struct sockaddr*)&sa, &buf, 1);
  }
}
Esempio n. 3
0
static int lluv_tcp_getpeername(lua_State *L){
  lluv_handle_t *handle = lluv_check_tcp(L, 1, LLUV_FLAG_OPEN);
  struct sockaddr_storage sa; int sa_len = sizeof(sa);
  int err = uv_tcp_getpeername(LLUV_H(handle, uv_tcp_t), (struct sockaddr*)&sa, &sa_len);
  lua_settop(L, 1);
  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
  }
  return lluv_push_addr(L, &sa);
}
Esempio n. 4
0
static int lluv_process_kill(lua_State *L){
  lluv_handle_t *handle = lluv_check_process(L, 1, LLUV_FLAG_OPEN);
  int sig = luaL_optint(L, 2, SIGTERM);
  int err = uv_process_kill(LLUV_H(handle, uv_process_t), sig);
  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, (uv_errno_t)err, NULL);
  }
  lua_settop(L, 1);
  return 1;
}
Esempio n. 5
0
static int lluv_tcp_open(lua_State *L){
  lluv_handle_t  *handle = lluv_check_tcp(L, 1, LLUV_FLAG_OPEN);
  uv_os_sock_t sock = lluv_check_os_sock(L, 2);
  int err = uv_tcp_open(LLUV_H(handle, uv_tcp_t), sock);
  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
  }

  lua_settop(L, 1);
  return 1;
}
Esempio n. 6
0
static int lluv_tcp_simultaneous_accepts(lua_State *L){
  lluv_handle_t *handle = lluv_check_tcp(L, 1, LLUV_FLAG_OPEN);
  int enable = lua_toboolean(L, 2);
  int err = uv_tcp_simultaneous_accepts(LLUV_H(handle, uv_tcp_t), enable);

  lua_settop(L, 1);

  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
  }
  return 1;
}
Esempio n. 7
0
static int lluv_udp_set_ttl(lua_State *L){
  lluv_handle_t  *handle = lluv_check_udp(L, 1, LLUV_FLAG_OPEN);
  int ttl = luaL_checkint(L, 2);

  int err = uv_udp_set_ttl(LLUV_H(handle, uv_udp_t), ttl);
  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
  }

  lua_settop(L, 1);
  return 1;
}
Esempio n. 8
0
static int lluv_udp_set_broadcast(lua_State *L){
  lluv_handle_t  *handle = lluv_check_udp(L, 1, LLUV_FLAG_OPEN);
  int enable = lua_toboolean(L, 2);

  int err = uv_udp_set_broadcast(LLUV_H(handle, uv_udp_t), enable);
  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
  }

  lua_settop(L, 1);
  return 1;
}
Esempio n. 9
0
static int lluv_udp_set_multicast_interface(lua_State *L){
  lluv_handle_t  *handle = lluv_check_udp(L, 1, LLUV_FLAG_OPEN);
  const char *interface_addr = luaL_checkstring(L, 2);

  int err = uv_udp_set_multicast_interface(LLUV_H(handle, uv_udp_t), interface_addr);
  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
  }

  lua_settop(L, 1);
  return 1;
}
Esempio n. 10
0
static int lluv_poll_stop(lua_State *L){
  lluv_handle_t *handle = lluv_check_poll(L, 1, LLUV_FLAG_OPEN);
  int err = uv_poll_stop(LLUV_H(handle, uv_poll_t));
  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
  }

  lluv_handle_unlock(L, handle, LLUV_LOCK_START);

  lua_settop(L, 1);
  return 1;
}
Esempio n. 11
0
static int lluv_tcp_keepalive(lua_State *L){
  lluv_handle_t *handle = lluv_check_tcp(L, 1, LLUV_FLAG_OPEN);
  int enable = lua_toboolean(L, 2);
  unsigned int delay = 0; int err;

  if(enable) delay = (unsigned int)luaL_checkint(L, 3);
  err = uv_tcp_keepalive(LLUV_H(handle, uv_tcp_t), enable, delay);

  lua_settop(L, 1);

  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
  }
  return 1;
}
Esempio n. 12
0
static int lluv_tcp_connect(lua_State *L){
  lluv_handle_t  *handle = lluv_check_tcp(L, 1, LLUV_FLAG_OPEN);
  struct sockaddr_storage sa; lluv_req_t *req;
  int err = lluv_check_addr(L, 2, &sa);

  if(err < 0){
    lua_settop(L, 3);
    lua_pushliteral(L, ":");lua_insert(L, -2);lua_concat(L, 3);
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, lua_tostring(L, -1));
  }

  lluv_check_args_with_cb(L, 4);

  req = lluv_req_new(L, UV_CONNECT, handle);

  err = uv_tcp_connect(LLUV_R(req, connect), LLUV_H(handle, uv_tcp_t), (struct sockaddr *)&sa, lluv_on_stream_connect_cb);

  return lluv_return_req(L, handle, req, err);
}
Esempio n. 13
0
static int lluv_udp_stop_recv(lua_State *L){
  lluv_handle_t *handle = lluv_check_udp(L, 1, LLUV_FLAG_OPEN);
  int err;

  lluv_check_none(L, 2);

  err = uv_udp_recv_stop(LLUV_H(handle, uv_udp_t));
  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
  }

  if(LLUV_READ_CB(handle) != LUA_NOREF){
    luaL_unref(L, LLUV_LUA_REGISTRY, LLUV_READ_CB(handle));
    LLUV_READ_CB(handle) = LUA_NOREF;
    lluv_handle_unlock(L, handle, LLUV_LOCK_READ);
  }

  lua_settop(L, 1);
  return 1;
}
Esempio n. 14
0
static int lluv_udp_set_membership(lua_State *L){
  static const lluv_uv_const_t FLAGS[] = {
    { UV_LEAVE_GROUP,   "leave" },
    { UV_JOIN_GROUP,    "join"  },

    { 0, NULL }
  };

  lluv_handle_t  *handle = lluv_check_udp(L, 1, LLUV_FLAG_OPEN);
  const char *multicast_addr = luaL_checkstring(L, 2);
  const char *interface_addr = luaL_checkstring(L, 3);
  uv_membership membership   = (uv_membership)lluv_opt_named_const(L, 4, UV_JOIN_GROUP, FLAGS);

  int err = uv_udp_set_membership(LLUV_H(handle, uv_udp_t), multicast_addr, interface_addr, membership);
  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
  }

  lua_settop(L, 1);
  return 1;
}
Esempio n. 15
0
static int lluv_udp_send_t(lua_State *L, lluv_handle_t  *handle, struct sockaddr *sa){
  int i, n = lua_rawlen(L, 4);
  uv_buf_t *buf;

  assert(lua_type(L, 4) == LUA_TTABLE);

  luaL_argcheck(L, n > 0, 4, "Empty array not supported");

  buf = (uv_buf_t*)lluv_alloca(sizeof(uv_buf_t) * n);
  if(!buf){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, ENOMEM, NULL); 
  }

  for(i = 0; i < n; ++i){
    size_t len; const char *str;
    lua_rawgeti(L, 4, i + 1);
    str = luaL_checklstring(L, -1, &len);
    buf[i] = uv_buf_init((char*)str, len);
    lua_pop(L, 1);
  }

  return lluv_udp_send_(L, handle, sa, buf, n);
}
Esempio n. 16
0
static int lluv_tcp_bind(lua_State *L){
  static const lluv_uv_const_t FLAGS[] = {
    { UV_TCP_IPV6ONLY ,   "ipv6only"   },

    { 0, NULL }
  };

  lluv_handle_t  *handle = lluv_check_tcp(L, 1, LLUV_FLAG_OPEN);
  struct sockaddr_storage sa; int err = lluv_check_addr(L, 2, &sa);
  unsigned int flags = 0;
  int top = lua_gettop(L);
  if(top > 5)lua_settop(L, top = 5);

  if((top > 4) || (!lua_isfunction(L, 4))){
    flags = lluv_opt_flags_ui(L, 4, flags, FLAGS);
  }

  if(err < 0){
    lua_checkstack(L, 3);

    lua_pushvalue(L, 2); lua_pushliteral(L, ":"); lua_pushvalue(L, 3); lua_concat(L, 3);

    if(!lua_isfunction(L, top)){
      return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, lua_tostring(L, -1));
    }

    lluv_error_create(L, LLUV_ERR_UV, err, lua_tostring(L, -1));
    lua_remove(L, -2);
    lua_pushvalue(L, 1);
    lua_insert(L, -2);
    lluv_loop_defer_call(L, lluv_loop_by_handle(&handle->handle), 2);
    lua_settop(L, 1);
    return 1;
  }

  err = uv_tcp_bind(LLUV_H(handle, uv_tcp_t), (struct sockaddr *)&sa, flags);
  if(err < 0){
    lua_checkstack(L, 3);

    lua_pushvalue(L, 2); lua_pushliteral(L, ":"); lua_pushvalue(L, 3); lua_concat(L, 3);

    if(!lua_isfunction(L, top)){
      return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, lua_tostring(L, -1));
    }

    lluv_error_create(L, LLUV_ERR_UV, err, lua_tostring(L, -1));
    lua_remove(L, -2);
    lua_pushvalue(L, 1);
    lua_insert(L, -2);
    lluv_loop_defer_call(L, lluv_loop_by_handle(&handle->handle), 2);
    lua_settop(L, 1);
    return 1;
  }

  if(lua_isfunction(L, top)){
    lua_pushvalue(L, 1);
    lua_pushnil(L);
    lluv_loop_defer_call(L,
      lluv_loop_by_handle(&handle->handle),
      lluv_push_addr(L, &sa) + 2
    );
  }

  lua_settop(L, 1);
  return 1;
}