summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XMainClient/XConnection.cs
blob: 43a5a30b824ec8cb0be37d6b122b4e28331ef322 (plain)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
using System;
using KKSG;
using UILib;
using XMainClient.UI;
using XMainClient.UI.UICommon;
using XUtliPoolLib;

namespace XMainClient
{
	internal class XConnection
	{
		public bool OnConnectDelay
		{
			get
			{
				return this._is_connect_delay;
			}
		}

		public bool OnRpcDelay
		{
			get
			{
				return Rpc.OnRpcDelay;
			}
		}

		public bool OnReconnect
		{
			get
			{
				return this._is_on_reconnection;
			}
		}

		public bool ReconnectionEnabled
		{
			get
			{
				return this._reconnection_enabled;
			}
			set
			{
				this._reconnection_enabled = value;
			}
		}

		private float ConnectElapsed
		{
			get
			{
				return (float)DateTime.Now.Subtract(this._last_connect_at).TotalSeconds;
			}
		}

		private float ReconnectElapsed
		{
			get
			{
				return (float)DateTime.Now.Subtract(this._reconnect_at).TotalSeconds;
			}
		}

		private readonly uint _buffer_size = 262140u;

		private readonly float _connect_time_out = 5f;

		private readonly float _connect_delay_notice_threshold = 0.5f;

		private DateTime _last_connect_at = DateTime.Now;

		private DateTime _reconnect_at = DateTime.Now;

		private CNetwork _network = null;

		private bool _reconnection_enabled = true;

		private bool _is_on_reconnection = false;

		private bool _is_manually_reconnection = false;

		private bool _is_manually_reconnect_ui_bshow = false;

		private bool _is_connect_delay = false;

		private string _reconnect_ip = null;

		private int _reconnect_port = 0;

		private int _try_reconnect_count = 0;

		public CNetwork Init(INetObserver observer)
		{
			this._network = new CNetwork();
			CNetSender oSender = new CNetSender(this._network);
			CNetProcessor oProc = new CNetProcessor(this._network, observer);
			bool flag = !this._network.Init(oProc, oSender, new CPacketBreaker(), this._buffer_size, this._buffer_size);
			if (flag)
			{
				XSingleton<XDebug>.singleton.AddErrorLog("network initialization failed!", null, null, null, null, null);
				this._network = null;
			}
			return this._network;
		}

		public bool Connet(string IP, int Port)
		{
			this.Close(NetErrCode.Net_NoError);
			this._last_connect_at = DateTime.Now;
			bool flag = this._network.Connect(IP, Port);
			bool flag2 = !flag;
			if (flag2)
			{
				XSingleton<XDebug>.singleton.AddLog("connect to ", IP, "failed!", null, null, null, XDebugColor.XDebug_None);
			}
			return flag;
		}

		public void Reconnect(string IP, int Port)
		{
			XSingleton<XDebug>.singleton.AddLog("Begin Reconnection.", null, null, null, null, null, XDebugColor.XDebug_None);
			this._reconnect_ip = IP;
			this._reconnect_port = Port;
			bool flag = !this._is_on_reconnection;
			if (flag)
			{
				this._is_on_reconnection = true;
				this._is_manually_reconnection = false;
				this._is_manually_reconnect_ui_bshow = false;
				this._reconnect_at = DateTime.Now;
			}
		}

		public bool Close(NetErrCode err)
		{
			bool flag = !this._network.IsDisconnect();
			bool result;
			if (flag)
			{
				this._network.Close(err);
				result = true;
			}
			else
			{
				result = false;
			}
			return result;
		}

		public void OnReconnected()
		{
			this._is_on_reconnection = false;
			XSingleton<XDebug>.singleton.AddLog("Reconnected.", null, null, null, null, null, XDebugColor.XDebug_None);
			Rpc.delayRpcName = "";
		}

