Ejemplo n.º 1
0
        private void SendAsyncInternal <T>(string caption, double timeout, TLObject obj, Action <T> callback, Action fastCallback, Action <TLRPCError> faultCallback)
        {
            int  sequenceNumber;
            long messageId;

            lock (_activeTransportRoot)
            {
                sequenceNumber = _activeTransport.SequenceNumber * 2 + 1;
                _activeTransport.SequenceNumber++;
                messageId = _activeTransport.GenerateMessageId(true);
            }

            var transportMessage = new TLContainerTransportMessage
            {
                MsgId = messageId,
                SeqNo = sequenceNumber,
                Query = obj
            };

            var now            = DateTime.Now;
            var sendBeforeTime = now.AddSeconds(timeout);
            var sendingItem    = new HistoryItem
            {
                SendTime           = now,
                SendBeforeTime     = sendBeforeTime,
                Caption            = caption,
                Object             = obj,
                Message            = transportMessage,
                Callback           = result => callback((T)result),
                FastCallback       = fastCallback,
                FaultCallback      = null,          // чтобы не вылететь по таймауту не сохраняем сюда faultCallback, а просто запоминаем последнюю ошибку,
                FaultQueueCallback = faultCallback, // для MTProto.CleanupQueue
                InvokeAfter        = null,          // устанвливаем в момент создания контейнера historyItems.LastOrDefault(),
                Status             = RequestStatus.ReadyToSend,
            };

            //обрабатываем ошибки
            sendingItem.FaultCallback = error => ProcessFault(sendingItem, error);

            AddActionInfoToFile(TLUtils.DateToUniversalTimeTLInt(ClientTicksDelta, sendBeforeTime), obj);

            lock (_sendingQueueSyncRoot)
            {
                _sendingQueue.Add(sendingItem);

                StartSendingTimer();
            }

            ProcessQueue();
        }
        private static TLEncryptedTransportMessage CreateTLEncryptedMessage(byte[] authKey, TLContainerTransportMessage containerTransportMessage)
        {
            var message = new TLEncryptedTransportMessage {
                Data = containerTransportMessage.ToBytes()
            };

            return(message.Encrypt(authKey));
        }
        private void GetPassportRequestsInternal(TLObject[] requests, Action <TLObject> getResultCallback, Action <TLRPCError> faultCallback = null)
        {
            var container = new TLContainer {
                Messages = new List <TLContainerTransportMessage>()
            };
            var historyItems = new List <HistoryItem>();

            for (var i = 0; i < requests.Length; i++)
            {
                var    obj = requests[i];
                int    sequenceNumber;
                TLLong messageId;
                lock (_activeTransportRoot)
                {
                    sequenceNumber = _activeTransport.SequenceNumber * 2 + 1;
                    _activeTransport.SequenceNumber++;
                    messageId = _activeTransport.GenerateMessageId(true);
                }

                var data = i > 0 ? new TLInvokeAfterMsg {
                    MsgId = container.Messages[i - 1].MessageId, Object = obj
                } : obj;

                var transportMessage = new TLContainerTransportMessage
                {
                    MessageId   = messageId,
                    SeqNo       = new TLInt(sequenceNumber),
                    MessageData = data
                };

                var historyItem = new HistoryItem
                {
                    SendTime         = DateTime.Now,
                    Caption          = "passport.item" + i,
                    Object           = obj,
                    Message          = transportMessage,
                    Callback         = getResultCallback,
                    AttemptFailed    = null,
                    FaultCallback    = faultCallback,
                    ClientTicksDelta = ClientTicksDelta,
                    Status           = RequestStatus.Sent,
                };
                historyItems.Add(historyItem);

                container.Messages.Add(transportMessage);
            }

            lock (_historyRoot)
            {
                foreach (var item in historyItems)
                {
                    _history[item.Hash] = item;
                }
            }
#if DEBUG
            NotifyOfPropertyChange(() => History);
#endif

            SendNonInformativeMessage <TLObject>("passport.container", container,
                                                 result =>
            {
            },
                                                 faultCallback);
        }