Пример #1
0
        private static unsafe void HandleSessionError(IntPtr sessionHandlePtr, ErrorCode errorCode, byte *messageBuffer, IntPtr messageLength, IntPtr userInfoPairs, int userInfoPairsLength, bool isClientReset)
        {
            try
            {
                var handle  = new SessionHandle(sessionHandlePtr);
                var session = new Session(handle);
                var message = Encoding.UTF8.GetString(messageBuffer, (int)messageLength);

                SessionException exception;

                if (isClientReset)
                {
                    var userInfo = StringStringPair.UnmarshalDictionary(userInfoPairs, userInfoPairsLength);
                    exception = new ClientResetException(session.User.App, message, userInfo);
                }
                else if (errorCode == ErrorCode.PermissionDenied)
                {
                    var userInfo = StringStringPair.UnmarshalDictionary(userInfoPairs, userInfoPairsLength);
                    exception = new PermissionDeniedException(session.User.App, message, userInfo);
                }
                else
                {
                    exception = new SessionException(message, errorCode);
                }

                Session.RaiseError(session, exception);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        private static async void ExecuteRequest(HttpClientRequest request, IntPtr callback)
        {
            try
            {
                try
                {
                    using var message = new HttpRequestMessage(request.method.ToHttpMethod(), request.Url);
                    foreach (var header in StringStringPair.UnmarshalDictionary(request.headers, request.headers_len))
                    {
                        message.Headers.TryAddWithoutValidation(header.Key, header.Value);
                    }

                    if (request.method != NativeHttpMethod.get)
                    {
                        message.Content = new StringContent(request.Body, Encoding.UTF8, "application/json");
                    }

                    using var cts = new CancellationTokenSource();
                    cts.CancelAfter((int)request.timeout_ms);

                    var response = await _httpClient.SendAsync(message, cts.Token);

                    var headers = new List <StringStringPair>(response.Headers.Count());
                    foreach (var header in response.Headers)
                    {
                        headers.Add(new StringStringPair
                        {
                            Key   = header.Key,
                            Value = header.Value.FirstOrDefault()
                        });
                    }

                    foreach (var header in response.Content.Headers)
                    {
                        headers.Add(new StringStringPair
                        {
                            Key   = header.Key,
                            Value = header.Value.FirstOrDefault()
                        });
                    }

                    var nativeResponse = new HttpClientResponse
                    {
                        http_status_code = (int)response.StatusCode,
                        Body             = await response.Content.ReadAsStringAsync(),
                    };

                    respond(nativeResponse, headers.ToArray(), headers.Count, callback);
                }
                catch (HttpRequestException rex)
                {
                    var sb = new StringBuilder("An unexpected error occurred while sending the request");

                    // We're doing this because the message for the top-level exception is usually pretty useless.
                    // If there's inner exception, we want to skip it and directly go for the more specific messages.
                    var innerEx = rex.InnerException ?? rex;
                    while (innerEx != null)
                    {
                        sb.Append($": {innerEx.Message}");
                        innerEx = innerEx.InnerException;
                    }

                    var nativeResponse = new HttpClientResponse
                    {
                        custom_status_code = CustomErrorCode.UnknownHttp,
                        Body = sb.ToString(),
                    };

                    respond(nativeResponse, null, 0, callback);
                }
                catch (TaskCanceledException)
                {
                    var nativeResponse = new HttpClientResponse
                    {
                        custom_status_code = CustomErrorCode.Timeout,
                        Body = $"Operation failed to complete within {request.timeout_ms} ms.",
                    };

                    respond(nativeResponse, null, 0, callback);
                }
                catch (Exception ex)
                {
                    var nativeResponse = new HttpClientResponse
                    {
                        custom_status_code = CustomErrorCode.Unknown,
                        Body = ex.Message,
                    };

                    respond(nativeResponse, null, 0, callback);
                }
            }
            catch (Exception outerEx)
            {
                Debug.WriteLine($"Unexpected error occurred while trying to respond to a request: {outerEx}");
            }
        }