private void BgwOnDoWork(object sender, DoWorkEventArgs e) { _messagePipe = new NamedPipeServerStream(@"CentipedeMessagePipe", PipeDirection.InOut); _messagePipe.WaitForConnection(); var bgw = (BackgroundWorker)sender; try { while (!bgw.CancellationPending) { string actionName; MessageEventArgs messageEventArgs = CentipedeSerializer.DeserializeMessage(_messagePipe, out actionName); messageEventArgs.Message = String.Format("Sub job message from {0}: {1}", actionName, messageEventArgs.Message); OnMessage(messageEventArgs); } } catch (IOException) { e.Cancel = true; bgw.CancelAsync(); } }
private void OnJobCompleted(object sender, JobCompletedEventArgs jobCompletedEventArgs) { if (!jobCompletedEventArgs.Completed) { CentipedeSerializer.Serialize(ClientStream, false); } ClientStream.Close(); _messagePipe.Close(); System.Windows.Forms.Application.Exit(); }
private void SubJobMessageHandler(object sender, MessageEventArgs e) { if (!_messagePipe.IsConnected) { //MessageBox.Show("a"); _messagePipe.Connect(); } //MessageBox.Show("b"); CentipedeSerializer.SerializeMessage(_messagePipe, sender, e); }
protected override void DoAction() { var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(IPAddress.Loopback, SubJobAction.PortNumber); ClientStream = new SocketStream(socket); try { //recieve vars, if any var varsToReceive = CentipedeSerializer.Deserialize <int>(ClientStream); var received = new List <object>(varsToReceive); for (int i = 0; i < varsToReceive; i++) { received.Add(CentipedeSerializer.Deserialize(ClientStream)); } if (!String.IsNullOrWhiteSpace(this.InputVars)) { var enumerable = InputVars.Split(',').Select(s => s.Trim()).ToList(); if (varsToReceive != enumerable.Count) { throw new FatalActionException( string.Format("Wrong number of variables, expected {0}, got {1}", enumerable.Count, varsToReceive), this); } foreach (var tuple in enumerable.Zip(received, Tuple.Create)) { Variables[tuple.Item1] = tuple.Item2; } } else { if (varsToReceive != 0) { throw new ActionException( string.Format( "Recieved {0} {1} from parent job, was not expecting any.", varsToReceive, "variable".Pluralize(varsToReceive)), this); } } } catch (ObjectDisposedException e) { throw new FatalActionException("Getting variables from parent job failed.", e, this); } }
protected override void DoAction() { MemoryStream stream = new MemoryStream(); StreamWriter sw = new StreamWriter(stream); sw.Write(Variables[InVar]); sw.Flush(); stream.Seek(0, SeekOrigin.Begin); Variables[OutVar] = CentipedeSerializer.Deserialize(stream); }
protected override void DoAction() { CentipedeSerializer.Serialize(ClientStream, true); //ClientStream.WaitForPipeDrain(); try { if (!String.IsNullOrWhiteSpace(OutVars)) { var variables = this.OutVars.Split(',').Select(s => s.Trim()).ToList(); CentipedeSerializer.Serialize(ClientStream, variables.Count); foreach (var variable in variables) { object o = Variables[variable]; try { CentipedeSerializer.Serialize(ClientStream, o); } catch (SerializationException e) { var message = string.Format( "Cannot send variable {0} to parent job, type {1} is not supported.", variable, o.GetType()); throw new FatalActionException(message, e, this); } } } else { CentipedeSerializer.Serialize(ClientStream, 0); } } catch (IOException e) { OnMessage(new MessageEventArgs { Message = string.Format( "Sending variables to parent raised IOException: {0}", e.Message), Level = MessageLevel.Debug }); } finally { ClientStream.Close(); } }
protected override void DoAction() { Stream stream = new MemoryStream(); var variable = Variables[InVar]; CentipedeSerializer.Serialize(stream, variable); stream.Seek(0, SeekOrigin.Begin); string text = new StreamReader(stream).ReadToEnd(); if (!String.IsNullOrEmpty(OutVar)) { Variables[OutVar] = text; } else { MessageBox.Show(text); } }
protected override void DoAction() { this._process = new Process { StartInfo = { FileName = this.JobFileName, Verb = "Run", UseShellExecute = true } }; this._process.Start(); this._serverStream = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); this._serverStream.Bind(new IPEndPoint(IPAddress.Loopback, PortNumber)); this._bgw = new BackgroundWorker(); this._bgw.WorkerSupportsCancellation = true; this._bgw.DoWork += BgwOnDoWork; this._bgw.RunWorkerAsync(); Socket connection = null; try { this._serverStream.Listen(0); connection = this._serverStream.Accept(); var ss = new SocketStream(connection); if (!String.IsNullOrWhiteSpace(this.InputVars)) { List <string> variables = this.InputVars.Split(',').Select(s => s.Trim()).ToList(); //send number of variables CentipedeSerializer.Serialize(ss, variables.Count); try { foreach (var variable in variables) { object o = this.Variables[variable]; try { CentipedeSerializer.Serialize(ss, o); } catch (SerializationException e) { throw new FatalActionException( string.Format( "Cannot send variable {0} to subjob, type {1} is not supported.", variable, o.GetType()), e, this); } } } catch (Exception e) { throw new ActionException(e, this); } } else { //no variables to send CentipedeSerializer.Serialize(ss, 0); } var subJobSuccess = (bool)CentipedeSerializer.Deserialize(ss); if (!subJobSuccess) { throw new ActionException("Subjob was not completed", this); } //recieve vars, if any var varsToRecieve = CentipedeSerializer.Deserialize <int>(ss); var received = new List <object>(varsToRecieve); for (int i = 0; i < varsToRecieve; i++) { received.Add(CentipedeSerializer.Deserialize(ss)); } if (!String.IsNullOrWhiteSpace(this.OutputVars)) { List <string> outputVars = this.OutputVars.Split(',').Select(s => s.Trim()).ToList(); if (varsToRecieve != outputVars.Count) { throw new FatalActionException( string.Format("Wrong number of variables, expected {0}, got {1}", outputVars.Count, varsToRecieve)); } foreach (var tuple in outputVars.Zip(received, Tuple.Create)) { this.Variables[tuple.Item1] = tuple.Item2; } } else { if (varsToRecieve != 0) { throw new ActionException( string.Format("Recieved {0} {1} from subjob, was not expecting any.", varsToRecieve, "variable".Pluralize(varsToRecieve))); } } } finally { try { this._serverStream.Close(); if (connection != null) { connection.Close(); } this._messagePipe.Close(); if (!this._process.HasExited) { this._process.CloseMainWindow(); this._process.Close(); if (!this._process.WaitForExit(10000)) { this._process.Kill(); this._process = null; } } } catch (Exception e) { this.OnMessage(new MessageEventArgs { Message = e.Message, Level = MessageLevel.Debug }); } } }