예제 #1
0
파일: lluv_udp.c 프로젝트: kidaa/lua-lluv
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);
  }
}
예제 #2
0
파일: lluv_udp.c 프로젝트: tbastos/lua-lluv
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;
}
예제 #3
0
파일: lluv_udp.c 프로젝트: kidaa/lua-lluv
static int lluv_udp_open(lua_State *L){
  lluv_handle_t  *handle = lluv_check_udp(L, 1, LLUV_FLAG_OPEN);
  uv_os_sock_t sock = (uv_os_sock_t)lutil_checkint64(L, 2);
  int err = uv_udp_open(LLUV_H(handle, uv_udp_t), sock);
  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
  }

  lua_settop(L, 1);
  return 1;
}
예제 #4
0
파일: lluv_udp.c 프로젝트: kidaa/lua-lluv
static int lluv_udp_getsockname(lua_State *L){
  lluv_handle_t *handle = lluv_check_udp(L, 1, LLUV_FLAG_OPEN);
  struct sockaddr_storage sa; int sa_len = sizeof(sa);
  int err = uv_udp_getsockname(LLUV_H(handle, uv_udp_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);
}
예제 #5
0
파일: lluv_udp.c 프로젝트: kidaa/lua-lluv
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;
}
예제 #6
0
파일: lluv_udp.c 프로젝트: kidaa/lua-lluv
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;
}
예제 #7
0
파일: lluv_udp.c 프로젝트: kidaa/lua-lluv
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;
}
예제 #8
0
파일: lluv_udp.c 프로젝트: kidaa/lua-lluv
static int lluv_udp_start_recv(lua_State *L){
  lluv_handle_t *handle = lluv_check_udp(L, 1, LLUV_FLAG_OPEN);
  int err;

  lluv_check_args_with_cb(L, 2);
  LLUV_READ_CB(handle) = luaL_ref(L, LLUV_LUA_REGISTRY);

  err = uv_udp_recv_start(LLUV_H(handle, uv_udp_t), lluv_alloc_buffer_cb, lluv_on_udp_recv_cb);

  if(err >= 0) lluv_handle_lock(L, handle, LLUV_LOCK_READ);

  return lluv_return(L, handle, LLUV_READ_CB(handle), err);
}
예제 #9
0
파일: lluv_udp.c 프로젝트: kidaa/lua-lluv
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;
}
예제 #10
0
파일: lluv_udp.c 프로젝트: kidaa/lua-lluv
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;
}
예제 #11
0
파일: lluv_udp.c 프로젝트: kidaa/lua-lluv
static int lluv_udp_bind(lua_State *L){
  static const lluv_uv_const_t FLAGS[] = {
    { UV_UDP_IPV6ONLY ,   "ipv6only"   },
    { UV_UDP_REUSEADDR,   "reuseaddr"  },

    { 0, NULL }
  };

  lluv_handle_t  *handle = lluv_check_udp(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_udp_bind(LLUV_H(handle, uv_udp_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;
}