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.

feiqcommu.h 2.4 kB

8 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef FEIQCOMMU_H
  2. #define FEIQCOMMU_H
  3. #include <string>
  4. #include <tuple>
  5. #include <vector>
  6. #include <memory>
  7. #include <array>
  8. #include "post.h"
  9. #include "protocol.h"
  10. #include "udpcommu.h"
  11. #include <atomic>
  12. #include "encoding.h"
  13. #include "tcpsocket.h"
  14. #include "tcpserver.h"
  15. #include "uniqueid.h"
  16. using namespace std;
  17. struct VersionInfo
  18. {
  19. string mac;
  20. };
  21. /**
  22. * @brief 提供基础的feiq通信功能,对udp、tcp封装,负责消息的打包、解包
  23. */
  24. class FeiqCommu
  25. {
  26. public:
  27. typedef function<void (unique_ptr<TcpSocket>, int packetNo, int fileId, int offset)> FileServerHandler;
  28. FeiqCommu();
  29. public:
  30. void setMyHost(string host);
  31. void setMyName(string name);
  32. void addRecvProtocol(RecvProtocol* protocol);
  33. public:
  34. /**
  35. * @brief start 启动feiq通信
  36. * @return 是否启动成功,如果失败,返回具体失败原因
  37. */
  38. pair<bool, string> start();
  39. void stop();
  40. /**
  41. * @brief send 发送sender打包的内容
  42. * @param ip 发给谁
  43. * @param sender 要发送什么
  44. * @return 发送成功,返回发送包ID,否则返回-1,并设置失败原因
  45. */
  46. pair<IdType, string> send(const string& ip, SendProtocol& sender);
  47. /**
  48. * @brief requestFileData 请求好友开始发送文件数据
  49. * @param ip 向谁请求
  50. * @param file 要请求的文件
  51. * @return 如果请求成功,返回tcp连接,据此获取数据,否则返回nullptr
  52. */
  53. unique_ptr<TcpSocket> requestFileData(const string& ip, const FileContent &file, int offset);
  54. /**
  55. * @brief setFileServerHandler 设置文件服务的处理
  56. * @param fileServerHandler 参数:客户端socket连接,请求的文件id,请求的数据偏移
  57. */
  58. void setFileServerHandler(FileServerHandler fileServerHandler);
  59. public:
  60. static bool dumpRaw(vector<char> &data, Post &post);
  61. static VersionInfo dumpVersionInfo(const string& version);
  62. private:
  63. void onRecv(const string& ip, vector<char> &data);
  64. vector<char> pack(SendProtocol& sender, IdType *packetId = nullptr);
  65. void onTcpClientConnected(int socket);
  66. private:
  67. vector<RecvProtocol*> mRecvPrtocols;
  68. UdpCommu mUdp;
  69. string mHost="";
  70. string mName="";
  71. string mVersion="";
  72. UniqueId mPacketNo;
  73. string mMac;
  74. TcpServer mTcpServer;
  75. FileServerHandler mFileServerHandler;
  76. };
  77. #endif // FEIQCOMMU_H

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

Contributors (1)