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; }
void knet_channel_ref_update_accept(kchannel_ref_t* channel_ref) { kchannel_ref_t* client_ref = 0; kloop_t* loop = 0; socket_t client_fd = 0; verify(channel_ref); /* 查看选取器是否有自定义实现 */ client_fd = knet_impl_channel_accept(channel_ref); if (!client_fd) { /* 默认实现 */ client_fd = socket_accept(knet_channel_get_socket_fd(channel_ref->ref_info->channel)); } verify(client_fd > 0); knet_channel_ref_set_state(channel_ref, channel_state_accept); knet_channel_ref_set_event(channel_ref, channel_event_recv); if (client_fd) { loop = knet_channel_ref_choose_loop(channel_ref); if (loop) { client_ref = knet_channel_ref_accept_from_socket_fd(channel_ref, loop, client_fd, 0); verify(client_ref); knet_channel_ref_set_user_data(client_ref, channel_ref->ref_info->user_data); /* 设置回调 */ knet_channel_ref_set_cb(client_ref, channel_ref->ref_info->cb); /* 添加到其他loop */ knet_loop_notify_accept(loop, client_ref); } else { client_ref = knet_channel_ref_accept_from_socket_fd(channel_ref, channel_ref->ref_info->loop, client_fd, 1); verify(client_ref); knet_channel_ref_set_user_data(client_ref, channel_ref->ref_info->user_data); /* 调用回调 */ if (channel_ref->ref_info->cb) { channel_ref->ref_info->cb(client_ref, channel_cb_event_accept); } } } }
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 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)); } }
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; }
/* 监听者回调 */ void acceptor_cb(kchannel_ref_t* channel, knet_channel_cb_event_e e) { if (e & channel_cb_event_accept) { /* 新连接 */ /* 设置回调 */ knet_channel_ref_set_cb(channel, client_cb); current_count++; /* 加入到广播组 */ knet_broadcast_join(broadcast, channel); if (current_count == MAX_CONNECTOR) { /* 全部连接完成,广播 */ knet_broadcast_write(broadcast, "hello world", 12); } } }
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; }
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; }
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; }
/* 监听者回调 */ void acceptor_cb(kchannel_ref_t* channel, knet_channel_cb_event_e e) { if (e & channel_cb_event_accept) { /* 新连接 */ /* 设置回调 */ knet_channel_ref_set_cb(channel, client_cb); } }
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; }