Exemplo n.º 1
0
 public BattleNetPacket(bnet.protocol.Header h, IProtoBuf b)
 {
     this.headerSize = -1;
     this.bodySize   = -1;
     this.header     = h;
     this.body       = b;
 }
Exemplo n.º 2
0
 public BattleNetPacket()
 {
     this.headerSize = -1;
     this.bodySize   = -1;
     this.header     = null;
     this.body       = null;
 }
Exemplo n.º 3
0
 protected bnet.protocol.Header CreateHeader(uint serviceId, uint methodId, uint objectId, uint token, uint size)
 {
     bnet.protocol.Header header = new bnet.protocol.Header();
     header.SetServiceId(serviceId);
     header.SetMethodId(methodId);
     if (objectId != 0)
     {
         header.SetObjectId((ulong)objectId);
     }
     header.SetToken(token);
     header.SetSize(size);
     return(header);
 }
Exemplo n.º 4
0
    public override int Decode(byte[] bytes, int offset, int available)
    {
        int num = 0;

        if (this.headerSize < 0)
        {
            if (available < 2)
            {
                return(num);
            }
            this.headerSize = (bytes[offset] << 8) + bytes[offset + 1];
            available      -= 2;
            num            += 2;
            offset         += 2;
        }
        if (this.header == null)
        {
            if (available < this.headerSize)
            {
                return(num);
            }
            this.header = new bnet.protocol.Header();
            this.header.Deserialize(new MemoryStream(bytes, offset, this.headerSize));
            this.bodySize = !this.header.HasSize ? 0 : ((int)this.header.Size);
            if (this.header == null)
            {
                throw new Exception("failed to parse BattleNet packet header");
            }
            available -= this.headerSize;
            num       += this.headerSize;
            offset    += this.headerSize;
        }
        if (this.body != null)
        {
            return(num);
        }
        if (available < this.bodySize)
        {
            return(num);
        }
        byte[] destinationArray = new byte[this.bodySize];
        Array.Copy(bytes, offset, destinationArray, 0, this.bodySize);
        this.body = destinationArray;
        return(num + this.bodySize);
    }
Exemplo n.º 5
0
    private void PrintHeader(bnet.protocol.Header h)
    {
        object[] args    = new object[] { h.ServiceId, h.MethodId, h.Token, h.Size, (BattleNetErrors)h.Status };
        string   message = string.Format("Packet received: Header = [ ServiceId: {0}, MethodId: {1} Token: {2} Size: {3} Status: {4}", args);

        if (h.ErrorCount > 0)
        {
            message = message + " Error:[";
            foreach (bnet.protocol.ErrorInfo info in h.ErrorList)
            {
                string   str2      = message;
                object[] objArray2 = new object[] { str2, " ErrorInfo{ ", info.ObjectAddress.Host.Label, "/", info.ObjectAddress.Host.Epoch, "}" };
                message = string.Concat(objArray2);
            }
            message = message + "]";
        }
        message = message + "]";
        this.m_logSource.LogDebug(message);
    }
Exemplo n.º 6
0
 private void LogOutgoingPacket(BattleNetPacket packet, bool wasMetered)
 {
     if (this.m_logSource == null)
     {
         UnityEngine.Debug.LogWarning("tried to log with null log source, skipping");
     }
     else
     {
         bool                 flag      = false;
         IProtoBuf            body      = (IProtoBuf)packet.GetBody();
         bnet.protocol.Header header    = packet.GetHeader();
         uint                 serviceId = header.ServiceId;
         uint                 methodId  = header.MethodId;
         string               str       = !wasMetered ? "QueueRequest" : "QueueRequest (METERED)";
         if (!string.IsNullOrEmpty(body.ToString()))
         {
             ServiceDescriptor importedServiceById = this.serviceHelper.GetImportedServiceById(serviceId);
             object[]          args = new object[] { str, (importedServiceById != null) ? importedServiceById.GetMethodName(methodId) : "null", header.ToString(), body.ToString() };
             this.m_logSource.LogDebug("{0}: type = {1}, header = {2}, request = {3}", args);
         }
         else
         {
             ServiceDescriptor descriptor2 = this.serviceHelper.GetImportedServiceById(serviceId);
             string            str2        = (descriptor2 != null) ? descriptor2.GetMethodName(methodId) : null;
             if ((str2 != "bnet.protocol.connection.ConnectionService.KeepAlive") && (str2 != null))
             {
                 object[] objArray2 = new object[] { str, str2, header.ToString() };
                 this.m_logSource.LogDebug("{0}: type = {1}, header = {2}", objArray2);
             }
             else
             {
                 flag = true;
             }
         }
         if (!flag)
         {
             this.m_logSource.LogDebugStackTrace("LogOutgoingPacket: ", 0x20, 1);
         }
     }
 }
