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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using XUpdater;
namespace XUtliPoolLib
{
public class XFetchVersionNetwork
{
private Socket m_oSocket;
private XFetchVersionNetwork.SocketState m_nState;
private static byte[] m_oRecvBuff;
private int m_nCurrRecvLen;
public static int TotalRecvBytes;
private AsyncCallback m_RecvCb = null;
private AsyncCallback m_ConnectCb = null;
public bool m_bRecvMsg = true;
public bool m_bPause = false;
public int m_nPauseRecvLen = 0;
private AddressFamily m_NetworkType = AddressFamily.InterNetwork;
public enum SocketState
{
State_Closed,
State_Connecting,
State_Connected
}
public XFetchVersionNetwork()
{
this.m_oSocket = null;
this.m_nState = XFetchVersionNetwork.SocketState.State_Closed;
XFetchVersionNetwork.m_oRecvBuff = null;
this.m_nCurrRecvLen = 0;
this.m_RecvCb = new AsyncCallback(this.RecvCallback);
this.m_ConnectCb = new AsyncCallback(this.OnConnect);
}
private void GetNetworkType()
{
try
{
string hostNameOrAddress = XSingleton<XUpdater.XUpdater>.singleton.XPlatform.GetLoginServer("QQ").Substring(0, XSingleton<XUpdater.XUpdater>.singleton.XPlatform.GetLoginServer("QQ").LastIndexOf(':'));
IPAddress[] hostAddresses = Dns.GetHostAddresses(hostNameOrAddress);
this.m_NetworkType = hostAddresses[0].AddressFamily;
}
catch (Exception ex)
{
XSingleton<XDebug>.singleton.AddWarningLog(ex.Message, null, null, null, null, null);
}
}
public bool Init()
{
this.GetNetworkType();
try
{
this.m_nState = XFetchVersionNetwork.SocketState.State_Closed;
this.m_oSocket = new Socket(this.m_NetworkType, SocketType.Stream, ProtocolType.Tcp);
this.m_oSocket.NoDelay = true;
bool flag = XFetchVersionNetwork.m_oRecvBuff == null;
if (flag)
{
XFetchVersionNetwork.m_oRecvBuff = new byte[512];
}
this.m_nCurrRecvLen = 0;
}
catch (Exception ex)
{
XSingleton<XDebug>.singleton.AddWarningLog(ex.Message, "new Socket Error!", null, null, null, null);
return false;
}
return true;
}
public void UnInit()
{
this.Close();
}
private void OnConnect(IAsyncResult iar)
{
try
{
bool flag = this.m_nState == XFetchVersionNetwork.SocketState.State_Closed;
if (!flag)
{
Socket socket = (Socket)iar.AsyncState;
socket.EndConnect(iar);
this.SetState(XFetchVersionNetwork.SocketState.State_Connected);
socket.BeginReceive(XFetchVersionNetwork.m_oRecvBuff, this.m_nCurrRecvLen, XFetchVersionNetwork.m_oRecvBuff.Length - this.m_nCurrRecvLen, SocketFlags.None, this.m_RecvCb, socket);
}
}
catch (Exception ex)
{
XSingleton<XDebug>.singleton.AddWarningLog(ex.Message, null, null, null, null, null);
this.SetState(XFetchVersionNetwork.SocketState.State_Closed);
this.Close();
}
}
public bool Connect(string host, int port)
{
bool result;
try
{
this.SetState(XFetchVersionNetwork.SocketState.State_Connecting);
this.m_oSocket.BeginConnect(host, port, this.m_ConnectCb, this.m_oSocket);
result = true;
}
catch (Exception ex)
{
XSingleton<XDebug>.singleton.AddWarningLog(ex.Message, null, null, null, null, null);
result = false;
}
return result;
}
public void Close()
{
this.m_nState = XFetchVersionNetwork.SocketState.State_Closed;
bool flag = this.m_oSocket == null;
if (!flag)
{
try
{
this.m_oSocket.Close();
}
catch (Exception ex)
{
XSingleton<XDebug>.singleton.AddWarningLog(ex.Message, null, null, null, null, null);
}
this.m_oSocket = null;
}
}
public Socket GetSocket()
{
return this.m_oSocket;
}
public bool IsClosed()
{
return this.m_nState == XFetchVersionNetwork.SocketState.State_Closed;
}
private void SetState(XFetchVersionNetwork.SocketState nState)
{
this.m_nState = nState;
}
public void RecvCallback(IAsyncResult ar)
{
try
{
bool flag = this.m_nState == XFetchVersionNetwork.SocketState.State_Closed;
if (!flag)
{
Socket socket = (Socket)ar.AsyncState;
int num = socket.EndReceive(ar);
bool flag2 = num > 0;
if (flag2)
{
XFetchVersionNetwork.TotalRecvBytes += num;
this.m_nCurrRecvLen += num;
bool flag3 = this.DetectPacket();
if (!flag3)
{
bool flag4 = this.m_nCurrRecvLen == XFetchVersionNetwork.m_oRecvBuff.Length;
if (flag4)
{
XSingleton<XDebug>.singleton.AddWarningLog("RecvCallback error ! m_nCurrRecvLen == m_oRecvBuff.Length", null, null, null, null, null);
}
socket.BeginReceive(XFetchVersionNetwork.m_oRecvBuff, this.m_nCurrRecvLen, XFetchVersionNetwork.m_oRecvBuff.Length - this.m_nCurrRecvLen, SocketFlags.None, this.m_RecvCb, socket);
}
}
else
{
bool flag5 = num == 0;
if (flag5)
{
XSingleton<XDebug>.singleton.AddWarningLog("Close socket normally", null, null, null, null, null);
this.Close();
}
else
{
XSingleton<XDebug>.singleton.AddWarningLog("Close socket, recv error!", null, null, null, null, null);
this.Close();
}
}
}
}
catch (ObjectDisposedException)
{
}
catch (SocketException ex)
{
XSingleton<XDebug>.singleton.AddWarningLog(ex.Message, null, null, null, null, null);
this.Close();
}
catch (Exception ex2)
{
XSingleton<XDebug>.singleton.AddWarningLog(ex2.Message, null, null, null, null, null);
this.Close();
}
}
public bool DetectPacket()
{
if (this.m_nCurrRecvLen > 0)
{
int num = this.BreakPacket(XFetchVersionNetwork.m_oRecvBuff, 0, this.m_nCurrRecvLen);
bool flag = num == 0;
if (!flag)
{
bool bRecvMsg = this.m_bRecvMsg;
if (bRecvMsg)
{
byte[] array = new byte[num];
Array.Copy(XFetchVersionNetwork.m_oRecvBuff, 0, array, 0, num);
Encoding utf = Encoding.UTF8;
XSingleton<XUpdater.XUpdater>.singleton.SetServerVersion(utf.GetString(array, 4, num - 4));
return true;
}
}
}
return false;
}
public int BreakPacket(byte[] data, int index, int len)
{
bool flag = len < 4;
int result;
if (flag)
{
result = 0;
}
else
{
int num = BitConverter.ToInt32(data, index);
bool flag2 = len < 4 + num;
if (flag2)
{
result = 0;
}
else
{
result = num + 4;
}
}
return result;
}
}
}
|