示例#1
0
        private bool HandleFutureSalts(ulong messageId, int sequence, BinaryReader messageReader)
        {
            uint  code      = messageReader.ReadUInt32();
            ulong requestId = messageReader.ReadUInt64();

            messageReader.BaseStream.Position -= 12;

            if (!runningRequests.ContainsKey(requestId))
            {
                logger.info("future salts on unknown request");
                return(false);
            }

            MTProtoRequest request = runningRequests[requestId];

            runningRequests.Remove(requestId);
            request.OnResponse(messageReader);

            return(true);
        }
示例#2
0
        //private List<MTProtoRequest> pendingRequests = new List<MTProtoRequest>();

        public async Task <T> Call <T>(byte[] requestData)
        {
            logger.info("call data: {0}", BitConverter.ToString(requestData));
            MTProtoRequest <T> request = new MTProtoRequest <T>(requestData);

            //Task.Run(() => Submit(request));
            try {
                await Submit(request);
            } catch (Exception e) {
                logger.warning("failed to submit request: {0}", e);
            }

            return(await request.Task);

            /*
             * try {
             *  return await request.Task;
             * } catch (MTProtoBadServerSaltException e) {
             *  salt = e.Salt;
             * } catch (MTProtoBadMessageException e) {
             *  logger.warning(("bad msg notification, possible session is broken"));
             *  OnBrokenSessionEvent();
             * }*/
        }
示例#3
0
        public async Task Submit(MTProtoRequest requestToSubmit)
        {
            List <MTProtoRequest> requests = new List <MTProtoRequest>();

            if (requestToSubmit != null)
            {
                requests.Add(requestToSubmit);
            }


            lock (runningRequests) {
                foreach (var runningRequest in runningRequests.Values)
                {
                    if (runningRequest.NeedResend)
                    {
                        requests.Add(runningRequest);
                    }
                }

                if (needConfirmation.Count > 0)
                {
                    requests.Add(makeAck());
                }

                lastSend = DateTime.Now;
            }

            // nothing to send (impossible)
            if (requests.Count == 0)
            {
                return;
            }

            // send one packet
            if (requests.Count == 1 && requestToSubmit != null)
            {
                MTProtoRequest request = requests[0];
                request.MessageId = GetNewMessageId();
                request.Sequence  = session.GenerateSequence(request.Confirmed);

                logger.info("send single request: {0} with id {1}", request, request.MessageId);

                byte[] requestBytes;
                using (MemoryStream memory = new MemoryStream()) {
                    using (BinaryWriter writer = new BinaryWriter(memory)) {
                        request.OnSend(writer);
                        requestBytes = memory.ToArray();
                    }
                }

                if (request.Responded)
                {
                    lock (runningRequests) {
                        if (!runningRequests.ContainsKey(request.MessageId))
                        {
                            runningRequests[request.MessageId] = request;
                        }
                        //runningRequests.Add(request.MessageId, request);
                    }
                }

                await RawSend(request.MessageId, request.Sequence, requestBytes);

                request.OnSendSuccess();

                return;
            }

            // send multiple packets in container
            byte[] container;
            using (MemoryStream memoryStream = new MemoryStream())
                using (BinaryWriter writer = new BinaryWriter(memoryStream)) {
                    writer.Write(0x73f1f8dc);
                    writer.Write(requests.Count);

                    foreach (MTProtoRequest request in requests)
                    {
                        if (runningRequests.ContainsKey(request.MessageId))
                        {
                            runningRequests.Remove(request.MessageId);
                        }

                        if (!request.Sended)
                        {
                            request.MessageId = GetNewMessageId();
                            request.Sequence  = session.GenerateSequence(request.Confirmed);
                        }

                        logger.info("send request in container: {0} with id {1}", request, request.MessageId);

                        writer.Write(request.MessageId);
                        writer.Write(request.Sequence);

                        byte[] packet;
                        using (MemoryStream packetMemoryStream = new MemoryStream()) {
                            using (BinaryWriter packetWriter = new BinaryWriter(packetMemoryStream)) {
                                request.OnSend(packetWriter);
                                packet = packetMemoryStream.ToArray();
                            }
                        }

                        writer.Write(packet.Length);
                        writer.Write(packet);

                        if (request.Responded)
                        {
                            lock (runningRequests) {
                                if (!runningRequests.ContainsKey(request.MessageId))
                                {
                                    runningRequests.Add(request.MessageId, request);
                                }
                            }
                            //runningRequests.Add(request.MessageId, request);
                        }

                        request.OnSendSuccess();
                    }

                    container = memoryStream.ToArray();
                }

            await RawSend(GetNewMessageId(), session.GenerateSequence(false), container);
        }