コード例 #1
0
        private TypedObject ReadDSA()
        {
            TypedObject ret = new TypedObject("DSA");

            int        flag;
            List <int> flags = ReadFlags();

            for (int i = 0; i < flags.Count; i++)
            {
                flag = flags[i];
                int bits = 0;
                if (i == 0)
                {
                    if ((flag & 0x01) != 0)
                    {
                        ret.Add("body", Decode());
                    }
                    if ((flag & 0x02) != 0)
                    {
                        ret.Add("clientId", Decode());
                    }
                    if ((flag & 0x04) != 0)
                    {
                        ret.Add("destination", Decode());
                    }
                    if ((flag & 0x08) != 0)
                    {
                        ret.Add("headers", Decode());
                    }
                    if ((flag & 0x10) != 0)
                    {
                        ret.Add("messageId", Decode());
                    }
                    if ((flag & 0x20) != 0)
                    {
                        ret.Add("timeStamp", Decode());
                    }
                    if ((flag & 0x40) != 0)
                    {
                        ret.Add("timeToLive", Decode());
                    }
                    bits = 7;
                }
                else if (i == 1)
                {
                    if ((flag & 0x01) != 0)
                    {
                        ReadByte();
                        byte[] temp = ReadByteArray();
                        ret.Add("clientIdBytes", temp);
                        ret.Add("clientId", ByteArrayToID(temp));
                    }
                    if ((flag & 0x02) != 0)
                    {
                        ReadByte();
                        byte[] temp = ReadByteArray();
                        ret.Add("messageIdBytes", temp);
                        ret.Add("messageId", ByteArrayToID(temp));
                    }
                    bits = 2;
                }

                ReadRemaining(flag, bits);
            }

            flags = ReadFlags();
            for (int i = 0; i < flags.Count; i++)
            {
                flag = flags[i];
                int bits = 0;

                if (i == 0)
                {
                    if ((flag & 0x01) != 0)
                    {
                        ret.Add("correlationId", Decode());
                    }
                    if ((flag & 0x02) != 0)
                    {
                        ReadByte();
                        byte[] temp = ReadByteArray();
                        ret.Add("correlationIdBytes", temp);
                        ret.Add("correlationId", ByteArrayToID(temp));
                    }
                    bits = 2;
                }

                ReadRemaining(flag, bits);
            }

            return(ret);
        }
コード例 #2
0
        private object ReadObject()
        {
            int  handle = ReadInt();
            bool inline = ((handle & 1) != 0);

            handle = handle >> 1;

            if (inline)
            {
                bool inlineDefine = ((handle & 1) != 0);
                handle = handle >> 1;

                ClassDefinition cd;
                if (inlineDefine)
                {
                    cd      = new ClassDefinition();
                    cd.type = ReadString();

                    cd.externalizable = ((handle & 1) != 0);
                    handle            = handle >> 1;
                    cd.dynamic        = ((handle & 1) != 0);
                    handle            = handle >> 1;

                    for (int i = 0; i < handle; i++)
                    {
                        cd.members.Add(ReadString());
                    }

                    classDefinitions.Add(cd);
                }
                else
                {
                    cd = classDefinitions[handle];
                }

                TypedObject ret = new TypedObject(cd.type);

                // Need to add reference here due to circular references
                objectReferences.Add(ret);

                if (cd.externalizable)
                {
                    if (cd.type.Equals("DSK"))
                    {
                        ret = ReadDSK();
                    }
                    else if (cd.type.Equals("DSA"))
                    {
                        ret = ReadDSA();
                    }
                    else if (cd.type.Equals("flex.messaging.io.ArrayCollection"))
                    {
                        object obj = Decode();
                        ret = TypedObject.MakeArrayCollection((object[])obj);
                    }
                    else if (cd.type.Equals("com.riotgames.platform.systemstate.ClientSystemStatesNotification") || cd.type.Equals("com.riotgames.platform.broadcast.BroadcastNotification"))
                    {
                        int size = 0;
                        for (int i = 0; i < 4; i++)
                        {
                            size = size * 256 + ReadByteAsInt();
                        }

                        byte[]        data = ReadBytes(size);
                        StringBuilder sb   = new StringBuilder();
                        for (int i = 0; i < data.Length; i++)
                        {
                            sb.Append(Convert.ToChar(data[i]));
                        }

                        JavaScriptSerializer serializer = new JavaScriptSerializer();
                        ret      = serializer.Deserialize <TypedObject>(sb.ToString());
                        ret.type = cd.type;
                    }
                    else
                    {
                        //for (int i = dataPos; i < dataBuffer.length; i++)
                        //System.out.print(String.format("%02X", dataBuffer[i]));
                        //System.out.println();
                        throw new NotImplementedException("Externalizable not handled for " + cd.type);
                    }
                }
                else
                {
                    for (int i = 0; i < cd.members.Count; i++)
                    {
                        String key   = cd.members[i];
                        object value = Decode();
                        ret.Add(key, value);
                    }

                    if (cd.dynamic)
                    {
                        String key;
                        while ((key = ReadString()).Length != 0)
                        {
                            object value = Decode();
                            ret.Add(key, value);
                        }
                    }
                }

                return(ret);
            }
            else
            {
                return(objectReferences[handle]);
            }
        }
