Пример #1
0
int main(int argc, char **argv)
{
    printf("libuev..\r\n");
    
    xloop = uv_default_loop();
//    uv_loop_init(&xloop);

	uv_watch_init(xloop, &watch1);
	uv_watch_start(&watch1, watch1_func, get_state, 5010);
    
	uv_timer_init(xloop, &timer1);
	uv_timer_start(&timer1, time1_func, 1000, 1000);
	
	uv_timer_init(xloop, &timer2);
	uv_timer_start(&timer2, time2_func, 5011, 0);
	
//	uv_timer_init(xloop, &timer3);
// 	uv_timer_start(&timer3, time3_func, 0, 5000);
	
// 	uv_timer_init(xloop, &timer4);
// 	uv_timer_start(&timer4, time4_func, 5000, 0);

    uv_run(xloop);

}
Пример #2
0
void Initialize()
{
	uv_timer_init(uv_default_loop(), &G_ThreadTimer);

	G_pGateCore = new class CGateCore();

	if (G_pGateCore->bInit() == false) {
		std::fprintf(stderr, "ERROR: Init fail!\n");
		std::exit(1);
		return;	
	}

	G_pListenSock = new class XSocket(300);
	if (!G_pListenSock->bListen(G_pGateCore->m_cGateServerAddr, G_pGateCore->m_iGateServerPort, WM_USER_ACCEPT))
	{
		OnDestroy();
		std::exit(1);
		return;
	}

	// 서버 검사용 타이머 시작 
	uv_timer_init(uv_default_loop(), &G_mmTimer0);
	uv_timer_start(&G_mmTimer0, &_TimerFunc, 0, 3000);
	PutLogList("(!) Gate Server Listening...");
}
Пример #3
0
int main(int argc,char** argv) {
    loop = uv_default_loop();
    if (argc != 2) {
        printf("./tdial localaddr");
        return -1;
    }

    msg_len = htonl(132);
    //uv_ip4_addr("192.168.8.90", 0, &addr);
    if (UV_EINVAL == uv_ip4_addr(argv[1], 0, &addr)) {
        printf("bind error: %s\n",argv[1]);
        return -1;
    }
    addr_str = argv[1];
    uv_ip4_addr("192.168.3.99", 8999, &addr1);
    size_t i;
    for(i=0;i<CONN_NUM;i++) {
        uv_tcp_init(loop, &c[i].ts);
        c[i].ts.data = (void*)i;
        c[i].status = STATUS_NOT_CONNECT;
    }
    uv_timer_t timer_req;
    timer_req.data = (void*)0;

    uv_timer_init(loop, &timer_req);
    uv_timer_start(&timer_req, timer_cb, CONNECT_INTERVAL, CONNECT_INTERVAL);
    
    return uv_run(loop, UV_RUN_DEFAULT);
}
Пример #4
0
void on_connection(uv_stream_t* server, int status)
{
	std::cout << "client come in\n";
	if (-1 == status)
	{
		std::cout << uv_strerror(status)<<"\n";
		return;
	}

	uv_tcp_t* client = new uv_tcp_t();
	uv_tcp_init(uv_default_loop(), client);
	uv_accept(server, (uv_stream_t*)client);
	uv_read_start((uv_stream_t*)client, on_alloc, on_read);

	uv_write_t *req = new uv_write_t();
	uv_buf_t buf;
	char* base = new char[100];
	strncpy(base, "server to client\n", sizeof("server to client\n"));
	buf = uv_buf_init(base, 100);
	uv_write(req, (uv_stream_t*)client, &buf, 1, on_write);


	uv_timer_t* timer = new uv_timer_t();
	timer->data = client;
	uv_timer_init(uv_default_loop(), timer);
	uv_timer_start(timer, on_timer, 1000, 1000);
	/*uv_poll_t poll_handle;
	uv_poll_init(uv_default_loop(), &poll_handle, client->socket);
	uv_poll_start(&poll_handle, UV_WRITABLE, on_disconnect);*/
}
Пример #5
0
int main(int argc, char **argv) {
    int i;
    loop = uv_default_loop();

    if (curl_global_init(CURL_GLOBAL_ALL)) {
        fprintf(stderr, "Could not init cURL\n");
        return 1;
    }

    uv_timer_init(loop, &timeout);

    curl_handle = curl_multi_init();
    curl_multi_setopt(curl_handle, CURLMOPT_SOCKETFUNCTION, handle_socket);
    curl_multi_setopt(curl_handle, CURLMOPT_TIMERFUNCTION, start_timeout);

    char upstream[128];
    memset(upstream,0,128);
    snprintf(upstream,128,"%d:%s:%d",i,"upstream",i+1);
    add_download(upstream,strlen(upstream));


    uv_run(loop, UV_RUN_DEFAULT);
    curl_multi_cleanup(curl_handle);
    return 0;
}
Пример #6
0
int uv_fs_poll_start(uv_fs_poll_t* handle, uv_fs_poll_cb cb, const char* path,
                     unsigned int interval) {
  struct poll_ctx* ctx;
  uv_loop_t* loop;
  size_t len;

  if (uv__is_active(handle)) return 0;

  loop = handle->loop;
  len = strlen(path);
  ctx = calloc(1, sizeof(*ctx) + len);

  if (ctx == NULL) return uv__set_artificial_error(loop, UV_ENOMEM);

  ctx->loop = loop;
  ctx->poll_cb = cb;
  ctx->interval = interval ? interval : 1;
  ctx->start_time = uv_now(loop);
  ctx->parent_handle = handle;
  memcpy(ctx->path, path, len + 1);

  if (uv_timer_init(loop, &ctx->timer_handle)) abort();

  ctx->timer_handle.flags |= UV__HANDLE_INTERNAL;
  uv__handle_unref(&ctx->timer_handle);

  if (uv_fs_stat(loop, &ctx->fs_req, ctx->path, poll_cb)) abort();

  handle->poll_ctx = ctx;
  uv__handle_start(handle);

  return 0;
}
Пример #7
0
static int event_sched(lua_State *L, unsigned timeout, unsigned repeat)
{
    uv_timer_t *timer = malloc(sizeof(*timer));
    if (!timer) {
        format_error(L, "out of memory");
        lua_error(L);
    }

    /* Start timer with the reference */
    uv_loop_t *loop = uv_default_loop();
    uv_timer_init(loop, timer);
    int ret = uv_timer_start(timer, event_callback, timeout, repeat);
    if (ret != 0) {
        free(timer);
        format_error(L, "couldn't start the event");
        lua_error(L);
    }

    /* Save callback and timer in registry */
    lua_newtable(L);
    lua_pushvalue(L, 2);
    lua_rawseti(L, -2, 1);
    lua_pushlightuserdata(L, timer);
    lua_rawseti(L, -2, 2);
    int ref = luaL_ref(L, LUA_REGISTRYINDEX);

    /* Save reference to the timer */
    timer->data = (void *) (intptr_t)ref;
    lua_pushinteger(L, ref);
    return 1;
}
Пример #8
0
void main(int argc, char** argv)
{
    loop = uv_default_loop();
    if(argc > 1)
    {
        bench = atoi(argv[1]);
    }
    if(argc > 2)
    {
        interval = atoi(argv[2]); // Seconds
    }

    // init log client
    melo_log_init(&mlog, loop, "127.0.0.1", 8004, "xlog-test");

    // some small tests
    test_shorten_path();
    test_serialize_log();

    // start a timer
    uv_timer_t timer;

    uv_timer_init(loop, &timer);
    uv_timer_start(&timer, on_timer, 0, interval/*seconds*/ * 1000);

    uv_run(loop, UV_RUN_DEFAULT);
}
Пример #9
0
int main(int argc, char **argv)
{
  loop = uv_default_loop();

  if (argc <= 1)
    return 0;

  if (curl_global_init(CURL_GLOBAL_ALL)) {
    fprintf(stderr, "Could not init cURL\n");
    return 1;
  }

  uv_timer_init(loop, &timeout);

  curl_handle = curl_multi_init();
  curl_multi_setopt(curl_handle, CURLMOPT_SOCKETFUNCTION, handle_socket);
  curl_multi_setopt(curl_handle, CURLMOPT_TIMERFUNCTION, start_timeout);

  while (argc-- > 1) {
    add_download(argv[argc], argc);
  }

  uv_run(loop, UV_RUN_DEFAULT);
  curl_multi_cleanup(curl_handle);
  return 0;
}
Пример #10
0
void loop_init(Loop *loop, void *data)
{
  uv_loop_init(&loop->uv);
  loop->recursive = 0;
  loop->uv.data = loop;
  loop->children = kl_init(WatcherPtr);
  loop->children_stop_requests = 0;
  loop->events = multiqueue_new_parent(loop_on_put, loop);
  loop->fast_events = multiqueue_new_child(loop->events);
  loop->thread_events = multiqueue_new_parent(NULL, NULL);
  uv_mutex_init(&loop->mutex);
  uv_async_init(&loop->uv, &loop->async, async_cb);
  uv_signal_init(&loop->uv, &loop->children_watcher);
  uv_timer_init(&loop->uv, &loop->children_kill_timer);
  uv_timer_init(&loop->uv, &loop->poll_timer);
}
Пример #11
0
Client::Client(int id, const char *agent, IClientListener *listener) :
    m_ipv6(false),
    m_nicehash(false),
    m_quiet(false),
    m_agent(agent),
    m_listener(listener),
    m_id(id),
    m_retryPause(5000),
    m_failures(0),
    m_recvBufPos(0),
    m_state(UnconnectedState),
    m_expire(0),
    m_jobs(0),
    m_stream(nullptr),
    m_socket(nullptr)
{
    memset(m_ip, 0, sizeof(m_ip));
    memset(&m_hints, 0, sizeof(m_hints));

    m_resolver.data = this;

    m_hints.ai_family   = AF_UNSPEC;
    m_hints.ai_socktype = SOCK_STREAM;
    m_hints.ai_protocol = IPPROTO_TCP;

    m_recvBuf.base = m_buf;
    m_recvBuf.len  = sizeof(m_buf);

#   ifndef XMRIG_PROXY_PROJECT
    m_keepAliveTimer.data = this;
    uv_timer_init(uv_default_loop(), &m_keepAliveTimer);
#   endif
}
Пример #12
0
static connection_context_t* create_connection_context(
    uv_os_sock_t sock, int is_server_connection) {
  int r;
  connection_context_t* context;

  context = (connection_context_t*) malloc(sizeof *context);
  ASSERT(context != NULL);

  context->sock = sock;
  context->is_server_connection = is_server_connection;
  context->read = 0;
  context->sent = 0;
  context->open_handles = 0;
  context->events = 0;
  context->delayed_events = 0;
  context->got_fin = 0;
  context->sent_fin = 0;
  context->got_disconnect = 0;

  r = uv_poll_init_socket(uv_default_loop(), &context->poll_handle, sock);
  context->open_handles++;
  context->poll_handle.data = context;
  ASSERT(r == 0);

  r = uv_timer_init(uv_default_loop(), &context->timer_handle);
  context->open_handles++;
  context->timer_handle.data = context;
  ASSERT(r == 0);

  return context;
}
Пример #13
0
void
client_accept_cb(uv_stream_t *server, int status) {
    struct client_context *client = new_client();
    struct remote_context *remote = new_remote(idle_timeout);

    client->remote = remote;
    remote->client = client;

    uv_timer_init(server->loop, remote->timer);

    uv_tcp_init(server->loop, &client->handle.tcp);
    uv_tcp_init(server->loop, &remote->handle.tcp);


    int rc = uv_accept(server, &client->handle.stream);
    if (rc == 0) {
        int namelen = sizeof client->addr;
        uv_tcp_getpeername(&client->handle.tcp, &client->addr, &namelen);
        reset_timer(remote); // start timer
        connect_to_remote(remote);
    } else {
        logger_log(LOG_ERR, "accept error: %s", uv_strerror(rc));
        close_client(client);
        close_remote(remote);
    }
}
Пример #14
0
pc_client_t *pc_client_new_with_reconnect(int delay, int delay_max, int exp_backoff) {
  pc_client_t* client = pc_client_new();
  assert(client);
  if (delay <= 0 || delay_max <= 0) {
    fprintf(stderr, "Bad arguments, delay: %d, delay_max: %d", delay, delay_max);
    pc_client_destroy(client);
    return NULL;
  }

  client->enable_reconnect = 1;
  client->reconnect_delay = delay;
  client->reconnect_delay_max = delay_max;
  client->enable_exp_backoff = exp_backoff ? 1 : 0; 

  srand(time(0));

  if(!client->enable_exp_backoff){
    client->max_reconnects_incr = client->reconnect_delay_max / client->reconnect_delay + 1;
  } else {
    client->max_reconnects_incr = (int)log(1.0 * client->reconnect_delay_max / client->reconnect_delay) / log(2.0) + 1;
  }
  /* uv_timer_init never fail */
  uv_timer_init(client->uv_loop, &client->reconnect_timer); 

  return client;
}
Пример #15
0
int loq_timer(lua_State *L)
{
  int time = luaL_checkinteger(L, 1);
  int r = 0;
  
  uv_loop_t* loop = uv_default_loop();
  
  // Create a timer callback and fill it up with what we need.
  loq_timer_t *timer = (loq_timer_t*)loq_malloc(sizeof(loq_timer_t));
  r = uv_timer_init(loop, &timer->handle);
  if(r) {
    loq_free(timer);
    uv_err_t err = loop->last_err;
    return luaL_error(L, uv_strerror(err));
  }
  timer->L = L;
  timer->handle.data = timer;
  
  // Stash the callback in lua.
  lua_pushlightuserdata(L, timer);
  lua_pushvalue(L, 2);
  lua_rawset(L, LUA_REGISTRYINDEX);
  
  // Tell uv we want the timer to start
  r = uv_timer_start(&timer->handle, timer_cb, time, 0);
  if(r) {
    uv_close((uv_handle_t*)&timer->handle, timer_close_cb);
    uv_err_t err = loop->last_err;
    return luaL_error(L, uv_strerror(err));
  }
  
  return 0;
}
Пример #16
0
/* TODO: share this with windows? */
int uv_ares_init_options(uv_loop_t* loop, ares_channel *channelptr,
    struct ares_options *options, int optmask) {
  int rc;

  /* only allow single init at a time */
  if (loop->channel != NULL) {
    uv__set_artificial_error(loop, UV_EALREADY);
    return -1;
  }

  /* set our callback as an option */
  options->sock_state_cb = uv__ares_sockstate_cb;
  options->sock_state_cb_data = loop;
  optmask |= ARES_OPT_SOCK_STATE_CB;

  /* We do the call to ares_init_option for caller. */
  rc = ares_init_options(channelptr, options, optmask);

  /* if success, save channel */
  if (rc == ARES_SUCCESS) {
    loop->channel = *channelptr;
  } 

  /*
   * Initialize the timeout timer. The timer won't be started until the
   * first socket is opened.
   */
  uv_timer_init(loop, &loop->timer);
  uv_unref(loop);
  loop->timer.data = loop;

  return rc;
}
Пример #17
0
/// Initializes job control resources
void job_init(void)
{
  uv_disable_stdio_inheritance();
  uv_timer_init(uv_default_loop(), &job_stop_timer);
  uv_signal_init(uv_default_loop(), &schld);
  uv_signal_start(&schld, chld_handler, SIGCHLD);
}
Пример #18
0
void http_request_cache_configure_listener(uv_loop_t* loop, uv_async_t* handle)
{
    uv_timer_t* cache_invalidation_timer = malloc(sizeof(uv_timer_t));
    uv_timer_init(loop, cache_invalidation_timer);
    uv_timer_start(cache_invalidation_timer, http_request_cache_timer, 500, 500);
    uv_unref((uv_handle_t*) cache_invalidation_timer);
}
Пример #19
0
LUA_LIB_METHOD static int new_user_timer(lua_State* l_thread) {
    luaw_timer_t* timer = (luaw_timer_t*)calloc(1, sizeof(luaw_timer_t));
    if (timer == NULL) {
        return raise_lua_error(l_thread, "Could not allocate memory for user timer");
    }

    luaw_timer_t** lua_ref = lua_newuserdata(l_thread, sizeof(luaw_timer_t*));
    if (lua_ref == NULL) {
        free(timer);
        return raise_lua_error(l_thread, "Could not allocate memory for user timer Lua reference");
    }

    /* link C side connection reference and Lua's full userdata that represents it to each other */
    luaL_setmetatable(l_thread, LUA_USER_TIMER_META_TABLE);
    *lua_ref = timer;
    INCR_REF_COUNT(timer)
    timer->lua_ref = lua_ref;

    /* init libuv artifacts */
    uv_timer_init(uv_default_loop(), &timer->handle);
    timer->handle.data = timer;
    INCR_REF_COUNT(timer)
    clear_user_timer(timer);

    return 1;
}
Пример #20
0
void HTTPRequest::retryHTTPRequest(std::unique_ptr<Response> &&res, uint64_t timeout) {
    assert(uv_thread_self() == thread_id);
    assert(!backoff_timer);
    backoff_timer = new uv_timer_t();
    uv_timer_init(loop, backoff_timer);
    backoff_timer->data = new RetryBaton(this, std::move(res));

#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
    uv_timer_start(backoff_timer, [](uv_timer_t *timer, int) {
#else
    uv_timer_start(backoff_timer, [](uv_timer_t *timer) {
#endif
        std::unique_ptr<RetryBaton> pair { static_cast<RetryBaton *>(timer->data) };
        pair->first->startHTTPRequest(std::move(pair->second));
        pair->first->backoff_timer = nullptr;
        uv_timer_stop(timer);
        uv_close((uv_handle_t *)timer, [](uv_handle_t *handle) { delete (uv_timer_t *)handle; });
    }, timeout, 0);
}

void HTTPRequest::removeHTTPBaton() {
    assert(uv_thread_self() == thread_id);
    if (http_baton) {
        http_baton->request = nullptr;
        HTTPRequestBaton::stop(http_baton);
        http_baton.reset();
    }
}
Пример #21
0
Network::Network(const Options *options) :
    m_options(options),
    m_donate(nullptr)
{
    srand(time(0) ^ (uintptr_t) this);

    Workers::setListener(this);

    const std::vector<Url*> &pools = options->pools();

#ifndef XMRIG_NO_TLS
    ssl_init();
#endif

    if (pools.size() > 1) {
        m_strategy = new FailoverStrategy(pools, Platform::userAgent(), this);
    }
    else {
        m_strategy = new SinglePoolStrategy(pools.front(), Platform::userAgent(), this);
    }

    if (m_options->donateLevel() > 0) {
        m_donate = new DonateStrategy(Platform::userAgent(), this);
    }

    m_timer.data = this;
    uv_timer_init(uv_default_loop(), &m_timer);

    uv_timer_start(&m_timer, Network::onTick, kTickInterval, kTickInterval);
}
Пример #22
0
static void(timer_start)(void* t, unsigned int ms)
{
   struct LwqqAsyncTimer_* t_ = (struct LwqqAsyncTimer_*)t;
   t_->h.data = t;
   uv_timer_init(loop, &t_->h);
   uv_timer_start(&t_->h, timer_cb_wrap, ms / 10, ms / 10);
}
Пример #23
0
static void _tcp_accept(uv_stream_t *master, int status, bool tls)
{
	if (status != 0) {
		return;
	}
	uv_stream_t *client = handle_alloc(master->loop);
	if (!client) {
		return;
	}
	memset(client, 0, sizeof(*client));
	io_create(master->loop, (uv_handle_t *)client, SOCK_STREAM);
	if (uv_accept(master, client) != 0) {
		uv_close((uv_handle_t *)client, io_free);
		return;
	}

	/* Set deadlines for TCP connection and start reading.
	 * It will re-check every half of a request time limit if the connection
	 * is idle and should be terminated, this is an educated guess. */
	struct session *session = client->data;
	session->has_tls = tls;
	if (tls && !session->tls_ctx) {
		session->tls_ctx = tls_new(master->loop->data);
	}
	uv_timer_t *timer = &session->timeout;
	uv_timer_init(master->loop, timer);
	timer->data = client;
	uv_timer_start(timer, tcp_timeout_trigger, KR_CONN_RTT_MAX/2, KR_CONN_RTT_MAX/2);
	io_start_read((uv_handle_t *)client);
}
Пример #24
0
Файл: util.c Проект: tarruda/luv
static int new_timer(lua_State* L) {
  uv_timer_t* handle = luv_create_timer(L);
  if (uv_timer_init(uv_default_loop(), handle)) {
    uv_err_t err = uv_last_error(uv_default_loop());
    return luaL_error(L, "new_timer: %s", uv_strerror(err));
  }
  return 1;
}
Пример #25
0
static void next_tick(uv_idle_t* handle, int status) {
  uv_loop_t* loop = handle->loop;
  uv_idle_stop(handle);
  uv_idle_init(loop, &idle_handle);
  uv_idle_start(&idle_handle, idle_cb);
  uv_timer_init(loop, &timer_handle);
  uv_timer_start(&timer_handle, timer_cb, 0, 0);
}
Пример #26
0
Файл: temp.c Проект: josl/urbit
/* u3_temp(): initialize time timer.
*/
void
u3_temp_io_init(void)
{
  u3_temp* teh_u = &u3_Host.teh_u;

  uv_timer_init(u3L, &teh_u->tim_u);
  teh_u->alm = c3n;
}
Пример #27
0
cq_verifier *cq_verifier_create(grpc_completion_queue *cq) {
  cq_verifier *v = gpr_malloc(sizeof(cq_verifier));
  v->cq = cq;
  v->first_expectation = NULL;
  uv_timer_init(uv_default_loop(), &v->timer);
  v->timer.data = (void *)TIMER_STARTED;
  return v;
}
Пример #28
0
SyncWorker::SyncWorker()
{
    this->timer_ = { 0 };
    this->timer_.data = this;
    this->loop_count_ = 1;
    this->loop_time_ = 1;
    uv_timer_init( uv_default_loop() , &this->timer_ );
}
Пример #29
0
/**
 * start logger writer timer.
 */
void css_logger_start_timer()
{
// start timer
	css_logger.timer = (uv_timer_t*) malloc(sizeof(uv_timer_t));
	uv_timer_init(css_logger.loop, css_logger.timer);
	uv_timer_start(css_logger.timer, css_logger_dump_to_file_cb, css_logger.timeout, css_logger.timeout);

}
Пример #30
0
/* Sets the timer up on the event loop. */
static void setup(MVMThreadContext *tc, uv_loop_t *loop, MVMObject *async_task, void *data) {
    TimerInfo *ti = (TimerInfo *)data;
    uv_timer_init(loop, &ti->handle);
    ti->work_idx    = MVM_io_eventloop_add_active_work(tc, async_task);
    ti->tc          = tc;
    ti->handle.data = ti;
    uv_timer_start(&ti->handle, timer_cb, ti->timeout, ti->repeat);
}