-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfc.cpp
372 lines (341 loc) · 9.41 KB
/
tfc.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "tfc.h"
#include <algorithm>
using namespace TRC;
using namespace TFC;
//判断是否是小端字节
bool IsLittleEndian()
{
unsigned short n = 1;
return((unsigned char*)&n)[0] ? true : false;
}
template<class T>
T ByteToInteger(const unsigned char* buff)
{
T num;
memcpy(&num, buff, sizeof(T));
if (IsLittleEndian())
{
unsigned char* h = (unsigned char*)#
std::reverse(h, h + sizeof(T));
}
return num;
}
template<class T>
void ToBigEndian(T n, unsigned char* buff)
{
memcpy(buff, &n, sizeof(T));
if (IsLittleEndian())
std::reverse(buff, buff + sizeof(T));
}
PacketHeader::PacketHeader()
{
memset(byPkgLen, 0, static_cast<int>(PacketSize::eFDFS_PROTO_PKG_LEN_SIZE));
}
//将数据包中的数据转换为64位整数
long long PacketHeader::PacketLength()const
{
return ByteToInteger<long long>(byPkgLen);
}
void PacketHeader::PacketLength(unsigned long long val)
{
ToBigEndian(val, byPkgLen);
}
UploadFileHeader::UploadFileHeader()
{
memset(Length, 0, static_cast<int>(PacketSize::eFDFS_PROTO_PKG_LEN_SIZE));
memset(Extension, 0, static_cast<int>(PacketSize::eFDFS_FILE_EXT_NAME_MAX_LEN));
byCmd = Command::eSTORAGE_PROTO_CMD_UPLOAD_FILE;
}
//设置文件长度
void UploadFileHeader::FileLenth(unsigned long long nLen)
{
ToBigEndian(nLen, Length);
}
//设置文件的扩展名
void UploadFileHeader::FileExtension(const char* ext)
{
if (!ext) return;
int n = strlen(ext);
memcpy(Extension, ext, min(n, static_cast<int>(PacketSize::eFDFS_FILE_EXT_NAME_MAX_LEN)));
}
//文件名为变长,独立写入
DeleteFileHeader::DeleteFileHeader()
{
memset(Group, 0, static_cast<int>(PacketSize::eFDFS_GROUP_NAME_MAX_LEN));
byCmd = Command::eSTORAGE_PROTO_CMD_DELETE_FILE;
}
//设置分组名称
void DeleteFileHeader::GroupName(const char* g)
{
if (!g)
return;
int nLen = strlen(g);
memcpy(Group, g, min(nLen, static_cast<int>(PacketSize::eFDFS_GROUP_NAME_MAX_LEN)));
}
//文件名为边长,独立写入
DownloadFileHeader::DownloadFileHeader()
{
memset(Length1, 0, static_cast<int>(PacketSize::eFDFS_PROTO_PKG_LEN_SIZE));
memset(Length2, 0, static_cast<int>(PacketSize::eFDFS_PROTO_PKG_LEN_SIZE));
memset(Group, 0, static_cast<int>(PacketSize::eFDFS_GROUP_NAME_MAX_LEN));
byCmd = Command::eSTORAGE_PROTO_CMD_DOWNLOAD_FILE;
}
void DownloadFileHeader::GroupName(const char* g)
{
if (!g)
return;
int nLen = strlen(g);
memcpy(Group, g, min(nLen, static_cast<int>(PacketSize::eFDFS_GROUP_NAME_MAX_LEN)));
}
StorageInfo::StorageInfo()
{
memset(Packet, 0, 40);
}
StorageInfo::StorageInfo(const StorageInfo& r)
{
memcpy(Packet, r.Packet, sizeof(Packet));
}
StorageInfo::StorageInfo(StorageInfo&& rr)
{
Swap(rr);
}
StorageInfo& StorageInfo::operator =(const StorageInfo& r)
{
memcpy(Packet, r.Packet, sizeof(Packet));
return *this;
}
StorageInfo& StorageInfo::operator =(StorageInfo&& r)
{
Swap(r);
return *this;
}
void StorageInfo::Swap(StorageInfo& r)
{
StorageInfo t = *this;
memcpy(Packet, r.Packet, sizeof(Packet));
memcpy(r.Packet, t.Packet, sizeof(Packet));
}
StorageInfo::operator bool()const
{
return strlen(GroupName()) > 0 && strlen(IPAddress()) > 0 && Port() > 0;
}
const char* StorageInfo::GroupName()const
{
return (const char*)Packet;
}
const char* StorageInfo::IPAddress()const
{
return (const char*)(Packet + static_cast<int>(PacketSize::eFDFS_GROUP_NAME_MAX_LEN));
}
unsigned int StorageInfo::Port()const
{
return ByteToInteger<unsigned long long>(Packet + static_cast<int>(PacketSize::eFDFS_GROUP_NAME_MAX_LEN) +
static_cast<int>(PacketSize::eIP_ADDRESS_SIZE) -1);
}
unsigned char StorageInfo::StorePathIndex()
{
return Packet[sizeof(Packet)-1];
}
TrackerServer::TrackerServer(const char* address, int port):TinySocketClient(address, port)
{
Connect();
}
StorageInfo TrackerServer::QueryStorageStore()
{
StorageInfo info;
PacketHeader header;
header.byCmd = Command::eTRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE;
int nSize = sizeof(PacketHeader);
if (Send((const unsigned char*)&header, nSize) != nSize)
return info;
PacketHeader resp;
if (!Read((unsigned char*)&resp, nSize))
return info;
if (resp.byStatus != 0) return info;
if (resp.byCmd != Command::eTRACKER_PROTO_CMD_RESP) return info;
nSize = resp.PacketLength();
if (!Read((unsigned char*)&info, nSize))
return info;
return info;
}
FileFullName::operator bool()const
{
return Content.size() > static_cast<int>(PacketSize::eFDFS_GROUP_NAME_MAX_LEN);
}
const char* FileFullName::GroupName()const
{
return Content.c_str();
}
const char* FileFullName::FileName()const
{
if (Content.size() < static_cast<int>(PacketSize::eFDFS_GROUP_NAME_MAX_LEN))
return NULL;
return Content.data() + static_cast<int>(PacketSize::eFDFS_GROUP_NAME_MAX_LEN);
}
StorageServer::StorageServer(const char* address, int port) :TinySocketClient(address, port)
{
Connect();
}
//上传文件,返回文件的的名称
struct FileFullName StorageServer::Upload(const unsigned char* fileContent, int nLen, const char* ext)
{
struct FileFullName name;
UploadFileHeader upload;
upload.byCmd = Command::eSTORAGE_PROTO_CMD_UPLOAD_FILE;
upload.FileExtension(ext);
upload.FileLenth(nLen);
//整个包的长度,长度需要减去包头的长度。
upload.PacketLength(sizeof(UploadFileHeader) + nLen - sizeof(PacketHeader));
//发送头
SendT(upload);
//发送文件内容
Send(fileContent,nLen);
//接收结果
PacketHeader resp;
int nSize = sizeof(PacketHeader);
if (!Read((unsigned char*)&resp, nSize))
return name;
if (resp.byStatus != 0) return name;
if (resp.byCmd != Command::eTRACKER_PROTO_CMD_RESP) return name;
nSize = resp.PacketLength();
name.Content.resize(nSize);
if (!Read((unsigned char*)name.Content.data(), nSize))
{
name.Content.clear();
return name;
}
return name;
}
//删除文件
bool StorageServer::Delete(const char* group, const char* fileName)
{
if (!group || !fileName)
return false;
int nGroupLen = strlen(group);
int nFileNameLen = strlen(fileName);
if (nGroupLen <= 0 || nFileNameLen <= 0)
return false;
DeleteFileHeader header;
int nTotalSize = static_cast<int>(PacketSize::eFDFS_GROUP_NAME_MAX_LEN) + nFileNameLen;
header.byCmd = Command::eSTORAGE_PROTO_CMD_DELETE_FILE;
header.PacketLength(nTotalSize);
header.GroupName(group);
if (SendT(header) != sizeof(DeleteFileHeader))
return false;
if (Send((const unsigned char*)fileName, nFileNameLen) != nFileNameLen)
return false;
PacketHeader resp;
if (!Read((unsigned char*)&resp, sizeof(PacketHeader)))
return false;
if (resp.byStatus != 0) return false;
if (resp.byCmd != Command::eTRACKER_PROTO_CMD_RESP) return false;
if (resp.PacketLength() < 0)
return false;
return true;
}
//下载文件
bool StorageServer::Download(const char* group, const char* fileName,
const std::function<unsigned char* (int nLen)>& cb)
{
if (!group || !fileName)
return false;
int nGroupLen = strlen(group);
int nFileNameLen = strlen(fileName);
if (nGroupLen <= 0 || nFileNameLen <= 0)
return false;
DownloadFileHeader header;
header.GroupName(group);
int nSize = sizeof(DownloadFileHeader) - sizeof(PacketHeader) + nFileNameLen;
header.PacketLength(nSize);
if (Send((const unsigned char*)&header, sizeof(DownloadFileHeader)) != sizeof(DownloadFileHeader))
return false;
if (Send((const unsigned char*)fileName, nFileNameLen) != nFileNameLen)
return false;
//接收结果
PacketHeader resp;
nSize = sizeof(PacketHeader);
if (!Read((unsigned char*)&resp, nSize))
return false;
if (resp.byStatus != 0) return false;
if (resp.byCmd != Command::eTRACKER_PROTO_CMD_RESP) return false;
nSize = resp.PacketLength();
unsigned char* buff = cb(nSize);
if (!buff)
return false;
//读取文件体
return Read(buff, nSize);
}
FastClient::FastClient(const char* address, int port)
{
m_ptrTrackerServer.reset(new TrackerServer(address, port));
if (!m_ptrTrackerServer->Good())
m_ptrTrackerServer.reset();
}
bool FastClient::Good()const
{
if (!m_ptrTrackerServer)
return false;
return m_ptrTrackerServer->Good();
}
StorageServerPtr FastClient::OpenStorage(const StorageInfo& info)
{
std::string str = info.IPAddress();
str += ":";
str += std::to_string(info.Port());
auto it = m_StorageServer.find(str);
if (it != m_StorageServer.end())
return it->second;
StorageServerPtr ptr(new StorageServer(info.IPAddress(), info.Port()));
if (!ptr->Good())
return 0;
m_StorageServer[str] = ptr;
return ptr;
}
//上传文件,返回文件的的名称
struct FileFullName FastClient::Upload(const unsigned char* fileContent, int nLen,const char* ext)
{
FileFullName name;
auto info = m_ptrTrackerServer->QueryStorageStore();
if (!info)
return name;
auto ptrStorage = OpenStorage(info);
if (!ptrStorage)
return name;
return ptrStorage->Upload(fileContent, nLen, ext);
}
bool FastClient::Download(const char* group, const char* fileName,
const std::function<unsigned char* (int nLen)>& cb)
{
auto info = m_ptrTrackerServer->QueryStorageStore();
if (!info)
return false;
auto ptrStorage = OpenStorage(info);
if (!ptrStorage)
return false;
return ptrStorage->Download(group,fileName,cb);
}
bool FastClient::Download(FileFullName& info,
const std::function<unsigned char* (int nLen)>& cb)
{
if (!info || !cb)
return false;
return Download(info.GroupName(),info.FileName(),cb);
}
//删除文件
bool FastClient::Delete(FileFullName& info)
{
if (!info)
return false;
return Delete(info.GroupName(), info.FileName());
}
//删除文件
bool FastClient::Delete(const char* group, const char* fileName)
{
auto info = m_ptrTrackerServer->QueryStorageStore();
if (!info)
return false;
auto ptrStorage = OpenStorage(info);
if (!ptrStorage)
return false;
return ptrStorage->Delete(group, fileName);
}