public void StartReceiving()
        {
            if (_recievingCts != null && _recievingCts.IsCancellationRequested)
            {
                return;
            }

            _recievingCts = new CancellationTokenSource();

            Task.Run(
                () =>
            {
                while (!_recievingCts.Token.IsCancellationRequested)
                {
                    try
                    {
                        var recieveTask = TcpTransport.Receieve();
                        recieveTask.Wait(_recievingCts.Token);
                        var recieveData = recieveTask.Result;

                        var decodedData = DecodeMessage(recieveData);

                        Log.Debug($"Recieve message with remote id: {decodedData.Item2}");

                        ProcessReceivedMessage(decodedData.Item1);

                        ConfirmationSendService.AddForSend(decodedData.Item2);
                    }
                    catch (Exception e)
                    {
                        Log.Error("Recieve message failed", e);
                    }
                }
            });
        }
Example #2
0
        public async Task <byte[]> SendAndReceive(byte[] data)
        {
            var preparedPacket = PrepareToSend(data);

            await TcpTransport.Send(preparedPacket).ConfigureAwait(false);

            var result = await TcpTransport.Receieve().ConfigureAwait(false);

            return(ProcessReceivedMessage(result));
        }
        public async Task <byte[]> SendAndReceive(byte[] data, CancellationToken cancellationToken = default(CancellationToken))
        {
            var preparedPacket = PrepareToSend(data);

            await TcpTransport.Send(preparedPacket, cancellationToken).ConfigureAwait(false);

            var result = await TcpTransport.Receieve().ConfigureAwait(false);

            return(ProcessReceivedMessage(result));
        }
        private async Task StartRecievingTask(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    var recieveData = await TcpTransport.Receieve().ConfigureAwait(false);

                    var decodedData = DecodeMessage(recieveData);

                    Log.Debug($"Receive message with remote id: {decodedData.Item2}");

                    ProcessReceivedMessage(decodedData.Item1);

                    ConfirmationSendService.AddForSend(decodedData.Item2);
                }
                catch (Exception e)
                {
                    Log.Error("Receive message failed. Reconnecting", e);

                    var request = new RequestInvokeWithLayer
                    {
                        Layer = SchemaInfo.SchemaVersion,
                        Query = new RequestInitConnection
                        {
                            ApiId          = ClientSettings.AppId,
                            AppVersion     = "1.0.0",
                            DeviceModel    = "PC",
                            LangCode       = "en",
                            LangPack       = "tdesktop",
                            Query          = new RequestGetConfig(),
                            SystemLangCode = "en",
                            SystemVersion  = "Win 10.0"
                        }
                    };

                    try
                    {
                        await TcpTransport.Disconnect().ConfigureAwait(false);

                        var sendTask = await Sender.SendWithConfim(request).ConfigureAwait(false);

                        ResponseResultGetter.Receive(sendTask.Item2).ContinueWith(
                            async task =>
                        {
                            await sendTask.Item1.ConfigureAwait(false);
                        });
                    }
                    catch
                    {
                        Log.Error("Failed to reconnect", e);
                    }
                }
            }
        }
        private async Task StartRecievingTask(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    var recieveData = await TcpTransport.Receieve().ConfigureAwait(false);

                    var decodedData = DecodeMessage(recieveData);

                    Log.Debug($"Receive message with remote id: {decodedData.Item2}");

                    ProcessReceivedMessage(decodedData.Item1);

                    ConfirmationSendService.AddForSend(decodedData.Item2);
                }
                catch (Exception e)
                {
                    Log.Error("Receive message failed", e);
                }
            }
        }