private void OnClientMessage(NamedPipeConnection <PipeMessageCommand, PipeMessageCommand> connection, PipeMessageCommand message) { stopwatchLastCommand.Restart(); if (message.Command == "Quit!") { KeepRunning = false; } if (message.Command.StartsWith("File")) { string fullFilePath = message.FullFileName; WriteResponseLine("Client {" + connection.Id + "} Proccessing file: {" + fullFilePath + "}"); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Metadata metadata = databaseWindowsLivePhotGallery.Read(MetadataBrokerType.WindowsLivePhotoGallery, fullFilePath); WriteResponseLine( "Client ID: {" + connection.Id + "}, " + (metadata == null ? "File not found" : "Data found") + " " + "File: {" + fullFilePath + "}, " + "Time Elapsed Milliseconds: {" + stopwatch.ElapsedMilliseconds.ToString() + "}"); PipeMessageCommand pipeMessageCommand = new PipeMessageCommand(); pipeMessageCommand.FullFileName = fullFilePath; pipeMessageCommand.Command = "File"; pipeMessageCommand.Message = "File:" + fullFilePath; pipeMessageCommand.Metadata = metadata; connection.PushMessage(pipeMessageCommand); } else { WriteResponseLine("Unknown command: " + message.Command); } }
private void OnClientConnected(NamedPipeConnection <PipeMessageCommand, PipeMessageCommand> connection) { WriteResponseLine("Client {" + connection.Id + "} is now connected!"); PipeMessageCommand pipeMessageCommand = new PipeMessageCommand(); pipeMessageCommand.FullFileName = ""; pipeMessageCommand.Command = "Hello"; pipeMessageCommand.Message = "Hello"; connection.PushMessage(pipeMessageCommand); }
private void PipeClient_ServerMessage(NamedPipeConnection <PipeMessageCommand, PipeMessageCommand> connection, PipeMessageCommand message) { Logger.Trace("[Windows Live Photo Gallery | Pipe Client] ServerMessage - Id: {" + connection.Id + "}, " + "Metadata: " + (message.Metadata == null ? "NULL" : "Found") + " " + "Command: " + message.Command + " " + "Message: " + message.Message); metadataReadFromPipe = message.Metadata; if (message.Command == "File") { pipeClientEventWaitPipeCommandReturn.Set(); } }
public Metadata Read(MetadataBrokerType broker, string fullFilePath) { if (errorHasOccurdDoNotReconnect) { return(null); } if (checkIfWindowLivePhotoGalleryExists) { checkIfWindowLivePhotoGalleryExists = false; //Check only once string sourceFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft\\Windows Live Photo Gallery\\Pictures.pd6"); try { doesWindowLivePhotoGalleryExists = File.Exists(sourceFile); } catch { } } if (!doesWindowLivePhotoGalleryExists) { return(null); } Stopwatch stopWatch = new Stopwatch(); ErrorMessageReset(); bool retryConnect = false; do { PipeClientForceDisconnectAfterError(ref pipeClientProcessErrorOccurred, ref pipeClientDiconnected, ref consoleProcessDisconnected); #region Start PipeClient, if "HANGING" Console process exist, kill that first if (pipeClient == null) { //Chech if Console Procss running after crash if (IsConsoleProcessRunning()) { try { consoleProcessWaitEventServerExited.Reset(); if (process != null) { process.CloseMainWindow(); } else { Logger.Warn("[Windows Live Photo Gallery | Console Process] Console Server running, but lost process connection with it. Can't close it."); } consoleProcessWaitEventServerExited.WaitOne(2000); } catch (Exception ex) { Logger.Error(ex, "[Windows Live Photo Gallery | Console Process] Close process was not preformed. "); } } if (IsConsoleProcessRunning()) { try { if (process != null) { process.Kill(); } else { Logger.Warn("[Windows Live Photo Gallery | Console Process] Console Server running, but lost process connection with it. Can't kill it."); } } catch (Exception ex) { Logger.Error(ex, "[Windows Live Photo Gallery | Console Process] Kill process was not preformed. "); } } PipeClient_Start_WithoutConnect(); } #endregion if (!pipeClientDiconnected || !pipeClientProcessErrorOccurred || pipeClient != null) { ConsoleProcessStart(); } #region PipeMessageCommand if (!pipeClientProcessErrorOccurred && !consoleProcessErrorOccurred) { metadataReadFromPipe = null; stopWatch.Restart(); PipeMessageCommand pipeMessageCommand = new PipeMessageCommand(); pipeMessageCommand.FullFileName = fullFilePath; pipeMessageCommand.Command = "File"; pipeMessageCommand.Message = "File:" + fullFilePath; if (pipeClient != null) { try { pipeClientEventWaitPipeCommandReturn.Reset(); //Clear in case of timeout pipeClient.PushMessage(pipeMessageCommand); } catch (Exception ex) { Logger.Error(ex, "Metadata Read - Pipe Client"); } } Logger.Trace("[Windows Live Photo Gallery | Pipe Client] Push message: Request File {0}ms...", stopWatch.ElapsedMilliseconds); stopWatch.Restart(); #region Retry timeout bool retryWait; do { retryWait = false; Application.DoEvents(); if (pipeClientEventWaitPipeCommandReturn.WaitOne(20000)) { Logger.Trace("[Windows Live Photo Gallery | Console Process] Push message: Wait answer {0}ms...", stopWatch.ElapsedMilliseconds); return(metadataReadFromPipe); } else { Logger.Trace("[Windows Live Photo Gallery | Console Process] Push message: Wait answer failed {0}ms...", stopWatch.ElapsedMilliseconds); lock (_ErrorHandlingLock) { pipeClientProcessErrorOccurred = true; globalErrorMessageHandler += (globalErrorMessageHandler == "" ? "" : "\r\n") + "[Windows Live Photo Gallery | Pipe Client] No response from server. Wait message timeout..."; } Logger.Error(globalErrorMessageHandler); if (!pipeClientDiconnected && !pipeClientProcessErrorOccurred && !consoleProcessDisconnected) { switch (MessageBox.Show( "Retry - and wait more.\r\n" + "Ignor - and continue trying.\r\n" + "Abort - and stop loading data from Windows Live Photo Gallery", "Waiting answer from server timeouted...", MessageBoxButtons.AbortRetryIgnore)) { case DialogResult.Retry: retryWait = true; break; case DialogResult.Ignore: retryWait = false; break; case DialogResult.Abort: errorHasOccurdDoNotReconnect = true; return(null); } } } } while (retryWait); #endregion } #endregion #region MessageBox - retry bool errorOccured; string errorMessage; lock (_ErrorHandlingLock) { errorOccured = (pipeClientProcessErrorOccurred || consoleProcessErrorOccurred || !string.IsNullOrWhiteSpace(globalErrorMessageHandler)); errorMessage = globalErrorMessageHandler; globalErrorMessageHandler = ""; } if (errorOccured) { switch (MessageBox.Show( "Error occured:\r\n" + errorMessage + "\r\n\r\n\r\n" + "Retry - and try again.\r\n" + "Ignor - and continue trying later.\r\n" + "Abort - and stop loading data from Windows Live Photo Gallery", "Error from Windows Live Photo Gallery background process", MessageBoxButtons.AbortRetryIgnore)) { case DialogResult.Retry: retryConnect = true; break; case DialogResult.Ignore: retryConnect = false; break; case DialogResult.Abort: retryConnect = false; errorHasOccurdDoNotReconnect = true; break; } } else { retryConnect = false; } #endregion } while (retryConnect); return(null); }