forked from compilelife/feiq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeiqcommu.h
More file actions
86 lines (77 loc) · 2.34 KB
/
feiqcommu.h
File metadata and controls
86 lines (77 loc) · 2.34 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
#ifndef FEIQCOMMU_H
#define FEIQCOMMU_H
#include <string>
#include <tuple>
#include <vector>
#include <memory>
#include <array>
#include "post.h"
#include "protocol.h"
#include "udpcommu.h"
#include <atomic>
#include "encoding.h"
#include "tcpsocket.h"
#include "tcpserver.h"
#include "uniqueid.h"
using namespace std;
struct VersionInfo
{
string mac;
};
/**
* @brief 提供基础的feiq通信功能,对udp、tcp封装,负责消息的打包、解包
*/
class FeiqCommu
{
public:
typedef function<void (unique_ptr<TcpSocket>, int packetNo, int fileId, int offset)> FileServerHandler;
FeiqCommu();
public:
void setMyHost(string host);
void setMyName(string name);
void addRecvProtocol(RecvProtocol* protocol);
public:
/**
* @brief start 启动feiq通信
* @return 是否启动成功,如果失败,返回具体失败原因
*/
pair<bool, string> start();
void stop();
/**
* @brief send 发送sender打包的内容
* @param ip 发给谁
* @param sender 要发送什么
* @return 发送成功,返回发送包ID,否则返回-1,并设置失败原因
*/
pair<IdType, string> send(const string& ip, SendProtocol& sender);
/**
* @brief requestFileData 请求好友开始发送文件数据
* @param ip 向谁请求
* @param file 要请求的文件
* @return 如果请求成功,返回tcp连接,据此获取数据,否则返回nullptr
*/
unique_ptr<TcpSocket> requestFileData(const string& ip, const FileContent &file, int offset);
/**
* @brief setFileServerHandler 设置文件服务的处理
* @param fileServerHandler 参数:客户端socket连接,请求的文件id,请求的数据偏移
*/
void setFileServerHandler(FileServerHandler fileServerHandler);
public:
static bool dumpRaw(vector<char> &data, Post &post);
static VersionInfo dumpVersionInfo(const string& version);
private:
void onRecv(const string& ip, vector<char> &data);
vector<char> pack(SendProtocol& sender, IdType *packetId = nullptr);
void onTcpClientConnected(int socket);
private:
vector<RecvProtocol*> mRecvPrtocols;
UdpCommu mUdp;
string mHost="";
string mName="";
string mVersion="";
UniqueId mPacketNo;
string mMac;
TcpServer mTcpServer;
FileServerHandler mFileServerHandler;
};
#endif // FEIQCOMMU_H