// Converts the body byte buffer into a deserialized object. // Returns null if the deserialization failed. public PegasusPacket DecodePacket(PegasusPacket packet) { Type targetType; _pegasusTypes.TryGetValue(packet.Type, out targetType); if (targetType == null) { return(null); } // Setup decoding for the packet data Type decoder = typeof(ProtobufUtil); MethodInfo decoderMethod = decoder.GetMethod("ParseFromGeneric", BindingFlags.Public | BindingFlags.Static); // Specialize the method to fabricate the detected type. MethodInfo specializedDecoder = decoderMethod?.MakeGenericMethod(targetType); if (specializedDecoder == null) { return(null); } var protoMessage = (IProtoBuf)specializedDecoder.Invoke(null, new object[] { packet.GetBody() }); return(new PegasusPacket(packet.Type, 0, protoMessage)); }
private void ProcessResponse(PegasusPacket packet, ClientRequestType clientRequest) { if (packet.Type != 0xfe) { this.m_state.m_pendingDelivery.Enqueue(packet); } }
private void ClientRequestCallback(RPCContext context) { BattleNetErrors status = (BattleNetErrors)context.Header.Status; if (status != BattleNetErrors.ERROR_OK) { base.m_battleNet.EnqueueErrorInfo(BnetFeature.Games, BnetFeatureEvent.Games_OnClientRequest, status, context.Context); } else { ClientResponse response = ClientResponse.ParseFrom(context.Payload); if (response.AttributeCount >= 2) { bnet.protocol.attribute.Attribute attribute = response.AttributeList[0]; bnet.protocol.attribute.Attribute attribute2 = response.AttributeList[1]; if (!attribute.Value.HasIntValue || !attribute2.Value.HasBlobValue) { base.ApiLog.LogError("Malformed Attribute in Util Packet: incorrect values"); } int intValue = (int)attribute.Value.IntValue; byte[] blobValue = attribute2.Value.BlobValue; PegasusPacket item = new PegasusPacket(intValue, blobValue.Length, blobValue) { Context = context.Context }; this.m_utilPackets.Enqueue(item); } else { base.ApiLog.LogError("Malformed Attribute in Util Packet: missing values"); } } }
private void ProcessSubscribeResponse(PegasusPacket packet, ClientRequestType request) { if (packet.Body is SubscribeResponse) { SystemChannel system = request.System; int systemId = system.SystemId; SubscribeResponse body = (SubscribeResponse)packet.Body; if (body.Result == SubscribeResponse.ResponseResult.FAILED_UNAVAILABLE) { this.ScheduleResubscribeWithNewRoute(system); } else { system.SubscriptionStatus.CurrentState = SubscriptionStatusType.State.SUBSCRIBED; system.SubscriptionStatus.SubscribedTime = DateTime.Now; system.Route = body.Route; system.CurrentPhase = RequestPhase.STARTUP; system.SubscribeAttempt = 0; system.KeepAliveSecs = body.KeepAliveSecs; system.MaxResubscribeAttempts = body.MaxResubscribeAttempts; system.PendingResponseTimeout = body.PendingResponseTimeout; system.PendingSubscribeTimeout = body.PendingSubscribeTimeout; this.m_state.m_pendingDelivery.Enqueue(packet); system.m_subscribePacketsReceived++; } } }
object OnCall(string typeName, string methodName, object thisObj, params object[] args) { if (typeName == "ConnectAPI" && methodName == "QueueGamePacket") { int packetID = (int)args[0]; IProtoBuf body = (IProtoBuf)args[1]; return(ConnectAPI_QueueGamePacket(packetID, body)); } if (typeName == "ConnectAPI" && methodName == "PacketReceived") { PegasusPacket p = (PegasusPacket)args[0]; Queue <PegasusPacket> state = (Queue <PegasusPacket>)args[1]; return(ConnectAPI_PacketReceived(p, state)); } else if (typeName == "bgs.RPCConnection" && methodName == "QueuePacket") { RPCConnection thiz = (RPCConnection)thisObj; BattleNetPacket packet = (BattleNetPacket)args[0]; return(RPCConnection_QueuePacket(thiz, packet)); } else if (typeName == "bgs.RPCConnection" && methodName == "PacketReceived") { RPCConnection thiz = (RPCConnection)thisObj; BattleNetPacket packet = (BattleNetPacket)args[0]; return(RPCConnection_PacketReceived(thiz, packet)); } return(null); }
private void DropNextClientRequestImpl() { if (this.m_state.m_pendingDelivery.Count != 0) { PegasusPacket packet = this.m_state.m_pendingDelivery.Dequeue(); } }
private void NotifyResponseReceivedImpl(PegasusPacket packet) { uint context = (uint)packet.Context; ClientRequestType request = this.GetClientRequest(context, "received_response", true); if (request == null) { if ((packet.Context == 0) || (this.GetDroppedRequest(context, "received_response", true) == null)) { this.m_state.m_pendingDelivery.Enqueue(packet); } } else { switch (packet.Type) { case 0x13b: this.ProcessSubscribeResponse(packet, request); break; case 0x148: this.ProcessClientRequestResponse(packet, request); break; default: this.ProcessResponse(packet, request); break; } } }
private object ConnectAPI_PacketReceived(PegasusPacket p, Queue <PegasusPacket> queue) { using (StreamWriter sw = new StreamWriter("packets.txt", true)) { var s_packetDecoders = typeof(ConnectAPI).GetField("s_packetDecoders", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null) as SortedDictionary <int, ConnectAPI.PacketDecoder>; ConnectAPI.PacketDecoder packetDecoder; PegasusPacket decoded = null; if (s_packetDecoders.TryGetValue(p.Type, out packetDecoder)) { //sw.WriteLine(packetDecoder); PegasusPacket decode = new PegasusPacket(p.Type, p.Context, p.Size, p.Body); decoded = packetDecoder.HandlePacket(decode); using (StreamWriter sw2 = new StreamWriter("PowerHistory.txt", true)) { if (packetDecoder.ToString().Contains("PowerHistory")) { sw2.Write("{\n" + "\"method\": \"ConnectAPI.PacketReceived\",\n" + "\"type\": " + p.Type + ",\n" + "\"decoder\": \"" + packetDecoder + "\",\n" + "\"body\": " + JsonConvert.SerializeObject(decoded.Body, Formatting.Indented) + "\n" + "}\n"); } } if (packetDecoder.ToString().Contains("PowerHistory") || packetDecoder.ToString().Contains("Pong")) { return(null); } sw.Write("{\n" + "\"method\": \"ConnectAPI.PacketReceived\",\n" + "\"type\": " + p.Type + ",\n" + "\"decoder\": \"" + packetDecoder + "\",\n" ); if (decoded == null) { sw.Write("\"body\": null\n"); } else { sw.Write("\"body\": " + JsonConvert.SerializeObject(decoded.Body, Formatting.Indented) + "\n"); } sw.Write("}\n"); } else { sw.WriteLine("{\n" + "\"method\": \"ConnectAPI.PacketReceived\",\n" + "\"type\": " + p.Type + ",\n" + "\"decoder\": null,\n" + "\"dump\": " + JsonConvert.SerializeObject(p, Formatting.Indented) + "\n" + "}"); } } return(null); }
private void ProcessClientRequestResponse(PegasusPacket packet, ClientRequestType clientRequest) { if (packet.Body is ClientRequestResponse) { ClientRequestResponse body = (ClientRequestResponse)packet.Body; if (body.ResponseFlags == ClientRequestResponse.ClientRequestResponseFlags.CRRF_SERVICE_UNAVAILABLE) { this.ProcessServiceUnavailable(body, clientRequest); } } }
// Manually serializes the contents of a PegasusPacket. public static byte[] SerializePacket(PegasusPacket packet) { int type = packet.Type; object body = packet.GetBody(); var bodyProto = body as IProtoBuf; if (bodyProto != null) { // There is an encode routine defined for a body which is a protobuffer // instance. return(packet.Encode()); } /* Manual encoding */ var bodyBuffer = body as byte[]; if (bodyBuffer == null) { string message = string.Format("Body of this packet (`{0}`) is not a byte buffer!", body.GetType().Name); HookRegistry.Panic(message); } var dataStream = new MemoryStream(); int bodySize = bodyBuffer.Length; // Write sizes to buffer. byte[] typeBytes = BitConverter.GetBytes(type); // 4 bytes byte[] sizeBytes = BitConverter.GetBytes(bodySize); // 4 bytes dataStream.Write(typeBytes, 0, 4); dataStream.Write(sizeBytes, 0, 4); // Write body to the stream. dataStream.Write(bodyBuffer, 0, bodySize); byte[] packetData = dataStream.ToArray(); return(packetData); }
private static void DispatcherHandler(Packet packet) { int e = packet.Ethernet.IpV4.Tcp.Http.Length; var d = packet.Buffer.Reverse().Take(e).Reverse().ToArray(); PegasusPacket x = new PegasusPacket(); try { x.Decode(d, 0, d.Length); } catch { } finally { ConnectAPI.PacketDecoder decoder; if (s_packetDecoders.TryGetValue(x.Type, out decoder)) { Console.WriteLine(packet.Timestamp.ToString("yyyy-MM-dd hh:mm:ss.fff")); PegasusPacket item = decoder.HandlePacket(x); var bod = item.Body; var typ = item.Type; Console.WriteLine(typ); //var i = item.Body.GetType(); if (typ == 19) { PowerHistory history = (PowerHistory)bod; IList<PowerHistoryData> listy = history.ListList; foreach (var phd in listy) { Console.WriteLine(phd); } } else if (typ == 14) { AllOptions history = (AllOptions)bod; IList<PegasusGame.Option> listy = history.OptionsList; foreach (var phd in listy) { Console.WriteLine(phd); } } /* else if () { }*/ else { Console.WriteLine(x.Body.ToString()); } } } //tt++; }
public void NotifyResponseReceived(PegasusPacket packet) { this.NotifyResponseReceivedImpl(packet); }
private void DispatcherHandler(Packet packet) { int le = packet.Ethernet.IpV4.Tcp.Http.Length; var d = packet.Buffer.Reverse().Take(le).Reverse().ToArray(); PegasusPacket x = new PegasusPacket(); try { x.Decode(d, 0, d.Length); } catch { } finally { ConnectAPI.PacketDecoder decoder; if (s_packetDecoders.TryGetValue(x.Type, out decoder)) { PegasusPacket item = decoder.HandlePacket(x); var bod = item.Body; var typ = item.Type; if (typ == 19) { PowerHistory history = (PowerHistory)bod; IList<PowerHistoryData> listy = history.ListList; foreach (var phd in listy) { if (phd.HasShowEntity) { try { entity_dictionary.Add(phd.ShowEntity.Entity, phd.ShowEntity.Name); } catch { } } else if (phd.HasPowerStart) { if (phd.PowerStart.Type == PegasusGame.PowerHistoryStart.Types.Type.PLAY) { if (entity_dictionary.TryGetValue(phd.PowerStart.Source, out cardid)) { string cardname = CardList.List.Find(r => r.ID == cardid).Name; choose_row(cardname); } } } } } else { //Console.WriteLine(x.Body.ToString()); } } } }
private void DispatcherHandler(Packet packet) { int le = packet.Ethernet.IpV4.Tcp.Http.Length; var d = packet.Buffer.Reverse().Take(le).Reverse().ToArray(); PegasusPacket x = new PegasusPacket(); try { x.Decode(d, 0, d.Length); } catch { } finally { ConnectAPI.PacketDecoder decoder; if (s_packetDecoders.TryGetValue(x.Type, out decoder)) { PegasusPacket item = decoder.HandlePacket(x); var bod = item.Body; var typ = item.Type; if (typ == 19) { PowerHistory history = (PowerHistory)bod; IList <PowerHistoryData> listy = history.ListList; foreach (var phd in listy) { if (phd.HasShowEntity) { try { entity_dictionary.Add(phd.ShowEntity.Entity, phd.ShowEntity.Name); } catch { } } else if (phd.HasPowerStart) { if (phd.PowerStart.Type == PegasusGame.PowerHistoryStart.Types.Type.PLAY) { if (entity_dictionary.TryGetValue(phd.PowerStart.Source, out cardid)) { string cardname = CardList.List.Find(r => r.ID == cardid).Name; choose_row(cardname); } } } } } else { //Console.WriteLine(x.Body.ToString()); } } } }