Beispiel #1
0
 public void ClearAsyncCallbacks()
 {
     lock (waiting_object_lock)
     {
         waiting_object = null;
     }
 }
Beispiel #2
0
 private Printer(PrinterProfile profile, SpoolerClient client)
 {
     this.client             = client;
     mylockID                = Guid.Empty;
     m_printer_profile.Value = profile;
     thread_sync             = new object();
     spool_lock              = new object();
     spool_up_to_date        = false;
     incoming_data           = null;
     Found = new ThreadSafeVariable <bool>
     {
         Value = false
     };
     _connected = new ThreadSafeVariable <bool>
     {
         Value = false
     };
     log                       = new CircularArray <string>(200);
     LogWaits                  = true;
     LogFeedback               = true;
     waiting_object            = null;
     waiting_object_lock       = new object();
     lockstatus                = new ThreadSafeVariable <PrinterLockStatus>(PrinterLockStatus.Unlocked);
     lockstepmode              = new ThreadSafeVariable <bool>(true);
     lockTimeOutSeconds        = new ThreadSafeVariable <int>(0);
     keeplockalive_clock       = new Stopwatch();
     keeplockalive_limit_clock = new Stopwatch();
     finished_lock             = new object();
     m_ChangedKeyValuePairs    = new ConcurrentDictionary <string, string>();
 }
Beispiel #3
0
        public SpoolerResult AcquireLock(AsyncCallback callback, object state, EventLockTimeOutCallBack LockTimeOutCallBack, int locktimeoutseconds)
        {
            if (HasLock && callback != null)
            {
                callback(new AsyncCallObject(callback, state, (IPrinter)this)
                {
                    callresult = CommandResult.Success_LockAcquired
                });
                return(SpoolerResult.OK);
            }
            AsyncCallObject newWaitingObject = CreateNewWaitingObject(callback, state, false);

            if (newWaitingObject == null)
            {
                return(SpoolerResult.Fail_PreviousCommandNotComplete);
            }

            newWaitingObject.callbackType = CallBackType.Special;
            lockstatus.Value         = PrinterLockStatus.OurLockPending;
            lockTimeOutSeconds.Value = locktimeoutseconds;
            lock (timeout_lock_sync)
            {
                __LockTimeOutCallBack = LockTimeOutCallBack;
            }

            return(SendRPCToSpooler(nameof(AcquireLock), newWaitingObject.callID));
        }
Beispiel #4
0
        private SpoolerResult SendRPCToSpooler(AsyncCallback callback, object state, CallBackType callbacktype, bool always_send, string function_name, params object[] options)
        {
            if (!HasLock && !always_send)
            {
                if (callback != null)
                {
                    var ar = (IAsyncCallResult) new SimpleAsyncCallResult(state, CommandResult.Failed_PrinterDoesNotHaveLock);
                    callback(ar);
                }
                return(SpoolerResult.Fail_DoesNotHaveLock);
            }
            AsyncCallObject newWaitingObject = CreateNewWaitingObject(callback, state, LockStepMode && !always_send);

            if (newWaitingObject == null)
            {
                callback?.Invoke(new AsyncCallObject(callback, state, (IPrinter)this)
                {
                    callresult = CommandResult.Failed_PreviousCommandNotCompleted
                });

                return(SpoolerResult.Fail_PreviousCommandNotComplete);
            }
            newWaitingObject.callbackType = callbacktype;
            return(SendRPCToSpooler(function_name, newWaitingObject.callID, options));
        }
Beispiel #5
0
        public SpoolerResult ReleaseLock(AsyncCallback callback, object state)
        {
            AsyncCallObject newWaitingObject = CreateNewWaitingObject(callback, state, false);

            if (newWaitingObject == null)
            {
                return(SpoolerResult.Fail_PreviousCommandNotComplete);
            }

            newWaitingObject.callbackType = CallBackType.Special;
            lockstatus.Value = PrinterLockStatus.OurReleasePending;
            return(SendRPCToSpooler(nameof(ReleaseLock), newWaitingObject.callID));
        }
