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); } }
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; } } }
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; }
/* 服务端 - 客户端回调 */ void client_cb(kchannel_ref_t* channel, knet_channel_cb_event_e e) { if (e & channel_cb_event_close) { connector_count--; if (connector_count == 0) { printf("all client closed\n"); knet_loop_exit(knet_channel_ref_get_loop(channel)); } } }
/* 服务端 - 客户端回调 */ void client_cb(kchannel_ref_t* channel, knet_channel_cb_event_e e) { char buffer[32] = {0}; /* 获取对端地址 */ kaddress_t* peer_addr = knet_channel_ref_get_peer_address(channel); kstream_t* stream = knet_channel_ref_get_stream(channel); if (e & channel_cb_event_recv) { /* 有数据可以读 */ /* 读取 */ knet_stream_pop(stream, buffer, sizeof(buffer)); /* 关闭 */ knet_channel_ref_close(channel); /* 退出循环 */ knet_loop_exit(knet_channel_ref_get_loop(channel)); printf("recv from connector: %s, ip: %s, port: %d\n", buffer, address_get_ip(peer_addr), address_get_port(peer_addr)); } }
/* 服务端 - 客户端回调 */ 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)); } } }