summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XUtliPoolLib/XFetchVersionNetwork.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Client/Assets/Scripts/XUtliPoolLib/XFetchVersionNetwork.cs')
-rw-r--r--Client/Assets/Scripts/XUtliPoolLib/XFetchVersionNetwork.cs265
1 files changed, 265 insertions, 0 deletions
diff --git a/Client/Assets/Scripts/XUtliPoolLib/XFetchVersionNetwork.cs b/Client/Assets/Scripts/XUtliPoolLib/XFetchVersionNetwork.cs
new file mode 100644
index 00000000..6b610d76
--- /dev/null
+++ b/Client/Assets/Scripts/XUtliPoolLib/XFetchVersionNetwork.cs
@@ -0,0 +1,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;
+ }
+ }
+}