コード例 #3
0
        private void BeginReceive()
        {
            decodeThread = new Thread(() =>
            {
                try
                {
                    Dictionary <int, Packet> previousReceivedPacket = new Dictionary <int, Packet>();
                    Dictionary <int, Packet> currentPackets         = new Dictionary <int, Packet>();

                    while (true)
                    {
                        #region Basic Header
                        byte basicHeader = (byte)sslStream.ReadByte();
                        List <byte> basicHeaderStorage = new List <byte>();
                        if ((int)basicHeader == 255)
                        {
                            Disconnect();
                        }

                        int channel = 0;
                        //1 Byte Header
                        if ((basicHeader & 0x03) != 0)
                        {
                            channel = basicHeader & 0x3F;
                            basicHeaderStorage.Add(basicHeader);
                        }
                        //2 Byte Header
                        else if ((basicHeader & 0x01) != 0)
                        {
                            byte byte2 = (byte)sslStream.ReadByte();
                            channel    = 64 + byte2;
                            basicHeaderStorage.Add(basicHeader);
                            basicHeaderStorage.Add(byte2);
                        }
                        //3 Byte Header
                        else if ((basicHeader & 0x02) != 0)
                        {
                            byte byte2 = (byte)sslStream.ReadByte();
                            byte byte3 = (byte)sslStream.ReadByte();
                            basicHeaderStorage.Add(basicHeader);
                            basicHeaderStorage.Add(byte2);
                            basicHeaderStorage.Add(byte3);
                            channel = 64 + byte2 + (256 * byte3);
                        }
                        #endregion

                        #region Message Header
                        int headerType = (basicHeader & 0xC0);
                        int headerSize = 0;
                        if (headerType == 0x00)
                        {
                            headerSize = 12;
                        }
                        else if (headerType == 0x40)
                        {
                            headerSize = 8;
                        }
                        else if (headerType == 0x80)
                        {
                            headerSize = 4;
                        }
                        else if (headerType == 0xC0)
                        {
                            headerSize = 0;
                        }

                        // Retrieve the packet or make a new one
                        if (!currentPackets.ContainsKey(channel))
                        {
                            currentPackets.Add(channel, new Packet());
                        }
                        //Console.Out.WriteLine("Packet:" + currentPackets.Count);
                        Packet p = currentPackets[channel];
                        p.AddToRaw(basicHeaderStorage.ToArray());

                        if (headerSize == 12)
                        {
                            //Timestamp
                            byte[] timestamp = new byte[3];
                            for (int i = 0; i < 3; i++)
                            {
                                timestamp[i] = (byte)sslStream.ReadByte();
                                p.AddToRaw(timestamp[i]);
                            }

                            //Message Length
                            byte[] messageLength = new byte[3];
                            for (int i = 0; i < 3; i++)
                            {
                                messageLength[i] = (byte)sslStream.ReadByte();
                                p.AddToRaw(messageLength[i]);
                            }
                            int size = 0;
                            for (int i = 0; i < 3; i++)
                            {
                                size = size * 256 + (messageLength[i] & 0xFF);
                            }
                            p.SetSize(size);

                            //Message Type
                            int messageType = sslStream.ReadByte();
                            p.AddToRaw((byte)messageType);
                            p.SetType(messageType);

                            //Message Stream ID
                            byte[] messageStreamID = new byte[4];
                            for (int i = 0; i < 4; i++)
                            {
                                messageStreamID[i] = (byte)sslStream.ReadByte();
                                p.AddToRaw(messageStreamID[i]);
                            }
                        }
                        else if (headerSize == 8)
                        {
                            //Timestamp
                            byte[] timestamp = new byte[3];
                            for (int i = 0; i < 3; i++)
                            {
                                timestamp[i] = (byte)sslStream.ReadByte();
                                p.AddToRaw(timestamp[i]);
                            }

                            //Message Length
                            byte[] messageLength = new byte[3];
                            for (int i = 0; i < 3; i++)
                            {
                                messageLength[i] = (byte)sslStream.ReadByte();
                                p.AddToRaw(messageLength[i]);
                            }
                            int size = 0;
                            for (int i = 0; i < 3; i++)
                            {
                                size = size * 256 + (messageLength[i] & 0xFF);
                            }
                            p.SetSize(size);

                            //Message Type
                            int messageType = sslStream.ReadByte();
                            p.AddToRaw((byte)messageType);
                            p.SetType(messageType);
                        }
                        else if (headerSize == 4)
                        {
                            //Timestamp
                            byte[] timestamp = new byte[3];
                            for (int i = 0; i < 3; i++)
                            {
                                timestamp[i] = (byte)sslStream.ReadByte();
                                p.AddToRaw(timestamp[i]);
                            }

                            if (p.GetSize() == 0 && p.GetPacketType() == 0)
                            {
                                if (previousReceivedPacket.ContainsKey(channel))
                                {
                                    p.SetSize(previousReceivedPacket[channel].GetSize());
                                    p.SetType(previousReceivedPacket[channel].GetPacketType());
                                }
                            }
                        }
                        else if (headerSize == 0)
                        {
                            if (p.GetSize() == 0 && p.GetPacketType() == 0)
                            {
                                if (previousReceivedPacket.ContainsKey(channel))
                                {
                                    p.SetSize(previousReceivedPacket[channel].GetSize());
                                    p.SetType(previousReceivedPacket[channel].GetPacketType());
                                }
                            }
                        }
                        #endregion

                        #region Message Body
                        //DefaultChunkSize is 128
                        for (int i = 0; i < 128; i++)
                        {
                            byte b = (byte)sslStream.ReadByte();
                            p.Add(b);
                            p.AddToRaw(b);

                            if (p.IsComplete())
                            {
                                break;
                            }
                        }

                        if (!p.IsComplete())
                        {
                            continue;
                        }

                        if (previousReceivedPacket.ContainsKey(channel))
                        {
                            previousReceivedPacket.Remove(channel);
                        }

                        previousReceivedPacket.Add(channel, p);

                        if (currentPackets.ContainsKey(channel))
                        {
                            currentPackets.Remove(channel);
                        }
                        #endregion


                        // Decode result
                        TypedObject result;
                        RTMPSDecoder decoder = new RTMPSDecoder();
                        if (p.GetPacketType() == 0x14) // Connect
                        {
                            result = decoder.DecodeConnect(p.GetData());
                        }
                        else if (p.GetPacketType() == 0x11) // Invoke
                        {
                            result = decoder.DecodeInvoke(p.GetData());
                        }
                        else if (p.GetPacketType() == 0x06) // Set peer bandwidth
                        {
                            byte[] data    = p.GetData();
                            int windowSize = 0;
                            for (int i = 0; i < 4; i++)
                            {
                                windowSize = windowSize * 256 + (data[i] & 0xFF);
                            }
                            int type = data[4];
                            continue;
                        }
                        else if (p.GetPacketType() == 0x05) // Window Acknowledgement Size
                        {
                            byte[] data    = p.GetData();
                            int windowSize = 0;
                            for (int i = 0; i < 4; i++)
                            {
                                windowSize = windowSize * 256 + (data[i] & 0xFF);
                            }
                            continue;
                        }
                        else if (p.GetPacketType() == 0x03) // Ack
                        {
                            byte[] data = p.GetData();
                            int ackSize = 0;
                            for (int i = 0; i < 4; i++)
                            {
                                ackSize = ackSize * 256 + (data[i] & 0xFF);
                            }
                            continue;
                        }
                        else if (p.GetPacketType() == 0x02) //ABORT
                        {
                            byte[] data = p.GetData();
                            continue;
                        }
                        else if (p.GetPacketType() == 0x01) //MaxChunkSize
                        {
                            byte[] data = p.GetData();
                            continue;
                        }
                        else
                        // Skip most messages
                        {
                            continue;
                        }

                        // Store result
                        int?id = result.GetInt("invokeId");

                        //Check to see if the result is valid.
                        //If it isn't, give an error and remove the callback if there is one.
                        if (result["result"].Equals("_error"))
                        {
                            Error(GetErrorMessage(result), GetErrorCode(result), ErrorType.Receive);
                        }

                        if (result["result"].Equals("receive"))
                        {
                            if (result.GetTO("data") != null)
                            {
                                TypedObject to = result.GetTO("data");
                                if (to.ContainsKey("body"))
                                {
                                    if (to["body"] is TypedObject)
                                    {
                                        new Thread(new ThreadStart(() =>
                                        {
                                            TypedObject body = (TypedObject)to["body"];
                                            if (body.type.Equals("com.riotgames.platform.game.GameDTO"))
                                            {
                                                MessageReceived(new GameDTO(body));
                                            }
                                            else if (body.type.Equals("com.riotgames.platform.game.PlayerCredentialsDto"))
                                            {
                                                MessageReceived(new PlayerCredentialsDto(body));
                                            }
                                            else if (body.type.Equals("com.riotgames.platform.game.message.GameNotification"))
                                            {
                                                MessageReceived(new GameNotification(body));
                                            }
                                            else if (body.type.Equals("com.riotgames.platform.matchmaking.SearchingForMatchNotification"))
                                            {
                                                MessageReceived(new SearchingForMatchNotification(body));
                                            }
                                            else if (body.type.Equals("com.riotgames.platform.messaging.StoreFulfillmentNotification"))
                                            {
                                                MessageReceived(new StoreFulfillmentNotification(body));
                                            }
                                            else if (body.type.Equals("com.riotgames.platform.messaging.StoreFulfillmentNotification"))
                                            {
                                                MessageReceived(new StoreAccountBalanceNotification(body));
                                            }
                                            else
                                            {
                                                MessageReceived(body);
                                            }
                                        })).Start();
                                    }
                                }
                            }
                            //MessageReceived(
                        }

                        if (id == null)
                        {
                            continue;
                        }

                        if (id == 0)
                        {
                        }
                        else if (callbacks.ContainsKey((int)id))
                        {
                            RiotGamesObject cb = callbacks[(int)id];
                            callbacks.Remove((int)id);
                            if (cb != null)
                            {
                                TypedObject messageBody = result.GetTO("data").GetTO("body");
                                new Thread(() =>
                                {
                                    cb.DoCallback(messageBody);
                                }).Start();
                            }
                        }

                        else
                        {
                            results.Add((int)id, result);
                        }

                        pendingInvokes.Remove((int)id);
                    }
                }
                catch (Exception e)
                {
                    if (IsConnected())
                    {
                        Error(e.Message, ErrorType.Receive);
                    }

                    //Disconnect();
                }
            });
            decodeThread.Start();
        }