Beispiel #6
0
        private AsyncCallObject CreateNewWaitingObject(AsyncCallback callback, object state, bool lockstepmode)
        {
            var asyncCallObject = (AsyncCallObject)null;

            lock (waiting_object_lock)
            {
                if (!lockstepmode && waiting_object != null)
                {
                    AsyncCallObject waitingObject = waiting_object;
                    waitingObject.callresult = CommandResult.OverridedByNonLockStepCall;
                    ThreadPool.QueueUserWorkItem(new WaitCallback(DoAsyncCallBack), waitingObject);
                    waiting_object = null;
                }
                if (waiting_object == null)
                {
                    waiting_object       = new AsyncCallObject(callback, state, this);
                    can_check_idle.Value = false;
                    asyncCallObject      = waiting_object;
                }
            }
            return(asyncCallObject);
        }
Beispiel #7
0
        public void ProcessSpoolerMessage(SpoolerMessage message)
        {
            var asyncCallObject = (AsyncCallObject)null;
            var flag            = false;

            if (message.Type == MessageType.RawData)
            {
                lock (thread_sync)
                {
                    incoming_data = message.GetRawData();
                }
            }
            else if (message.Type == MessageType.PluginMessage)
            {
                ProcessPluginMessageFromSpooler(message);
            }
            else if (message.Type == MessageType.LoggingMessage)
            {
                AddMessageToLog(Base64Convert.Base64Decode(message.Message));
                lock (log)
                {
                    log_updated = true;
                }
            }
            else if (message.Type == MessageType.FullLoggingData)
            {
                string[] strArray = Base64Convert.Base64Decode(message.Message).Split('\n');
                log.Clear();
                foreach (var message1 in strArray)
                {
                    AddMessageToLog(message1);
                }

                lock (log)
                {
                    log_updated = true;
                }
            }
            else if (message.Type == MessageType.BedLocationMustBeCalibrated || message.Type == MessageType.BedOrientationMustBeCalibrated || message.Type == MessageType.CheckGantryClips)
            {
                mylockID         = Guid.Empty;
                lockstatus.Value = PrinterLockStatus.Unlocked;
                lock (waiting_object_lock)
                {
                    if (waiting_object != null)
                    {
                        asyncCallObject            = waiting_object;
                        waiting_object             = null;
                        asyncCallObject.callresult = CommandResult.Failed_GantryClipsOrInvalidZ;
                    }
                }
            }
            else if ((message.Type == MessageType.LockConfirmed || message.Type == MessageType.LockResult) && message.SerialNumber == Info.serial_number)
            {
                uint num = 0;
                EventLockTimeOutCallBack callback = null;
                CommandResult            commandResult;
                if (message.Type == MessageType.LockResult)
                {
                    var s   = message.Message.Substring(0, 8);
                    var str = message.Message.Substring(8);
                    try
                    {
                        num           = uint.Parse(s);
                        commandResult = (CommandResult)Enum.Parse(typeof(CommandResult), str);
                    }
                    catch (ArgumentException ex)
                    {
                        commandResult = CommandResult.Failed_Exception;
                    }
                    if (commandResult == CommandResult.Success)
                    {
                        can_check_idle.Value = true;
                        return;
                    }
                    if (commandResult == CommandResult.Pending)
                    {
                        lockstatus.Value = PrinterLockStatus.OurLockPending;
                        return;
                    }
                    if (commandResult == CommandResult.LockForcedOpen)
                    {
                        lockstatus.Value = PrinterLockStatus.Unlocked;
                    }
                    else if (commandResult == CommandResult.LockLost_TimedOut)
                    {
                        lock (timeout_lock_sync)
                        {
                            callback = __LockTimeOutCallBack;
                            __LockTimeOutCallBack = null;
                        }
                    }
                    if (commandResult != CommandResult.CommandInterruptedByM0)
                    {
                        flag = true;
                    }
                }
                else
                {
                    mylockID         = Guid.Parse(message.Message);
                    commandResult    = CommandResult.Success_LockAcquired;
                    lockstatus.Value = PrinterLockStatus.WeOwnLocked;
                }
                lock (waiting_object_lock)
                {
                    if (waiting_object != null)
                    {
                        if (commandResult == CommandResult.SuccessfullyReceived)
                        {
                            if (waiting_object.callbackType != CallBackType.SuccessfullyReceived)
                            {
                                goto label_59;
                            }
                        }
                        asyncCallObject = waiting_object;
                        waiting_object  = null;
                    }
                }
label_59:
                if (flag)
                {
                    mylockID         = Guid.Empty;
                    lockstatus.Value = PrinterLockStatus.Unlocked;
                }
                if (asyncCallObject != null)
                {
                    if (num != 0U && (int)num != (int)asyncCallObject.callID)
                    {
                        commandResult = CommandResult.Failed_AsyncCallbackError;
                    }

                    asyncCallObject.callresult = commandResult;
                }
                if (callback != null)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(DoTimeoutCallBack), new Printer.TimeoutProperties(callback, (IPrinter)this));
                }
            }
            if (asyncCallObject != null)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(DoAsyncCallBack), asyncCallObject);
            }

            if (OnProcessSpoolerMessage == null)
            {
                return;
            }

            OnProcessSpoolerMessage(message);
        }
