示例#1
0
文件: main.c 项目: sunnyboy00/uvbook
int main() {
    loop = uv_default_loop();

    setup_workers();

    uv_tcp_t server;
    uv_tcp_init(loop, &server);

    struct sockaddr_in bind_addr = uv_ip4_addr("0.0.0.0", 7000);
    uv_tcp_bind(&server, bind_addr);
    if (uv_listen((uv_stream_t*) &server, 128, on_new_connection)) {
        fprintf(stderr, "Listen error %s\n", uv_err_name(uv_last_error(loop)));
        return 2;
    }
    return uv_run(loop, UV_RUN_DEFAULT);
}
示例#2
0
int main() {
  int r;
  loop = uv_default_loop();

  setup_workers();

  struct sockaddr_in bind_addr;
  r = uv_ip4_addr("0.0.0.0", 7000, &bind_addr);
  if (r) ERROR("obtaining ipv4 address", r);

  uv_tcp_t server;
  uv_tcp_init(loop, &server);
  r = uv_tcp_bind(&server, (const struct sockaddr*) &bind_addr);
  if (r) ERROR("binding server to socket", r);

  r = uv_listen((uv_stream_t*) &server, 128/*backlog*/, connection_cb);
  if (r) ERROR("listening for connections", r);

  return uv_run(loop, UV_RUN_DEFAULT);
}
示例#3
0
int main(int argc, char** argv) {
    if (argc < 4) {
        printf("Usage: %s ip port path2worker [shmsize]\n", argv[0]);
        return EXIT_FAILURE;
    }


    struct listener_s listener;
    listener.argc = argc;
    listener.argv = argv;
    listener.stop_moniter = 0;
    listener.shm_id = -1;
    listener.shm_size = -1;

    if (argc > 4) {
        listener.shm_size = atoi(argv[4]);
        if (listener.shm_size > 0) {
            listener.shm_id = shm_alloc(0, listener.shm_size);
        }
        if (listener.shm_id != -1) {
            shm_free(listener.shm_id); // lazy free
        }
    }


    // get loop
    struct ev_loop* loop = ev_default_loop(EVBACKEND_EPOLL);
    ev_set_userdata(loop, &listener);


    // setup signal handler
    struct ev_signal quitwatcher;
    struct ev_signal hupwatcher;
    ev_signal_init(&quitwatcher, listener_stop, SIGQUIT);
    ev_signal_init(&hupwatcher, restart_workers, SIGHUP);
    ev_signal_start(loop, &quitwatcher);
    ev_unref(loop); // 将loop中的watchercnt--,保证不停止此watcher的情况下loop也能正常退出
    ev_signal_start(loop, &hupwatcher);
    ev_unref(loop); // 将loop中的watchercnt--,保证不停止此watcher的情况下loop也能正常退出


    // get cpu number
    listener.worker_count = (int)sysconf(_SC_NPROCESSORS_CONF);


    // init workers
    struct worker_s workers[listener.worker_count];
    listener.workers = &workers[0];

    if (0 != setup_workers(loop, &listener)) {
        return EXIT_FAILURE;
    }


    int r = ev_run(loop, 0);


    ev_ref(loop);
    ev_signal_stop(loop, &quitwatcher);
    ev_ref(loop);
    ev_signal_stop(loop, &hupwatcher);


    return r;
}