Browse Source

上传文件至 'test_bufffer_filter_zlib'

更新
master
adminPyf 3 years ago
parent
commit
4e97ca4b9a
3 changed files with 49 additions and 49 deletions
  1. +1
    -1
      test_bufffer_filter_zlib/Makefile
  2. +31
    -31
      test_bufffer_filter_zlib/zlib_client.cpp
  3. +17
    -17
      test_bufffer_filter_zlib/zlib_server.cpp

+ 1
- 1
test_bufffer_filter_zlib/Makefile View File

@ -1,2 +1,2 @@
test_bufffer_filter: test_bufffer_filter.cpp
test_bufffer_filter_zlib: test_bufffer_filter_zlib.cpp zlib_client.cpp zlib_server.cpp
g++ $^ -o $@ -levent g++ $^ -o $@ -levent

+ 31
- 31
test_bufffer_filter_zlib/zlib_client.cpp View File

@ -39,51 +39,51 @@ bufferevent_filter_result clinet_filter_out(struct evbuffer *src, struct evbuffe
evbuffer_add(dst, data, len); evbuffer_add(dst, data, len);
return BEV_OK; return BEV_OK;
} }
//开始压缩文件
//1.取出buffer中的数据的引用
//寮€濮嬪帇缂╂枃浠�
//1.鍙栧嚭buffer涓�殑鏁版嵁鐨勫紩鐢�
evbuffer_iovec v_in[1] = {0}; evbuffer_iovec v_in[1] = {0};
int n = evbuffer_peek(src, -1, 0, v_in, 1); int n = evbuffer_peek(src, -1, 0, v_in, 1);
if (n <= 0) { if (n <= 0) {
//没有数据
//娌℃湁鏁版嵁
return BEV_NEED_MORE; return BEV_NEED_MORE;
} }
z_stream *p = clientStatus->z_output; z_stream *p = clientStatus->z_output;
if (!p) { if (!p) {
return BEV_ERROR; return BEV_ERROR;
} }
//输入数据大小
//杈撳叆鏁版嵁澶у皬
p->avail_in = v_in[0].iov_len; p->avail_in = v_in[0].iov_len;
//输入数据地址
//杈撳叆鏁版嵁鍦板潃
p->next_in = (Byte*)v_in[0].iov_base; p->next_in = (Byte*)v_in[0].iov_base;
//申请输出空间大小
//鐢宠�杈撳嚭绌洪棿澶у皬
evbuffer_iovec v_out[1] = { 0 }; evbuffer_iovec v_out[1] = { 0 };
n = evbuffer_reserve_space(dst, 4096, v_out, 1); n = evbuffer_reserve_space(dst, 4096, v_out, 1);
if (n < 0) if (n < 0)
return BEV_ERROR; return BEV_ERROR;
//zlib输出空间大小
//zlib杈撳嚭绌洪棿澶у皬
p->avail_out = v_out[0].iov_len; p->avail_out = v_out[0].iov_len;
//zlib输出空间地址
//zlib杈撳嚭绌洪棿鍦板潃
p->next_out = (Byte*)v_out[0].iov_base; p->next_out = (Byte*)v_out[0].iov_base;
//zlib 压缩
//zlib 鍘嬬缉
int re = deflate(p, Z_SYNC_FLUSH); int re = deflate(p, Z_SYNC_FLUSH);
if (re != Z_OK) { if (re != Z_OK) {
cerr << "deflate falid" << endl; cerr << "deflate falid" << endl;
} }
//压缩用了多少数据
//p->avail_in 未处理数据大小
//鍘嬬缉鐢ㄤ簡澶氬皯鏁版嵁
//p->avail_in 鏈��鐞嗘暟鎹�ぇ灏�
int nread = v_in[0].iov_len - p->avail_in; int nread = v_in[0].iov_len - p->avail_in;
//压缩后数据大小
//v_out[0].iov_len 剩余空间大小
//鍘嬬缉鍚庢暟鎹�ぇ灏�
//v_out[0].iov_len 鍓╀綑绌洪棿澶у皬
int nwrite = v_out[0].iov_len - p->avail_out; int nwrite = v_out[0].iov_len - p->avail_out;
//移除 source evbuffer中数据
//绉婚櫎 source evbuffer涓�暟鎹�
evbuffer_drain(src, nread); evbuffer_drain(src, nread);
//传入des evbuffer
//浼犲叆des evbuffer
v_out[0].iov_len = nwrite; v_out[0].iov_len = nwrite;
evbuffer_commit_space(dst, v_out, 1); evbuffer_commit_space(dst, v_out, 1);
cout << "nread = " << nread << " nwirte=" << nwrite << endl; cout << "nread = " << nread << " nwirte=" << nwrite << endl;
@ -91,13 +91,13 @@ bufferevent_filter_result clinet_filter_out(struct evbuffer *src, struct evbuffe
} }
void clinet_read_cb(bufferevent *bev, void *arg) { void clinet_read_cb(bufferevent *bev, void *arg) {
ClientStatus *clientStatus = (ClientStatus *)arg; ClientStatus *clientStatus = (ClientStatus *)arg;
//002 接收服务端发送的OK消息
//002 鎺ユ敹鏈嶅姟绔�彂閫佺殑OK娑堟伅
char data[1024] = { 0 }; char data[1024] = { 0 };
int len = bufferevent_read(bev, data, sizeof(data) - 1); int len = bufferevent_read(bev, data, sizeof(data) - 1);
if (strcmp(data, "ok") == 0) { if (strcmp(data, "ok") == 0) {
cout << data << endl; cout << data << endl;
clientStatus->startSend = true; clientStatus->startSend = true;
//开始发送文件
//寮€濮嬪彂閫佹枃浠�
bufferevent_trigger(bev, EV_WRITE, 0); bufferevent_trigger(bev, EV_WRITE, 0);
} }
else { else {
@ -110,10 +110,10 @@ void clinet_read_cb(bufferevent *bev, void *arg) {
void clinet_write_cb(bufferevent *bev, void *arg) { void clinet_write_cb(bufferevent *bev, void *arg) {
ClientStatus *clientStatus = (ClientStatus *)arg; ClientStatus *clientStatus = (ClientStatus *)arg;
FILE *fp = clientStatus->fp; FILE *fp = clientStatus->fp;
//判断什么时候清理资源
//鍒ゆ柇浠€涔堟椂鍊欐竻鐞嗚祫婧�
if (clientStatus->end) { if (clientStatus->end) {
//判断缓冲是否有数据 如果有就刷新
//获取过滤器绑定的buffer
//鍒ゆ柇缂撳啿鏄�惁鏈夋暟鎹� 濡傛灉鏈夊氨鍒锋柊
//鑾峰彇杩囨护鍣ㄧ粦瀹氱殑buffer
bufferevent * be = bufferevent_get_underlying(bev); bufferevent * be = bufferevent_get_underlying(bev);
evbuffer *evb = bufferevent_get_output(be); evbuffer *evb = bufferevent_get_output(be);
int len = evbuffer_get_length(evb); int len = evbuffer_get_length(evb);
@ -127,7 +127,7 @@ void clinet_write_cb(bufferevent *bev, void *arg) {
return; return;
} }
if (!fp) { if (!fp) {
cout << "open file " << FILEPATH << "faild" << endl;
cout << "open file " << FILEPATH << "faild锛�" << endl;
return; return;
} }
char data[1024] = { 0 }; char data[1024] = { 0 };
@ -148,26 +148,26 @@ void client_event_cb(struct bufferevent *bev, short what, void *ctx) {
cout << "client_event_cb" << what << endl; cout << "client_event_cb" << what << endl;
if (what & BEV_EVENT_CONNECTED) { if (what & BEV_EVENT_CONNECTED) {
cout << "BEV_EVENT_CONNECTED" << endl; cout << "BEV_EVENT_CONNECTED" << endl;
//001 发送文件名
//001 鍙戦€佹枃浠跺悕
bufferevent_write(bev, FILEPATH,strlen(FILEPATH)); bufferevent_write(bev, FILEPATH,strlen(FILEPATH));
FILE *fp = fopen(FILEPATH, "rb"); FILE *fp = fopen(FILEPATH, "rb");
if (!fp) { if (!fp) {
cout << "open file " << FILEPATH << "faild" << endl;
cout << "open file " << FILEPATH << "faild锛�" << endl;
} }
ClientStatus *clientStatus = new ClientStatus; ClientStatus *clientStatus = new ClientStatus;
clientStatus->fp = fp; clientStatus->fp = fp;
//初始化zlib上下文
//鍒濆�鍖杬lib涓婁笅鏂�
clientStatus->z_output = new z_stream(); clientStatus->z_output = new z_stream();
deflateInit(clientStatus->z_output, Z_DEFAULT_COMPRESSION); deflateInit(clientStatus->z_output, Z_DEFAULT_COMPRESSION);
//创建输出过滤
//鍒涘缓杈撳嚭杩囨护
bufferevent *bev_filter = bufferevent_filter_new(bev, bufferevent *bev_filter = bufferevent_filter_new(bev,
0,//输入过滤函数
clinet_filter_out, //输出过滤
BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS, //关闭filter同时关闭bufferevent 延时调用
0, //清理回调
0,//杈撳叆杩囨护鍑芥暟
clinet_filter_out, //杈撳嚭杩囨护
BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS, //鍏抽棴filter鍚屾椂鍏抽棴bufferevent 寤舵椂璋冪敤
0, //娓呯悊鍥炶皟
clientStatus); clientStatus);
@ -177,7 +177,7 @@ void client_event_cb(struct bufferevent *bev, short what, void *ctx) {
} }
void Client(event_base* base) { void Client(event_base* base) {
cout << "begin Client" << endl; cout << "begin Client" << endl;
//连接服务器
//杩炴帴鏈嶅姟鍣�
sockaddr_in sin; sockaddr_in sin;
memset(&sin, 0, sizeof(sin)); memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
@ -185,7 +185,7 @@ void Client(event_base* base) {
evutil_inet_pton(AF_INET,"127.0.0.1",&sin.sin_addr.s_addr); evutil_inet_pton(AF_INET,"127.0.0.1",&sin.sin_addr.s_addr);
bufferevent* bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE); bufferevent* bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
//只绑定连接事件回调,用于确认连接成功
//鍙�粦瀹氳繛鎺ヤ簨浠跺洖璋冿紝鐢ㄤ簬纭��杩炴帴鎴愬姛
bufferevent_enable(bev, EV_READ | EV_WRITE); bufferevent_enable(bev, EV_READ | EV_WRITE);
bufferevent_setcb(bev, 0, 0, client_event_cb, 0); bufferevent_setcb(bev, 0, 0, client_event_cb, 0);


+ 17
- 17
test_bufffer_filter_zlib/zlib_server.cpp View File

@ -25,7 +25,7 @@ bufferevent_filter_result server_filter_in(struct evbuffer *src, struct evbuffer
{ {
cout << "server_filter_in" << endl; cout << "server_filter_in" << endl;
//1.接收客户端发送的文件名
//1.鎺ユ敹瀹㈡埛绔�彂閫佺殑鏂囦欢鍚�
char data[1024] = { 0 }; char data[1024] = { 0 };
int len = evbuffer_remove(src, data, sizeof(data) - 1); int len = evbuffer_remove(src, data, sizeof(data) - 1);
evbuffer_add(dst, data, len); evbuffer_add(dst, data, len);
@ -42,7 +42,7 @@ void server_read_cb(bufferevent *bev, void *arg) {
out += data; out += data;
status->fp = fopen(out.c_str(), "wb"); status->fp = fopen(out.c_str(), "wb");
if (!status->fp) { if (!status->fp) {
cout << "open file " << out << "faild" << endl;
cout << "open file " << out << "faild锛�" << endl;
return; return;
} }
@ -51,7 +51,7 @@ void server_read_cb(bufferevent *bev, void *arg) {
return; return;
} }
//写入文件
//鍐欏叆鏂囦欢
do { do {
char data[1024] = { 0 }; char data[1024] = { 0 };
int len = bufferevent_read(bev, data, sizeof(data) - 1); int len = bufferevent_read(bev, data, sizeof(data) - 1);
@ -80,17 +80,17 @@ void server_listen_cb(struct evconnlistener *e, evutil_socket_t s, struct sockad
{ {
std::cout << "server_listen_cb" << std::endl; std::cout << "server_listen_cb" << std::endl;
event_base *base = (event_base *)arg; event_base *base = (event_base *)arg;
// 1.创建bufferevent 用于通信
// 1.鍒涘缓bufferevent 鐢ㄤ簬閫氫俊
bufferevent *bev = bufferevent_socket_new(base, s, BEV_OPT_CLOSE_ON_FREE); bufferevent *bev = bufferevent_socket_new(base, s, BEV_OPT_CLOSE_ON_FREE);
// 2.添加过滤 并设置输入回调
// 2.娣诲姞杩囨护 骞惰�缃�緭鍏ュ洖璋�
Status *status = new Status; Status *status = new Status;
bufferevent *bev_filter = bufferevent_filter_new(bev, bufferevent *bev_filter = bufferevent_filter_new(bev,
server_filter_in,//输入过滤函数
0, //输出过滤
BEV_OPT_CLOSE_ON_FREE, //关闭filter同时关闭bufferevent
0, //清理回调
status); //传递参数
// 3.设置回调 读取 事件(处理连接断开)
server_filter_in,//杈撳叆杩囨护鍑芥暟
0, //杈撳嚭杩囨护
BEV_OPT_CLOSE_ON_FREE, //鍏抽棴filter鍚屾椂鍏抽棴bufferevent
0, //娓呯悊鍥炶皟
status); //浼犻€掑弬鏁�
// 3.璁剧疆鍥炶皟 璇诲彇 浜嬩欢锛堝�鐞嗚繛鎺ユ柇寮€锛�
bufferevent_setcb(bev_filter, server_read_cb, 0, server_event_cb, status); bufferevent_setcb(bev_filter, server_read_cb, 0, server_event_cb, status);
bufferevent_enable(bev_filter, EV_READ | EV_WRITE); bufferevent_enable(bev_filter, EV_READ | EV_WRITE);
} }
@ -104,11 +104,11 @@ void Server(event_base* base) {
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(SPORT); sin.sin_port = htons(SPORT);
evconnlistener *ev = evconnlistener_new_bind(base, //libevent上下文
server_listen_cb, //接收到连接的回调函数
base, //回调函数获取的参数(根据业务来)
LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE, //地址重用,evconnlistener关闭同时关闭socket
10, //连接队列大小,对应listen函数参数
(sockaddr *)&sin, //绑定的地址和端口
evconnlistener *ev = evconnlistener_new_bind(base, //libevent涓婁笅鏂�
server_listen_cb, //鎺ユ敹鍒拌繛鎺ョ殑鍥炶皟鍑芥暟
base, //鍥炶皟鍑芥暟鑾峰彇鐨勫弬鏁�(鏍规嵁涓氬姟鏉�)
LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE, //鍦板潃閲嶇敤锛宔vconnlistener鍏抽棴鍚屾椂鍏抽棴socket
10, //杩炴帴闃熷垪澶у皬锛屽�搴攍isten鍑芥暟鍙傛暟
(sockaddr *)&sin, //缁戝畾鐨勫湴鍧€鍜岀�鍙�
sizeof(sin)); sizeof(sin));
} }

Loading…
Cancel
Save