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(); }