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) { 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); } }
/* 服务端 - 客户端回调 */ 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)); } } }