#include <iostream>
#include <event2/event.h>
#include <thread>
#include <event2/listener.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#ifndef _WIN32
#include <signal.h>
#endif
#include <errno.h>
#include <string.h>
#define SPORT 5001
using namespace std;
bufferevent_filter_result filter_in(evbuffer* s, evbuffer* d, ev_ssize_t limit, bufferevent_flush_mode mode, void* ctx) {
cout << "filter_in" << endl;
//读取并清理源数据
char data[1024] = { 0 };
int len = evbuffer_remove(s, data, sizeof(data) - 1);
//把所有字母转成大小
for (size_t i = 0; i < len; i++)
{
data[i] = toupper(data[i]);
}
evbuffer_add(d, data, len);
return BEV_OK;
}
bufferevent_filter_result filter_out(evbuffer* s, evbuffer* d, ev_ssize_t limit, bufferevent_flush_mode mode, void* ctx) {
cout << "filter_out" << endl;
//读取并清理源数据
char data[1024] = { 0 };
int len = evbuffer_remove(s, data, sizeof(data) - 1);
//将回复给客户端的消息进行预处理
string str = "";
str += "=========\n";
str += data;
str += "=========\n";
evbuffer_add(d, 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(evconnlistener* e, evutil_socket_t s, sockaddr *a, int socklen, void* arg) {
cout << "====listen_cb======" << endl;
event_base* base = (event_base*)arg;
//创建bufferevent 绑定bufferevent filter
bufferevent* bev = bufferevent_socket_new(base, s, BEV_OPT_CLOSE_ON_FREE);
bufferevent* bev_filter = bufferevent_filter_new(
bev, // bufferevent_socket上下文
filter_in, // 输入过滤函数
filter_out, // 输出过滤函数
BEV_OPT_CLOSE_ON_FREE, // 关闭filter同时关闭bufferevent
0, // 清理的回调函数
0 // 传递给清理回调函数的参数
);
//设置bufferevent的回调函数
bufferevent_setcb(bev_filter, read_cb, write_cb, event_cb, NULL);
bufferevent_enable(bev_filter, EV_READ | EV_WRITE);
}
int main(int argc, char** argv) {
#if _WIN32
//windowns 初始化socket库
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
#else
//linux 忽略管道信号,发送数据给已关闭的socket
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
return 1;
#endif
//创建socket(使用tcp/ip协议)
event_base* base = event_base_new();
if (base)
{
cout << "event_base_new success" << endl;
}
// 绑定端口与地址
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, // 回调函数的参数arg
LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE, // 地址重用,关闭evconnlistener同时关闭socket
10, // 连接队列大小,对应listen函数
(sockaddr*)&sin, sizeof(sin) // 绑定端口与地址
);
//进入事件主循环
event_base_dispatch(base);
event_base_free(base);
#ifdef _WIN32
WSACleanup();
#endif // _WIN32
return 0;
}
Makefile
test_buffer_filter:test_buffer_filter.cpp
g++ $^ -o $@ -levent
./$@
clean:
rm -rf test_buffer_filter
rm -rf *.o
服务端与客户端