提交 | 用户 | age
|
4b03ae
|
1 |
#pragma once
|
Q |
2 |
#include <vector>
|
|
3 |
#include <memory>
|
|
4 |
#include "MyCircularBuf.h"
|
|
5 |
// CMyAsyncSocket1 命令目标
|
|
6 |
double GetTimeMs();
|
|
7 |
|
|
8 |
class CMyDataSocket;
|
|
9 |
class CMyServerSocket : public CAsyncSocket
|
|
10 |
{
|
|
11 |
public:
|
|
12 |
CMyServerSocket();
|
|
13 |
virtual ~CMyServerSocket();
|
|
14 |
int Idx;
|
|
15 |
int SN;
|
|
16 |
virtual void OnAccept(int nErrorCode);
|
|
17 |
virtual void OnClose(int nErrorCode);
|
|
18 |
virtual void OnConnect(int nErrorCode);
|
|
19 |
std::vector<std::shared_ptr<CMyDataSocket>> MySockets;
|
|
20 |
int UpdataStat();
|
|
21 |
|
|
22 |
};
|
|
23 |
struct stVerInfo
|
|
24 |
{
|
|
25 |
short nMainVer;
|
|
26 |
short nSubVer;
|
|
27 |
};
|
|
28 |
struct ClientInfo
|
|
29 |
{
|
|
30 |
stVerInfo ClientVer;
|
|
31 |
int nType;
|
|
32 |
CString TypeStr;
|
|
33 |
int nGroup;
|
|
34 |
CString GroupStr;
|
|
35 |
int Online;
|
|
36 |
CTime OnTime;
|
|
37 |
CTime OffTime;
|
|
38 |
};
|
|
39 |
class CMyDataSocket : public CAsyncSocket
|
|
40 |
{
|
|
41 |
public:
|
|
42 |
enum enConnectionStat
|
|
43 |
{
|
|
44 |
statIdel=0,
|
|
45 |
statListening=1,
|
|
46 |
statToConnect=2,
|
|
47 |
statConnected=3,
|
|
48 |
statMeToClose=4,
|
|
49 |
statBeToClose=5,
|
|
50 |
statClosed=6,
|
|
51 |
statTimeWait=7,
|
|
52 |
statToDel=8,
|
|
53 |
};
|
|
54 |
enum enCMDs
|
|
55 |
{
|
|
56 |
cmdNone=0,
|
|
57 |
cmdPing=1,
|
|
58 |
cmdPingReply=2,
|
|
59 |
cmdHeartBeat=3,
|
|
60 |
cmdMessage=4,
|
|
61 |
cmdGetVersion=5,
|
|
62 |
cmdVerInfo=6,
|
|
63 |
|
|
64 |
cmdNeedLogin=10,
|
|
65 |
cmdLogin=11,
|
|
66 |
cmdLogout=12,
|
|
67 |
cmdAcceptLogin=13,
|
|
68 |
cmdDenyLogin=14,
|
|
69 |
cmdPutName=15,
|
|
70 |
|
|
71 |
cmdSendDummyData=20,
|
|
72 |
|
|
73 |
cmdSendFile=30,
|
|
74 |
cmdAcceptFile=31,
|
|
75 |
cmdPutFile=32,
|
|
76 |
cmdGetFile=33,
|
|
77 |
cmdSendFileData=35,
|
|
78 |
|
|
79 |
cmdListDir=40,
|
|
80 |
cmdDirData=41,
|
|
81 |
cmdGetStat=42,
|
|
82 |
cmdSentStat=43,
|
|
83 |
|
|
84 |
};
|
|
85 |
enum enDataType
|
|
86 |
{
|
|
87 |
dataNone=0,
|
|
88 |
dataFileInfo=1,
|
|
89 |
dataFileData=2,
|
|
90 |
dataTypeVerInfo=5,
|
|
91 |
dataTypeUserPass=10,
|
|
92 |
|
|
93 |
};
|
|
94 |
struct stFileInfo
|
|
95 |
{
|
|
96 |
CTime m_cTime;
|
|
97 |
CTime m_mTime;
|
|
98 |
CTime m_aTime;
|
|
99 |
INT64 nFileSize;
|
|
100 |
DWORD nFileAttr;
|
|
101 |
};
|
|
102 |
#pragma warning(disable : 4200)
|
|
103 |
typedef struct tagFileInfo
|
|
104 |
{
|
|
105 |
CTime m_cTime;
|
|
106 |
CTime m_mTime;
|
|
107 |
CTime m_aTime;
|
|
108 |
INT64 nFileSize;
|
|
109 |
DWORD nFileAttr;
|
|
110 |
DWORD nFileNameLength;
|
|
111 |
char m_FileName[];
|
|
112 |
}*pFileInfo;
|
|
113 |
|
|
114 |
typedef struct tagPacket
|
|
115 |
{
|
|
116 |
short sign; //包头标记
|
|
117 |
short nChannel; //通道
|
|
118 |
short nCMD; //命令
|
|
119 |
short Param1; //参数1
|
|
120 |
short param2; //参数2
|
|
121 |
short param3; //参数3
|
|
122 |
short nDataType; //数据类型
|
|
123 |
USHORT nDataSize; //数据大小
|
|
124 |
char data[0]; //数据载荷
|
|
125 |
}* pPacket;
|
|
126 |
public:
|
|
127 |
CMyDataSocket();
|
|
128 |
virtual ~CMyDataSocket();
|
|
129 |
virtual void OnAccept(int nErrorCode);
|
|
130 |
virtual void OnClose(int nErrorCode);
|
|
131 |
virtual void OnConnect(int nErrorCode);
|
|
132 |
virtual void OnOutOfBandData(int nErrorCode);
|
|
133 |
virtual void OnReceive(int nErrorCode);
|
|
134 |
virtual void OnSend(int nErrorCode);
|
|
135 |
int TriggerSend();
|
|
136 |
int MakeCtrlPacket(pPacket p2, int Type, int nParam1);
|
|
137 |
int MakeDataPacket(pPacket p2, int Type, int nParam1, int Datatype, int nDataLen, void * DataBuf);
|
|
138 |
|
|
139 |
int SendPacket(pPacket p2, int length);
|
|
140 |
int StartSendMass(int Length, int Count);
|
|
141 |
int SendFile(CString sFilePathName);
|
|
142 |
int ContinueReadFile(char * pFileBuf, int nBlockSize);
|
|
143 |
int ProcessPacket(pPacket p1);
|
|
144 |
int DoRecvFile(pFileInfo pf1);
|
|
145 |
int DoPing();
|
|
146 |
int SocketType;
|
|
147 |
int Idx;
|
|
148 |
int SN;
|
|
149 |
int ConnectionStat; //״̬
|
|
150 |
// CString sipaddrto;
|
|
151 |
// int portto;
|
|
152 |
// INT64 BytesToSend;
|
|
153 |
// DWORD dwError;
|
|
154 |
CTime m_cTime; //连接时刻;
|
|
155 |
CTime m_kTime; //断开时刻;
|
|
156 |
|
|
157 |
stVerInfo TargetVer; //对方版本;
|
|
158 |
CString sTargetIp; //对方IP;
|
|
159 |
UINT nTargetPort; //对方端口
|
|
160 |
int Logined; //是否登录;
|
|
161 |
|
|
162 |
|
|
163 |
int nPingInterval; //ping间隔,0表示没有
|
|
164 |
int nIntervalCount; //间隔计数
|
|
165 |
int bPinging; //是否正在ping
|
|
166 |
int PingSeq; //ping序号
|
|
167 |
double pingTime; //ping发出时间;
|
|
168 |
double latancy; //延迟;
|
|
169 |
|
|
170 |
int bToSendFile;
|
|
171 |
int bSendingFile;
|
|
172 |
int bRecvingFile;
|
|
173 |
INT64 DummyToSend;
|
|
174 |
CFileException e1;
|
|
175 |
CFile m_filesend1;
|
|
176 |
CFile m_filerecv1;
|
|
177 |
INT64 nSendFileSize;
|
|
178 |
INT64 nFileSentSize;
|
|
179 |
INT64 nRecvFileSize;
|
|
180 |
INT64 nFileRecvedSize;
|
|
181 |
stFileInfo mRecvFileInfo;
|
|
182 |
static const int tempsendbufsize=65536;
|
|
183 |
static const int nMaxFileBlockSize=60000;
|
|
184 |
static const int nMaxDummyBlockSize=60000;
|
|
185 |
static const int SendBufSize=2560000;
|
|
186 |
static const int RecvBufSize=2560000;
|
|
187 |
CMyCircularBuf SendBuf;
|
|
188 |
CMyCircularBuf RecvBuf;
|
|
189 |
char tempSendbuf1[tempsendbufsize];
|
|
190 |
char tempRecvbuf1[65536];
|
|
191 |
volatile INT64 TotalSendBytes,TotalRecvBytes;
|
|
192 |
volatile DWORD SendBytes,RecvBytes;
|
|
193 |
volatile DWORD LastSendBytes,LastRecvBytes;
|
|
194 |
volatile DWORD SendSpeed,RecvSpeed;
|
|
195 |
int UpdateSpeed();
|
|
196 |
};
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
class CMyPingSocket : public CAsyncSocket
|
|
201 |
{
|
|
202 |
public:
|
|
203 |
CMyPingSocket();
|
|
204 |
~CMyPingSocket();
|
|
205 |
typedef struct IpHeader{//IP报头格式
|
|
206 |
unsigned char hdr_len:4;//4位头部长度
|
|
207 |
unsigned char version:4;//4位版本号
|
|
208 |
unsigned char tos;//8位服务类型
|
|
209 |
unsigned short total_len;//16位总长度
|
|
210 |
unsigned short identifier;//16位标示符
|
|
211 |
unsigned short frag_and_flags;//3位标志加13位片偏移
|
|
212 |
unsigned char tt1;//8位生存时间
|
|
213 |
unsigned char protocol;//8位上层协议号
|
|
214 |
unsigned short checksum;//16位校验和
|
|
215 |
unsigned long sourceIP;//32位源IP地址
|
|
216 |
unsigned long destIP;//32位目的IP地址
|
|
217 |
}* pIpHeader;
|
|
218 |
typedef struct IcmpHeader{//ICMP的报头格式
|
|
219 |
BYTE i_type;//ICMP类型码
|
|
220 |
BYTE i_code;//子类型码
|
|
221 |
USHORT i_cksum;//校验和
|
|
222 |
USHORT i_id;//ICMP数据报的ID号
|
|
223 |
USHORT i_seq;//ICMP数据报的序列号
|
|
224 |
// ULONG timestamp;//可选数据部分,可以忽略
|
|
225 |
char datas[];
|
|
226 |
}* pIcmpHeader;
|
|
227 |
|
|
228 |
int Init();
|
|
229 |
virtual void OnReceive(int nErrorCode);
|
|
230 |
char RecvBuf1[65536];
|
|
231 |
char SendBuf1[65536];
|
|
232 |
int seq;
|
|
233 |
struct stPingStruct
|
|
234 |
{
|
|
235 |
CString sHostName;
|
|
236 |
double PingTime;
|
|
237 |
short seq;
|
|
238 |
bool Pinging;
|
|
239 |
bool Replied;
|
|
240 |
}Hosts[100];
|
|
241 |
int TotalHosts;
|
|
242 |
static USHORT checksum(USHORT * pBuf,int iSize);
|
|
243 |
static void fill_icmp_data(char *pBuf,int iSize);
|
|
244 |
int DoPings();
|
|
245 |
int AddHostToList(CString HostAddr);
|
|
246 |
int DelHostFromList(int index);
|
|
247 |
int EmptyList();
|
|
248 |
};
|
|
249 |
|
|
250 |
class CMyUDPSocket1 : public CAsyncSocket
|
|
251 |
{
|
|
252 |
virtual void OnReceive(int nErrorCode);
|
|
253 |
//virtual void OnSend(int nErrorCode);
|
|
254 |
|
|
255 |
};
|
|
256 |
|
|
257 |
class CMyUDPSocket2 : public CAsyncSocket
|
|
258 |
{
|
|
259 |
virtual void OnReceive(int nErrorCode);
|
|
260 |
//virtual void OnSend(int nErrorCode);
|
|
261 |
|
|
262 |
};
|
|
263 |
|
|
264 |
class CMyUDPSocket3 : public CAsyncSocket
|
|
265 |
{
|
|
266 |
virtual void OnReceive(int nErrorCode);
|
|
267 |
//virtual void OnSend(int nErrorCode);
|
|
268 |
|
|
269 |
}; |