private void WrapStatusAndRaiseBackgroundError(IntPtr apiStatusHandle)
        {
            using (ApiStatus status = new ApiStatus(apiStatusHandle))
            {
                EventHandler <ApiStatus> targetEventLocal = this.BackgroundErrorInternal;
                if (targetEventLocal != null)
                {
                    targetEventLocal.Invoke(this, status);
                }
                else
                {
                    // This comes strictly from the background thread - so simply throwing here has
                    // the right semantics with respect to AppDomain.UnhandledException. Unfortunately,
                    // that seems to bring down the process, if there is nothing Managed under the native
                    // stack this will cause an application-level unhandled native exception, and will
                    // likely terminate the application. So new up a thread, and throw from it.
                    // See https://stackoverflow.com/questions/42298126/raising-exception-on-managed-and-unmanaged-callback-chain-with-p-invoke

                    // IMPORTANT: This is safe solely because the status string is marshaled into the
                    // exception message on construction (in other words, before control returns to the
                    // unmanaged call-stack - the Dispose() is a no-op because in this case NativeObject does
                    // not own the unmanaged pointer, but we use it to remove itself from the finalizer queue)
                    RLException e = new RLException(status);
                    new System.Threading.Thread(() => throw e).Start();
                }
            }
        }
        public bool TryInit(ApiStatus apiStatus = null)
        {
            int result = NativeMethods.LiveModelInit(this.DangerousGetHandle(), apiStatus.ToNativeHandleOrNullptrDangerous());

            GC.KeepAlive(this);
            return(result == NativeMethods.SuccessStatus);
        }
        private async Task SendAsyncAndUnwrapExceptions(SharedBuffer buffer)
        {
            using (ApiStatus status = new ApiStatus())
            {
                try
                {
                    await this.SendAsync(buffer);
                }
                catch (RLException e)
                {
                    e.UpdateApiStatus(status);
                    this.RaiseBackgroundError(status);
                }
                catch (Exception e)
                {
                    ApiStatusBuilder builder = new ApiStatusBuilder(NativeMethods.OpaqueBindingError)
                                               .AppendLine(e.Message)
                                               .AppendLine(e.StackTrace);
                    builder.UpdateApiStatus(status);

                    this.RaiseBackgroundError(status);
                }
            }

            GC.KeepAlive(buffer);
        }
        public bool TryQueueActionTakenEvent(string eventId, ApiStatus apiStatus = null)
        {
            int result = LiveModelReportActionTaken(this.DangerousGetHandle(), eventId, apiStatus.ToNativeHandleOrNullptrDangerous());

            GC.KeepAlive(this);
            return(result == NativeMethods.SuccessStatus);
        }
        public bool TryQueueOutcomeEvent(string eventId, string outcomeJson, ApiStatus apiStatus = null)
        {
            int result = LiveModelReportOutcomeJson(this.DangerousGetHandle(), eventId, outcomeJson, apiStatus.ToNativeHandleOrNullptrDangerous());

            GC.KeepAlive(this);
            return(result == NativeMethods.SuccessStatus);
        }
Esempio n. 6
0
        public ApiStatus ToApiStatus()
        {
            ApiStatus result = new ApiStatus();

            ApiStatus.Update(result, this.errorCode, this.messageBuilder.ToString());
            return(result);
        }
