Пример #1
0
 public Api(ApiSetting setting)
 {
     _locker          = new ManualResetEvent(true);
     _setting         = setting;
     _responseHandler = new ApiResponseHandler(this);
     _globalHandlers  = new ConcurrentDictionary <Type, Action <BaseObject> >();
     functions        = new Functions(this);
 }
Пример #2
0
        /// <summary>
        /// executes custom tdlib command synchronously
        /// </summary>
        /// <param name="command">command to execute</param>
        /// <param name="expectedType">expected type to reveice after command</param>
        /// <param name="timeOut">timeout (ms) to wait for command response</param>
        /// <returns>ResponseObject providing results</returns>
        public async Task <ResponseObject> executeCommand(Function command, Type expectedType = null, int timeOut = 5000)
        {
            // object to return as response
            ResponseObject res = new ResponseObject();

            // function to get invoked when update came ( any type )
            Action <BaseObject> _handler = (obj) =>
            {
                // setting response
                res.obj = obj;

                // check if expected type is set
                if (expectedType != null)
                {
                    // check if gathered update type is same as expected type
                    if (obj.GetType() == expectedType)
                    {
                        res.status = Enum.ResponseStatus.Success;
                    }
                    else
                    {
                        res.status = Enum.ResponseStatus.Failed;
                    }
                }
                else
                {
                    // if expectedType is null, status is always successfull
                    res.status = Enum.ResponseStatus.Success;
                }
            };

            // custom handler
            ApiResponseHandler customHandler = new ApiResponseHandler(this, _handler);

            // sending request
            _client.Send(command, customHandler);

            // waiting for timeOut
            // if (!resetEvent.WaitOne(timeOut)) res.status = Enum.ResponseStatus.TimedOut;
            for (int i = 0; i < timeOut; i += 1000)
            {
                // check if response gathered
                if (res.status != Enum.ResponseStatus.Processing)
                {
                    break;
                }

                await Task.Delay(1000);
            }

            // if response object is null, means operation is timed out
            if (res.obj is null)
            {
                res.status = Enum.ResponseStatus.TimedOut;
            }

            return(res);
        }