void event_loop(thread_context_t *ctx)
{
	ctx->tid = syscall(SYS_gettid);
	ctx->random_seed = ctx->tid;

	while (!ctx->global_data->shutdown || ctx->event_loop.conn_num)
		h2o_evloop_run(ctx->event_loop.h2o_ctx.loop);
}
Example #2
0
static void *worker_main(void *_unused)
{
    while (!worker_thread.should_exit) {
#if H2O_USE_LIBUV
        uv_run(worker_thread.loop, UV_RUN_ONCE);
#else
        h2o_evloop_run(worker_thread.loop, INT32_MAX);
#endif
    }

    return NULL;
}
Example #3
0
int main(int argc, char **argv)
{
    static struct option longopts[] = {
        { "conf", required_argument, NULL, 'c' },
        { "help", no_argument, NULL, 'h' },
        { NULL, 0, NULL, 0 }
    };
    static struct port_configurator_t port_configurator = {
        { NULL, "port", NULL, on_config_port, on_config_port_complete },
        0
    };


    const char *conf_fn = "h2o.conf";
    int opt_ch;
    yoml_t *config;
    h2o_context_t ctx;

    /* parse options */
    while ((opt_ch = getopt_long(argc, argv, "c:h:", longopts, NULL)) != -1) {
        switch (opt_ch) {
        case 'c':
            conf_fn = optarg;
            break;
        case 'h':
            usage();
            exit(0);
            break;
        default:
            assert(0);
            break;
        }
    }
    argc -= optind;
    argv += optind;

    /* load configuration */
    if ((config = load_config(conf_fn)) == NULL)
        exit(EX_CONFIG);

    /* setup h2o context */
    h2o_context_init(&ctx, h2o_evloop_create());
    h2o_register_configurator(&ctx, &port_configurator.super);

    /* apply the configuration */
    if (h2o_context_configure(&ctx, conf_fn, config) != 0)
        exit(EX_CONFIG);

    while (h2o_evloop_run(ctx.loop) == 0)
        ;

    return 0;
}
Example #4
0
File: main.c Project: Gwill/h2o
static void *run_loop(void *_config)
{
    h2o_global_configuration_t *config = _config;
    h2o_evloop_t *loop;
    h2o_context_t ctx;

    loop = h2o_evloop_create();
    h2o_context_init(&ctx, loop, config);

    while (1)
        h2o_evloop_run(loop);

    return NULL;
}
Example #5
0
int main(int argc, char **argv)
{
    h2o_hostconf_t *hostconf;

    signal(SIGPIPE, SIG_IGN);

    h2o_config_init(&config);
    hostconf = h2o_config_register_host(&config, "default");
    register_handler(hostconf, "/post-test", post_test);
    register_handler(hostconf, "/chunked-test", chunked_test);
    h2o_file_register(h2o_config_register_path(hostconf, "/"), "examples/doc_root", NULL, NULL, 0);

#if 0 /* reproxy is not yet implemented */
    register_handler(hostconf, "/reproxy-test", reproxy_test);
    h2o_reproxy_register(hostconf);
#endif

#if H2O_USE_LIBUV
    uv_loop_t loop;
    uv_loop_init(&loop);
    h2o_context_init(&ctx, &loop, &config);
#else
    h2o_context_init(&ctx, h2o_evloop_create(), &config);
#endif

    /* disabled by default: uncomment the block below to use HTTPS instead of HTTP */
    /*
    if (setup_ssl("server.crt", "server.key") != 0)
        goto Error;
    */

    /* disabled by default: uncomment the line below to enable access logging */
    /* h2o_access_log_register(&config.default_host, "/dev/stdout", NULL); */

    if (create_listener() != 0) {
        fprintf(stderr, "failed to listen to 127.0.0.1:7890:%s\n", strerror(errno));
        goto Error;
    }

#if H2O_USE_LIBUV
    uv_run(ctx.loop, UV_RUN_DEFAULT);
#else
    while (h2o_evloop_run(ctx.loop) == 0)
        ;
#endif

Error:
    return 1;
}
Example #6
0
File: main.c Project: Debug-Orz/h2o
static void *run_loop(void *_conf)
{
    struct config_t *conf = _conf;
    h2o_evloop_t *loop;
    h2o_context_t ctx;
    struct listener_ctx_t *listeners = alloca(sizeof(*listeners) * conf->num_listeners);
    size_t i;

    /* setup loop and context */
    loop = h2o_evloop_create();
    h2o_context_init(&ctx, loop, &conf->global_config);

    /* setup listeners */
    for (i = 0; i != conf->num_listeners; ++i) {
        listeners[i].ctx = &ctx;
        listeners[i].ssl_ctx = conf->listeners[i]->ssl_ctx;
        listeners[i].sock = h2o_evloop_socket_create(
            ctx.loop, conf->listeners[i]->fd,
            (struct sockaddr*)&conf->listeners[i]->addr, conf->listeners[i]->addrlen,
            H2O_SOCKET_FLAG_IS_ACCEPT);
        listeners[i].sock->data = listeners + i;
    }

    /* the main loop */
    while (1) {
        /* start / stop trying to accept new connections */
        if (conf->state.num_connections < conf->max_connections) {
            for (i = 0; i != conf->num_listeners; ++i) {
                if (! h2o_socket_is_reading(listeners[i].sock))
                    h2o_socket_read_start(listeners[i].sock, on_accept);
            }
        } else {
            for (i = 0; i != conf->num_listeners; ++i) {
                if (h2o_socket_is_reading(listeners[i].sock))
                    h2o_socket_read_stop(listeners[i].sock);
            }
        }
        /* run the loop once */
        h2o_evloop_run(loop);
    }

    return NULL;
}
Example #7
0
int main(int argc, char **argv)
{
    h2o_hostconf_t *hostconf;
    h2o_pathconf_t *pathconf;

    h2o_config_init(&config);
    hostconf = h2o_config_register_host(&config, h2o_iovec_init(H2O_STRLIT("default")), 65535);
    pathconf = h2o_config_register_path(hostconf, "/", 0);
    h2o_create_handler(pathconf, sizeof(h2o_handler_t))->on_req = on_req;

#if H2O_USE_LIBUV
    uv_loop_t loop;
    uv_loop_init(&loop);
    h2o_context_init(&ctx, &loop, &config);
#else
    h2o_context_init(&ctx, h2o_evloop_create(), &config);
#endif

    /* disabled by default: uncomment the block below to use HTTPS instead of HTTP */
    /*
    if (setup_ssl("server.crt", "server.key") != 0)
        goto Error;
    */

    accept_ctx.ctx = &ctx;
    accept_ctx.hosts = config.hosts;

    if (create_listener() != 0) {
        fprintf(stderr, "failed to listen to 127.0.0.1:7890:%s\n", strerror(errno));
        goto Error;
    }

#if H2O_USE_LIBUV
    uv_run(ctx.loop, UV_RUN_DEFAULT);
#else
    while (h2o_evloop_run(ctx.loop, INT32_MAX) == 0)
        ;
#endif

Error:
    return 1;
}
Example #8
0
int main(int argc, char **argv)
{
    h2o_http1client_ctx_t ctx = {
        NULL,
        &zero_timeout,
        &io_timeout
    };

    if (argc != 2) {
        fprintf(stderr, "Usage: %s <url>\n", argv[0]);
        return 1;
    }
    url = argv[1];

    h2o_mempool_init(&pool);

    /* setup context */
#if H2O_USE_LIBUV
    ctx.loop = uv_loop_new();
#else
    ctx.loop = h2o_evloop_create();
#endif
    h2o_timeout_init(ctx.loop, &zero_timeout, 0);
    h2o_timeout_init(ctx.loop, &io_timeout, 5000); /* 5 seconds */

    /* setup the first request */
    start_request(&ctx);

    while (cnt_left != 0) {
#if H2O_USE_LIBUV
        uv_run(ctx.loop, UV_RUN_ONCE);
#else
        h2o_evloop_run(ctx.loop);
#endif
    }

    return 0;
}
Example #9
0
File: simple.c Project: Gwill/h2o
int main(int argc, char **argv)
{
    signal(SIGPIPE, SIG_IGN);

    h2o_config_init(&config);
    register_handler(&config.default_host, post_test);
    register_handler(&config.default_host, chunked_test);
    register_handler(&config.default_host, reproxy_test);
    h2o_register_file_handler(&config.default_host, "/", "htdocs", "index.html");
    h2o_define_mimetype(&config.default_host.mimemap, "html", "text/html");
    h2o_register_reproxy_filter(&config.default_host);

#if H2O_USE_LIBUV
    uv_loop_t loop;
    uv_loop_init(&loop);
    h2o_context_init(&ctx, &loop, &config);
#else
    h2o_context_init(&ctx, h2o_evloop_create(), &config);
#endif

    //ssl_ctx = h2o_ssl_new_server_context("server.crt", "server.key", h2o_http2_tls_identifiers);
    //h2o_register_access_logger(&ctx, "/dev/stdout");

    if (create_listener() != 0) {
        fprintf(stderr, "failed to listen to 127.0.0.1:7890:%s\n", strerror(errno));
        goto Error;
    }

#if H2O_USE_LIBUV
    uv_run(ctx.loop, UV_RUN_DEFAULT);
#else
    while (h2o_evloop_run(ctx.loop) == 0)
        ;
#endif

Error:
    return 1;
}
Example #10
0
void test_lib__common__multithread_c(void)
{
    pthread_t tid;

    main_thread.loop = create_loop();
    main_thread.queue = h2o_multithread_create_queue(main_thread.loop);
    h2o_multithread_register_receiver(main_thread.queue, &main_thread.pong_receiver, on_pong);
    h2o_multithread_register_receiver(main_thread.queue, &main_thread.shutdown_receiver, on_shutdown);
    worker_thread.loop = create_loop();
    worker_thread.queue = h2o_multithread_create_queue(worker_thread.loop);
    h2o_multithread_register_receiver(worker_thread.queue, &worker_thread.ping_receiver, on_ping);

    pthread_create(&tid, NULL, worker_main, NULL);

    /* send first message */
    send_empty_message(&worker_thread.ping_receiver);

    while (!main_thread.received_shutdown) {
#if H2O_USE_LIBUV
        uv_run(main_thread.loop, UV_RUN_ONCE);
#else
        h2o_evloop_run(main_thread.loop, INT32_MAX);
#endif
    }

    pthread_join(tid, NULL);

    h2o_multithread_unregister_receiver(worker_thread.queue, &worker_thread.ping_receiver);
    h2o_multithread_destroy_queue(worker_thread.queue);
    destroy_loop(worker_thread.loop);
    h2o_multithread_unregister_receiver(main_thread.queue, &main_thread.pong_receiver);
    h2o_multithread_unregister_receiver(main_thread.queue, &main_thread.shutdown_receiver);
    h2o_multithread_destroy_queue(main_thread.queue);
    destroy_loop(main_thread.loop);

    ok(1);
}
void event_loop(thread_context_t *ctx)
{
	while (!ctx->global_data->shutdown || ctx->event_loop.conn_num)
		h2o_evloop_run(ctx->event_loop.h2o_ctx.loop, INT32_MAX);
}