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); } }
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; }