Example #1
0
        private object RPCConnection_PacketReceived(RPCConnection thisObj, BattleNetPacket packet)
        {
            using (StreamWriter sw = new StreamWriter("rpc_packets.txt", true))
            {
                object body = packet.GetBody();
                MethodDescriptor.ParseMethod parseMethod = null;
                string serviceDescriptor = "";

                if (packet.GetHeader().ServiceId == 254)
                {
                    RPCContext rPCContext;
                    var        waitingForResponse = typeof(RPCConnection).GetField("waitingForResponse", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(thisObj) as Dictionary <uint, RPCContext>;
                    if (waitingForResponse.TryGetValue(packet.GetHeader().Token, out rPCContext))
                    {
                        ServiceDescriptor importedServiceById = thisObj.serviceHelper.GetImportedServiceById(rPCContext.Header.ServiceId);
                        if (importedServiceById != null)
                        {
                            parseMethod = importedServiceById.GetParser(rPCContext.Header.MethodId);
                            body        = parseMethod((byte[])body);
                        }
                    }
                }
                else
                {
                    MethodInfo        dynMethod = typeof(RPCConnection).GetMethod("GetExportedServiceDescriptor", BindingFlags.NonPublic | BindingFlags.Instance);
                    ServiceDescriptor exportedServiceDescriptor = (ServiceDescriptor)dynMethod.Invoke(thisObj, new object[] { packet.GetHeader().ServiceId });
                    serviceDescriptor = exportedServiceDescriptor.ToString();

                    if (exportedServiceDescriptor != null)
                    {
                        parseMethod = thisObj.serviceHelper.GetExportedServiceById(packet.GetHeader().ServiceId).GetParser(packet.GetHeader().MethodId);
                        //   sw.WriteLine("parseMethod: " + parseMethod);
                        if (parseMethod != null)
                        {
                            body = parseMethod((byte[])body);
                        }
                    }
                    //ServiceDescriptor exportedServiceDescriptor = thisObj.GetExportedServiceDescriptor();
                }



                sw.WriteLine("{\n" +
                             " \"method\": \"RPCConnection.PacketReceived\",\n" +
                             " \"serviceDescriptor\": \"" + serviceDescriptor + "\",\n" +
                             " \"type\": \"" + body.ToString() + "\",\n" +
                             " \"header\": " + JsonConvert.SerializeObject(packet.GetHeader(), Formatting.Indented).Replace("\n", "\n ") + ",\n" +
                             " \"body\": " + JsonConvert.SerializeObject(body, Formatting.Indented).Replace("\n", "\n ") + ",\n" +
                             " \"parseMethod\": " + (parseMethod != null ? "\"" + parseMethod.ToString() + "\"" : "null") + "\n" +
                             "}");
            }
            return(null);
        }
Example #2
0
 private object RPCConnection_QueuePacket(RPCConnection thisObj, BattleNetPacket packet)
 {
     using (StreamWriter sw = new StreamWriter("rpc_packets.txt", true))
     {
         sw.WriteLine("{\n" +
                      " \"method\": \"RPCConnection.QueuePacket\",\n" +
                      " \"type\": \"" + packet.GetBody().ToString() + "\",\n" +
                      " \"header\": " + JsonConvert.SerializeObject(packet.GetHeader(), Formatting.Indented).Replace("\n", "\n ") + ",\n" +
                      " \"body\": " + JsonConvert.SerializeObject(packet.GetBody(), Formatting.Indented).Replace("\n", "\n ") + "\n" +
                      "}");
     }
     return(null);
 }
Example #3
0
        public static byte[] SerializePacket(BattleNetPacket packet)
        {
            Header header    = packet.GetHeader();
            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();
            uint headerSize        = header.GetSerializedSize();
            int  shiftedHeaderSize = ((int)headerSize >> 8);

            // Body is byte buffer because packet is incoming/serialised!
            int bodySize = bodyBuffer.Length;

            dataStream.WriteByte((byte)(shiftedHeaderSize & 0xff));
            dataStream.WriteByte((byte)(headerSize & 0xff));

            // Write header to buffer.
            header.Serialize(dataStream);

            // Copy body to buffer.
            dataStream.Write(bodyBuffer, 0, bodySize);

            byte[] packetData = dataStream.ToArray();
            return(packetData);
        }