forked from caozhiyi/CppNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_file_server.cpp
120 lines (100 loc) · 3.48 KB
/
send_file_server.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
#include <map>
#include <string>
#include <fstream>
#include <string.h>
#include <iostream>
#include <string.h> // for strlen
#include "md5.h"
#include "common.h"
#include "include/cppnet.h"
using namespace cppnet;
struct Content {
FileHeader _head;
STATUS _status;
Content() {
_status = hello;
_file = nullptr;
}
~Content() {
if (_file) {
delete _file;
}
}
std::fstream* _file;
};
std::map<Handle, Content> _recv_map;
void WriteFunc(Handle handle, uint32_t len) {
//std::cout << "something err while write : " << std::endl;
}
void ReadFunc(Handle handle, std::shared_ptr<cppnet::Buffer> data, uint32_t len) {
auto iter = _recv_map.find(handle);
if (iter == _recv_map.end()) {
std::cout << "can't find handle" << std::endl;
return;
}
if (iter->second._status == hello) {
if (data->GetCanReadLength() < __header_len) {
std::cout << "continue recv ... " << std::endl;
return;
}
data->Read((char*)&(iter->second._head), __header_len);
strncat(iter->second._head._name, ".bk", sizeof(".bk"));
iter->second._file = new std::fstream(iter->second._head._name, std::ios::out | std::ios::binary);
std::cout << "get file name : " << iter->second._head._name << std::endl;
std::cout << "get file length : " << iter->second._head._length << std::endl;
std::cout << "get file md5 : " << iter->second._head._md5 << std::endl;
if (*(iter->second._file)) {
handle->Write("OK", (uint32_t)strlen("OK"));
std::cout << "start to recv. " << std::endl;
iter->second._status = sending;
} else {
handle->Write("NO", (uint32_t)strlen("NO"));
std::cout << "refuse to recv. " << std::endl;
}
} else if (iter->second._status == sending) {
while (data->GetCanReadLength() > 0) {
char buf[__read_len];
int len = data->Read(buf, __read_len);
iter->second._head._length -= len;
iter->second._file->write(buf, len);
if (iter->second._head._length <= 0) {
std::cout << "recv end. " << std::endl;
iter->second._file->sync();
iter->second._file->close();
char md5_buf[128] = { 0 };
Compute_file_md5(iter->second._head._name, md5_buf);
if (strcmp(md5_buf, iter->second._head._md5) == 0) {
handle->Write("OK", (uint32_t)strlen("OK"));
std::cout << "recv ok. " << std::endl;
} else {
handle->Write("NO", (uint32_t)strlen("NO"));
std::cout << "recv failed. " << std::endl;
}
}
}
}
}
void ConnectFunc(Handle handle, uint32_t error) {
if (error != CEC_SUCCESS) {
std::cout << "something err while connect : " << error << std::endl;
}
if (_recv_map.find(handle) == _recv_map.end()) {
_recv_map[handle] = Content();
}
}
void DisConnectFunc(Handle handle, uint32_t error) {
auto iter = _recv_map.find(handle);
if (iter != _recv_map.end()) {
_recv_map.erase(iter);
}
}
int main() {
cppnet::CppNet net;
net.Init(1);
net.SetAcceptCallback(ConnectFunc);
net.SetWriteCallback(WriteFunc);
net.SetReadCallback(ReadFunc);
net.SetDisconnectionCallback(DisConnectFunc);
net.ListenAndAccept("0.0.0.0", 8921);
net.Join();
}