Пример #1
0
        public static Session FromBytes(byte[] buffer, ISessionStore store, string sessionUserId)
        {
            using (var stream = new MemoryStream(buffer))
                using (var reader = new TBinaryReader(stream))
                {
                    var id            = reader.ReadUInt64();
                    var sequence      = reader.ReadInt32();
                    var salt          = reader.ReadUInt64();
                    var lastMessageId = reader.ReadInt64();
                    var timeOffset    = reader.ReadInt32();
                    var serverAddress = reader.ReadString();
                    var port          = reader.ReadInt32();

                    User user = null;
                    if (reader.ReadBoolean())
                    {
                        user = reader.Read <User>();
                    }

                    var authData = reader.ReadBytes();

                    return(new Session(store)
                    {
                        AuthKey = new AuthKey(authData),
                        Id = id,
                        Salt = salt,
                        Sequence = sequence,
                        LastMessageId = lastMessageId,
                        TimeOffset = timeOffset,
                        User = user,
                        SessionUserId = sessionUserId,
                        ServerAddress = serverAddress,
                        Port = port
                    });
                }
        }
Пример #2
0
        private bool HandleRpcResult(long messageId, int sequence, TBinaryReader messageReader, MTProtoRequest request)
        {
            uint  code      = messageReader.ReadUInt32();
            ulong requestId = messageReader.ReadUInt64();

            if (requestId == (ulong)request.MessageId)
            {
                request.ConfirmReceived = true;
            }

            //throw new NotImplementedException();

            /*
             *          lock (runningRequests)
             *          {
             *                  if (!runningRequests.ContainsKey(requestId))
             *                  {
             *                          logger.warning("rpc response on unknown request: {0}", requestId);
             *                          messageReader.BaseStream.Position -= 12;
             *                          return false;
             *                  }
             *                  request = runningRequests[requestId];
             *                  runningRequests.Remove(requestId);
             *          }
             */

            uint innerCode = messageReader.ReadUInt32();

            if (innerCode == 0x2144ca19)
            { // rpc_error
                int    errorCode    = messageReader.ReadInt32();
                string errorMessage = messageReader.ReadString();

                if (errorMessage.StartsWith("FLOOD_WAIT_"))
                {
                    var resultString = Regex.Match(errorMessage, @"\d+").Value;
                    var seconds      = int.Parse(resultString);
                    Debug.WriteLine($"Should wait {seconds} sec.");
                    Thread.Sleep(1000 * seconds);
                }
                else if (errorMessage.StartsWith("PHONE_MIGRATE_"))
                {
                    var resultString = Regex.Match(errorMessage, @"\d+").Value;
                    var dcIdx        = int.Parse(resultString);
                    throw new InvalidOperationException($"Your phone number registered to {dcIdx} dc. Please update settings. See https://github.com/sochix/TeleTurk#i-get-an-error-migrate_x for details.");
                }
                else
                {
                    throw new InvalidOperationException(errorMessage);
                }
            }
            else if (innerCode == 0x3072cfa1)
            {
                // gzip_packed
                byte[] packedData = messageReader.ReadBytes();
                using (MemoryStream packedStream = new MemoryStream(packedData, false))
                    using (System.IO.Compression.GZipStream zipStream = new System.IO.Compression.GZipStream(packedStream, System.IO.Compression.CompressionMode.Decompress))
                        using (TBinaryReader compressedReader = new TBinaryReader(zipStream))
                        {
                            request.OnResponse(compressedReader);
                        }
            }
            else
            {
                messageReader.BaseStream.Position -= 4;

                request.OnResponse(messageReader);
            }

            return(false);
        }