Esempio n. 1
0
        public TonClientRustAdapter(string configJson, ILogger <TonClientRustAdapter> logger)
        {
            _logger = logger;
            _logger.LogTrace("Creating context with options: {config}", configJson);
            using var optionsInteropJson = configJson.ToInteropStringDisposable();
            IntPtr resultPtr = RustInteropInterface.tc_create_context(optionsInteropJson);

            _logger.LogTrace("Reading context creation result");
            InteropString resultInterop = RustInteropInterface.tc_read_string(resultPtr);
            var           resultJson    = resultInterop.ToString();

            RustInteropInterface.tc_destroy_string(resultPtr);
            _logger.LogTrace("Got context creation result: {result}", resultJson);

            var createContextResult = JsonSerializer.Deserialize <CreateContextResponse>(resultJson, JsonOptionsProvider.JsonSerializerOptions);

            if (createContextResult?.ContextNumber == null)
            {
                throw new TonClientException($"Raw result: {resultJson}", new NullReferenceException("Result of context creation or context number is null"));
            }
            ClientError error = createContextResult.Error;

            if (error != null)
            {
                throw TonClientException.CreateExceptionWithCodeWithData(error.Code, error.Data?.ToObject <Dictionary <string, object> >(), error.Message);
            }

            _contextNumber = (uint)createContextResult.ContextNumber;
        }
 private static TonClientException CreateException(ClientError clientError, Exception innerException, Func <string> errorRawTextFunc)
 {
     return(clientError == null
         ? new TonClientException($"Raw result: {errorRawTextFunc()}",
                                  innerException ?? new NullReferenceException("Result of error response is null or not valid"))
         : TonClientException.CreateExceptionWithCodeWithData(clientError.Code, clientError.Data.ToObject <Dictionary <string, object> >(),
                                                              clientError.Message));
 }