Exemplo n.º 7
0
 public void QueueResponse(RPCContext context, IProtoBuf message)
 {
     if ((message == null) || (context.Header == null))
     {
         this.m_logSource.LogError("QueueResponse: invalid response");
     }
     else if (this.serviceHelper.GetImportedServiceById(context.Header.ServiceId) == null)
     {
         this.m_logSource.LogError("QueueResponse: error, unrecognized service id: " + context.Header.ServiceId);
     }
     else
     {
         this.m_logSource.LogDebug(string.Concat(new object[] { "QueueResponse: type=", this.serviceHelper.GetImportedServiceById(context.Header.ServiceId).GetMethodName(context.Header.MethodId), " data=", message }));
         bnet.protocol.Header header = context.Header;
         header.SetServiceId(0xfe);
         header.SetMethodId(0);
         header.SetSize(message.GetSerializedSize());
         context.Header = header;
         BattleNetPacket packet = new BattleNetPacket(context.Header, message);
         this.QueuePacket(packet);
     }
 }
Exemplo n.º 8
0
 private void ProcessPendingOutboundPackets()
 {
     if (this.m_pendingOutboundPackets.Count > 0)
     {
         List <BattleNetPacket> list = new List <BattleNetPacket>();
         foreach (BattleNetPacket packet in this.m_pendingOutboundPackets)
         {
             bnet.protocol.Header header = packet.GetHeader();
             uint serviceId = header.ServiceId;
             uint methodId  = header.MethodId;
             if (this.m_connMetering.AllowRPCCall(serviceId, methodId))
             {
                 this.QueuePacket(packet);
             }
             else
             {
                 list.Add(packet);
             }
         }
         this.m_pendingOutboundPackets = list;
     }
 }
Exemplo n.º 9
0
    public RPCContext QueueRequest(uint serviceId, uint methodId, IProtoBuf message, RPCContextDelegate callback = null, uint objectId = 0)
    {
        uint nextToken;

        if (message == null)
        {
            return(null);
        }
        object tokenLock = this.tokenLock;

        lock (tokenLock)
        {
            nextToken = RPCConnection.nextToken;
            RPCConnection.nextToken++;
        }
        RPCContext context = new RPCContext();

        if (callback != null)
        {
            context.Callback = callback;
            this.waitingForResponse.Add(nextToken, context);
        }
        bnet.protocol.Header h    = this.CreateHeader(serviceId, methodId, objectId, nextToken, message.GetSerializedSize());
        BattleNetPacket      item = new BattleNetPacket(h, message);

        context.Header  = h;
        context.Request = message;
        if (!this.m_connMetering.AllowRPCCall(serviceId, methodId))
        {
            this.m_pendingOutboundPackets.Add(item);
            this.LogOutgoingPacket(item, true);
            return(context);
        }
        this.QueuePacket(item);
        return(context);
    }
