private void ProcessingTimerToCefBrowser_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            processingTimerToCefBrowser.Stop();
            try
            {
                CefBrowserListsLock.AcquireWriterLock(Options.LockTimeOut);

                Dictionary <string, string> forDeletion = new Dictionary <string, string>();
                //each browser session
                foreach (KeyValuePair <string, Dictionary <string, object> > browserCommandsUcidToObjectList in _browserCommandsList)
                {
                    string uid = browserCommandsUcidToObjectList.Key;
                    //commands for each browser [
                    foreach (KeyValuePair <string, object> browserUcidToCommand in browserCommandsUcidToObjectList.Value)
                    {
                        string commandType = browserUcidToCommand.Value.GetType().ToString();
                        //Console.WriteLine(browserUcidToCommand.ExpectedValue.GetType().ToString());
                        switch (commandType)
                        {
                        case "CefBrowserControl.BrowserCommands.Open":
                            Open open = (Open)browserUcidToCommand.Value;
                            forDeletion.Add(open.UID, open.UCID);
                            if (!CefBrowserSessions.ContainsKey(open.UID))
                            {
                                //Thread thread = new Thread(startBrowser);
                                //thread.AttributeName = open.UID;
                                //thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
                                //thread.Start();
                                ////Todo: Clean Shutdown
                                ////thread.Join(); //Wait for the thread to end
                                Process p = Process.Start("CefBrowser.exe", open.UID + " --debug");

                                CefBrowserSessions.Add(open.UID, p);
                                //TODO: Add code that checks thread
                                open.Successful = true;
                            }
                            while (true)
                            {
                                try
                                {
                                    MessagesLock.AcquireWriterLock(Options.LockTimeOut);
                                    _rpcReaderWriter.AddClient(open.UID);
                                    break;
                                }
                                catch (ApplicationException ex1)
                                {
                                    ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
                                }
                                finally
                                {
                                    if (MessagesLock.IsWriterLockHeld)
                                    {
                                        MessagesLock.ReleaseWriterLock();
                                    }
                                }
                            }
                            open.Completed = true;
                            break;
                        }
                    }
                }
                foreach (KeyValuePair <string, string> uidAndUcid in forDeletion)
                {
                    object obj = _browserCommandsList[uidAndUcid.Key][uidAndUcid.Value];
                    BrowserCommandsCompleted.Add(uidAndUcid.Key, obj);
                    _browserCommandsList[uidAndUcid.Key].Remove(uidAndUcid.Value);
                }
                try
                {
                    MessagesLock.AcquireWriterLock(Options.LockTimeOut);
                    try
                    {
                        //Send rest of pending commands and actions to cef
                        foreach (KeyValuePair <string, Dictionary <string, object> > browserCommandsUcidToObjectList in _browserCommandsList)
                        {
                            string uid = browserCommandsUcidToObjectList.Key; //=client for rcp
                                                                              //commands for each browser [
                            foreach (KeyValuePair <string, object> browserUcidToCommand in browserCommandsUcidToObjectList.Value)
                            {
                                PendingMessagesList.Add(uid, CefEncoding.Encode(browserUcidToCommand.Key, browserUcidToCommand.Value));
                            }
                        }
                        foreach (var uidToCommand in _browserCommandsList)
                        {
                            uidToCommand.Value.Clear();
                        }
                        foreach (KeyValuePair <string, Dictionary <string, object> > browserActionsUcidToObjectList in _browserActionsList)
                        {
                            string uid = browserActionsUcidToObjectList.Key; //=client for rcp
                                                                             //commands for each browser [
                            foreach (KeyValuePair <string, object> browserUcidToAction in browserActionsUcidToObjectList.Value)
                            {
                                PendingMessagesList.Add(uid, CefEncoding.Encode(browserUcidToAction.Key, browserUcidToAction.Value));
                            }
                        }
                        foreach (var uidToAction in _browserActionsList)
                        {
                            uidToAction.Value.Clear();
                        }
                    }
                    catch (Exception ex)
                    {
                        ExceptionHandling.Handling.GetException("Unexpected", ex);
                    }
                }
                catch (ApplicationException ex1)
                {
                    ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
                }
                finally
                {
                    if (MessagesLock.IsWriterLockHeld)
                    {
                        MessagesLock.ReleaseWriterLock();
                    }
                }
            }
            catch (ApplicationException ex1)
            {
                ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
            }
            finally
            {
                if (CefBrowserListsLock.IsWriterLockHeld)
                {
                    CefBrowserListsLock.ReleaseWriterLock();
                }
            }
            processingTimerToCefBrowser.Start();
        }