Beispiel #8
0
        public void UpdateData(PrinterInfo info)
        {
            printer_info.CopyFrom(info);
            current_job = printer_info.current_job == null ? null : new JobInfo(printer_info.current_job);
            var other = new FilamentSpool(printer_info.filament_info);

            if (spool_up_to_date)
            {
                if (other == current_spool)
                {
                    spool_up_to_date = false;
                }
            }
            else
            {
                current_spool = new FilamentSpool(other);
            }

            CheckForUpdatedValues();
            var asyncCallObject = (AsyncCallObject)null;
            var flag1           = false;
            var flag2           = false;

            lock (waiting_object_lock)
            {
                if (waiting_object != null)
                {
                    var lastCompletedRpcid = Info.synchronization.LastCompletedRPCID;
                    if (Info.Status == PrinterStatus.Firmware_Idle && can_check_idle.Value)
                    {
                        flag1 = true;
                    }

                    if (waiting_object.callbackType == CallBackType.FirmwareMode && !Info.InFirmwareMode || waiting_object.callbackType == CallBackType.BootloaderMode && !Info.InBootloaderMode)
                    {
                        inRequestedMode = false;
                    }

                    if (waiting_object.callbackType == CallBackType.CallID && (int)waiting_object.callID == (int)lastCompletedRpcid || !inRequestedMode && (waiting_object.callbackType == CallBackType.FirmwareMode && Info.InFirmwareMode || waiting_object.callbackType == CallBackType.BootloaderMode && Info.InBootloaderMode))
                    {
                        flag2 = true;
                        flag1 = false;
                    }
                    if (flag1 | flag2)
                    {
                        asyncCallObject = waiting_object;
                        waiting_object  = null;
                    }
                }
            }
            if (asyncCallObject != null)
            {
                asyncCallObject.callresult    = CommandResult.Success;
                asyncCallObject.idle_callback = flag1;
                ThreadPool.QueueUserWorkItem(new WaitCallback(DoAsyncCallBack), asyncCallObject);
                lock (finished_lock)
                {
                    AllCommandsFinished?.Invoke(new SimpleAsyncCallResult((object)this, CommandResult.Success));
                }
            }
            if (HasLock && Info.IsIdle)
            {
                var num = lockTimeOutSeconds.Value;
                if (!keeplockalive_clock.IsRunning)
                {
                    keeplockalive_clock.Restart();
                }

                if (!keeplockalive_limit_clock.IsRunning && num > 0)
                {
                    keeplockalive_limit_clock.Restart();
                }

                if (keeplockalive_clock.Elapsed.TotalSeconds > 15.0 && (num <= 0 || keeplockalive_limit_clock.Elapsed.TotalSeconds < num))
                {
                    var spooler = (int)SendRPCToSpooler("KeepLockAlive", 0U);
                    keeplockalive_clock.Restart();
                }
            }
            else if (keeplockalive_clock.IsRunning)
            {
                keeplockalive_clock.Stop();
                keeplockalive_limit_clock.Stop();
            }
            if (Info.InBootloaderMode)
            {
                m_bPluginsRegistered = false;
            }
            else if (!m_bPluginsRegistered)
            {
                DoPluginRegistration();
            }

            if (OnUpdateData == null)
            {
                return;
            }

            OnUpdateData(info);
        }