Exemplo n.º 10
0
 public void Update()
 {
     this.ProcessPendingOutboundPackets();
     if (this.outBoundPackets.Count > 0)
     {
         Queue <BattleNetPacket> queue;
         Queue <BattleNetPacket> outBoundPackets = this.outBoundPackets;
         lock (outBoundPackets)
         {
             queue = new Queue <BattleNetPacket>(this.outBoundPackets.ToArray());
             this.outBoundPackets.Clear();
         }
         while (queue.Count > 0)
         {
             BattleNetPacket packet = queue.Dequeue();
             if (this.Connection != null)
             {
                 this.Connection.QueuePacket(packet);
             }
             else
             {
                 this.m_logSource.LogError("##Client Connection object does not exists!##");
             }
         }
     }
     if (this.Connection != null)
     {
         this.Connection.Update();
     }
     if (this.incomingPackets.Count > 0)
     {
         Queue <BattleNetPacket> queue3;
         Queue <BattleNetPacket> incomingPackets = this.incomingPackets;
         lock (incomingPackets)
         {
             queue3 = new Queue <BattleNetPacket>(this.incomingPackets.ToArray());
             this.incomingPackets.Clear();
         }
         while (queue3.Count > 0)
         {
             BattleNetPacket      packet2 = queue3.Dequeue();
             bnet.protocol.Header h       = packet2.GetHeader();
             this.PrintHeader(h);
             byte[] body = (byte[])packet2.GetBody();
             if (h.ServiceId == 0xfe)
             {
                 RPCContext context;
                 if (this.waitingForResponse.TryGetValue(h.Token, out context))
                 {
                     ServiceDescriptor            importedServiceById = this.serviceHelper.GetImportedServiceById(context.Header.ServiceId);
                     MethodDescriptor.ParseMethod parser = null;
                     if (importedServiceById != null)
                     {
                         parser = importedServiceById.GetParser(context.Header.MethodId);
                     }
                     if (parser == null)
                     {
                         if (importedServiceById != null)
                         {
                             object[] args = new object[] { importedServiceById.Name, context.Header.MethodId };
                             this.m_logSource.LogWarning("Incoming Response: Unable to find method for serviceName={0} method id={1}", args);
                             int      methodCount = importedServiceById.GetMethodCount();
                             object[] objArray2   = new object[] { methodCount };
                             this.m_logSource.LogDebug("  Found {0} methods", objArray2);
                             for (int i = 0; i < methodCount; i++)
                             {
                                 MethodDescriptor methodDescriptor = importedServiceById.GetMethodDescriptor((uint)i);
                                 if ((methodDescriptor == null) && (i != 0))
                                 {
                                     object[] objArray3 = new object[] { i, "<null>" };
                                     this.m_logSource.LogDebug("  Found method id={0} name={1}", objArray3);
                                 }
                                 else
                                 {
                                     object[] objArray4 = new object[] { i, methodDescriptor.Name };
                                     this.m_logSource.LogDebug("  Found method id={0} name={1}", objArray4);
                                 }
                             }
                         }
                         else
                         {
                             object[] objArray5 = new object[] { context.Header.ServiceId };
                             this.m_logSource.LogWarning("Incoming Response: Unable to identify service id={0}", objArray5);
                         }
                     }
                     context.Header           = h;
                     context.Payload          = body;
                     context.ResponseReceived = true;
                     if (context.Callback != null)
                     {
                         context.Callback(context);
                     }
                     this.waitingForResponse.Remove(h.Token);
                 }
             }
             else
             {
                 ServiceDescriptor exportedServiceDescriptor = this.GetExportedServiceDescriptor(h.ServiceId);
                 if (exportedServiceDescriptor != null)
                 {
                     if (this.serviceHelper.GetExportedServiceById(h.ServiceId).GetParser(h.MethodId) == null)
                     {
                         this.m_logSource.LogDebug("Incoming Packet: NULL TYPE service=" + this.serviceHelper.GetExportedServiceById(h.ServiceId).Name + ", method=" + this.serviceHelper.GetExportedServiceById(h.ServiceId).GetMethodName(h.MethodId));
                     }
                     if (exportedServiceDescriptor.HasMethodListener(h.MethodId))
                     {
                         RPCContext context2 = new RPCContext {
                             Header           = h,
                             Payload          = body,
                             ResponseReceived = true
                         };
                         exportedServiceDescriptor.NotifyMethodListener(context2);
                     }
                     else
                     {
                         string str = ((exportedServiceDescriptor == null) || string.IsNullOrEmpty(exportedServiceDescriptor.Name)) ? "<null>" : exportedServiceDescriptor.Name;
                         this.m_logSource.LogError(string.Concat(new object[] { "[!]Unhandled Server Request Received (Service Name: ", str, " Service id:", h.ServiceId, " Method id:", h.MethodId, ")" }));
                     }
                 }
                 else
                 {
                     this.m_logSource.LogError(string.Concat(new object[] { "[!]Server Requested an Unsupported (Service id:", h.ServiceId, " Method id:", h.MethodId, ")" }));
                 }
             }
         }
     }
 }
