Example #1
0
int main() {
    int i = 0;
    kchannel_ref_t* connector = 0;
    /* 创建循环 */
    kloop_t* loop = knet_loop_create();
    /* 创建监听者 */
    kchannel_ref_t* acceptor = knet_loop_create_channel(loop, 8, 1024);
    /* 设置回调 */
    knet_channel_ref_set_cb(acceptor, acceptor_cb);
    /* 监听 */
    knet_channel_ref_accept(acceptor, 0, 80, 500);
    /* 连接 */
    for (; i < MAX_CONNECTOR; i++) {
        /* 创建客户端 */
        connector = knet_loop_create_channel(loop, 8, 1024);
        /* 设置回调 */
        knet_channel_ref_set_cb(connector, connector_cb);
        knet_channel_ref_connect(connector, "127.0.0.1", 80, 2);
    }
    /* 启动 */
    knet_loop_run(loop);
    /* 销毁, connector, acceptor不需要手动销毁 */
    knet_loop_destroy(loop);
    return 0;
}
Example #2
0
void connector_cb(kchannel_ref_t* channel, knet_channel_cb_event_e e) {
    kchannel_ref_t* connector  = 0;
    char      buffer[1024]    = {0};
    char*     hello           = "hello world";
    int       bytes           = 0;
    kstream_t* stream          = knet_channel_ref_get_stream(channel);
    if (e & channel_cb_event_connect) { /* 连接成功 */
        active_channel++;
        /* 写入 */
        send_bytes += 12;
        knet_stream_push(stream, hello, 12);
        if (active_channel < client_n) {
            connector = knet_loop_create_channel(knet_channel_ref_get_loop(channel), 8, 121);
            knet_channel_ref_set_cb(connector, connector_cb);
            knet_channel_ref_set_timeout(connector, 1);
            knet_channel_ref_connect(connector, ip, port, 120);
        }
    } else if (e & channel_cb_event_recv) {
        bytes = knet_stream_available(stream);
        if (error_ok == knet_stream_pop(stream, buffer, bytes)) {
            recv_bytes += bytes;
        }
    } else if (e & channel_cb_event_close) {
        active_channel--;
        printf("unexpect close\n");
    } else if (e & channel_cb_event_connect_timeout) {
        printf("connect timeout!\n");
        knet_channel_ref_close(channel);
    } else if (e & channel_cb_event_timeout) {
        /* 写入 */
        if (error_ok == knet_stream_push(stream, hello, 12)) {
            send_bytes += 12;
        }
    }
}
Example #3
0
int knet_channel_ref_reconnect(kchannel_ref_t* channel_ref, int timeout) {
    int              error               = error_ok;
    char             ip[32]              = {0};
    int              port                = 0;
    kchannel_ref_t*   new_channel         = 0;
    kaddress_t*       peer_address        = 0;
    time_t           connect_timeout     = 0;
    knet_channel_ref_cb_t cb                  = 0;
    kloop_t*          loop                = 0;
    uint32_t         max_send_list_len   = 0;
    uint32_t         max_recv_buffer_len = 0;
    int              auto_reconnect      = 0;
    void*            user_data           = 0;
    verify(channel_ref);
    verify(channel_ref->ref_info->channel);
    if (!knet_channel_ref_check_state(channel_ref, channel_state_connect)) {
        /* 未处于正在连接状态的管道不能重连 */
        return error_channel_not_connect;
    }
    /* 获取原有管道属性 */
    loop                = knet_channel_ref_get_loop(channel_ref);
    max_send_list_len   = knet_channel_get_max_send_list_len(channel_ref->ref_info->channel);
    max_recv_buffer_len = knet_channel_get_max_recv_buffer_len(channel_ref->ref_info->channel);
    cb                  = knet_channel_ref_get_cb(channel_ref);
    user_data           = knet_channel_ref_get_user_data(channel_ref);
    auto_reconnect      = knet_channel_ref_check_auto_reconnect(channel_ref);
    peer_address        = channel_ref->ref_info->peer_address;
    verify(peer_address);
    strcpy(ip, address_get_ip(peer_address));
    port = address_get_port(peer_address);
    /* 建立新管道 */
    new_channel = knet_loop_create_channel(loop, max_send_list_len, max_recv_buffer_len);
    verify(new_channel);
    if (timeout > 0) {
        /* 设置新的超时时间戳 */
        connect_timeout = timeout;
    } else {
        /* 使用原有的超时时间戳 */
        if (channel_ref->ref_info->connect_timeout) {
            connect_timeout = channel_ref->ref_info->connect_timeout;
        }
    }
    /* 设置原有回调 */
    knet_channel_ref_set_cb(new_channel, cb);
    /* 设置原有用户数据 */
    knet_channel_ref_set_user_data(new_channel, user_data);
    /* 设置自动重连标志 */
    knet_channel_ref_set_auto_reconnect(new_channel, auto_reconnect);
    /* 启动新的连接器 */
    error = knet_channel_ref_connect(new_channel, ip, port, (int)connect_timeout);
    if (error_ok != error) {
        return error;
    }
    /* 销毁原有管道 */
    knet_channel_ref_close(channel_ref);
    return error;
}
Example #4
0
int main() {
    int i = 0;
    kloop_t* loop = knet_loop_create();
    kchannel_ref_t* acceptor = 0;
    kchannel_ref_t* connector = 0;
    acceptor = knet_loop_create_channel(loop, 8, 1024);
    knet_channel_ref_accept(acceptor, 0, 8000, 50);
    knet_channel_ref_set_cb(acceptor, acceptor_cb);
    broadcast = knet_broadcast_create();
    for (; i < MAX_CONNECTOR; i++) {
        connector = knet_loop_create_channel(loop, 8, 1024);
        knet_channel_ref_set_cb(connector, connector_cb);
        knet_channel_ref_connect(connector, "127.0.0.1", 8000, 0);
    }

    knet_loop_run(loop);
    knet_broadcast_destroy(broadcast);
    knet_loop_destroy(loop);

    return 0;
}
Example #5
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;
}
Example #6
0
int _create_connector_channel(kframework_connector_config_t* cc, kloop_t* loop) {
    int            error   = error_ok;
    kchannel_ref_t* channel = 0;
    /* 建立连接器管道 */
    channel = knet_loop_create_channel(loop, framework_connector_config_get_max_send_list_count(cc),
        framework_connector_config_get_max_recv_buffer_length(cc));
    verify(channel);
    knet_channel_ref_set_cb(channel, framework_connector_config_get_cb(cc));
    knet_channel_ref_set_auto_reconnect(channel, framework_connector_config_get_auto_reconnect(cc));
    /* 连接 */
    error = knet_channel_ref_connect(channel, framework_connector_config_get_remote_ip(cc),
        framework_connector_config_get_remote_port(cc), framework_connector_config_get_connect_timeout(cc));
    if (error != error_ok) {
        knet_channel_ref_destroy(channel);
    }
    return error;
}
Example #7
0
int _create_acceptor_channel(kframework_acceptor_config_t* ac, kloop_t* loop) {
    int            error   = error_ok;
    kchannel_ref_t* channel = 0;
    /* 建立监听管道 */
    channel = knet_loop_create_channel(loop, framework_acceptor_config_get_client_max_send_list_count(ac),
        framework_acceptor_config_get_client_max_recv_buffer_length(ac));
    verify(channel);
    knet_channel_ref_set_cb(channel, acceptor_cb);
    knet_channel_ref_set_user_data(channel, ac);
    /* 监听 */
    error = knet_channel_ref_accept(channel, framework_acceptor_config_get_ip(ac),
        framework_acceptor_config_get_port(ac), framework_acceptor_config_get_backlog(ac));
    if (error != error_ok) {
        knet_channel_ref_destroy(channel);
    }
    return error;
}
Example #8
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;
}