示例#1
0
        /// <summary>
        /// Asynchronously executes function and returns response
        /// </summary>
        public override Task <TResult> ExecuteAsync <TResult>(TdApi.Function <TResult> function)
        {
            if (_receiver == null)
            {
                throw new ObjectDisposedException("TDLib client was disposed");
            }

            var id  = Interlocked.Increment(ref _taskId);
            var tcs = new TaskCompletionSource <TResult>(TaskCreationOptions.RunContinuationsAsynchronously);

            function.Extra = id.ToString();
            _tasks.TryAdd(id, structure =>
            {
                if (structure is TdApi.Error error)
                {
                    tcs.SetException(new TdException(error));
                }
                else if (structure is TResult result)
                {
                    tcs.SetResult(result);
                }
            });

            Send(function);

            return(tcs.Task);
        }
示例#2
0
        public void Send <TResut>(TdApi.Function <TResut> function)
        {
            var data = JsonConvert.SerializeObject(function);

            _logger.LogDebug(data);
            _client.Send(data);
        }
示例#3
0
        public TdApi.Object Execute <TResult>(TdApi.Function <TResult> function)
        {
            var data = JsonConvert.SerializeObject(function);

            data = _client.Execute(data);
            var structure = JsonConvert.DeserializeObject <TdApi.Object>(data, new Converter());

            return(structure);
        }
示例#4
0
        /// <summary>
        /// Executes function and ignores response
        /// </summary>
        public override void Send <TResut>(TdApi.Function <TResut> function)
        {
            if (_receiver == null)
            {
                throw new ObjectDisposedException("TDLib client was disposed");
            }

            var data = JsonConvert.SerializeObject(function);

            _tdJsonClient.Send(data);
        }
示例#5
0
        public IObservable <T> Execute <T>(TdApi.Function <T> function, CancellationToken cancellationToken)
            where T : TdApi.Object
        {
            var delay = Task.Delay(Timeout.Infinite, cancellationToken)
                        .ContinueWith <T>(_ => throw new TaskCanceledException("Execution timeout"));

            var task = Task.WhenAny(delay, _dialer.ExecuteAsync(function))
                       .ContinueWith(t => t.Result.Result);

            return(task.ToObservable());
        }
示例#6
0
        /// <summary>
        /// Synchronously executes function and returns response
        /// </summary>
        public override TResult Execute <TResult>(TdApi.Function <TResult> function)
        {
            if (_receiver == null)
            {
                throw new ObjectDisposedException("TDLib client was disposed");
            }

            var data = JsonConvert.SerializeObject(function);

            data = _tdJsonClient.Execute(data);
            var structure = JsonConvert.DeserializeObject <TdApi.Object>(data, new Converter());

            if (structure is TdApi.Error error)
            {
                throw new TdException(error);
            }

            return((TResult)structure);
        }
示例#7
0
        public Task <TResult> ExecuteAsync <TResult>(TdApi.Function <TResult> function)
            where TResult : TdApi.Object
        {
            var id  = Interlocked.Increment(ref _id);
            var tcs = new TaskCompletionSource <TResult>();

            function.Extra = id.ToString();
            _tasks.TryAdd(id, structure =>
            {
                if (structure is TdApi.Error err)
                {
                    tcs.SetException(new ErrorException(err));
                }
                else if (structure is TResult result)
                {
                    tcs.SetResult(result);
                }
            });

            Send(function);

            return(tcs.Task);
        }
示例#8
0
 public IObservable <T> Execute <T>(TdApi.Function <T> function)
     where T : TdApi.Object
 {
     return(_dialer.ExecuteAsync(function).ToObservable());
 }