Ejemplo n.º 1
0
        /// <summary>
        /// Waits while the machine pauses or stops
        /// </summary>
        public async Task WaitForPause()
        {
            try
            {
                ExecutionCompletionReason = await _completionTask;
            }
            catch (TaskCanceledException)
            {
                // --- Ok, run successfully cancelled
                ExecutionCompletionReason = ExecutionCompletionReason.Cancelled;
            }
            catch (Exception ex)
            {
                // --- Some other exception raised
                ExecutionCompletionReason = ExecutionCompletionReason.Exception;
                ExceptionRaised?.Invoke(this, new VmExceptionArgs(ex));
            }

            if (ExecutionCompletionReason != ExecutionCompletionReason.Cancelled &&
                ExecutionCompletionReason != ExecutionCompletionReason.Exception &&
                MachineState != VmState.Stopped)
            {
                MachineState = VmState.Paused;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds the binding.
        /// </summary>
        /// <param name="endpoint">The endpoint.</param>
        public void AddBinding(IPEndPoint endpoint)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            if (Active)
            {
                throw new InvalidOperationException("Must be called when Active == false");
            }

            if (Bindings.Any(existed => existed.Endpoint.Equals(endpoint)))
            {
                return;
            }

            ListenerBinding binding = new ListenerBinding(Users, endpoint);

            binding.ExceptionRaised += (o, args) =>
            {
                ExceptionRaised?.Invoke(o, args);
            };
            binding.MessageReceived += (o, args) =>
            {
                MessageReceived?.Invoke(o, args);
            };
            Bindings.Add(binding);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Function to actually invoke the delegate, after synchronization is checked.
 /// </summary>
 /// <param name="param">The parameter.</param>
 private void RaiseExceptionRaised(object param)
 {
     if (param is ExceptionEventArgs paramArgs)
     {
         // We are in the creator thread, call the base implementation directly
         ExceptionRaised?.Invoke(this, paramArgs);
     }
 }
Ejemplo n.º 4
0
        private void Run()
        {
            int counter = 0;

            try
            {
                while (running && dasm.MoveNext())
                {
                    // Debug.Print("emu: {0} {1,-15} {2}", dasm.Current.Address, dasm.Current, DumpRegs());
                    Action bpAction;
                    TWord  eip = (uint)dasm.Current.Address.ToLinear();
                    if (bpExecute.TryGetValue(eip, out bpAction))
                    {
                        ++counter;
                        stepOverAddress = 0;
                        stepInto        = false;
                        bpAction();
                        if (!running)
                        {
                            break;
                        }
                    }
                    else if (stepInto)
                    {
                        stepInto = false;
                        var s = stepAction;
                        stepAction = null;
                        s();
                        if (!running)
                        {
                            break;
                        }
                    }
                    else if (stepOverAddress == eip)
                    {
                        stepOverAddress = 0;
                        var s = stepAction;
                        stepAction = null;
                        s();
                        if (!running)
                        {
                            break;
                        }
                    }
                    Execute(dasm.Current);
                }
            }
            catch (Exception ex)
            {
                Debug.Print("Emulator exception when executing {0}. {1}\r\n{2}", dasm.Current, ex.Message, ex.StackTrace);
                ExceptionRaised.Fire(this);
            }
        }
Ejemplo n.º 5
0
 public void Start()
 {
     IsRunning = true;
     BeforeStart?.Fire(this);
     try
     {
         Run();
     }
     catch (Exception ex)
     {
         Debug.Print("Emulator exception when executing {0}. {1}\r\n{2}", CurrentInstruction, ex.Message, ex.StackTrace);
         ExceptionRaised?.Invoke(this, new EmulatorExceptionEventArgs(ex));
     }
 }
Ejemplo n.º 6
0
        private void SocketEvent_Receive(IAsyncResult ar)
        {
            EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);


            try
            {
                lock (this)
                {
                    var socket     = _socket;
                    int transBytes = socket?.EndReceiveFrom(ar, ref remoteEP) ?? -1;
                    if (transBytes == -1)
                    {
                        return;
                    }

                    byte[] dispatchBuffer = new byte[transBytes];
                    Array.Copy(_receivedBuffer, dispatchBuffer, transBytes);
                    SpinWorker.Dispatch(() =>
                    {
                        EventRead?.Invoke(new IOEventResult(remoteEP, IOEventType.Read, dispatchBuffer, 0, transBytes, 0));
                    });

                    WaitForReceive();
                }
            }
            catch (SocketException)
            {
                SpinWorker.Dispatch(() =>
                {
                    EventClose?.Invoke(new IOEventResult(remoteEP, IOEventType.Close, AegisResult.ClosedByRemote));
                });

                WaitForReceive();
            }
            catch (Exception e)
            {
                if (ExceptionRaised == null)
                {
                    Logger.Err(LogMask.Aegis, e.ToString());
                }
                else
                {
                    ExceptionRaised.Invoke(e);
                }
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Starts the machine in a background thread with the specified options.
        /// </summary>
        /// <remarks>
        /// Reports completion when the machine starts executing its cycles. The machine can
        /// go into Paused or Stopped state, if the execution options allow, for example,
        /// when it runs to a predefined breakpoint.
        /// </remarks>
        public void StartWithOptions(ExecuteCycleOptions options)
        {
            if (MachineState == VmState.Running)
            {
                return;
            }

            // --- Prepare the machine to run
            IsFirstStart = MachineState == VmState.None || MachineState == VmState.Stopped;
            SpectrumVm.DebugInfoProvider?.PrepareBreakpoints();
            MachineState = VmState.Starting;
            if (IsFirstStart)
            {
                SpectrumVm.Reset();
                SpectrumVm.Cpu.StackDebugSupport.Reset();
                SpectrumVm.DebugInfoProvider?.ResetHitCounts();
                CpuFrameCount    = 0;
                RenderFrameCount = 0;
            }

            // --- Dispose the previous cancellation token, and create a new one
            _cancellationTokenSource?.Dispose();
            _cancellationTokenSource = new CancellationTokenSource();

            // --- Set up the task that runs the machine
            MachineState = VmState.Running;
            try
            {
                _completionTask = StartAndRun(_cancellationTokenSource.Token, options);
                _completionTask.GetAwaiter().OnCompleted(async() =>
                {
                    await WaitForPause();
                });
            }
            catch (TaskCanceledException)
            {
                ExecutionCompletionReason = ExecutionCompletionReason.Cancelled;
            }
            catch (Exception ex)
            {
                ExceptionRaised?.Invoke(this, new VmExceptionArgs(ex));
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Raise the ExceptionRaised event
        /// </summary>
        /// <param name="code"></param>
        /// <param name="ex"></param>
        protected void SetError(Exception ex)
        {
            try
            {
                if (Context != null)
                {
                    Context.Post(o => ExceptionRaised?.Invoke(this, ex), null);
                }
                else
                {
                    ExceptionRaised?.Invoke(this, ex);
                }

                Logger?.LogErrorAsync(ex);
            }
            catch (Exception)
            {
                // This can fail, and at this point we're in a really hard state to effectively report
                // what just happened, so we are going to (reluctantly) swallow this.
            }
        }
        private object SafeCapsule <T>(T _action, EventHandler MethodExceptionRaised)
        {
            object result = null;

            try
            {
                if (_action is Func <Driver, T> funcAction)
                {
                    result = funcAction.Invoke(this);
                }
                else if (_action is Action <Driver> action)
                {
                    action.Invoke(this);
                }
                else
                {
                    Log.Write(new NotImplementedException(), "Scenario not implemented in DriverActions.SafeCapsule", Logger.LogEntry.SeverityType.Critical);
                }
                Sleep();
            }
            catch (WebDriverException ex)
            {
                if (MethodExceptionRaised == null && ExceptionRaised == null)
                {
                    Log.Write(ex, "An error occured with the WebDriver (no Driver EventHandler will be raised)", Logger.LogEntry.SeverityType.High);
                }
                else if (MethodExceptionRaised != null && !IsEmptyEventHandler(MethodExceptionRaised))
                {
                    Log.Write($"An error occured with the WebDriver (Driver's specific EventHandler will be raised)");
                    MethodExceptionRaised.Invoke(ex, EventArgs.Empty);
                }
                else if (ExceptionRaised != null && !IsEmptyEventHandler(MethodExceptionRaised))
                {
                    Log.Write($"An error occured with the WebDriver (Driver's generic EventHandler will be raised)");
                    ExceptionRaised.Invoke(ex, EventArgs.Empty);
                }
            }
            return(result);
        }
Ejemplo n.º 10
0
 private void Socket_Send(IAsyncResult ar)
 {
     try
     {
         lock (this)
         {
             var socket = _socket;
             socket?.EndSend(ar);
         }
     }
     catch (Exception e)
     {
         if (ExceptionRaised == null)
         {
             Logger.Err(LogMask.Aegis, e.ToString());
         }
         else
         {
             ExceptionRaised.Invoke(e);
         }
     };
 }
Ejemplo n.º 11
0
 private void WaitForReceive()
 {
     try
     {
         lock (this)
         {
             EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
             var      socket   = _socket;
             socket?.BeginReceiveFrom(_receivedBuffer, 0, _receivedBuffer.Length, SocketFlags.None,
                                      ref remoteEP, SocketEvent_Receive, null);
         }
     }
     catch (Exception e)
     {
         if (ExceptionRaised == null)
         {
             Logger.Err(LogMask.Aegis, e.ToString());
         }
         else
         {
             ExceptionRaised.Invoke(e);
         }
     }
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Cancels the execution of the virtual machine.
 /// </summary>
 /// <param name="cancelledState">Virtual machine state after cancellation</param>
 private async Task CancelVmExecution(VmState cancelledState)
 {
     try
     {
         // --- Wait for cancellation
         _cancellationTokenSource?.Cancel();
         ExecutionCompletionReason = await _completionTask;
     }
     catch (TaskCanceledException)
     {
         // --- Ok, run successfully cancelled
         ExecutionCompletionReason = ExecutionCompletionReason.Cancelled;
     }
     catch (Exception ex)
     {
         // --- Some other exception raised
         ExceptionRaised?.Invoke(this, new VmExceptionArgs(ex));
     }
     finally
     {
         // --- Now, it's cancelled
         MachineState = cancelledState;
     }
 }
Ejemplo n.º 13
0
 public void RuntimeInitialize()
 {
     _runtimeEngine = new RuntimeEngine(ConfigData);
     _runtimeEngine.GlobalInfo.ExceptionManager.ExceptionRaised += (exception) => { ExceptionRaised?.Invoke(exception); };
 }
Ejemplo n.º 14
0
 private void HandleException(Exception exception)
 {
     ExceptionRaised?.Invoke(this, new ExceptionRaisedEventArgs(exception));
 }
Ejemplo n.º 15
0
 private void OnExceptionRaised(Exception exception)
 {
     ExceptionRaised?.Invoke(exception);
 }
Ejemplo n.º 16
0
 protected virtual void OnExceptionRaised(EventArgs e)
 {
     ExceptionRaised?.Invoke(this, e);
 }
Ejemplo n.º 17
0
 /// <summary>Raises the<see
 ///     cref="E:MyBlogLister.BusinessLayer.BusinessLayer.BloggingServiceBase.ExceptionRaised" />
 /// event.</summary>
 /// <param name="ex">A <see cref="T:System.Exception" /> that provides
 /// information on the error that occurred.</param>
 protected virtual void OnExceptionRaised(Exception ex) => ExceptionRaised?.Invoke(ex);