コード例 #1
0
ファイル: echo_server.c プロジェクト: ceasar09/asnw
int main(int argc, char *argv[])
{
    if (argc < 2) {
        printf("usage: %s bind...\n", argv[0]);
        exit(0);
    }

    int bind_count = argc - 1;
    nw_svr_bind *bind_arr = malloc(sizeof(nw_svr_bind) * bind_count);
    if (bind_arr == NULL) {
        printf("malloc fail\n");
        exit(0);
    }
    memset(bind_arr, sizeof(nw_svr_bind) * bind_count, 0);
    for (int i = 0; i < bind_count; ++i) {
        int ret = nw_sock_cfg_parse(argv[i + 1], &bind_arr[i].addr, &bind_arr[i].sock_type);
        if (ret < 0) {
            printf("parse bind: %s fail: %d\n", argv[1], ret);
            exit(0);
        }
    }

    nw_svr_cfg cfg;
    memset(&cfg, 0, sizeof(cfg));
    cfg.bind_count = bind_count;
    cfg.bind_arr = bind_arr;
    cfg.max_pkg_size = 10240;

    nw_svr_type type;
    memset(&type, 0, sizeof(type));
    type.decode_pkg = decode_pkg;
    type.on_new_connection = on_new_connection;
    type.on_connection_close = on_connection_close;
    type.on_recv_pkg = on_recv_pkg;
    type.on_recv_fd = on_recv_fd;
    type.on_error_msg = on_error_msg;

    nw_svr *svr = nw_svr_create(&cfg, &type, NULL);
    if (svr == NULL) {
        printf("nw_svr_create fail\n");
        exit(0);
    }
    nw_svr_start(svr);

    nw_timer timer;
    nw_timer_set(&timer, 5.0, true, on_timer, svr);
    nw_timer_start(&timer);

    printf("echo server start\n");
    nw_loop_run();
    printf("echo server stop\n");

    return 0;
}
コード例 #2
0
int main(int argc, char *argv[])
{
    printf("process: %s version: %s, compile date: %s %s\n", __process__, __version__, __DATE__, __TIME__);

    if (argc < 2) {
        printf("usage: %s config.json\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    if (process_exist(__process__) != 0) {
        printf("process: %s exist\n", __process__);
        exit(EXIT_FAILURE);
    }

    int ret;
    ret = init_config(argv[1]);
    if (ret < 0) {
        error(EXIT_FAILURE, errno, "load config fail: %d", ret);
    }
    ret = init_process();
    if (ret < 0) {
        error(EXIT_FAILURE, errno, "init process fail: %d", ret);
    }
    ret = init_log();
    if (ret < 0) {
        error(EXIT_FAILURE, errno, "init log fail: %d", ret);
    }

    daemon(1, 1);
    process_keepalive();

    ret = init_server();
    if (ret < 0) {
        error(EXIT_FAILURE, errno, "init server fail: %d", ret);
    }

    nw_timer_set(&cron_timer, 0.5, true, on_cron_check, NULL);
    nw_timer_start(&cron_timer);

    log_vip("server start");
    log_stderr("server start");
    nw_loop_run();
    log_vip("server stop");

    return 0;
}
コード例 #3
0
ファイル: test_rpc_svr.c プロジェクト: ceasar09/asnw
int main(int argc, char *argv[])
{
    if (argc < 2) {
        printf("usage: %s bind...\n", argv[0]);
        exit(0);
    }

    int bind_count = argc - 1;
    nw_svr_bind *bind_arr = malloc(sizeof(nw_svr_bind) * bind_count);
    assert(bind_arr != NULL);
    memset(bind_arr, sizeof(nw_svr_bind) * bind_count, 0);
    for (int i = 0; i < bind_count; ++i) {
        int ret = nw_sock_cfg_parse(argv[i + 1], &bind_arr[i].addr, &bind_arr[i].sock_type);
        if (ret < 0) {
            printf("parse bind: %s fail: %d\n", argv[1], ret);
            exit(0);
        }
    }

    nw_svr_cfg cfg;
    memset(&cfg, 0, sizeof(cfg));
    cfg.bind_count = bind_count;
    cfg.bind_arr = bind_arr;

    rpc_svr_type type;
    memset(&type, 0, sizeof(type));
    type.on_recv_pkg = on_recv_pkg;
    type.on_new_connection = on_new_connection;
    type.on_connection_close = on_connection_close;

    rpc_svr *svr = rpc_svr_create(&cfg, &type);
    if (svr == NULL)
        error(1, errno, "rpc_svr_create fail");
    rpc_svr_start(svr);

    nw_loop_run();

    return 0;
}