• c++ libevent demo


    Server::Server(const char *ip, int port)
    {
    	//创建事件集合
    	base = event_base_new();
    
    	struct sockaddr_in server_addr;
    	memset(&server_addr, 0, sizeof(server_addr));
    	server_addr.sin_family = AF_INET;
    	server_addr.sin_port = htons(port);
    	server_addr.sin_addr.s_addr = inet_addr(ip);
    
    	//创建监听对象
    	listener = evconnlistener_new_bind(base, listener_cb, NULL, 
    		LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, 10, (struct sockaddr *)&server_addr, 
    			sizeof(server_addr));
    	if (NULL == listener)
    	{
    		cout << "evconnlistener_new_bind error" << endl;
    	}
    
    	cout << "服务器初始化成功 开始监听客户端" << endl;
    	event_base_dispatch(base);       //监听集合
    }
    
    //当有客户端发起连接的时候,会触发该函数
    void Server::listener_cb(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *addr, int socklen, void *arg)
    {
    	cout << "接受客户端的连接,fd = " << fd << endl;
    
    	//创建工作线程来处理该客户端
    	thread client_thread(client_handler, fd);
    	client_thread.detach();    //线程分离,当线程运行结束后,自动释放资源
    }
    
    void Server::client_handler(int fd)
    {
    	//创建集合
    	struct event_base *base = event_base_new();
    
    	//创建bufferevent对象
    	struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
    	if (NULL == bev)
    	{
    		cout << "bufferevent_socket_new error" << endl;
    	}
    
    	//给bufferevent设置回调函数
    	bufferevent_setcb(bev, read_cb, NULL, event_cb, NULL);
    
    	//使能回调函数
    	bufferevent_enable(bev, EV_READ);
    
    	event_base_dispatch(base);    //监听集合(监听客户端是否有数据发送过来)
    
    	event_base_free(base);
    	cout << "线程退出、释放集合" << endl;
    }
    
    void Server::read_cb(struct bufferevent *bev, void *ctx)
    {
    	char buf[1024] = {0};
    	int size = bufferevent_read(bev, buf, sizeof(buf));
    	if (size < 0)
    	{
    		cout << "bufferevent_read error" << endl;
    	}
    
    	cout << buf << endl;
    
    	Json::Reader reader;       //解析json对象
    	Json::FastWriter writer;   //封装json对象
    	Json::Value val;
    
    	if (!reader.parse(buf, val))    //把字符串转换成 json 对象
    	{
    		cout << "服务器解析数据失败" << endl;
    	}
    
    	string cmd = val["cmd"].asString();
    
    	if (cmd == "register")   //注册功能
    	{
    		server_register(bev, val);	
    	}
    	
    }
    
    void Server::event_cb(struct bufferevent *bev, short what, void *ctx)
    {
    	
    }
    
    Server::~Server()
    {
    	event_base_free(base);
    }
    
    void Server::server_register(struct bufferevent *bev, Json::Value val)
    {
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101

    推荐一个零声学院项目课,个人觉得老师讲得不错,分享给大家:
    零声白金学习卡(含基础架构/高性能存储/golang云原生/音视频/Linux内核)
    https://xxetb.xet.tech/s/3Zqhgt

  • 相关阅读:
    Python飞行棋游戏源代码,基于socket网络通信的小游戏,可设置多个游戏房间及参与飞行棋游戏的玩家
    wazuh运营使用
    性能测试之使用Jemeter对HTTP接口压测
    NextJs 与 Tailwind 入门开发笔记
    LeetCode: 2. 两数相加
    SICP:求值和环境模型(Python实现)
    10.4Cookie和Session
    【图形学】16 光照模型(一、理论与公式)
    第6章 Mybatis高级查询(详解篇)
    NodeJs 中 eval的替代函数
  • 原文地址:https://blog.csdn.net/qq_40135848/article/details/134230830