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

Function ReadFunc

test/sendfile/SendFileServer.cpp:39–98  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

37}
38
39void ReadFunc(const Handle& handle, base::CBuffer* data, uint32_t len, uint32_t error) {
40 if (error != CEC_SUCCESS) {
41 std::cout << "something err while read : " << error << std::endl;
42
43 } else {
44 auto iter = _recv_map.find(handle);
45 if (iter == _recv_map.end()) {
46 std::cout << "can't find handle" << std::endl;
47 return;
48 }
49 if (iter->second._status == hello) {
50 if (data->GetCanReadLength() < __header_len) {
51 std::cout << "continue recv ... " << std::endl;
52 return;
53 }
54
55 data->Read((char*)&(iter->second._head), __header_len);
56
57 strncat(iter->second._head._name, ".bk", sizeof(".bk"));
58
59 iter->second._file = new std::fstream(iter->second._head._name, std::ios::out | std::ios::binary);
60
61 std::cout << "get file name : " << iter->second._head._name << std::endl;
62 std::cout << "get file length : " << iter->second._head._length << std::endl;
63 std::cout << "get file md5 : " << iter->second._head._md5 << std::endl;
64 if (*(iter->second._file)) {
65 handle->Write("OK", strlen("OK"));
66 std::cout << "start to recv. " << std::endl;
67 iter->second._status = sending;
68
69 } else {
70 handle->Write("NO", strlen("NO"));
71 std::cout << "refuse to recv. " << std::endl;
72 }
73
74 } else if (iter->second._status == sending) {
75 while (data->GetCanReadLength() > 0) {
76 char buf[__read_len];
77 int len = data->Read(buf, __read_len);
78 iter->second._head._length -= len;
79 iter->second._file->write(buf, len);
80 if (iter->second._head._length <= 0) {
81 std::cout << "recv end. " << std::endl;
82 iter->second._file->sync();
83 iter->second._file->close();
84 char md5_buf[128] = { 0 };
85 Compute_file_md5(iter->second._head._name, md5_buf);
86 if (strcmp(md5_buf, iter->second._head._md5) == 0) {
87 handle->Write("OK", strlen("OK"));
88 std::cout << "recv ok. " << std::endl;
89
90 } else {
91 handle->Write("NO", strlen("NO"));
92 std::cout << "recv failed. " << std::endl;
93 }
94 }
95 }
96 }

Callers

nothing calls this directly

Calls 4

Compute_file_md5Function · 0.85
GetCanReadLengthMethod · 0.45
ReadMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected