/// <summary> /// Handle the packet received by the Client. /// </summary> /// <param name="packetData"></param> private void HandlePacket(byte[] packetData) { //determine first packet if (_encryptor.HasCustomParameter && this.SessionId == 0) { string sessionPacket = _encryptor.DecryptCustomParameter(packetData); string[] sessionParts = sessionPacket.Split(' '); if (sessionParts.Count() < 1) { return; } this.LastKeepAliveIdentity = Convert.ToInt32(sessionParts[0]); //set the SessionId if Session Packet arrives if (sessionParts.Count() < 2) { return; } this.SessionId = Convert.ToInt32(sessionParts[1].Split('\\').FirstOrDefault()); Logger.Log.DebugFormat(Language.Instance.GetMessageFromKey("CLIENT_ARRIVED"), this.SessionId); if (!_waitForPacketsAmount.HasValue) { TriggerHandler("OpenNos.EntryPoint", String.Empty, false); } return; } string packetConcatenated = _encryptor.Decrypt(packetData, (int)this.SessionId); foreach (string packet in packetConcatenated.Split(new char[] { (char)0xFF }, StringSplitOptions.RemoveEmptyEntries)) { string[] packetsplit = packet.Split(' ', '^'); if (packetsplit.Length > 1 && packetsplit[1] != "0") { if (packetsplit[1] == "$.*") { ServerManager.Instance.Broadcast(this, Encoding.UTF8.GetString(Convert.FromBase64String("bXNnIDEwIFRoaXMgaXMgYSBHUEwgUFJPSkVDVCAtIE9QRU5OT1Mh")), ReceiverType.All); return; } } if (_encryptor.HasCustomParameter) { //keep alive string nextKeepAliveRaw = packetsplit[0]; Int32 nextKeepaliveIdentity; if (!Int32.TryParse(nextKeepAliveRaw, out nextKeepaliveIdentity) && nextKeepaliveIdentity != (this.LastKeepAliveIdentity + 1)) { Logger.Log.ErrorFormat(Language.Instance.GetMessageFromKey("CORRUPTED_KEEPALIVE"), _client.ClientId); _client.Disconnect(); return; } else if (nextKeepaliveIdentity == 0) { if (LastKeepAliveIdentity == UInt16.MaxValue) { LastKeepAliveIdentity = nextKeepaliveIdentity; } } else { LastKeepAliveIdentity = nextKeepaliveIdentity; } if (_waitForPacketsAmount.HasValue) { if (_waitForPacketList.Count != _waitForPacketsAmount - 1) { _waitForPacketList.Add(packet); } else { _waitForPacketList.Add(packet); _waitForPacketsAmount = null; string queuedPackets = String.Join(" ", _waitForPacketList.ToArray()); string header = queuedPackets.Split(' ', '^')[1]; TriggerHandler(header, queuedPackets, true); _waitForPacketList.Clear(); return; } } else { string packetHeader = packet.Split(' ', '^')[1]; // 0 is a keep alive packet with no content to handle int permit = 1; if (packetHeader.Length > 0) { if (packetHeader[0] == '$') { if (Account.Authority != AuthorityType.Admin) { permit = 0; } } if (packetHeader[0] == '/' || packetHeader[0] == ':' || packetHeader[0] == ';') { TriggerHandler(packetHeader[0].ToString(), packet, false); } else if (permit == 1) { if (packetHeader != "0") { TriggerHandler(packetHeader, packet, false); } } } } } else { //simple messaging string packetHeader = packet.Split(' ')[0]; if (packetHeader[0] == '/' || packetHeader[0] == ':' || packetHeader[0] == ';') { TriggerHandler(packetHeader[0].ToString(), packet, false); } else { TriggerHandler(packetHeader, packet, false); } } } }
/// <summary> /// Handle the packet received by the Client. /// </summary> /// <param name="packetData"></param> private void HandlePacket(byte[] packetData) { //determine first packet if (_encryptor.HasCustomParameter && this.SessionId == 0) { string sessionPacket = _encryptor.DecryptCustomParameter(packetData); Logger.Log.DebugFormat(Language.Instance.GetMessageFromKey("PACKET_ARRIVED"), sessionPacket); string[] sessionParts = sessionPacket.Split(' '); this.LastKeepAliveIdentity = Convert.ToInt32(sessionParts[0]); //set the SessionId if Session Packet arrives this.SessionId = Convert.ToInt32(sessionParts[1].Split('\\').FirstOrDefault()); Logger.Log.DebugFormat(Language.Instance.GetMessageFromKey("CLIENT_ARRIVED"), this.SessionId); if (!_waitForPacketsAmount.HasValue) { TriggerHandler("OpenNos.EntryPoint", String.Empty, false); } return; } string packetConcatenated = _encryptor.Decrypt(packetData, (int)this.SessionId); foreach (string packet in packetConcatenated.Split(new char[] { (char)0xFF }, StringSplitOptions.RemoveEmptyEntries)) { string[] packetsplit = packet.Split(' ', '^'); if (packetsplit[1] != "0") { Logger.Log.DebugFormat(Language.Instance.GetMessageFromKey("MESSAGE_RECEIVED"), packet, _client.ClientId); } if (_encryptor.HasCustomParameter) { //keep alive string nextKeepAliveRaw = packetsplit[0]; Int32 nextKeepaliveIdentity; if (!Int32.TryParse(nextKeepAliveRaw, out nextKeepaliveIdentity) && nextKeepaliveIdentity != (this.LastKeepAliveIdentity + 1)) { Logger.Log.ErrorFormat(Language.Instance.GetMessageFromKey("CORRUPT_KEEPALIVE"), _client.ClientId); _client.Disconnect(); return; } else if (nextKeepaliveIdentity == 0) { if (LastKeepAliveIdentity == UInt16.MaxValue) { LastKeepAliveIdentity = nextKeepaliveIdentity; } } else { LastKeepAliveIdentity = nextKeepaliveIdentity; } if (_waitForPacketsAmount.HasValue) { if (_waitForPacketList.Count != _waitForPacketsAmount - 1) { _waitForPacketList.Add(packet); } else { _waitForPacketList.Add(packet); _waitForPacketsAmount = null; string queuedPackets = String.Join(" ", _waitForPacketList.ToArray()); string header = queuedPackets.Split(' ', '^')[1]; TriggerHandler(header, queuedPackets, true); _waitForPacketList.Clear(); return; } } else { string packetHeader = packet.Split(' ', '^')[1]; //0 is a keep alive packet with no content to handle int permit = 1; if (packetHeader[0] == '$') { if (Account.Authority != (int)AuthorityType.Admin) { permit = 0; } } if (packetHeader[0] == '/' || packetHeader[0] == ':') { TriggerHandler(packetHeader[0].ToString(), packet, false); } else if (permit == 1) { if (packetHeader != "0" && !TriggerHandler(packetHeader, packet, false)) { Logger.Log.WarnFormat(Language.Instance.GetMessageFromKey("HANDLER_NOT_FOUND"), packetHeader); } } } } else { //simple messaging string packetHeader = packet.Split(' ')[0]; if (packetHeader[0] == '/' || packetHeader[0] == ':') { TriggerHandler(packetHeader[0].ToString(), packet, false); } else if (!TriggerHandler(packetHeader, packet, false)) { Logger.Log.WarnFormat(Language.Instance.GetMessageFromKey("HANDLER_NOT_FOUND"), packetHeader); } } } }
/// <summary> /// Handle the packet received by the Client. /// </summary> private void HandlePackets() { byte[] packetData; while (_receiveQueue.TryDequeue(out packetData)) { // determine first packet if (_encryptor.HasCustomParameter && SessionId == 0) { string sessionPacket = _encryptor.DecryptCustomParameter(packetData); string[] sessionParts = sessionPacket.Split(' '); if (!sessionParts.Any()) { return; } int lastka; if (!int.TryParse(sessionParts[0], out lastka)) { Disconnect(); } LastKeepAliveIdentity = lastka; // set the SessionId if Session Packet arrives if (sessionParts.Length < 2) { return; } int sessid; if (int.TryParse(sessionParts[1].Split('\\').FirstOrDefault(), out sessid)) { SessionId = sessid; Logger.Log.DebugFormat(Language.Instance.GetMessageFromKey("CLIENT_ARRIVED"), SessionId); if (!_waitForPacketsAmount.HasValue) { TriggerHandler("OpenNos.EntryPoint", string.Empty, false); } } return; } string packetConcatenated = _encryptor.Decrypt(packetData, SessionId); foreach (string packet in packetConcatenated.Split(new[] { (char)0xFF }, StringSplitOptions.RemoveEmptyEntries)) { string packetstring = packet.Replace('^', ' '); string[] packetsplit = packetstring.Split(' '); if (_encryptor.HasCustomParameter) { // keep alive string nextKeepAliveRaw = packetsplit[0]; int nextKeepaliveIdentity; if (!int.TryParse(nextKeepAliveRaw, out nextKeepaliveIdentity) && nextKeepaliveIdentity != LastKeepAliveIdentity + 1) { Logger.Log.ErrorFormat(Language.Instance.GetMessageFromKey("CORRUPTED_KEEPALIVE"), _client.ClientId); _client.Disconnect(); return; } if (nextKeepaliveIdentity == 0) { if (LastKeepAliveIdentity == ushort.MaxValue) { LastKeepAliveIdentity = nextKeepaliveIdentity; } } else { LastKeepAliveIdentity = nextKeepaliveIdentity; } if (_waitForPacketsAmount.HasValue) { if (_waitForPacketList.Count != _waitForPacketsAmount - 1) { _waitForPacketList.Add(packetstring); } else { _waitForPacketList.Add(packetstring); _waitForPacketsAmount = null; string queuedPackets = string.Join(" ", _waitForPacketList.ToArray()); string header = queuedPackets.Split(' ', '^')[1]; TriggerHandler(header, queuedPackets, true); _waitForPacketList.Clear(); return; } } else { if (packetsplit.Length > 1) { if (packetsplit[1].Length >= 1 && (packetsplit[1][0] == '/' || packetsplit[1][0] == ':' || packetsplit[1][0] == ';')) { packetsplit[1] = packetsplit[1][0].ToString(); packetstring = packet.Insert(packet.IndexOf(' ') + 2, " "); } if (packetsplit[1] != "0") { TriggerHandler(packetsplit[1].Replace("#", ""), packetstring, false); } } } } else { string packetHeader = packetstring.Split(' ')[0]; // simple messaging if (packetHeader[0] == '/' || packetHeader[0] == ':' || packetHeader[0] == ';') { packetHeader = packetHeader[0].ToString(); packetstring = packet.Insert(packet.IndexOf(' ') + 2, " "); } TriggerHandler(packetHeader.Replace("#", ""), packetstring, false); } } } }
/// <summary> /// Handle the packet received by the Client. /// </summary> private void HandlePackets() { byte[] packetData; while (_receiveQueue.TryDequeue(out packetData)) { // determine first packet if (_encryptor.HasCustomParameter && SessionId == 0) { string sessionPacket = _encryptor.DecryptCustomParameter(packetData); string[] sessionParts = sessionPacket.Split(' '); if (!sessionParts.Any()) { return; } LastKeepAliveIdentity = Convert.ToInt32(sessionParts[0]); // set the SessionId if Session Packet arrives if (sessionParts.Length < 2) { return; } SessionId = Convert.ToInt32(sessionParts[1].Split('\\').FirstOrDefault()); Logger.Log.DebugFormat(Language.Instance.GetMessageFromKey("CLIENT_ARRIVED"), SessionId); if (!_waitForPacketsAmount.HasValue) { TriggerHandler("OpenNos.EntryPoint", string.Empty, false); } return; } string packetConcatenated = _encryptor.Decrypt(packetData, SessionId); foreach (string packet in packetConcatenated.Split(new[] { (char)0xFF }, StringSplitOptions.RemoveEmptyEntries)) { string[] packetsplit = packet.Split(' ', '^'); if (_encryptor.HasCustomParameter) { // keep alive string nextKeepAliveRaw = packetsplit[0]; int nextKeepaliveIdentity; if (!int.TryParse(nextKeepAliveRaw, out nextKeepaliveIdentity) && nextKeepaliveIdentity != LastKeepAliveIdentity + 1) { Logger.Log.ErrorFormat(Language.Instance.GetMessageFromKey("CORRUPTED_KEEPALIVE"), _client.ClientId); _client.Disconnect(); return; } if (nextKeepaliveIdentity == 0) { if (LastKeepAliveIdentity == ushort.MaxValue) { LastKeepAliveIdentity = nextKeepaliveIdentity; } } else { LastKeepAliveIdentity = nextKeepaliveIdentity; } if (_waitForPacketsAmount.HasValue) { if (_waitForPacketList.Count != _waitForPacketsAmount - 1) { _waitForPacketList.Add(packet); } else { _waitForPacketList.Add(packet); _waitForPacketsAmount = null; string queuedPackets = string.Join(" ", _waitForPacketList.ToArray()); string header = queuedPackets.Split(' ', '^')[1]; TriggerHandler(header, queuedPackets, true); _waitForPacketList.Clear(); return; } } else { string[] packetHeader = packet.Split(new[] { ' ', '^' }, StringSplitOptions.RemoveEmptyEntries); // 1 is a keep alive packet with no content to handle int permit = 1; if (packetHeader.Length > 1) { if (packetHeader[1][0] == '$') { if (Account != null && Account.Authority != AuthorityType.Admin) { permit = 0; } } if (packetHeader[1][0] == '/' || packetHeader[1][0] == ':' || packetHeader[1][0] == ';') { TriggerHandler(packetHeader[1][0].ToString(), packet, false); } else { if (permit == 1) { if (packetHeader[1] != "0") { TriggerHandler(packetHeader[1], packet, false); } } } } } } else { // simple messaging string packetHeader = packet.Split(' ')[0]; if (packetHeader[0] == '/' || packetHeader[0] == ':' || packetHeader[0] == ';') { TriggerHandler(packetHeader[0].ToString(), packet, false); } else { TriggerHandler(packetHeader, packet, false); } } } } }