-
Notifications
You must be signed in to change notification settings - Fork 245
/
Copy pathsend_file_client.cpp
144 lines (116 loc) · 3.79 KB
/
send_file_client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <string>
#include <fstream>
#include <string.h> // for memset
#include <iostream>
#include <functional>
#include "md5.h"
#include "common.h"
#include "include/cppnet.h"
#include "common/util/time.h"
using namespace cppnet;
class SendFile {
public:
SendFile(const std::string& file, cppnet::CppNet* net):
_block(false),
_status(hello),
_file_name(file),
_net(net) {
}
~SendFile() {
_file.close();
}
void OnWrite(Handle handle, uint32_t len) {
if (_block) {
_block = false;
Send(handle);
}
}
void OnRecv(Handle handle, std::shared_ptr<cppnet::Buffer> data, uint32_t len) {
char ret_char[4] = {0};
if (data->GetCanReadLength() >= 2) {
data->Read(ret_char, 4);
} else {
std::cout << "3" << std::endl;
return;
}
std::cout << "4" << std::endl;
std::string ret(ret_char);
std::cout << "recv from server : " << ret << std::endl;
if (_status == hello) {
if (ret == "OK") {
std::cout << "start to send file ..." << std::endl;
_status = sending;
Send(handle);
} else {
std::cout << "server refuse recv the file!" << std::endl;
}
} else if (_status == sending) {
if (ret == "OK") {
std::cout << "send file success!" << std::endl;
} else {
std::cout << "something error while sending!" << std::endl;
}
_net->Destory();
}
}
void OnConnect(Handle handle, uint32_t err) {
if (err == CEC_SUCCESS) {
std::cout << "start to header ..." << std::endl;
GetFileHeader();
std::cout << "get file name : " << _header._name << std::endl;
std::cout << "get file length : " << _header._length << std::endl;
std::cout << "get file md5 : " << _header._md5 << std::endl;
handle->Write((char*)&_header, sizeof(_header));
} else {
std::cout << "connect to server failed." << std::endl;
}
}
private:
bool GetFileHeader() {
_file.open(_file_name, std::ios::binary | std::ios::in);
if (!_file.good()) {
return false;
}
sprintf(_header._name, "%s", _file_name.c_str());
_file.seekg(0, _file.end);
_header._length = (int)_file.tellg();
_file.seekg(0, _file.beg);
Compute_file_md5(_file_name.c_str(), _header._md5);
return true;
}
void Send(const Handle& handle) {
char buf[__read_len];
while (!_file.eof()) {
_file.read(buf, __read_len);
int len = (int)_file.gcount();
if (!handle->Write(buf, len)) {
_block = true;
return;
}
}
}
private:
std::fstream _file;
FileHeader _header;
STATUS _status;
std::string _file_name;
bool _block;
cppnet::CppNet* _net;
};
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cout << "must input a file." << std::endl;
return 1;
}
std::string file_name = argv[1];
cppnet::CppNet* net(new cppnet::CppNet());
SendFile file(file_name, net);
net->Init(1);
net->SetConnectionCallback(std::bind(&SendFile::OnConnect, &file, std::placeholders::_1, std::placeholders::_2));
net->SetReadCallback(std::bind(&SendFile::OnRecv, &file, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3));
net->SetWriteCallback(std::bind(&SendFile::OnWrite, &file, std::placeholders::_1, std::placeholders::_2));
net->Connection("127.0.0.1", 8921);
net->Join();
return 0;
}