Exemplo n.º 11
0
 public static void Serialize(Stream stream, Header instance)
 {
     stream.WriteByte(8);
     ProtocolParser.WriteUInt32(stream, instance.ServiceId);
     if (instance.HasMethodId)
     {
         stream.WriteByte(16);
         ProtocolParser.WriteUInt32(stream, instance.MethodId);
     }
     stream.WriteByte(24);
     ProtocolParser.WriteUInt32(stream, instance.Token);
     if (instance.HasObjectId)
     {
         stream.WriteByte(32);
         ProtocolParser.WriteUInt64(stream, instance.ObjectId);
     }
     if (instance.HasSize)
     {
         stream.WriteByte(40);
         ProtocolParser.WriteUInt32(stream, instance.Size);
     }
     if (instance.HasStatus)
     {
         stream.WriteByte(48);
         ProtocolParser.WriteUInt32(stream, instance.Status);
     }
     //if (instance.Error.Count > 0)
     //{
     //    foreach (ErrorInfo current in instance.Error)
     //    {
     //        stream.WriteByte(58);
     //        ProtocolParser.WriteUInt32(stream, current.GetSerializedSize());
     //        ErrorInfo.Serialize(stream, current);
     //    }
     //}
     if (instance.HasTimeout)
     {
         stream.WriteByte(64);
         ProtocolParser.WriteUInt64(stream, instance.Timeout);
     }
 }
Exemplo n.º 12
0
 public static Header DeserializeLengthDelimited(Stream stream, Header instance)
 {
     long num = (long)((ulong)ProtocolParser.ReadUInt32(stream));
     num += stream.Position;
     return Header.Deserialize(stream, instance, num);
 }
Exemplo n.º 13
0
 public static Header DeserializeLengthDelimited(Stream stream)
 {
     Header header = new Header();
     Header.DeserializeLengthDelimited(stream, header);
     return header;
 }
Exemplo n.º 14
0
 public static Header Deserialize(Stream stream, Header instance, long limit)
 {
     instance.ObjectId = 0uL;
     instance.Size = 0u;
     instance.Status = 0u;
     //if (instance.Error == null)
     //{
     //    instance.Error = new List<ErrorInfo>();
     //}
     while (limit < 0L || stream.Position < limit)
     {
         int num = stream.ReadByte();
         if (num == -1)
         {
             if (limit >= 0L)
             {
                 throw new EndOfStreamException();
             }
             return instance;
         }
         else
         {
             int num2 = num;
             if (num2 != 8)
             {
                 if (num2 != 16)
                 {
                     if (num2 != 24)
                     {
                         if (num2 != 32)
                         {
                             if (num2 != 40)
                             {
                                 if (num2 != 48)
                                 {
                                     if (num2 != 58)
                                     {
                                         if (num2 != 64)
                                         {
                                             Key key = ProtocolParser.ReadKey((byte)num, stream);
                                             uint field = key.Field;
                                             if (field == 0u)
                                             {
                                                 throw new ProtocolBufferException("Invalid field id: 0, something went wrong in the stream");
                                             }
                                             ProtocolParser.SkipKey(stream, key);
                                         }
                                         else
                                         {
                                             instance.Timeout = ProtocolParser.ReadUInt64(stream);
                                         }
                                     }
                                     //else
                                     //{
                                     //    instance.Error.Add(ErrorInfo.DeserializeLengthDelimited(stream));
                                     //}
                                 }
                                 else
                                 {
                                     instance.Status = ProtocolParser.ReadUInt32(stream);
                                 }
                             }
                             else
                             {
                                 instance.Size = ProtocolParser.ReadUInt32(stream);
                             }
                         }
                         else
                         {
                             instance.ObjectId = ProtocolParser.ReadUInt64(stream);
                         }
                     }
                     else
                     {
                         instance.Token = ProtocolParser.ReadUInt32(stream);
                     }
                 }
                 else
                 {
                     instance.MethodId = ProtocolParser.ReadUInt32(stream);
                 }
             }
             else
             {
                 instance.ServiceId = ProtocolParser.ReadUInt32(stream);
             }
         }
     }
     if (stream.Position == limit)
     {
         return instance;
     }
     throw new ProtocolBufferException("Read past max limit");
 }
Exemplo n.º 15
0
 public static Header Deserialize(Stream stream, Header instance)
 {
     return Header.Deserialize(stream, instance, -1L);
 }