From e58b288826352ff3d8e0ca1f4f1cf5db59214253 Mon Sep 17 00:00:00 2001 From: adminPyf <3043130461@qq.com> Date: Mon, 14 Jun 2021 17:03:06 +0800 Subject: [PATCH] =?UTF-8?q?bufferlievent=20=E8=BF=87=E6=BB=A4=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test_bufffer_filter/Makefile | 2 + test_bufffer_filter/first_libevent.vcxproj | 162 ++++++++++++++++++++ test_bufffer_filter/test_bufffer_filter.cpp | 155 +++++++++++++++++++ test_bufffer_filter/test_bufffer_filter.sln | 31 ++++ 4 files changed, 350 insertions(+) create mode 100644 test_bufffer_filter/Makefile create mode 100644 test_bufffer_filter/first_libevent.vcxproj create mode 100644 test_bufffer_filter/test_bufffer_filter.cpp create mode 100644 test_bufffer_filter/test_bufffer_filter.sln diff --git a/test_bufffer_filter/Makefile b/test_bufffer_filter/Makefile new file mode 100644 index 0000000..19021e7 --- /dev/null +++ b/test_bufffer_filter/Makefile @@ -0,0 +1,2 @@ +test_bufffer_filter: test_bufffer_filter.cpp + g++ $^ -o $@ -levent \ No newline at end of file diff --git a/test_bufffer_filter/first_libevent.vcxproj b/test_bufffer_filter/first_libevent.vcxproj new file mode 100644 index 0000000..02c91e8 --- /dev/null +++ b/test_bufffer_filter/first_libevent.vcxproj @@ -0,0 +1,162 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {129BF326-BFEE-4F1C-920C-0C302BB84895} + Win32Proj + firstlibevent + 10.0.17763.0 + test_bufffer_filter + + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + Disabled + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + NotUsing + Level3 + Disabled + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ..\..\include + + + Console + true + ..\..\lib + libevent.lib;ws2_32.lib;%(AdditionalDependencies) + + + + + + + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + Level3 + MaxSpeed + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/test_bufffer_filter/test_bufffer_filter.cpp b/test_bufffer_filter/test_bufffer_filter.cpp new file mode 100644 index 0000000..fdb8893 --- /dev/null +++ b/test_bufffer_filter/test_bufffer_filter.cpp @@ -0,0 +1,155 @@ + +#include +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 + +#else +#include +#endif // !_WIN32 + +using namespace std; + +#define SPORT 5001 + +bufferevent_filter_result filter_in(struct evbuffer *src, struct evbuffer *dst, ev_ssize_t dst_limit, + enum bufferevent_flush_mode mode, void *ctx) +{ + cout << "filter_in" << endl; + //读取并清理原数据 + char data[1024] = {0}; + int len = evbuffer_remove(src,data,sizeof(data)-1); + + //将所有转为大写 + for(int i=0;i < len ;i++){ + data[i] = toupper(data[i]); + } + + evbuffer_add(dst,data,len); + return BEV_OK; +} + +bufferevent_filter_result filter_out(struct evbuffer *src, struct evbuffer *dst, ev_ssize_t dst_limit, + enum bufferevent_flush_mode mode, void *ctx) +{ + cout << "filter_out" << endl; + + //读取并清理原数据 + char data[1024] = {0}; + int len = evbuffer_remove(src,data,sizeof(data)-1); + + string str = ""; + str += "==================\n"; + str += data; + str += "==================\n"; + + evbuffer_add(dst,str.c_str(),str.size()); + + return BEV_OK; +} + +void read_cb(bufferevent *bev ,void *arg){ + cout << "read_cb" << endl; + char data[1024] = {0}; + int len = bufferevent_read(bev,data,sizeof(data)-1); + cout << data << endl; + + //回复客户端消息,经过输出过滤器 + bufferevent_write(bev,data,len); + +} + +void write_cb(bufferevent *bev ,void *arg){ + cout << "write_cb" << endl; +} + +void event_cb(bufferevent *bev ,short events ,void *arg){ + cout << "event_cb" << endl; +} + +void listen_cb(struct evconnlistener *e, evutil_socket_t s, struct sockaddr *a, int socklen, void *arg) +{ + std::cout << "listen to be" << std::endl; + //创建bufferevent 绑定bufferevent filter + event_base *base = (event_base *)arg; + bufferevent *bev = bufferevent_socket_new(base, s, BEV_OPT_CLOSE_ON_FREE); + bufferevent *bev_filter = bufferevent_filter_new(bev, + filter_in, + filter_out, + BEV_OPT_CLOSE_ON_FREE, + 0, + 0); + + //设置bufferevent回调 + bufferevent_setcb(bev_filter,read_cb,write_cb,event_cb,NULL); + + bufferevent_enable(bev_filter,EV_READ|EV_WRITE); + + +} + +int main() +{ +#ifdef _WIN32 + //初始化socket库 + WSADATA wsa; + WSAStartup(MAKEWORD(2, 2), &wsa); +#else + if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) + { //忽略管道信号,发送数据给已关闭的socket,会飞掉! + return 1; + } +#endif + + //初始化libevent上下文 + event_config *conf = event_config_new(); + + //windows中支持IOCP(线程池) 要查看该项是否开启成功,可以在运行程序后打开任务管理器查看线程数量 +#ifdef _WIN32 + event_config_set_flag(conf, EVENT_BASE_FLAG_STARTUP_IOCP); + evthread_use_windows_threads(); + //设置cpu数量 + SYSTEM_INFO si; + GetSystemInfo(&si); + event_config_set_num_cpus_hint(conf, si.dwNumberOfProcessors); +#endif + + //初始化配置上下文 + event_base *base = event_base_new_with_config(conf); + + if (!base) + { + cout << "event_base_new_with_config failed!" << endl; + base = event_base_new(); + if (!base) + { + cerr << "event_base_new failed" << endl; + return 0; + } + } + else + { + sockaddr_in sin; + memset(&sin, 0, sizeof(sin)); + sin.sin_family = AF_INET; + sin.sin_port = htons(SPORT); + + evconnlistener *ev = evconnlistener_new_bind(base, //libevent上下文 + listen_cb, //接收到连接的回调函数 + base, //回调函数获取的参数(根据业务来) + LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE, //地址重用,evconnlistener关闭同时关闭socket + 10, //连接队列大小,对应listen函数参数 + (sockaddr *)&sin, //绑定的地址和端口 + sizeof(sin)); + event_base_dispatch(base); + event_base_free(base); + evconnlistener_free(ev); + + event_config_free(conf); + } + return 0; +} diff --git a/test_bufffer_filter/test_bufffer_filter.sln b/test_bufffer_filter/test_bufffer_filter.sln new file mode 100644 index 0000000..9343ad4 --- /dev/null +++ b/test_bufffer_filter/test_bufffer_filter.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.1525 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "first_libevent", "first_libevent.vcxproj", "{129BF326-BFEE-4F1C-920C-0C302BB84895}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Debug|x64.ActiveCfg = Debug|x64 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Debug|x64.Build.0 = Debug|x64 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Debug|x86.ActiveCfg = Debug|Win32 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Debug|x86.Build.0 = Debug|Win32 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Release|x64.ActiveCfg = Release|x64 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Release|x64.Build.0 = Release|x64 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Release|x86.ActiveCfg = Release|Win32 + {129BF326-BFEE-4F1C-920C-0C302BB84895}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {191D7F93-7554-493B-92E1-BB302C5A8383} + EndGlobalSection +EndGlobal