summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XMainClient/RPC/Rpc.cs
blob: 2effbbcd7e912d5568bf1df66bd65c77975652da (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
using System;
using System.IO;
using System.Threading;
using UnityEngine;
using XUtliPoolLib;

namespace XMainClient
{
	public abstract class Rpc
	{
		public static bool OnRpcDelay
		{
			get
			{
				return Rpc._is_rpc_delay;
			}
		}

		public static bool OnRpcTimeOutClosed
		{
			get
			{
				return Rpc._is_rpc_close_time_out;
			}
		}

		public static float RpcDelayedTime
		{
			get
			{
				return Rpc._rpc_delayed_time;
			}
		}

		public static float DelayThreshold
		{
			get
			{
				return Rpc._delayThreshold;
			}
		}

		public EProtocolErrCode ThreadErrCode
		{
			get
			{
				return this.m_threadErrCode;
			}
			set
			{
				this.m_threadErrCode = value;
			}
		}

		public float Timeout
		{
			get
			{
				return this.timeout;
			}
			set
			{
				this.timeout = value;
			}
		}

		public uint TimerToken
		{
			get
			{
				return this.timerToken;
			}
		}

		private static readonly float _delayThreshold = 1f;

		private static readonly float _timeout_close_Threshold = 15f;

		private static bool _is_rpc_delay = false;

		private static bool _is_rpc_close_time_out = false;

		private static float _rpc_delayed_time = 0f;

		public static uint sMaxTagID = 1024u;

		private static Rpc[] sm_RpcWaitingReplyCache = new Rpc[Rpc.sMaxTagID];

		private static uint sTagID = 0u;

		public static string delayRpcName = "";

		public int SocketID;

		private float sendTime;

		public long replyTick;

		private XTimerMgr.ElapsedEventHandler _timeCb = null;

		protected EProtocolErrCode m_threadErrCode = EProtocolErrCode.ENoErr;

		private uint tagID = 0u;

		private uint timerToken = 0u;

		private float timeout = Rpc._timeout_close_Threshold - 1f;

		public Rpc()
		{
			this._timeCb = new XTimerMgr.ElapsedEventHandler(this.TimerCallback);
			uint rpcType = this.GetRpcType();
			if (rpcType <= 10091u)
			{
				if (rpcType != 9179u)
				{
					if (rpcType == 10091u)
					{
						this.timeout = 5f;
					}
				}
				else
				{
					this.timeout = 5f;
				}
			}
			else if (rpcType != 25069u)
			{
				if (rpcType != 28358u)
				{
					if (rpcType == 30514u)
					{
						this.timeout = (float)XServerTimeMgr.SyncTimeOut / 1000f;
					}
				}
				else
				{
					this.timeout = 5f;
				}
			}
			else
			{
				this.timeout = 5f;
			}
		}

		public virtual uint GetRpcType()
		{
			return 0u;
		}

		public void SerializeWithHead(MemoryStream stream)
		{
			long position = stream.Position;
			ProtocolHead sharedHead = ProtocolHead.SharedHead;
			sharedHead.type = this.GetRpcType();
			sharedHead.flag = 3u;
			sharedHead.tagID = this.tagID;
			sharedHead.Serialize(stream);
			this.Serialize(stream);
			long position2 = stream.Position;
			uint value = (uint)(position2 - position - 4L);
			stream.Position = position;
			stream.Write(BitConverter.GetBytes(value), 0, 4);
			stream.Position = position2;
		}

		public abstract void Serialize(MemoryStream stream);

		public abstract void DeSerialize(MemoryStream stream);

		public virtual bool CheckPValid()
		{
			bool flag = this.m_threadErrCode == EProtocolErrCode.EDeSerializeErr;
			bool result;
			if (flag)
			{
				XSingleton<XDebug>.singleton.AddErrorLog("Roc EDeSerializeErr Type:", this.GetRpcType().ToString(), null, null, null, null);
				result = false;
			}
			else
			{
				result = true;
			}
			return result;
		}

		public void BeforeSend()
		{
			Rpc.sTagID += 1u;
			bool flag = Rpc.sTagID >= Rpc.sMaxTagID;
			if (flag)
			{
				Rpc.sTagID = 0u;
			}
			this.tagID = Rpc.sTagID;
		}

		public void AfterSend()
		{
			this.sendTime = Time.realtimeSinceStartup;
			Monitor.Enter(Rpc.sm_RpcWaitingReplyCache);
			bool flag = (ulong)this.tagID < (ulong)((long)Rpc.sm_RpcWaitingReplyCache.Length);
			if (flag)
			{
				bool flag2 = Rpc.sm_RpcWaitingReplyCache[(int)this.tagID] != null;
				if (flag2)
				{
					XSingleton<XDebug>.singleton.AddErrorLog("rpc not processed yet", null, null, null, null, null);
				}
				Rpc.sm_RpcWaitingReplyCache[(int)this.tagID] = this;
			}
			Monitor.Exit(Rpc.sm_RpcWaitingReplyCache);
		}

		public void SetTimeOut()
		{
			bool flag = this._timeCb == null;
			if (flag)
			{
				this._timeCb = new XTimerMgr.ElapsedEventHandler(this.TimerCallback);
			}
			this.timerToken = XSingleton<XTimerMgr>.singleton.SetGlobalTimer(this.timeout, this._timeCb, null);
		}

		public void CallTimeOut()
		{
			XSingleton<XTimerMgr>.singleton.KillTimer(this.timerToken);
			this.OnTimeout(null);
		}

		private void TimerCallback(object args)
		{
			Rpc.RemoveRpcByTag(this.tagID);
			this.OnTimeout(args);
			bool flag = this.GetRpcType() != 30514u && this.GetRpcType() != 39595u && XSingleton<XClientNetwork>.singleton.IsConnected();
			if (flag)
			{
				Rpc._is_rpc_close_time_out = true;
				Rpc.delayRpcName = this.ToString();
			}
			XSingleton<XDebug>.singleton.AddWarningLog("RPC TimeOut: ", this.ToString(), null, null, null, null);
		}

		public virtual void Process()
		{
			XSingleton<XTimerMgr>.singleton.KillTimer(this.timerToken);
		}

		public void RemoveTimer()
		{
			XSingleton<XTimerMgr>.singleton.KillTimer(this.timerToken);
		}

		public abstract void OnTimeout(object args);

		public static void RemoveRpcByTag(uint dwTag)
		{
			Monitor.Enter(Rpc.sm_RpcWaitingReplyCache);
			bool flag = (ulong)dwTag < (ulong)((long)Rpc.sm_RpcWaitingReplyCache.Length);
			if (flag)
			{
				Rpc.sm_RpcWaitingReplyCache[(int)dwTag] = null;
			}
			Monitor.Exit(Rpc.sm_RpcWaitingReplyCache);
		}

		public static Rpc GetRemoveRpcByTag(uint dwTag)
		{
			Rpc result = null;
			Monitor.Enter(Rpc.sm_RpcWaitingReplyCache);
			bool flag = (ulong)dwTag < (ulong)((long)Rpc.sm_RpcWaitingReplyCache.Length);
			if (flag)
			{
				result = Rpc.sm_RpcWaitingReplyCache[(int)dwTag];
				Rpc.sm_RpcWaitingReplyCache[(int)dwTag] = null;
			}
			Monitor.Exit(Rpc.sm_RpcWaitingReplyCache);
			return result;
		}

		public static void CheckDelay()
		{
			Rpc._is_rpc_delay = false;
			float realtimeSinceStartup = Time.realtimeSinceStartup;
			Rpc._rpc_delayed_time = 0f;
			Monitor.Enter(Rpc.sm_RpcWaitingReplyCache);
			int i = 0;
			while (i < Rpc.sm_RpcWaitingReplyCache.Length)
			{
				Rpc rpc = Rpc.sm_RpcWaitingReplyCache[i];
				bool flag = rpc != null;
				if (flag)
				{
					bool flag2 = rpc.GetRpcType() == 30514u || rpc.GetRpcType() == 28358u || rpc.GetRpcType() == 45201u || rpc.GetRpcType() == 39595u;
					if (!flag2)
					{
						float num = realtimeSinceStartup - rpc.sendTime;
						bool flag3 = Rpc._rpc_delayed_time < num;
						if (flag3)
						{
							Rpc._rpc_delayed_time = num;
						}
						bool flag4 = num > Rpc._delayThreshold;
						if (flag4)
						{
							Rpc._is_rpc_delay = true;
							Rpc.delayRpcName = rpc.ToString();
							break;
						}
					}
				}
				IL_C3:
				i++;
				continue;
				goto IL_C3;
			}
			Monitor.Exit(Rpc.sm_RpcWaitingReplyCache);
		}

		public static void Close()
		{
			Monitor.Enter(Rpc.sm_RpcWaitingReplyCache);
			for (int i = 0; i < Rpc.sm_RpcWaitingReplyCache.Length; i++)
			{
				Rpc rpc = Rpc.sm_RpcWaitingReplyCache[i];
				bool flag = rpc != null;
				if (flag)
				{
					XSingleton<XTimerMgr>.singleton.KillTimer(rpc.TimerToken);
					rpc.OnTimeout(null);
				}
				Rpc.sm_RpcWaitingReplyCache[i] = null;
			}
			Monitor.Exit(Rpc.sm_RpcWaitingReplyCache);
			Rpc._is_rpc_delay = false;
			Rpc._is_rpc_close_time_out = false;
		}
	}
}