当前位置:网站首页>14. Buffferevent timeout event processing
14. Buffferevent timeout event processing
2022-04-22 04:38:00 【doru_ cpp】
test.cpp
#include <iostream>
#include <event2/event.h>
#include <event2/thread.h>
#include<event2/listener.h>
#include <event2/bufferevent.h>
#include <errno.h>
#include <string.h>
#ifndef _WIN32
#include <signal.h>
#else
#endif
using namespace std;
// error , Overtime ( The connection is disconnected and the meeting enters )
void event_cb(bufferevent* be,short events, void* arg)
{
cout << "[E]" << flush;
// After the read timeout event , Data stop reading
if (events & BEV_EVENT_TIMEOUT && events & BEV_EVENT_READING)
{
cout << "BEV_EVENT_READING BEV_EVENT_TIMEOUT" << endl;
//bufferevent_enable(be, EV_READ);
bufferevent_free(be);
}
else if (events & BEV_EVENT_ERROR)
{
bufferevent_free(be);
}
else
{
cout << "OTHERS" << endl;
}
}
void write_cb(bufferevent* be, void* arg)
{
cout << "[W]" << flush;
}
void read_cb(bufferevent* be, void* arg)
{
cout << "[R]" << flush;
char data[1024] = { 0 };
// Read input buffer data
int len = bufferevent_read(be, data, sizeof(data) - 1);
cout << "[" << data << "]" << endl;
if (len <= 0) return;
if (strstr(data, "quit") != NULL)
{
cout << "quit";
// Exit and close socket BEV_OPT_CLOSE_ON_FREE
bufferevent_free(be);
}
// send data , Write to output buffer
bufferevent_write(be, "ok", 3);
}
void listen_cb(evconnlistener* ev, evutil_socket_t s, sockaddr* sin, int slen, void* arg)
{
cout << "listen_cb" << endl;
event_base* base = (event_base*)arg;
// establish bufferevent Context BEV_OPT_CLOSE_ON_FREE, clear bufferevent Turn off socket
bufferevent *bev=bufferevent_socket_new(base, s, BEV_OPT_CLOSE_ON_FREE);
// Add monitoring events
bufferevent_enable(bev, EV_READ|EV_WRITE);
// Set the water level
// Read water level
bufferevent_setwatermark(bev, EV_READ,
5, // Low water level ,0 Is unlimited , The default is 0
10); // High water level ,0 Is unlimited , The default is 0
bufferevent_setwatermark(bev, EV_WRITE,
5, // Low water level ,0 Is unlimited , The default is 0, Buffered data is lower than 5 The write callback is called
0); // Invalid high water level
// The setting of timeout time
timeval t1 = { 3,0 };
bufferevent_set_timeouts(bev, &t1,0);
// Set the callback function
bufferevent_setcb(bev, read_cb, write_cb, event_cb, base);
}
int main(int argc, char** argv)
{
#ifdef _WIN32
// initialization socket library
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
#else
// Ignore the pipe signal , Send data to the closed socket
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
return 1;
#endif
event_base* base = event_base_new();
// Create a web server
sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(6969);
evconnlistener* ev = evconnlistener_new_bind(base,
listen_cb, // Callback function
base, // The parameters of the callback function arg
LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE,
10, //listen back
(sockaddr*)&sin,
sizeof(sin)
);
event_base_dispatch(base);
evconnlistener_free(ev);
event_base_free(base);
return 0;
}
linux Next makefile
test:test.cpp
g++ $^ -o $@ -levent
./$@
clean:
rm -rf test
rm -rf *.o
版权声明
本文为[doru_ cpp]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220432307524.html
边栏推荐
- [concurrent programming 046] for the synchronization method, how does the processor realize atomic operation?
- [concurrent programming 043] how to solve the problems of CAS and ABA?
- 13.bufferevent接受和发送数据
- Redis 的过期数据会被立马删除么?
- 论文阅读 (48):A Library of Optimization Algorithms for Organizational Design
- Non reentrant information about gethostbyname()
- pycharm+anaconda安装包
- Replace vscode interpreter
- L1-051 discount (5 points)
- 11. Libevent tests horizontal trigger and edge trigger
猜你喜欢
随机推荐
14.buffferevent超时事件处理
爬演员名字加链接
spark 安装与使用 educoder
CommDGI: Community detection oriented deep graph infomax 2020 CIKM
L1-046 divide singles (20 points)
二叉树的层序遍历
The website is linked to the gambling dark chain
Binary search for qualified values and local minimum values
Torch file saving and loading -- [torch learning notes]
L1-050 penultimate string (15 points)
毕设-SSM校园二手书籍销售系统+论文
线程池状态+ThreadPoolExecutor
Cisco packet tracker experiment set
Training summary report
2022年山东省安全员C证考试题及在线模拟考试
Will the expired data of redis be deleted immediately?
Tensorfloweager's view is turned off and on
jsp hello world中文乱码
How expensive is the "salary" of software testing industry?
C entry - check the ID number with regular expressions.









