예제 #1
0
void knet_channel_ref_update_recv(kchannel_ref_t* channel_ref) {
    int error = 0;
    uint32_t bytes = 0;
    verify(channel_ref);
    bytes = knet_stream_available(channel_ref->ref_info->stream);
    error = knet_channel_update_recv(channel_ref->ref_info->channel);
    switch (error) {
        case error_recv_fail:
            knet_channel_ref_close_check_reconnect(channel_ref);
            break;
        case error_recv_buffer_full:
            knet_channel_ref_close_check_reconnect(channel_ref);
            break;
        default:
            break;
    }
    if (error == error_ok) {
        knet_loop_profile_add_recv_bytes(knet_loop_get_profile(channel_ref->ref_info->loop),
            knet_stream_available(channel_ref->ref_info->stream) - bytes);
        if (channel_ref->ref_info->cb) {
            channel_ref->ref_info->cb(channel_ref, channel_cb_event_recv);
        }
        knet_channel_ref_set_event(channel_ref, channel_event_recv);
    }
}
예제 #2
0
파일: multi_loop.c 프로젝트: dennis-kk/knet
void connector_cb(kchannel_ref_t* channel, knet_channel_cb_event_e e) {
    char buffer[1024] = {0};
    kstream_t* stream = 0;
    int bytes = 0;
    if (e & channel_cb_event_connect) {
        total_connected++;
        printf("connect finished: %d, %d\n", knet_channel_ref_get_socket_fd(channel), total_connected);
    } else if (e & channel_cb_event_recv) {
        stream = knet_channel_ref_get_stream(channel);
        bytes = knet_stream_available(stream);
        bytes = knet_stream_pop(stream, buffer, bytes);
        knet_stream_push(stream, buffer, bytes);
    } else if (e & channel_cb_event_close) {
        atomic_counter_dec(&client_count);
        if (client_count == 0) {
            printf("all client closed\n");
            knet_loop_exit(knet_channel_ref_get_loop(channel));
        }
    } else if (e & channel_cb_event_connect_timeout) {
        printf("connector close, connect timeout: %d\n", knet_channel_ref_get_socket_fd(channel));
        knet_channel_ref_close(channel);
    } else if (e & channel_cb_event_timeout) {
        printf("connector recv timeout, close: %d\n", knet_channel_ref_get_socket_fd(channel));
        knet_channel_ref_close(channel);
    }
}
예제 #3
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;
        }
    }
}
예제 #4
0
파일: multi_loop.c 프로젝트: dennis-kk/knet
void client_cb(kchannel_ref_t* channel, knet_channel_cb_event_e e) {
    kaddress_t* peer_address = 0;
    char buffer[10] = {0};
    kstream_t* stream = 0;
    int bytes = 0;
    if (e & channel_cb_event_recv) {
        stream = knet_channel_ref_get_stream(channel);
        bytes = knet_stream_available(stream);
        knet_stream_pop(stream, buffer, sizeof(buffer));
        knet_stream_push(stream, buffer, bytes);
        recv_count++;
        if (recv_count > MAX_ECHO_COUNT) {
            peer_address = knet_channel_ref_get_peer_address(channel);
            printf("client reach max echo, close: %s, %d\n", address_get_ip(peer_address), address_get_port(peer_address));
            knet_channel_ref_close(channel);
        }
    } else if (e & channel_cb_event_timeout) {
        peer_address = knet_channel_ref_get_peer_address(channel);
        printf("client timeout, close: %s, %d\n", address_get_ip(peer_address), address_get_port(peer_address));
        knet_channel_ref_close(channel);
    }
}