Exemple #1
0
 private void BytesReceived(byte[] bytes, int nBytes, int offset)
 {
     while (nBytes > 0)
     {
         if (this.m_currentPacket == null)
         {
             this.m_currentPacket = new BattleNetPacket();
         }
         int num = this.m_currentPacket.Decode(bytes, offset, nBytes);
         nBytes -= num;
         offset += num;
         if (this.m_currentPacket.IsLoaded())
         {
             ConnectionEvent item = new ConnectionEvent {
                 Type   = ConnectionEventTypes.OnPacketCompleted,
                 Packet = this.m_currentPacket
             };
             List <ConnectionEvent> connectionEvents = this.m_connectionEvents;
             lock (connectionEvents)
             {
                 this.m_connectionEvents.Add(item);
             }
             this.m_currentPacket = null;
         }
         else
         {
             Array.Copy(bytes, offset, this.m_backingBuffer, 0, nBytes);
             this.m_backingBufferBytes = nBytes;
             return;
         }
     }
     this.m_backingBufferBytes = 0;
 }
Exemple #2
0
        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);
        }
Exemple #3
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);
        }
Exemple #4
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);
 }
Exemple #5
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);
        }
Exemple #6
0
 public void QueuePacket(BattleNetPacket packet)
 {
     this.m_outQueue.Enqueue(packet);
 }