Esempio n. 7
0
 public void QueueOutcomeEvent(string eventId, uint slotIndex, float outcome)
 {
     using (ApiStatus apiStatus = new ApiStatus())
         if (!this.TryQueueOutcomeEvent(eventId, slotIndex, outcome, apiStatus))
         {
             throw new RLException(apiStatus);
         }
 }
 public void Init()
 {
     using (ApiStatus apiStatus = new ApiStatus())
         if (!this.TryInit(apiStatus))
         {
             throw new RLException(apiStatus);
         }
 }
 public void QueueOutcomeEvent(string eventId, string outcomeJson)
 {
     using (ApiStatus apiStatus = new ApiStatus())
         if (!this.TryQueueOutcomeEvent(eventId, outcomeJson, apiStatus))
         {
             throw new RLException(apiStatus);
         }
 }
 public void RefreshModel()
 {
     using (ApiStatus apiStatus = new ApiStatus())
         if (!this.TryRefreshModel(apiStatus))
         {
             throw new RLException(apiStatus);
         }
 }
 public void QueueActionTakenEvent(string eventId)
 {
     using (ApiStatus apiStatus = new ApiStatus())
         if (!this.TryQueueActionTakenEvent(eventId, apiStatus))
         {
             throw new RLException(apiStatus);
         }
 }
Esempio n. 12
0
        internal int UpdateApiStatus(ApiStatus status)
        {
            string message = this.Message;

            WriteInnerExceptionOnDebug(ref message, this);

            ApiStatus.Update(status, this.ErrorCode, this.Message);
            return(this.ErrorCode);
        }
Esempio n. 13
0
        public void Send(SharedBuffer buffer, ApiStatus status)
        {
            // Create a cloned handle so that we have our own copy of the shared pointer to the buffer
            SharedBuffer ownedHandle = new SharedBuffer(buffer);

            GC.KeepAlive(buffer);

            // Fire off the send task, and return
            Task backgroundTask = SendAsyncAndUnwrapExceptions(ownedHandle);
        }
Esempio n. 14
0
        private void WrapStatusAndRaiseBackgroundError(IntPtr apiStatusHandle)
        {
            ApiStatus status = new ApiStatus(apiStatusHandle);

            EventHandler <ApiStatus> localEvent = this.BackgroundErrorInternal;

            if (localEvent != null)
            {
                localEvent(this, status);
            }
        }
        public RankingResponse ChooseRank(string eventId, string contextJson, ActionFlags flags)
        {
            RankingResponse result = new RankingResponse();

            using (ApiStatus apiStatus = new ApiStatus())
                if (!this.TryChooseRank(eventId, contextJson, flags, result, apiStatus))
                {
                    throw new RLException(apiStatus);
                }

            return(result);
        }
Esempio n. 16
0
        public ContinuousActionResponse RequestContinuousAction(string eventId, string contextJson, ActionFlags flags)
        {
            ContinuousActionResponse result = new ContinuousActionResponse();

            using (ApiStatus apiStatus = new ApiStatus())
                if (!this.TryRequestContinuousAction(eventId, contextJson, flags, result, apiStatus))
                {
                    throw new RLException(apiStatus);
                }

            return(result);
        }
Esempio n. 17
0
        public static Configuration LoadConfigurationFromJson(string json)
        {
            using (ApiStatus apiStatus = new ApiStatus())
            {
                if (TryLoadConfigurationFromJson(json, out Configuration config, apiStatus))
                {
                    return(config);
                }

                throw new RLException(apiStatus);
            }
        }
Esempio n. 18
0
        public SlatesResponse RequestSlatesDecision(string eventId, string contextJson, ActionFlags flags)
        {
            SlatesResponse result = new SlatesResponse();

            using (ApiStatus apiStatus = new ApiStatus())
                if (!this.TryRequestSlatesDecision(eventId, contextJson, flags, result, apiStatus))
                {
                    throw new RLException(apiStatus);
                }

            return(result);
        }
        public DecisionResponse RequestDecision(string contextJson, ActionFlags flags)
        {
            DecisionResponse result = new DecisionResponse();

            using (ApiStatus apiStatus = new ApiStatus())
                if (!this.TryRequestDecision(contextJson, flags, result, apiStatus))
                {
                    throw new RLException(apiStatus);
                }

            return(result);
        }
