private void ProcessPacket(Packet p) { if (commandTable.ContainsKey(p.ID)) { Packet p1 = commandTable[p.ID].New(); p1.data = p.data; p1.size = (ushort)(p.data.Length); ClientManager.EnterCriticalArea(); try { p1.Parse(this.client); } catch (Exception ex) { Logger.ShowError(ex); } ClientManager.LeaveCriticalArea(); } else { if (commandTable.ContainsKey(0xFFFF)) { Packet p1 = commandTable[0xFFFF].New(); p1.data = p.data; p1.size = (ushort)(p.data.Length); ClientManager.EnterCriticalArea(); try { p1.Parse(this.client); } catch (Exception ex) { Logger.ShowError(ex); } ClientManager.LeaveCriticalArea(); } else { Logger.ShowDebug(string.Format("Unknown Packet:0x{0:X4}\r\n Data:{1}", p.ID, DumpData(p)), Logger.CurrentLogger); } } }
private void ReceiveData(IAsyncResult ar) { try { this.nlock.AcquireWriterLock(Timeout.Infinite); if (this.isDisconnected) { this.nlock.ReleaseWriterLock(); return; } if (!sock.Connected) { this.nlock.ReleaseWriterLock(); ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } try { stream.EndRead(ar); } catch (Exception) { this.nlock.ReleaseWriterLock(); ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } byte[] raw = (byte[])ar.AsyncState; if (this.isGateway) { raw = Encryption.Decrypt(raw, 2, this.ClientKey); } if (!isGateway) { ushort messageID = (ushort)(raw[7] + (raw[6] << 8)); if (!this.commandTable.ContainsKey((messageID))) { if (!this.fullHeader) { Logger.ShowWarning(string.Format("Un paquete desconocido {0:X} {1:X} desde " + sock.RemoteEndPoint.ToString(), raw[6], raw[7]), null); } else { if (commandTable.ContainsKey((ushort)0xFFFF)) { if (this.commandTable[(ushort)0xFFFF].SizeIsOk((ushort)raw.Length)) { Packet p = this.commandTable[(ushort)0xFFFF].New(); p.data = raw; p.size = (ushort)(raw.Length); ClientManager.EnterCriticalArea(); try { p.Parse(this.client); } catch (Exception ex) { Logger.ShowError(ex); } ClientManager.LeaveCriticalArea(); } else { string error = "Paquete invalido del cliente " + sock.RemoteEndPoint.ToString(); Console.WriteLine(error); Log.WriteLog(error); ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } } else { Logger.ShowWarning("Paquete universal 0xFFFF no definido!", null); } } } else { if (this.commandTable[messageID].SizeIsOk((ushort)raw.Length)) { Packet p = this.commandTable[messageID].New(); p.data = raw; p.size = (ushort)(raw.Length); Client client; if (p.SessionID != 0) { client = this.currentClientManager.GetClient(p.SessionID); if (client == null) { client = this.client; } } else { client = this.client; } ClientManager.EnterCriticalArea(); try { if (client.netIO == null) { client.netIO = this; } p.Parse(client); } catch (Exception ex) { Logger.ShowError(ex); } ClientManager.LeaveCriticalArea(); } else { string error = string.Format("Tamano del paquete invalido (Paquete:{0:X4}) de cliente {1}", messageID, sock.RemoteEndPoint.ToString()); Console.WriteLine(error); Log.WriteLog(error); this.nlock.ReleaseWriterLock(); ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } } } else { ushort messageID; if (!fullHeader) { messageID = (ushort)(raw[7] + (raw[6] << 8)); if (!this.commandTable.ContainsKey((messageID))) { Logger.ShowWarning(string.Format("Se ha obtenido un paquete desconocido {0:X} {1:X} desde " + sock.RemoteEndPoint.ToString(), raw[6], raw[7]), null); } else { if (this.commandTable[messageID].SizeIsOk((ushort)raw.Length)) { Packet p = this.commandTable[messageID].New(); p.data = raw; p.size = (ushort)(raw.Length); p.isGateway = this.isGateway; ClientManager.EnterCriticalArea(); try { p.Parse(this.client); } catch (Exception ex) { Logger.ShowError(ex); } ClientManager.LeaveCriticalArea(); } else { string error = "Invalid packet size from client " + sock.RemoteEndPoint.ToString(); Console.WriteLine(error); Log.WriteLog(error); this.nlock.ReleaseWriterLock(); ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } } } else { if (commandTable.ContainsKey((ushort)0xFFFF)) { if (this.commandTable[(ushort)0xFFFF].SizeIsOk((ushort)raw.Length)) { Packet p = this.commandTable[(ushort)0xFFFF].New(); p.data = raw; p.size = (ushort)(raw.Length); p.isGateway = this.isGateway; ClientManager.EnterCriticalArea(); try { p.Parse(this.client); } catch (Exception ex) { Logger.ShowError(ex); } ClientManager.LeaveCriticalArea(); } else { string error = "Invalid packet size from client " + sock.RemoteEndPoint.ToString(); Console.WriteLine(error); Log.WriteLog(error); this.nlock.ReleaseWriterLock(); ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } } else { Logger.ShowWarning("Universal Packet 0xFFFF not defined!", null); } } } try { this.nlock.ReleaseWriterLock(); stream.BeginRead(buffer, 0, 2, this.callbackSize, null); } catch (Exception) { ClientManager.EnterCriticalArea(); this.Disconnect(); ClientManager.LeaveCriticalArea(); return; } } catch (Exception e) { Logger.ShowError(e, null); } }