Пример #1
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;
        }
    }
}
Пример #2
0
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) {
    char* hello = "hello world";
    kstream_t* stream = knet_channel_ref_get_stream(channel);
    if (e & channel_cb_event_connect) { /* 连接成功 */
        /* 写入 */
        knet_stream_push(stream, hello, 12);
    }
}
Пример #4
0
void acceptor_cb(kchannel_ref_t* channel, knet_channel_cb_event_e e) {
    char buffer[100] = {0};
    kstream_t* stream = 0;
    if (e & channel_cb_event_accept) {
        knet_channel_ref_set_cb(channel, client_cb);
        knet_channel_ref_set_timeout(channel, 2);
        stream = knet_channel_ref_get_stream(channel);
        knet_stream_push(stream, buffer, sizeof(buffer));
    }
}
Пример #5
0
/* 客户端 - 连接器回调 */
void connector_cb(kchannel_ref_t* channel, knet_channel_cb_event_e e) {
    char buffer[32] = {0};
    char* hello = "hello world";
    kstream_t* stream = knet_channel_ref_get_stream(channel);
    if (e & channel_cb_event_connect) { /* 连接成功 */
        /* 写入 */
        knet_stream_push(stream, hello, 12);
    } else if (e & channel_cb_event_recv) {
        /* echo数据读取 */
        knet_stream_pop(stream, buffer, sizeof(buffer));
        /* 关闭 */
        knet_channel_ref_close(channel);
    } else if (e & channel_cb_event_connect_timeout) {
        /* 关闭 */
        knet_channel_ref_close(channel);
        printf("connector close: timeout\n");
    }
}
Пример #6
0
/* 服务端 - 客户端回调 */
void client_cb(kchannel_ref_t* channel, knet_channel_cb_event_e e) {
    char buffer[32] = {0};
    kaddress_t* peer_address = 0;
    kstream_t* stream = knet_channel_ref_get_stream(channel);
    if (e & channel_cb_event_recv) { /* 有数据可以读 */
        /* 读取 */
        knet_stream_pop(stream, buffer, sizeof(buffer));
        /* 不论是否读取完整, 写入12字节 */
        knet_stream_push(stream, buffer, 12);
    } else if (e & channel_cb_event_close) {
        peer_address = knet_channel_ref_get_peer_address(channel);
        printf("peer close: %s, %d, %d\n", address_get_ip(peer_address),
            address_get_port(peer_address), connector_count);
        /* 对端关闭 */
        connector_count--;
        if (connector_count == 0) { /* 全部关闭 */
            /* 退出 */
            knet_loop_exit(knet_channel_ref_get_loop(channel));
        }
    }
}
Пример #7
0
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);
    }
}
Пример #8
0
int knet_vrouter_route(kvrouter_t* router, kchannel_ref_t* c, const void* buffer, int size) {
    uint32_t   id    = 0;
    wire_t*    w     = 0;
    kstream_t* s     = 0;
    int        error = error_ok;
    verify(router);
    verify(c);
    verify(buffer);
    verify(size);
    lock_lock(router->lock);
    id = uuid_get_high32(knet_channel_ref_get_uuid(c));
    w = (wire_t*)hash_get(router->table, id);
    if (!w) {
        lock_unlock(router->lock);
        return error_router_wire_not_found;
    }
    verify(w->c2);
    s = knet_channel_ref_get_stream(w->c2);
    verify(s);    
    /* 发送 */
    error = knet_stream_push(s, buffer, size);
    lock_unlock(router->lock);
    return error;
}