Esempio n. 20
0
        public MultiSlotResponse RequestMultiSlotDecision(string eventId, string contextJson)
        {
            MultiSlotResponse result = new MultiSlotResponse();

            using (ApiStatus apiStatus = new ApiStatus())
                if (!this.TryRequestMultiSlotDecision(eventId, contextJson, result, apiStatus))
                {
                    throw new RLException(apiStatus);
                }

            return(result);
        }
        // TODO: Why does this method call, which seems like a "get" of a value, have an API status?
        public bool TryGetChosenAction(out long actionIndex, ApiStatus status = null)
        {
            actionIndex = -1;
            UIntPtr chosenAction;
            int     result = NativeMethods.GetRankingChosenAction(this.NativeHandle, out chosenAction, status.ToNativeHandleOrNullptr());

            if (result != NativeMethods.SuccessStatus)
            {
                return(false);
            }

            actionIndex = (long)(chosenAction.ToUInt64());
            return(true);
        }
Esempio n. 22
0
        // TODO: Why does this method call, which seems like a "get" of a value, have an API status?
        public bool TryGetChosenAction(out long actionIndex, ApiStatus status = null)
        {
            actionIndex = -1;
            UIntPtr chosenAction;
            int     result = NativeMethods.GetSlotChosenAction(this.DangerousGetHandle(), out chosenAction, status.ToNativeHandleOrNullptrDangerous());

            bool success = (result == NativeMethods.SuccessStatus);

            if (success)
            {
                actionIndex = (long)(chosenAction.ToUInt64());
            }

            GC.KeepAlive(this);
            return(success);
        }
        public bool TryRequestDecision(string contextJson, DecisionResponse response, ApiStatus apiStatus = null)
        {
            int result = LiveModelRequestDecision(this.DangerousGetHandle(), contextJson, response.DangerousGetHandle(), apiStatus.ToNativeHandleOrNullptrDangerous());

            GC.KeepAlive(this);
            return(result == NativeMethods.SuccessStatus);
        }
 public bool TryReportOutcome(string eventId, string outcomeJson, ApiStatus apiStatus = null)
 => this.TryQueueOutcomeEvent(eventId, outcomeJson, apiStatus);
 public bool TryRequestDecision(string contextJson, out DecisionResponse response, ApiStatus apiStatus = null)
 {
     response = new DecisionResponse();
     return(this.TryRequestDecision(contextJson, response, apiStatus));
 }
 public bool TryReportActionTaken(string eventId, ApiStatus apiStatus = null)
 => this.TryQueueActionTakenEvent(eventId, apiStatus);
        public bool TryRequestDecision(string contextJson, ActionFlags flags, DecisionResponse response, ApiStatus apiStatus)
        {
            int result = LiveModelRequestDecisionWithFlags(this.DangerousGetHandle(), contextJson, (uint)flags, response.DangerousGetHandle(), apiStatus.ToNativeHandleOrNullptrDangerous());

            return(result == NativeMethods.SuccessStatus);
        }
        public bool TryRequestDecision(string contextJson, ActionFlags flags, out DecisionResponse response, ApiStatus apiStatus)
        {
            response = new DecisionResponse();

            GC.KeepAlive(this);
            return(this.TryRequestDecision(contextJson, flags, response, apiStatus));
        }
        public bool TryRefreshModel(ApiStatus apiStatus = null)
        {
            int result = NativeMethods.LiveModelRefreshModel(this.DangerousGetHandle(), apiStatus.ToNativeHandleOrNullptrDangerous());

            return(result == NativeMethods.SuccessStatus);
        }
        public bool TryChooseRank(string eventId, string contextJson, ActionFlags flags, RankingResponse response, ApiStatus apiStatus = null)
        {
            int result = LiveModelChooseRankWithFlags(this.DangerousGetHandle(), eventId, contextJson, (uint)flags, response.DangerousGetHandle(), apiStatus.ToNativeHandleOrNullptrDangerous());

            GC.KeepAlive(this);
            return(result == NativeMethods.SuccessStatus);
        }