-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfc.h
161 lines (151 loc) · 4.65 KB
/
tfc.h
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
#pragma once
#include "trc.h"
#include <memory>
#include <string>
#include <map>
//tiny fastdfs client
// https://github.com/happyfish100/fastdfs
namespace TFC
{
enum class PacketSize
{
eIP_ADDRESS_SIZE = 16,
eFDFS_GROUP_NAME_MAX_LEN = 16,
eFDFS_FILE_PREFIX_MAX_LEN = 16,
eFDFS_STORAGE_ID_MAX_SIZE = 16,
eFDFS_REMOTE_FILE_NAME_MAX_LEN = 128,
eFDFS_FILE_EXT_NAME_MAX_LEN = 6,
eFDFS_MAX_SERVERS_EACH_GROUP = 32,
eFDFS_MAX_GROUPS = 512,
eFDFS_PROTO_PKG_LEN_SIZE = 8,
eFDFS_VERSION_SIZE = 6,
eFDFS_DOMAIN_NAME_MAX_SIZE = 128,
};
enum class Command:unsigned char
{
eEMPTY = 0,
eTRACKER_PROTO_CMD_SERVER_LIST_ALL_GROUPS = 91,
eTRACKER_PROTO_CMD_SERVER_LIST_STORAGE = 92,
eTRACKER_PROTO_CMD_RESP = 100,
eTRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE = 101,
eTRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE = 102,
eTRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE = 103,
eSTORAGE_PROTO_CMD_UPLOAD_FILE = 11,
eSTORAGE_PROTO_CMD_DELETE_FILE = 12,
eSTORAGE_PROTO_CMD_DOWNLOAD_FILE = 14,
eSTORAGE_PROTO_CMD_UPLOAD_SLAVE_FILE = 21,
};
#pragma pack(push)
#pragma pack(1)
struct PacketHeader
{
unsigned char byPkgLen[static_cast<int>(PacketSize::eFDFS_PROTO_PKG_LEN_SIZE)];
Command byCmd = Command::eEMPTY;
unsigned char byStatus = 0;
PacketHeader();
//获取包的长度。
long long PacketLength()const;
//设置包的长度
void PacketLength(unsigned long long nLen);
};
struct UploadFileHeader :public PacketHeader
{
unsigned char StorePathIndex = 0;
//文件长度
unsigned char Length[static_cast<int>(PacketSize::eFDFS_PROTO_PKG_LEN_SIZE)];
//文件扩展名
unsigned char Extension[static_cast<int>(PacketSize::eFDFS_FILE_EXT_NAME_MAX_LEN)];
UploadFileHeader();
//设置文件长度
void FileLenth(unsigned long long nLen);
//设置文件的扩展名
void FileExtension(const char* ext);
};
struct DeleteFileHeader :public PacketHeader
{
//分组名
unsigned char Group[static_cast<int>(PacketSize::eFDFS_GROUP_NAME_MAX_LEN)];
//文件名为变长,独立写入
DeleteFileHeader();
//设置分组名称
void GroupName(const char* group);
};
struct DownloadFileHeader :public PacketHeader
{
//两个整数,暂时都为0。
unsigned char Length1[static_cast<int>(PacketSize::eFDFS_PROTO_PKG_LEN_SIZE)];
unsigned char Length2[static_cast<int>(PacketSize::eFDFS_PROTO_PKG_LEN_SIZE)];
//分组名
unsigned char Group[static_cast<int>(PacketSize::eFDFS_GROUP_NAME_MAX_LEN)];
//文件名为变长,独立写入
DownloadFileHeader();
//设置分组名称
void GroupName(const char* group);
};
#pragma pack(pop)
struct StorageInfo
{
unsigned char Packet[static_cast<int>(PacketSize::eFDFS_GROUP_NAME_MAX_LEN) +
static_cast<int>(PacketSize::eIP_ADDRESS_SIZE) +
static_cast<int>(PacketSize::eFDFS_PROTO_PKG_LEN_SIZE)];
StorageInfo();
StorageInfo(const StorageInfo& r);
StorageInfo(StorageInfo&& rr);
StorageInfo& operator =(const StorageInfo& r);
StorageInfo& operator =(StorageInfo&& r);
void Swap(StorageInfo& r);
const char* GroupName()const;
const char* IPAddress()const;
unsigned int Port()const;
unsigned char StorePathIndex();
operator bool()const;
};
class TrackerServer :public TRC::TinySocketClient
{
public:
TrackerServer(const char* address, int port);
//查询存储信息
StorageInfo QueryStorageStore();
};
typedef std::shared_ptr<TrackerServer> TrackerServerPtr;
struct FileFullName
{
std::string Content;
const char* GroupName()const;
const char* FileName()const;
operator bool()const;
};
class StorageServer :public TRC::TinySocketClient
{
public:
StorageServer(const char* address, int port);
//上传文件,返回文件的的名称
FileFullName Upload(const unsigned char* fileContent, int nLen, const char* ext);
//下载文件
bool Download(const char* group, const char* fileName,
const std::function<unsigned char* (int nLen)>& cb);
//删除文件
bool Delete(const char* group, const char* fileName);
};
typedef std::shared_ptr<StorageServer> StorageServerPtr;
class FastClient
{
TrackerServerPtr m_ptrTrackerServer;
std::map<std::string, StorageServerPtr> m_StorageServer;
StorageServerPtr OpenStorage(const StorageInfo& info);
public:
FastClient(const char* address, int port);
bool Good()const;
//上传文件,返回文件的的名称
FileFullName Upload(const unsigned char* fileContent, int nLen, const char* ext);
bool Download(const char* group, const char* fileName,
const std::function<unsigned char* (int nLen)>& cb);
//下载文件
bool Download(FileFullName& info,
const std::function<unsigned char* (int nLen)>& cb);
//删除文件
bool Delete(FileFullName& info);
//删除文件
bool Delete(const char* group, const char* fileName);
};
}