依星源码资源网,依星资源网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

【好消息,好消息,好消息】VIP会员可以发表文章赚积分啦 !
查看: 26|回复: 0

一个高性能、易用的无阻塞网络 IO 库 快速易用的无阻塞网络库 跨平台网络事件库

[复制链接] 主动推送

1万

主题

1万

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
17064
发表于 3 天前 | 显示全部楼层 |阅读模式
一个高性能、易用的无阻塞网络 IO 库 快速易用的无阻塞网络库 跨平台网络事件库
一个高性能、易用的无阻塞网络 IO 库,支持 poll、select、epoll、kqueue、iocp 和 Windows GUI 等多种平台和事件。
包含 C 和 C++11 代码。C 部分实现了通用的 IO 事件库,C++11 部分封装了 C 部分,提供了更强大的功能
示例
以下是使用 的服务器和客户端示例:
2.1. 服务器示例
  1. #include <csignal>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <string>

  6. #include "nio/nio_event.hpp"
  7. #include "nio/client_socket.hpp"
  8. #include "nio/server_socket.hpp"

  9. using namespace nio;

  10. // Handle client IO process in async mode.
  11. static void handle_client(client_socket *cli, int timeout) {
  12.     (*cli).on_read([cli, timeout](socket_t fd, bool expired) {
  13.         if (expired) {
  14.             printf("Read timeout for fd %d\r\n", fd);
  15.             cli->close_await();
  16.             return;
  17.         }

  18.         char buf[1204];
  19.         ssize_t ret = cli->read(fd, buf, sizeof(buf));
  20.         if (ret <= 0 || cli->write(buf, ret, timeout) == -1) {
  21.             cli->close_await();
  22.         }
  23.     }).on_write([cli](socket_t fd, bool expired) {
  24.         if (expired) {
  25.             printf("Write expired for fd %d\r\n", fd);
  26.             cli->close_await();
  27.         }
  28.     }).on_error([cli](socket_t fd) {
  29.         cli->close_await();
  30.     }).on_close([cli](socket_t fd) {
  31.         printf("Closing client fd %d\r\n", fd);
  32.         delete cli;
  33.     });

  34.     // Add async read event.
  35.     cli->read_await(timeout);
  36. }

  37. int main() {
  38.     std::string ip("127.0.0.1");
  39.     int port = 8288, timeout = 5000;
  40.     nio_event_t etype = NIO_EVENT_T_KERNEL;

  41.     // Ignore SIGPIPE to avoid process exit.
  42.     signal(SIGPIPE, SIG_IGN);

  43.     nio_event::debug(true);
  44.     nio_event ev(102400, etype);

  45.     server_socket server;

  46.     // Bind and listen the specified address.
  47.     if (!server.open(ip.c_str(), port)) {
  48.         printf("Listen on %s:%d error\r\n", ip.c_str(), port);
  49.         return 1;
  50.     }

  51.     printf("Listen on %s:%d ok\r\n", ip.c_str(), port);

  52.     // Register callback handlers in server.
  53.     server.set_on_accept([&ev, timeout](socket_t fd, const std::string &addr) {
  54.         printf("Accept on client from %s, fd: %d\r\n", addr.c_str(), fd);
  55.         auto *cli = new client_socket(ev, fd);
  56.         handle_client(cli, timeout);
  57.     }).set_on_error([]() {
  58.         printf("Error on server socket\r\n");
  59.     }).set_on_close([]() {
  60.         printf("Server socket closed\r\n");
  61.     });

  62.     // The server socket will accecpt client connections in async mode.
  63.     server.accept_await(ev);

  64.     // IO event loop process.
  65.     while (true) {
  66.         ev.wait(1000);
  67.     }

  68.     return 0;
  69. }
复制代码


