summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XMainClient/CNetProcessor.cs
blob: 0b76dd17efd04dbbe559c60dfb9be8be34eea0a9 (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using System;
using System.IO;
using Ionic.Zlib;
using XUpdater;
using XUtliPoolLib;

namespace XMainClient
{
	public class CNetProcessor : ILuaNetProcess, INetProcess
	{
		private CNetwork m_oNetwork;

		private INetObserver m_oObserver;

		private Random r;

		public static int MaxBuffSize = 65536;

		private MemoryStream RecvStream = new MemoryStream(65536);

		private ProtocolHead head = new ProtocolHead();

		private ZlibStream zDecompress = null;

		private static NetEvent m_sCurrentEvent = null;

		public void OnLuaProcessBuffer(NetEvent evt)
		{
			bool flag = this.m_oNetwork.IsOnlyDispacherInLua(this.head.type);
			bool isPtc = this.head.IsPtc;
			if (isPtc)
			{
				bool flag2 = true;
				bool flag3 = this.m_oNetwork.ContainPtc(this.head.type, out flag2) || flag;
				if (flag3)
				{
					bool flag4 = this.RecvStream != null;
					if (flag4)
					{
						XSingleton<XDebug>.singleton.AddGreenLog("ptc new type: ", this.head.type.ToString(), " only: " + flag.ToString(), null, null, null);
						LuaNetNode node = this.m_oNetwork.GetNode(flag2 || flag);
						node.Reset();
						node.isRpc = false;
						node.isOnlyLua = flag;
						node.type = this.head.type;
						bool flag5 = flag2 || flag;
						if (flag5)
						{
							node.SetBuff(this.RecvStream.GetBuffer(), (int)this.RecvStream.Length);
						}
						evt.node = node;
						evt.IsOnlyLua = flag;
					}
				}
			}
			else
			{
				bool flag6 = flag;
				if (flag6)
				{
					LuaNetNode node2 = this.m_oNetwork.GetNode(true);
					node2.isOnlyLua = true;
					node2.isRpc = true;
					node2.type = this.head.type;
					byte[] buffer = this.RecvStream.GetBuffer();
					node2.SetBuff(buffer, (int)this.RecvStream.Length);
					evt.node = node2;
					evt.IsOnlyLua = true;
				}
				else
				{
					LuaNetNode removeRpc = this.m_oNetwork.GetRemoveRpc(this.head.tagID);
					bool flag7 = removeRpc == null;
					if (!flag7)
					{
						XSingleton<XDebug>.singleton.AddGreenLog("rpc new type: " + this.head.type, " only: " + flag.ToString(), null, null, null, null);
						removeRpc.type = this.head.type;
						removeRpc.isRpc = true;
						removeRpc.isOnlyLua = flag;
						bool copyBuffer = removeRpc.copyBuffer;
						if (copyBuffer)
						{
							removeRpc.SetBuff(this.RecvStream.GetBuffer(), (int)this.RecvStream.Length);
						}
						evt.node = removeRpc;
						evt.IsOnlyLua = false;
					}
				}
			}
		}

		public void OnLuaProcess(NetEvent evt)
		{
			LuaNetNode node = evt.node;
			bool flag = node != null;
			if (flag)
			{
				bool isOnlyLua = node.isOnlyLua;
				if (isOnlyLua)
				{
					XSingleton<XUpdater.XUpdater>.singleton.XLuaEngine.hotfixMgr.ProcessOveride(node.type, node.buffer, node.length);
				}
				else
				{
					bool isRpc = node.isRpc;
					if (isRpc)
					{
						bool flag2 = node.resp != null;
						if (flag2)
						{
							node.resp(node.buffer, node.length);
						}
					}
					else
					{
						XSingleton<XUpdater.XUpdater>.singleton.XLuaEngine.hotfixMgr.RegistedPtc(node.type, node.buffer, node.length);
					}
				}
				this.m_oNetwork.ReturnNode(node);
			}
		}

		public CNetProcessor(CNetwork network, INetObserver ob)
		{
			this.m_oNetwork = network;
			this.m_oObserver = ob;
			this.r = new Random(DateTime.Now.Millisecond);
			this.zDecompress = new ZlibStream(this.RecvStream, CompressionMode.Decompress, true);
		}

		public void OnConnect(bool bSuccess)
		{
			bool flag = this.m_oObserver != null;
			if (flag)
			{
				this.m_oObserver.OnConnect(bSuccess);
			}
		}

		public void OnClosed(NetErrCode nErrCode)
		{
			bool flag = this.m_oObserver != null;
			if (flag)
			{
				this.m_oObserver.OnClosed(nErrCode);
			}
		}

		private void ProcessStream(NetEvent evt)
		{
			bool flag = (long)evt.m_oBuffer.Count < 12L;
			if (flag)
			{
				XSingleton<XDebug>.singleton.AddErrorLog("head file size error", null, null, null, null, null);
			}
			else
			{
				this.head.Reset();
				this.head.Deserialize(ref evt.m_oBuffer);
				int size = this.head.Size;
				bool isInit = evt.m_oBuffer.IsInit;
				if (isInit)
				{
					bool isCompressed = this.head.IsCompressed;
					if (isCompressed)
					{
						this.zDecompress.Seek(0L, SeekOrigin.Begin);
						this.zDecompress.SetLength(0L);
						this.zDecompress.Write(evt.m_oBuffer.OriginalBuff, evt.m_oBuffer.StartOffset + size, evt.m_nBufferLength - size);
						this.zDecompress.Flush();
						this.RecvStream.Seek(0L, SeekOrigin.Begin);
					}
					else
					{
						this.RecvStream.Seek(0L, SeekOrigin.Begin);
						this.RecvStream.SetLength(0L);
						this.RecvStream.Write(evt.m_oBuffer.OriginalBuff, evt.m_oBuffer.StartOffset + size, evt.m_nBufferLength - size);
						this.RecvStream.Seek(0L, SeekOrigin.Begin);
					}
				}
			}
		}

		private void ProcessDeSerialize(NetEvent evt)
		{
			evt.IsPtc = this.head.IsPtc;
			bool isPtc = this.head.IsPtc;
			if (isPtc)
			{
				Protocol protocolThread = Protocol.GetProtocolThread(this.head.type);
				bool flag = protocolThread == null;
				if (flag)
				{
					bool flag2 = !this.m_oNetwork.ConatainPtc(this.head.type) && !this.m_oNetwork.IsOnlyDispacherInLua(this.head.type);
					if (flag2)
					{
						XSingleton<XDebug>.singleton.AddErrorLog("Ptc Not found: ", this.head.type.ToString(), null, null, null, null);
					}
				}
				else
				{
					try
					{
						bool flag3 = this.RecvStream.Length > 1024L;
						if (flag3)
						{
							XSingleton<XDebug>.singleton.AddWarningLog2("Recv Ptc:{0} to long:{1}b", new object[]
							{
								protocolThread.GetProtoType(),
								this.RecvStream.Length
							});
						}
						protocolThread.ThreadErrCode = EProtocolErrCode.ENoErr;
						protocolThread.DeSerialize(this.RecvStream);
					}
					catch (Exception ex)
					{
						XSingleton<XDebug>.singleton.AddErrorLog2("Ptc {0} deserialize fail: {1} ", new object[]
						{
							this.head.type,
							ex.Message
						});
						bool flag4 = protocolThread != null;
						if (flag4)
						{
							protocolThread.ThreadErrCode = EProtocolErrCode.EDeSerializeErr;
						}
					}
					evt.protocol = protocolThread;
				}
			}
			else
			{
				Rpc removeRpcByTag = Rpc.GetRemoveRpcByTag(this.head.tagID);
				bool flag5 = removeRpcByTag == null;
				if (!flag5)
				{
					try
					{
						bool flag6 = this.RecvStream.Length > 0L;
						if (flag6)
						{
							bool flag7 = this.RecvStream.Length > 1024L;
							if (flag7)
							{
								XSingleton<XDebug>.singleton.AddWarningLog2("Recv Rpc:{0} to long:{1}b", new object[]
								{
									removeRpcByTag.GetRpcType(),
									this.RecvStream.Length
								});
							}
							removeRpcByTag.ThreadErrCode = EProtocolErrCode.ENoErr;
							removeRpcByTag.DeSerialize(this.RecvStream);
						}
						else
						{
							bool isRpcNull = this.head.IsRpcNull;
							if (isRpcNull)
							{
								removeRpcByTag.ThreadErrCode = EProtocolErrCode.ENullProtocol;
							}
							else
							{
								removeRpcByTag.ThreadErrCode = EProtocolErrCode.ENoErr;
								removeRpcByTag.DeSerialize(this.RecvStream);
							}
						}
					}
					catch (Exception ex2)
					{
						XSingleton<XDebug>.singleton.AddErrorLog2("Rpc {0} deserialize fail: {1}", new object[]
						{
							this.head.type,
							ex2.Message
						});
						bool flag8 = removeRpcByTag != null;
						if (flag8)
						{
							removeRpcByTag.ThreadErrCode = EProtocolErrCode.EDeSerializeErr;
						}
					}
					evt.rpc = removeRpcByTag;
				}
			}
		}

		public void OnPrePropress(NetEvent evt)
		{
			this.ProcessStream(evt);
			this.ProcessDeSerialize(evt);
			this.OnLuaProcessBuffer(evt);
		}

		public void OnProcess(NetEvent evt)
		{
			CNetProcessor.m_sCurrentEvent = evt;
			bool flag = evt.protocol != null;
			if (flag)
			{
				bool flag2 = XSingleton<XDebug>.singleton.EnableRecord();
				if (flag2)
				{
					XSingleton<XDebug>.singleton.AddPoint(evt.protocol.GetProtoType(), evt.protocol.GetType().Name, (float)evt.m_nBufferLength, 0, XDebug.RecordChannel.ENetwork);
				}
				bool flag3 = !evt.IsOnlyLua;
				if (flag3)
				{
					evt.protocol.Process();
				}
				this.OnLuaProcess(evt);
			}
			else
			{
				bool flag4 = evt.rpc != null;
				if (!flag4)
				{
					this.OnLuaProcess(evt);
					return;
				}
				bool flag5 = XSingleton<XDebug>.singleton.EnableRecord();
				if (flag5)
				{
					XSingleton<XDebug>.singleton.AddPoint(evt.rpc.GetRpcType(), evt.rpc.GetType().Name, (float)evt.m_nBufferLength, 0, XDebug.RecordChannel.ENetwork);
				}
				evt.rpc.replyTick = evt.m_oTime;
				bool flag6 = !evt.IsOnlyLua;
				if (flag6)
				{
					evt.rpc.Process();
				}
				else
				{
					evt.rpc.RemoveTimer();
				}
				this.OnLuaProcess(evt);
			}
			bool flag7 = this.m_oObserver != null;
			if (flag7)
			{
				this.m_oObserver.OnReceive(this.head.type, 0);
			}
		}

		public void OnPostProcess(NetEvent evt)
		{
			bool flag = evt.protocol != null;
			if (flag)
			{
				Protocol.ReturnProtocolThread(evt.protocol);
			}
			CNetProcessor.m_sCurrentEvent = null;
		}

		public static void ManualReturnProtocol()
		{
			bool flag = CNetProcessor.m_sCurrentEvent != null;
			if (flag)
			{
				CNetProcessor.m_sCurrentEvent.ManualReturnProtocol();
			}
		}
	}
}