forked from compilelife/feiq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.h
More file actions
170 lines (147 loc) · 3.53 KB
/
content.h
File metadata and controls
170 lines (147 loc) · 3.53 KB
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
#ifndef CONTENT_H
#define CONTENT_H
#include <string>
#include "protocol.h"
#include "uniqueid.h"
#include <sys/stat.h>
#include "ipmsg.h"
#include "utils.h"
#include "parcelable.h"
using namespace std;
enum class ContentType{Text, Knock, File, Image, Id};
/**
* @brief 消息内容
*/
class Content : public Parcelable
{
public:
IdType packetNo;
void setPacketNo(string val){
packetNo = stoul(val);
}
void setPacketNo(IdType val){
packetNo = val;
}
virtual ~Content(){}
ContentType type() const {return mType;}
protected:
ContentType mType;
public:
virtual void writeTo(Parcel& out) const override
{
out.write(mType);
out.write(packetNo);
}
virtual void readFrom(Parcel& in) override
{
in.read(mType);
in.read(packetNo);
}
};
class IdContent : public Content
{
public:
IdContent(){mType = ContentType::Id;}
IdType id;
};
class TextContent : public Content
{
public:
TextContent(){mType = ContentType::Text;}
string text;
string format;
public:
virtual void writeTo(Parcel& out) const override
{
Content::writeTo(out);
out.writeString(text);
out.writeString(format);
}
virtual void readFrom(Parcel& in) override
{
Content::readFrom(in);
in.readString(text);
in.readString(format);
}
};
class FileContent : public Content
{
public:
FileContent(){mType = ContentType::File;}
IdType fileId;
string filename;
string path;//保存路径或要发送的文件的路径
int size = 0;
int modifyTime = 0;
int fileType = 0;
public:
static unique_ptr<FileContent> createFileContentToSend(const string& filePath)
{
static UniqueId mFileId;
struct stat fInfo;
auto ret = stat(filePath.c_str(), &fInfo);
if (ret != 0)
return nullptr;
unique_ptr<FileContent> file(new FileContent());
file->fileId = mFileId.get();
file->path = filePath;
file->filename = getFileNameFromPath(filePath);
if (S_ISREG(fInfo.st_mode))
file->fileType = IPMSG_FILE_REGULAR;
else if (S_ISREG(fInfo.st_mode))
file->fileType = IPMSG_FILE_DIR;
else
return nullptr;//先不支持其他类型
file->size = fInfo.st_size;
file->modifyTime = fInfo.st_mtimespec.tv_sec;
return file;
}
};
class KnockContent: public Content
{
public:
KnockContent(){mType = ContentType::Knock;}
};
class ImageContent: public Content
{
public:
ImageContent(){mType = ContentType::Image;}
string id;
};
class ContentParcelFactory
{
public:
static unique_ptr<Content> createFromParcel(Parcel& in)
{
//先读取父类信息
Content content;
auto pos = in.mark();
content.readFrom(in);
in.unmark(pos);
//根据类型读取剩余数据
Content* ptr = nullptr;
switch (content.type()) {
case ContentType::Text:
ptr = new TextContent;
break;
case ContentType::Knock:
ptr = new KnockContent;
break;
case ContentType::File:
ptr = new FileContent;
break;
case ContentType::Image:
ptr = new ImageContent;
break;
case ContentType::Id:
ptr = new IdContent;
break;
default:
break;
}
if (ptr)
ptr->readFrom(in);
return unique_ptr<Content>(ptr);
}
};
#endif // CONTENT_H