Beispiel #2
0
        private void ProcessingTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            processingTimer.Stop();
            try
            {
                MessagesLock.AcquireWriterLock(Options.LockTimeOut);
                try
                {
                    if (ReceivedMessagesList.Count > 0)
                    {
                        try
                        {
                            BrowserListsLock.AcquireWriterLock(Options.LockTimeOut);
                            try
                            {
                                foreach (string message in ReceivedMessagesList)
                                {
                                    //DEBUG
                                    //Console.WriteLine(message);
                                    string plain = EncodingEx.Base64.Decoder.DecodeString(Encoding.UTF8, message);
                                    try
                                    {
                                        CefDecodeResult cefDecodeResult = CefDecoding.Decode(plain);
                                        if (cefDecodeResult.DecodedObject is BrowserAction)
                                        {
                                            BrowserAction browserAction = (BrowserAction)cefDecodeResult.DecodedObject;
                                            BaseObject    baseObject    = ((BaseObject)browserAction.ActionObject);
                                            baseObject.ExecuteEventHandler = true;
                                            baseObject.SetFinished(true);

                                            BrowserActionsInTransit.Remove(cefDecodeResult.UCID);
                                            BrowserActionsCompleted.Add(cefDecodeResult.UCID, cefDecodeResult.DecodedObject);
                                        }
                                        else if (cefDecodeResult.DecodedObject is BrowserCommand)
                                        {
                                            BrowserCommand browerCommand = (BrowserCommand)cefDecodeResult.DecodedObject;
                                            browerCommand.ExecuteEventHandler = true;
                                            browerCommand.SetCompleted(true);

                                            BrowserCommandsInTransit.Remove(cefDecodeResult.UCID);
                                            BrowserCommandsCompleted.Add(cefDecodeResult.UCID, cefDecodeResult.DecodedObject);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        ExceptionHandling.Handling.GetException("Unexpected", ex);
                                    }
                                }
                                ReceivedMessagesList.Clear();
                            }
                            catch (Exception ex)
                            {
                                ExceptionHandling.Handling.GetException("Unexpected", ex);
                            }
                        }
                        catch (ApplicationException ex1)
                        {
                            ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
                        }
                        finally
                        {
                            if (BrowserListsLock.IsWriterLockHeld)
                            {
                                BrowserListsLock.ReleaseWriterLock();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    ExceptionHandling.Handling.GetException("Unexpected", ex);
                }
            }
            catch (ApplicationException ex1)
            {
                ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
            }
            finally
            {
                if (MessagesLock.IsWriterLockHeld)
                {
                    MessagesLock.ReleaseWriterLock();
                }
            }
            try
            {
                BrowserListsLock.AcquireWriterLock(Options.LockTimeOut);
                if (BrowserActions.Count > 0 || BrowserCommands.Count > 0)
                {
                    try
                    {
                        MessagesLock.AcquireWriterLock(Options.LockTimeOut);
                        foreach (var browserCommandKeyValuePair in BrowserCommands)
                        {
                            try
                            {
                                string commandType = browserCommandKeyValuePair.Value.GetType().ToString();
                                //Console.WriteLine(commandType);
                                switch (commandType)
                                {
                                //Pass to application, or ucid will be very wrong...
                                case "CefBrowserControl.BrowserCommands.Open":
                                    Open open = (Open)browserCommandKeyValuePair.Value;
                                    if (!CefBrowserSessions.ContainsKey(open.UID))
                                    {
                                        ////Todo: Clean Shutdown
                                        Process p = Process.Start("CefBrowser.exe", open.UID + (CefBrowserControl.Options.IsDebug?" --debug":""));
                                        CefBrowserSessions.Add(open.UID, p);
                                        //TODO: Add code that checks thread
                                    }
                                    _rpcReaderWriter.AddClient(open.UID);
                                    //BrowserCommandsCompleted.Add(open.UCID, open);
                                    goto default;

                                case "CefBrowserControl.BrowserCommands.Quit":
                                    Quit quit = (Quit)browserCommandKeyValuePair.Value;
                                    if (quit.All)
                                    {
                                        foreach (var cefBrowserSession in CefBrowserSessions)
                                        {
                                            Process process = cefBrowserSession.Value;
                                            if (process != null)
                                            {
                                                process.Kill();
                                            }
                                            _rpcReaderWriter.RemoveClient(quit.UID);
                                        }
                                        quit.Successful = true;
                                        CefBrowserSessions.Clear();
                                    }
                                    else
                                    {
                                        Process process = CefBrowserSessions[quit.UID];
                                        if (process != null)
                                        {
                                            process.Kill();
                                        }
                                        quit.Successful = true;
                                        CefBrowserSessions.Remove(quit.UID);
                                        _rpcReaderWriter.RemoveClient(quit.UID);
                                    }
                                    quit.Completed = true;
                                    BrowserCommandsCompleted.Add(quit.UCID, quit);
                                    break;

                                default:
                                    BrowserCommand cmd = (BrowserCommand)browserCommandKeyValuePair.Value;
                                    PendingMessagesList.Add(new KeyValuePairEx <string, string>(cmd.UID, CefEncoding.Encode(cmd.UCID, cmd)));
                                    BrowserCommandsInTransit.Add(cmd.UCID, cmd);
                                    break;
                                }
                            }
                            catch (Exception ex)
                            {
                                ExceptionHandling.Handling.GetException("Unexpected", ex);
                            }
                        }
                        foreach (var browserActionKeyValuePair in BrowserActions)
                        {
                            try
                            {
                                BrowserAction browserAction = (BrowserAction)browserActionKeyValuePair.Value;
                                PendingMessagesList.Add(new KeyValuePairEx <string, string>(browserAction.UID, CefEncoding.Encode(browserAction.UCID, browserActionKeyValuePair.Value)));
                                BrowserCommandsInTransit.Add(browserAction.UCID, browserAction);
                                //PendingMessagesList.Add("CefBAAS-1", CefEncoding.EncodeString(browserActionKeyValuePair.Key, browserActionKeyValuePair.ExpectedValue));
                                //BrowserActionsInTransit.Add(browserActionKeyValuePair.Key, browserActionKeyValuePair.ExpectedValue);
                            }
                            catch (Exception ex)
                            {
                                ExceptionHandling.Handling.GetException("Unexpected", ex);
                            }
                        }
                        BrowserCommands.Clear();
                        BrowserActions.Clear();
                    }
                    catch (ApplicationException ex2)
                    {
                        ExceptionHandling.Handling.GetException("ReaderWriterLock", ex2);
                    }
                    finally
                    {
                        if (MessagesLock.IsWriterLockHeld)
                        {
                            MessagesLock.ReleaseWriterLock();
                        }
                    }
                }
            }
            catch (ApplicationException ex1)
            {
                ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
            }
            finally
            {
                if (BrowserListsLock.IsWriterLockHeld)
                {
                    BrowserListsLock.ReleaseWriterLock();
                }
            }
            processingTimer.Start();
        }
 private void _timerMessageHandling_Elapsed(object sender, ElapsedEventArgs e)
 {
     _timerMessageHandling.Stop();
     try
     {
         TimerMessageHandlingLock.AcquireWriterLock(Options.LockTimeOut);
         try
         {
             MessageLock.AcquireWriterLock(Options.LockTimeOut);
             try
             {
                 if (MessagesReceived.Count > 0)
                 {
                     try
                     {
                         CefListLock.AcquireWriterLock(Options.LockTimeOut);
                         foreach (var message in MessagesReceived)
                         {
                             //DEBUG
                             //Console.WriteLine(message);
                             string plain = EncodingEx.Base64.Decoder.DecodeString(Encoding.UTF8, message);
                             try
                             {
                                 CefDecodeResult cefDecodeResult = CefDecoding.Decode(plain);
                                 if (cefDecodeResult.DecodedObject is CefBrowserControl.BrowserAction)
                                 {
                                     BrowserActionsList.Add(cefDecodeResult.UCID, cefDecodeResult.DecodedObject);
                                 }
                                 else if (cefDecodeResult.DecodedObject is BrowserCommand)
                                 {
                                     BrowserCommandsList.Add(cefDecodeResult.UCID, cefDecodeResult.DecodedObject);
                                 }
                             }
                             catch (Exception ex)
                             {
                                 ExceptionHandling.Handling.GetException("Unexpected", ex);
                             }
                         }
                         MessagesReceived.Clear();
                     }
                     catch (ApplicationException ex1)
                     {
                         ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
                     }
                     finally
                     {
                         if (CefListLock.IsWriterLockHeld)
                         {
                             CefListLock.ReleaseWriterLock();
                         }
                     }
                 }
             }
             catch (Exception ex)
             {
                 ExceptionHandling.Handling.GetException("Unexpected", ex);
             }
         }
         catch (ApplicationException ex1)
         {
             ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
         }
         finally
         {
             if (MessageLock.IsWriterLockHeld)
             {
                 MessageLock.ReleaseWriterLock();
             }
         }
         try
         {
             CefListLock.AcquireWriterLock(Options.LockTimeOut);
             try
             {
                 if (BrowserCommandsCompleted.Count > 0)
                 {
                     try
                     {
                         MessageLock.AcquireWriterLock(Options.LockTimeOut);
                         try
                         {
                             foreach (var ucidToCommand in BrowserCommandsCompleted)
                             {
                                 BrowserCommand cmd = (BrowserCommand)ucidToCommand.Value;
                                 MessagesPending.Add(new KeyValuePairEx <string, string>(RcpServerName,
                                                                                         CefEncoding.Encode(cmd.UCID, cmd)));
                             }
                             BrowserCommandsCompleted.Clear();
                         }
                         catch (Exception ex)
                         {
                             ExceptionHandling.Handling.GetException("Unexpected", ex);
                         }
                     }
                     catch (ApplicationException ex1)
                     {
                         ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
                     }
                     finally
                     {
                         if (MessageLock.IsWriterLockHeld)
                         {
                             MessageLock.ReleaseWriterLock();
                         }
                     }
                 }
                 if (BrowserActionsCompleted.Count > 0)
                 {
                     try
                     {
                         MessageLock.AcquireWriterLock(Options.LockTimeOut);
                         try
                         {
                             foreach (var ucidToAction in BrowserActionsCompleted)
                             {
                                 CefBrowserControl.BrowserAction action = (CefBrowserControl.BrowserAction)ucidToAction.Value;
                                 MessagesPending.Add(new KeyValuePairEx <string, string>(RcpServerName,
                                                                                         CefEncoding.Encode(action.UCID, action)));
                             }
                             BrowserActionsCompleted.Clear();
                         }
                         catch (Exception ex)
                         {
                             ExceptionHandling.Handling.GetException("Unexpected", ex);
                         }
                     }
                     catch (ApplicationException ex1)
                     {
                         ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
                     }
                     finally
                     {
                         if (MessageLock.IsWriterLockHeld)
                         {
                             MessageLock.ReleaseWriterLock();
                         }
                     }
                 }
             }
             catch (Exception ex)
             {
                 ExceptionHandling.Handling.GetException("Unexpected", ex);
             }
         }
         catch (ApplicationException ex1)
         {
             ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
         }
         finally
         {
             if (CefListLock.IsWriterLockHeld)
             {
                 CefListLock.ReleaseWriterLock();
             }
         }
     }
     catch (ApplicationException ex1)
     {
         ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
     }
     finally
     {
         if (TimerMessageHandlingLock.IsWriterLockHeld)
         {
             TimerMessageHandlingLock.ReleaseWriterLock();
         }
     }
     _timerMessageHandling.Start();
 }