		public void OnReconnectFailed()
		{
			bool is_on_reconnection = this._is_on_reconnection;
			if (is_on_reconnection)
			{
				this._is_on_reconnection = false;
				bool flag = this._network.GetSocketState() > SocketState.State_Closed;
				if (flag)
				{
					this.Close(NetErrCode.Net_ReconnectFailed);
				}
				XSingleton<XDebug>.singleton.AddLog("Reconnect Failed.", null, null, null, null, null, XDebugColor.XDebug_None);
				bool sceneReady = XSingleton<XScene>.singleton.SceneReady;
				if (sceneReady)
				{
					XSingleton<UiUtility>.singleton.OnFatalErrorClosed(ErrorCode.ERR_RECONNECT_FAIL);
				}
				else
				{
					XSingleton<XScene>.singleton.Error = ErrorCode.ERR_RECONNECT_FAIL;
				}
			}
		}

		public void StopReconnection()
		{
			bool is_on_reconnection = this._is_on_reconnection;
			if (is_on_reconnection)
			{
				this._is_on_reconnection = false;
			}
		}

		public SocketState GetSocketState()
		{
			return this._network.GetSocketState();
		}

		public void Update()
		{
			this._is_connect_delay = false;
			switch (this._network.GetSocketState())
			{
			case SocketState.State_Closed:
			{
				bool is_on_reconnection = this._is_on_reconnection;
				if (is_on_reconnection)
				{
					bool flag = !this._is_manually_reconnection;
					if (flag)
					{
						this._try_reconnect_count = XSingleton<XGlobalConfig>.singleton.GetInt("ReconnectTime");
						bool flag2 = this.Connet(this._reconnect_ip, this._reconnect_port);
						if (flag2)
						{
							bool flag3 = !string.IsNullOrEmpty(Rpc.delayRpcName);
							if (flag3)
							{
								XSingleton<XDebug>.singleton.AddWarningLog("rpc delay: ", Rpc.delayRpcName, null, null, null, null);
							}
							XSingleton<XDebug>.singleton.AddLog("reconnecting...", null, null, null, null, null, XDebugColor.XDebug_None);
						}
						this._is_manually_reconnection = true;
					}
					else
					{
						bool flag4 = !this._is_manually_reconnect_ui_bshow;
						if (flag4)
						{
							bool flag5 = this._try_reconnect_count > 0;
							if (flag5)
							{
								this._is_manually_reconnect_ui_bshow = true;
								XSingleton<UiUtility>.singleton.ShowModalDialog(XStringDefineProxy.GetString("RECONNECT_TIP"), XStringDefineProxy.GetString("RECONNECT"), new ButtonClickEventHandler(this.OnReconnetButtonClicked), 300);
								this._try_reconnect_count--;
							}
							else
							{
								this.OnReconnectFailed();
							}
						}
					}
				}
				break;
			}
			case SocketState.State_Connecting:
			{
				bool flag6 = this.ConnectElapsed > this._connect_time_out;
				if (flag6)
				{
					this.Close(NetErrCode.Net_ConnectError);
				}
				else
				{
					bool flag7 = this.ConnectElapsed > this._connect_delay_notice_threshold;
					if (flag7)
					{
						this._is_connect_delay = true;
						XSingleton<XVirtualTab>.singleton.Cancel();
					}
				}
				break;
			}
			case SocketState.State_Connected:
				Rpc.CheckDelay();
				break;
			}
			bool onRpcTimeOutClosed = Rpc.OnRpcTimeOutClosed;
			if (onRpcTimeOutClosed)
			{
				XSingleton<XDebug>.singleton.AddWarningLog("Rpc ", Rpc.delayRpcName + " delay closing...", null, null, null, null);
				this.Close(NetErrCode.Net_Rpc_Delay);
			}
		}

		private bool OnReconnetButtonClicked(IXUIButton button)
		{
			DlgBase<ModalDlg, ModalDlgBehaviour>.singleton.SetVisible(false, true);
			bool flag = this.Connet(this._reconnect_ip, this._reconnect_port);
			if (flag)
			{
				XSingleton<UiUtility>.singleton.ShowSystemTip(XStringDefineProxy.GetString("START_RECONNECT"), "fece00");
				XSingleton<XDebug>.singleton.AddLog("reconnecting...", null, null, null, null, null, XDebugColor.XDebug_None);
			}
			this._is_manually_reconnect_ui_bshow = false;
			return true;
		}
	}
}