public static DsoChatPacket Parse(string rawXmlData, byte[] rawData) { PacketLogger.LogPacket(rawXmlData); string xmlBody = SkipHeader(rawXmlData); if (string.IsNullOrEmpty(xmlBody)) { // Failed to match against XML // Try AMF DsoChatPacket amfPacket; if ((amfPacket = TryParseAmf(rawXmlData, rawData)) == null) { return(null); } return(amfPacket); } if (BodyDetection.IsMatch(xmlBody) == false) { return(null); } PacketLogger.LogChatPacket(xmlBody); // Remove namespaces.. xmlBody = Regex.Replace(xmlBody, " xmlns=(\"|')[^\"']*(\"|')", ""); // Get all messages MatchCollection matches = RegexMessages.Matches(xmlBody); if (matches == null || matches.Count == 0) { return(null); } // Build our xml struct // <body><messages>...</messages></body> string validXml = "<body><messages>"; foreach (Match match in matches) { validXml += match.Value; } validXml += "</messages></body>"; // Deserialize try { XmlSerializer serializer = new XmlSerializer(typeof(DsoChatPacket)); using (StringReader stringStream = new StringReader(validXml)) { DsoChatPacket doc = (DsoChatPacket)serializer.Deserialize(stringStream); return(doc); } } catch { // Incomplete packet maybe.. return(null); } }
private void HandleReceivedPacket(SocketAsyncEventArgs args, IPacket packet) { PacketLogger.LogPacket(packet, PacketDirection.Client); var handler = (PacketHandler)null; if (!PacketHandlers.TryGetValue(packet.ID, out handler)) { return; } handler(this, packet); }
public void QueuePacket(IPacket packet) { if (packet == null) { throw new ArgumentNullException("packet"); } if (NetworkManager == null) { throw new InvalidOperationException("Tried to send a packet before NetworkManager was initialized or before socket was connected."); } PacketLogger.LogPacket(packet, PacketDirection.Server); NetworkManager.SendPacket(packet); }
private void HandleNetwork() { while (true) { if (ShuttingDown) { return; } for (int i = 0; i < Clients.Count; i++) { //TODO: Kick client due to keep alive timeouts. while (Clients[i].Client.NetworkManager.DataAvailable) { try { /* S <- P <- C * Proxying data from client to server. */ var rawPacket = (byte[])null; // raw encrypted packet var decryptedPacket = (byte[])null; var packet = Clients[i].Client.NetworkManager.ReadPacket(out rawPacket, out decryptedPacket); // receive data from client if (packet != null) { HandlePacket(Clients[i], packet); } if (rawPacket != null && packet != null) { Clients[i].Server.NetworkManager.CoCStream.Write(rawPacket, 0, rawPacket.Length); // sends data back to server PacketLogger.LogPacket(packet, PacketDirection.Server); PacketDumper.LogPacket(packet, PacketDirection.Server, decryptedPacket); } } catch (Exception ex) { Console.WriteLine("[{0}]: Client => {1}", ex.GetType().Name, ex.Message); Clients.RemoveAt(i); goto ResetLoop; } } if (Clients[i].Server == null) { continue; } while (Clients[i].Server.NetworkManager.DataAvailable) { try { /* S -> P -> C * Proxying data from server to client. */ var rawPacket = (byte[])null; var decryptedPacket = (byte[])null; var packet = Clients[i].Server.NetworkManager.ReadPacket(out rawPacket, out decryptedPacket); // receive data from server if (packet != null) { HandlePacket(Clients[i], packet); } if (rawPacket != null && packet != null) { Clients[i].Client.NetworkManager.CoCStream.Write(rawPacket, 0, rawPacket.Length); // sends data back to client PacketLogger.LogPacket(packet, PacketDirection.Client); PacketDumper.LogPacket(packet, PacketDirection.Client, decryptedPacket); } } catch (Exception ex) { Console.WriteLine("[{0}]: Server => {1}", ex.GetType().Name, ex.Message); Clients.RemoveAt(i); goto ResetLoop; } } ResetLoop: break; } Thread.Sleep(1); } }
public virtual void LogPacket(IPacket packet, bool clientToServer) { Stream.Write(PacketLogger.LogPacket(packet, clientToServer ? Craft.Net.PacketDirection.Serverbound : Craft.Net.PacketDirection.Clientbound)); }