2.2. 客户端示例
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cerrno>
  5. #include <csignal>
  6. #include <string>

  7. #include "nio/nio_event.hpp"
  8. #include "nio/client_socket.hpp"

  9. using namespace nio;

  10. static long long total_count = 10000;
  11. static long long count = 0;
  12. static int nconns = 0;

  13. static bool connect_server(nio_event &ev, const char *ip, int port, int timeout) {
  14.     auto *cli = new client_socket(ev);

  15.     (*cli).on_connect([cli, timeout](socket_t fd, bool expired) {
  16.         if (fd == -1 || expired) {
  17.             printf("Connect failed, fd=%d, %s\r\n", fd,  expired ? "expired" : "error");
  18.             cli->close_await();
  19.             nconns--;
  20.             return;
  21.         }

  22.         printf("Connect ok, fd %d\r\n", fd);
  23.         const char *s = "hello world!\r\n";
  24.         if (cli->write( s, strlen(s), timeout) == -1) {
  25.             cli->close_await();
  26.         } else {
  27.             cli->read_await(timeout);
  28.         }
  29.     }).on_read([cli, timeout](socket_t fd, bool expired) {
  30.         char buf[1024];
  31.         ssize_t ret = cli->read(fd, buf, sizeof(buf));
  32.         if (ret <= 0 || ++count >= total_count
  33.             || cli->write(buf, ret, timeout) == -1) {
  34.             cli->close_await();
  35.         }
  36.     }).on_write([cli](socket_t fd, bool expired) {
  37.         printf("Write wait %s for fd %d\r\n", expired ? "expired" : "error", fd);
  38.         if (expired) {
  39.             cli->close_await();
  40.         }
  41.     }).on_error([cli](socket_t fd) {
  42.         cli->close_await();
  43.     }).on_close([cli](socket_t fd) {
  44.         delete cli;
  45.         nconns--;
  46.     });

  47.     if (!cli->connect_await(ip, port, 5000)) {
  48.         delete cli;
  49.         return false;
  50.     }

  51.     return true;
  52. }

  53. int main() {
  54.     int cocurrent = 100;
  55.     std::string ip("127.0.0.1");
  56.     int port = 8288, timeout = 5000;
  57.     nio_event_t etype = NIO_EVENT_T_KERNEL;

  58.     signal(SIGPIPE, SIG_IGN);

  59.     nio_event::debug(true);
  60.     nio_event ev(102400, etype);

  61.     for (int i = 0; i < cocurrent; i++) {
  62.         if (!connect_server(ev, ip.c_str(), port, timeout)) {
  63.             printf("Connect server error %s\r\n", strerror(errno));
  64.             break;
  65.         }
  66.         ++nconns;
  67.     }

  68.     struct timeval begin{0, 0};
  69.     gettimeofday(&begin, nullptr);

  70.     while (nconns > 0) {
  71.         ev.wait(1000);
  72.     }

  73.     return 0;
  74. }
复制代码


链接: https://pan.baidu.com/s/11KgETVXoJEj90wu0O78wBg
提取码下载:
文件名称:提取码下载.txt 
下载次数:0  文件大小:16 Bytes  售价:10金钱 [记录]
下载权限: 不限 [购买VIP]   [充值]   [在线充值]   【VIP会员6折;永久VIP4折】
安全检测,请放心下载






相关帖子

扫码关注微信公众号,及时获取最新资源信息!下载附件优惠VIP会员6折;永久VIP4折
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

免责声明:
1、本站提供的所有资源仅供参考学习使用,版权归原著所有,禁止下载本站资源参与商业和非法行为,请在24小时之内自行删除!
2、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,请勿任何商业目的与商业用途。
3、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。
4、论坛的所有内容都不保证其准确性,完整性,有效性,由于源码具有复制性,一经售出,概不退换。阅读本站内容因误导等因素而造成的损失本站不承担连带责任。
5、用户使用本网站必须遵守适用的法律法规,对于用户违法使用本站非法运营而引起的一切责任,由用户自行承担
6、本站所有资源来自互联网转载,版权归原著所有,用户访问和使用本站的条件是必须接受本站“免责声明”,如果不遵守,请勿访问或使用本网站
7、本站使用者因为违反本声明的规定而触犯中华人民共和国法律的,一切后果自己负责,本站不承担任何责任。
8、凡以任何方式登陆本网站或直接、间接使用本网站资料者,视为自愿接受本网站声明的约束。
9、本站以《2013 中华人民共和国计算机软件保护条例》第二章 “软件著作权” 第十七条为原则:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。若有学员需要商用本站资源,请务必联系版权方购买正版授权!
10、本网站如无意中侵犯了某个企业或个人的知识产权,请来信【站长信箱312337667@qq.com】告之,本站将立即删除。
郑重声明:
本站所有资源仅供用户本地电脑学习源代码的内含设计思想和原理,禁止任何其他用途!
本站所有资源、教程来自互联网转载,仅供学习交流,不得商业运营资源,不确保资源完整性,图片和资源仅供参考,不提供任何技术服务。
本站资源仅供本地编辑研究学习参考,禁止未经资源商正版授权参与任何商业行为,违法行为!如需商业请购买各资源商正版授权
本站仅收集资源,提供用户自学研究使用,本站不存在私自接受协助用户架设游戏或资源,非法运营资源行为。
 
在线客服
点击这里给我发消息 点击这里给我发消息 点击这里给我发消息
售前咨询热线
312337667

微信扫一扫,私享最新原创实用干货

QQ|免责声明|小黑屋|依星资源网 ( 鲁ICP备2021043233号-3 )|网站地图

GMT+8, 2025-6-7 01:18

Powered by Net188.com X3.4

邮箱:312337667@qq.com 客服QQ:312337667(工作时间:9:00~21:00)

快速回复 返回顶部 返回列表