コード例 #4
0
 private string GetErrorCode(TypedObject message)
 {
     return(message.GetTO("data").GetTO("rootCause").GetString("errorCode"));
 }
コード例 #5
0
 private string GetErrorMessage(TypedObject message)
 {
     // Works for clientVersion
     return(message.GetTO("data").GetTO("rootCause").GetString("message"));
 }
コード例 #6
0
        private bool GetAuthKey()
        {
            try
            {
                StringBuilder sb      = new StringBuilder();
                string        payload = "user="******",password="******"payload=" + payload;

                if (useGarena)
                {
                    payload = garenaToken;
                }

                WebRequest con = WebRequest.Create(loginQueue + "login-queue/rest/queue/authenticate");
                con.Method = "POST";

                Stream outputStream = con.GetRequestStream();
                outputStream.Write(Encoding.ASCII.GetBytes(query), 0, Encoding.ASCII.GetByteCount(query));

                WebResponse webresponse = con.GetResponse();
                Stream      inputStream = webresponse.GetResponseStream();

                int c;
                while ((c = inputStream.ReadByte()) != -1)
                {
                    sb.Append((char)c);
                }

                TypedObject result = serializer.Deserialize <TypedObject>(sb.ToString());
                outputStream.Close();
                inputStream.Close();
                con.Abort();

                if (!result.ContainsKey("token"))
                {
                    int    node  = (int)result.GetInt("node");
                    string champ = result.GetString("champ");
                    int    rate  = (int)result.GetInt("rate");
                    int    delay = (int)result.GetInt("delay");

                    int id  = 0;
                    int cur = 0;

                    object[] tickers = result.GetArray("tickers");
                    foreach (object o in tickers)
                    {
                        Dictionary <string, object> to = (Dictionary <string, object>)o;

                        int tnode = (int)to["node"];
                        if (tnode != node)
                        {
                            continue;
                        }

                        id  = (int)to["id"];
                        cur = (int)to["current"];
                        break;
                    }

                    while (id - cur > rate)
                    {
                        sb.Clear();
                        if (OnLoginQueueUpdate != null)
                        {
                            OnLoginQueueUpdate(this, id - cur);
                        }

                        Thread.Sleep(delay);
                        con         = WebRequest.Create(loginQueue + "login-queue/rest/queue/ticker/" + champ);
                        con.Method  = "GET";
                        webresponse = con.GetResponse();
                        inputStream = webresponse.GetResponseStream();

                        int d;
                        while ((d = inputStream.ReadByte()) != -1)
                        {
                            sb.Append((char)d);
                        }

                        result = serializer.Deserialize <TypedObject>(sb.ToString());


                        inputStream.Close();
                        con.Abort();

                        if (result == null)
                        {
                            continue;
                        }

                        cur = HexToInt(result.GetString(node.ToString()));
                    }



                    while (sb.ToString() == null || !result.ContainsKey("token"))
                    {
                        try
                        {
                            sb.Clear();

                            if (id - cur < 0)
                            {
                                if (OnLoginQueueUpdate != null)
                                {
                                    OnLoginQueueUpdate(this, 0);
                                }
                                else
                                if (OnLoginQueueUpdate != null)
                                {
                                    OnLoginQueueUpdate(this, id - cur);
                                }
                            }

                            Thread.Sleep(delay / 10);
                            con         = WebRequest.Create(loginQueue + "login-queue/rest/queue/authToken/" + user.ToLower());
                            con.Method  = "GET";
                            webresponse = con.GetResponse();
                            inputStream = webresponse.GetResponseStream();

                            int f;
                            while ((f = inputStream.ReadByte()) != -1)
                            {
                                sb.Append((char)f);
                            }

                            result = serializer.Deserialize <TypedObject>(sb.ToString());

                            inputStream.Close();
                            con.Abort();
                        }
                        catch
                        {
                        }
                    }
                }
                if (OnLoginQueueUpdate != null)
                {
                    OnLoginQueueUpdate(this, 0);
                }
                authToken = result.GetString("token");

                return(true);
            }
            catch (Exception e)
            {
                if (e.Message == "The remote name could not be resolved: '" + loginQueue + "'")
                {
                    Error("Please make sure you are connected the internet!", ErrorType.AuthKey);
                    Disconnect();
                }
                else if (e.Message == "The remote server returned an error: (403) Forbidden.")
                {
                    Error("usernamepassfail", ErrorType.Password);
                    Disconnect();
                }
                else
                {
                    Error("Unable to get Auth Key \n" + e, ErrorType.AuthKey);
                    Disconnect();
                }

                return(false);
            }
        }