You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

content.h 3.6 kB

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #ifndef CONTENT_H
  2. #define CONTENT_H
  3. #include <string>
  4. #include "protocol.h"
  5. #include "uniqueid.h"
  6. #include <sys/stat.h>
  7. #include "ipmsg.h"
  8. #include "utils.h"
  9. #include "parcelable.h"
  10. using namespace std;
  11. enum class ContentType{Text, Knock, File, Image, Id};
  12. /**
  13. * @brief 消息内容
  14. */
  15. class Content : public Parcelable
  16. {
  17. public:
  18. IdType packetNo;
  19. void setPacketNo(string val){
  20. packetNo = stoul(val);
  21. }
  22. void setPacketNo(IdType val){
  23. packetNo = val;
  24. }
  25. virtual ~Content(){}
  26. ContentType type() const {return mType;}
  27. protected:
  28. ContentType mType;
  29. public:
  30. virtual void writeTo(Parcel& out) const override
  31. {
  32. out.write(mType);
  33. out.write(packetNo);
  34. }
  35. virtual void readFrom(Parcel& in) override
  36. {
  37. in.read(mType);
  38. in.read(packetNo);
  39. }
  40. };
  41. class IdContent : public Content
  42. {
  43. public:
  44. IdContent(){mType = ContentType::Id;}
  45. IdType id;
  46. };
  47. class TextContent : public Content
  48. {
  49. public:
  50. TextContent(){mType = ContentType::Text;}
  51. string text;
  52. string format;
  53. public:
  54. virtual void writeTo(Parcel& out) const override
  55. {
  56. Content::writeTo(out);
  57. out.writeString(text);
  58. out.writeString(format);
  59. }
  60. virtual void readFrom(Parcel& in) override
  61. {
  62. Content::readFrom(in);
  63. in.readString(text);
  64. in.readString(format);
  65. }
  66. };
  67. class FileContent : public Content
  68. {
  69. public:
  70. FileContent(){mType = ContentType::File;}
  71. IdType fileId;
  72. string filename;
  73. string path;//保存路径或要发送的文件的路径
  74. int size = 0;
  75. int modifyTime = 0;
  76. int fileType = 0;
  77. public:
  78. static unique_ptr<FileContent> createFileContentToSend(const string& filePath)
  79. {
  80. static UniqueId mFileId;
  81. struct stat fInfo;
  82. auto ret = stat(filePath.c_str(), &fInfo);
  83. if (ret != 0)
  84. return nullptr;
  85. unique_ptr<FileContent> file(new FileContent());
  86. file->fileId = mFileId.get();
  87. file->path = filePath;
  88. file->filename = getFileNameFromPath(filePath);
  89. if (S_ISREG(fInfo.st_mode))
  90. file->fileType = IPMSG_FILE_REGULAR;
  91. else if (S_ISREG(fInfo.st_mode))
  92. file->fileType = IPMSG_FILE_DIR;
  93. else
  94. return nullptr;//先不支持其他类型
  95. file->size = fInfo.st_size;
  96. file->modifyTime = fInfo.st_mtimespec.tv_sec;
  97. return file;
  98. }
  99. };
  100. class KnockContent: public Content
  101. {
  102. public:
  103. KnockContent(){mType = ContentType::Knock;}
  104. };
  105. class ImageContent: public Content
  106. {
  107. public:
  108. ImageContent(){mType = ContentType::Image;}
  109. string id;
  110. };
  111. class ContentParcelFactory
  112. {
  113. public:
  114. static unique_ptr<Content> createFromParcel(Parcel& in)
  115. {
  116. //先读取父类信息
  117. Content content;
  118. auto pos = in.mark();
  119. content.readFrom(in);
  120. in.unmark(pos);
  121. //根据类型读取剩余数据
  122. Content* ptr = nullptr;
  123. switch (content.type()) {
  124. case ContentType::Text:
  125. ptr = new TextContent;
  126. break;
  127. case ContentType::Knock:
  128. ptr = new KnockContent;
  129. break;
  130. case ContentType::File:
  131. ptr = new FileContent;
  132. break;
  133. case ContentType::Image:
  134. ptr = new ImageContent;
  135. break;
  136. case ContentType::Id:
  137. ptr = new IdContent;
  138. break;
  139. default:
  140. break;
  141. }
  142. if (ptr)
  143. ptr->readFrom(in);
  144. return unique_ptr<Content>(ptr);
  145. }
  146. };
  147. #endif // CONTENT_H

mac下的“飞秋”大多数只是飞鸽传书协议,而且未发现令人满意的开源项目,所以基于c++与qt实现了基础的飞秋协议。

Contributors (1)