Пример #1
0
 public void Pulse()
 {
     if (handle.id != UIntPtr.Zero)
     {
         InterruptHandle.Pulse(handle);
     }
 }
Пример #2
0
        public bool ReleaseInterrupt()
        {
            bool ret = InterruptHandle.Dispose(handle);

            handle = new InterruptHandle();
            return(ret);
        }
Пример #3
0
 public bool AckInterrupt()
 {
     if (handle.id != UIntPtr.Zero)
     {
         return(InterruptHandle.Ack(handle));
     }
     return(false);
 }
Пример #4
0
 public bool WaitForInterrupt()
 {
     if (handle.id != UIntPtr.Zero)
     {
         return(InterruptHandle.Wait(handle));
     }
     return(false);
 }
Пример #5
0
        public bool RegisterInterrupt()
        {
            InterruptHandle handleOnStack;
            bool            success = InterruptHandle.Create(irq, out handleOnStack);

            handle = handleOnStack;
            return(success);
        }
Пример #6
0
        private async Task <string> GetResponseContent(string endpoint, string requestBody)
        {
            Logger.Verbose("Making request to " + endpoint + " endpoint...");

            var restClient  = new RestClient(ApiClient.ApiUrl);
            var restRequest = new RestRequest(endpoint, Method.POST)
            {
                RequestFormat = DataFormat.Json
            };

            restRequest.AddParameter("application/json", requestBody, ParameterType.RequestBody);

            if (IsAuthenticatedRequest)
            {
                if (string.IsNullOrEmpty(ApiClient.ApiKey))
                {
                    throw new MissingFieldException("API key not provided for authenticated request.");
                }

                if (string.IsNullOrEmpty(ApiClient.PrivateKeyPem))
                {
                    throw new MissingFieldException("Private key not provided");
                }

                var signature = AsymmetricCryptoUtil.CreateSignature(requestBody, ApiClient.PrivateKeyPem);
                restRequest.AddHeader("Authorization", "token " + ApiClient.ApiKey + ":" + signature);
            }

            IRestResponse restResponse = new RestResponse();

            if (InterruptHandleSet)
            {
                // Perform the request asynchronously, but block on the interrupt handle.
                var responseReceived = false;
                var asyncHandle      = restClient.ExecuteAsync(restRequest, response =>
                {
                    responseReceived = true;
                    restResponse     = response;
                    InterruptHandle.Set();
                });

                InterruptHandle.WaitOne();

                if (!responseReceived)
                {
                    asyncHandle.Abort();
                    throw new RequestException("Network error. Request was interrupted.");
                }
            }
            else
            {
                var cancellationTokenSource = new CancellationTokenSource();
                restResponse = await restClient.ExecuteTaskAsync(restRequest, cancellationTokenSource.Token);
            }

            if (restResponse.StatusCode == 0)
            {
                throw new RequestException("Network error. Could not connect to server.");
            }

            if (restResponse.StatusCode != HttpStatusCode.OK)
            {
                ErrorResponse errorResponse;
                try
                {
                    errorResponse = JsonConvert.DeserializeObject <ErrorResponse>(restResponse.Content);
                }
                catch (Exception)
                {
                    throw new NetworkErrorException("Unknown error whilst contacting server");
                }

                switch (restResponse.StatusCode)
                {
                case HttpStatusCode.BadRequest:
                    throw new BadRequestException(errorResponse.Error);

                case HttpStatusCode.Unauthorized:
                    throw new UnauthorizedException(errorResponse.Error);

                case HttpStatusCode.NotFound:
                    throw new NotFoundException(errorResponse.Error);

                case HttpStatusCode.Conflict:
                    throw new NotFoundException(errorResponse.Error);

                default:
                    throw new RequestException(errorResponse.Error);
                }
            }

            Logger.Verbose("Received response: " + restResponse.Content);

            return(restResponse.Content);
        }