MCPcopy Create free account
hub / github.com/caozhiyi/CppNet / CSendFile

Class CSendFile

test/sendfile/SendFileClient.cpp:13–108  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11using namespace cppnet;
12
13class CSendFile {
14public:
15 CSendFile(const std::string& file, cppnet::CCppNet* net) : _status(hello),
16 _file_name(file), _net(net) {
17 }
18
19 ~CSendFile() {
20
21 }
22
23 void OnRecv(const Handle& handle, base::CBuffer* data, uint32_t len, uint32_t err) {
24 if (err != CEC_SUCCESS) {
25 std::cout << "something error while read." << std::endl;
26 return;
27 }
28
29 char ret_char[4] = {0};
30 if (data->GetCanReadLength() >= 2) {
31 data->Read(ret_char, 4);
32
33 } else {
34 return;
35 }
36 std::string ret(ret_char);
37 std::cout << "recv from server : " << ret << std::endl;
38
39 if (_status == hello) {
40 if (ret == "OK") {
41 std::cout << "start to send file ..." << std::endl;
42 _status = sending;
43 Send(handle);
44
45 } else {
46 std::cout << "server refuse recv the file!" << std::endl;
47 }
48
49 } else if (_status == sending) {
50 if (ret == "OK") {
51 std::cout << "send file success!" << std::endl;
52
53 } else {
54 std::cout << "something error while sending!" << std::endl;
55 }
56
57 handle->Close();
58 delete _net;
59 }
60 }
61
62 void OnConnect(const Handle& handle, uint32_t err) {
63 if (err == CEC_SUCCESS) {
64 std::cout << "start to header ..." << std::endl;
65 GetFileHeader();
66 std::cout << "get file name : " << _header._name << std::endl;
67 std::cout << "get file length : " << _header._length << std::endl;
68 std::cout << "get file md5 : " << _header._md5 << std::endl;
69 handle->Write((char*)&_header, sizeof(_header));
70

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected