|
@ -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); |
|
|
|
|
|
|
|
|