Exemplo n.º 1
0
void knet_framework_raiser_destroy(kframework_raiser_t* raiser) {
    verify(raiser);
    if (raiser->runner) {
        if (thread_runner_check_start(raiser->runner)) {
            thread_runner_stop(raiser->runner);
        }
        thread_runner_destroy(raiser->runner);
    }
    destroy(raiser);
}
Exemplo n.º 2
0
int main(int argc, char* argv[]) {
    int                i            = 0;
    kloop_t*            loop         = 0;
    ktimer_t*          timer        = 0;
    kchannel_ref_t*     connector    = 0;
    kthread_runner_t*   timer_thread = 0;
    static const char* helper_string =
        "-n    client count\n"
        "-ip   remote host IP\n"
        "-port remote host port\n";

    if (argc > 2) {
        for (i = 1; i < argc; i++) {
            if (!strcmp("-n", argv[i])) {
                client_n = atoi(argv[i+1]);
            } else if (!strcmp("-ip", argv[i])) {
                ip = argv[i+1];
            } else if (!strcmp("-port", argv[i])) {
                port = atoi(argv[i+1]);
            }
        }
    } else {
        printf(helper_string);
        exit(0);
    }

    loop       = knet_loop_create();
    timer_loop = ktimer_loop_create(1000);
    timer      = ktimer_create(timer_loop);

    ktimer_start(timer, timer_cb, 0, 1000);
    timer_thread = thread_runner_create(0, 0);
    thread_runner_start_timer_loop(timer_thread, timer_loop, 0);

    connector = knet_loop_create_channel(loop, 8, 121);
    knet_channel_ref_set_cb(connector, connector_cb);
    knet_channel_ref_set_timeout(connector, 1);
    if (error_ok != knet_channel_ref_connect(connector, ip, port, 20)) {
        return 0;
    }

    knet_loop_run(loop);
    thread_runner_destroy(timer_thread);
    knet_loop_destroy(loop);
    ktimer_loop_destroy(timer_loop);

    return 0;
}
Exemplo n.º 3
0
int main() {
    int i = 0;
    int error = 0;
    kloop_t* main_loop = 0;
    kloop_t* sub_loop[MAX_LOOP] = {0};
    kthread_runner_t* runner[MAX_LOOP] = {0};
    kchannel_ref_t* acceptor = 0;
    kchannel_ref_t* connector = 0;
    kloop_balancer_t* balancer = 0;
    int times = 0;

    /* 建立一个负载均衡器 */
    balancer = knet_loop_balancer_create();
    /* 创建多个线程,每个线程运行一个kloop_t */
    for (i = 0; i < MAX_LOOP; i++) {
        sub_loop[i] = knet_loop_create();
        knet_loop_balancer_attach(balancer, sub_loop[i]);
        runner[i] = thread_runner_create(0, 0);
        thread_runner_start_loop(runner[i], sub_loop[i], 0);
    }
    main_loop = knet_loop_create();
    knet_loop_balancer_attach(balancer, main_loop);

    acceptor = knet_loop_create_channel(main_loop, 8, 1024 * 8);
    knet_channel_ref_set_cb(acceptor, acceptor_cb);
    error = knet_channel_ref_accept(acceptor, 0, 80, 5000);
    if (error_ok != error) {
        printf("knet_channel_ref_accept failed: %d\n", error);
    }
    
    /* 多次测试 */
    for (; times < TEST_TIMES; times++) {
        total_connected = 0;
        recv_count = 0;
        client_count = MAX_CLIENT;
        for (i = 0; i < MAX_CLIENT; i++) {
            connector = knet_loop_create_channel(main_loop, 8, 8192);
            knet_channel_ref_set_cb(connector, connector_cb);
            knet_channel_ref_set_timeout(connector, 2);
            knet_channel_ref_connect(connector, "127.0.0.1", 80, 2);
        }
        while (client_count > 0) {
            /* 主线程 */
            error = knet_loop_run_once(main_loop);
        }
        if (error != error_ok) {
            printf("knet_loop_run() failed: %d\n", error);
        }
    }
    for (i = 0; i < MAX_LOOP; i++) {
        thread_runner_stop(runner[i]);
        thread_runner_join(runner[i]);
        thread_runner_destroy(runner[i]);
    }
    for (i = 0; i < MAX_LOOP; i++) {
        knet_loop_destroy(sub_loop[i]);
    }
    knet_loop_destroy(main_loop);
    knet_loop_balancer_destroy(balancer);
    return 0;
}