internal static void ProcessData(string data, DataProcessingDelegates callbacks) { if (!string.IsNullOrEmpty(data)) { XmlReader xmlReader = XmlReader.Create(new StringReader(data), XmlReaderSettings); while (xmlReader.Read()) { switch (xmlReader.NodeType) { case XmlNodeType.Element: { ProcessElement(xmlReader, callbacks); continue; } case XmlNodeType.EndElement: { continue; } } throw new PSRemotingTransportException(PSRemotingErrorId.IPCUnknownNodeType, RemotingErrorIdStrings.IPCUnknownNodeType, new object[] { xmlReader.NodeType.ToString(), XmlNodeType.Element.ToString(), XmlNodeType.EndElement.ToString() }); } } }
private static void ProcessElement(XmlReader xmlReader, DataProcessingDelegates callbacks) { PowerShellTraceSource traceSource = PowerShellTraceSourceFactory.GetTraceSource(); switch (xmlReader.LocalName) { case "Data": { if (xmlReader.AttributeCount != 2) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForDataElement, RemotingErrorIdStrings.IPCWrongAttributeCountForDataElement, new object[] { "Stream", "PSGuid", "Data" }); } string attribute = xmlReader.GetAttribute("Stream"); string g = xmlReader.GetAttribute("PSGuid"); Guid psGuid = new Guid(g); if (!xmlReader.Read()) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCInsufficientDataforElement, RemotingErrorIdStrings.IPCInsufficientDataforElement, new object[] { "Data" }); } if (xmlReader.NodeType != XmlNodeType.Text) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCOnlyTextExpectedInDataElement, RemotingErrorIdStrings.IPCOnlyTextExpectedInDataElement, new object[] { xmlReader.NodeType, "Data", XmlNodeType.Text }); } string s = xmlReader.Value; traceSource.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_DATA received, psGuid : " + psGuid.ToString()); byte[] rawData = Convert.FromBase64String(s); callbacks.DataPacketReceived(rawData, attribute, psGuid); return; } case "DataAck": { if (xmlReader.AttributeCount != 1) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement, RemotingErrorIdStrings.IPCWrongAttributeCountForElement, new object[] { "PSGuid", "DataAck" }); } string str4 = xmlReader.GetAttribute("PSGuid"); Guid guid2 = new Guid(str4); traceSource.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_DATA_ACK received, psGuid : " + guid2.ToString()); callbacks.DataAckPacketReceived(guid2); return; } case "Command": { if (xmlReader.AttributeCount != 1) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement, RemotingErrorIdStrings.IPCWrongAttributeCountForElement, new object[] { "PSGuid", "Command" }); } string str5 = xmlReader.GetAttribute("PSGuid"); Guid guid3 = new Guid(str5); traceSource.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_COMMAND received, psGuid : " + guid3.ToString()); callbacks.CommandCreationPacketReceived(guid3); return; } case "CommandAck": { if (xmlReader.AttributeCount != 1) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement, RemotingErrorIdStrings.IPCWrongAttributeCountForElement, new object[] { "PSGuid", "CommandAck" }); } string str6 = xmlReader.GetAttribute("PSGuid"); Guid guid4 = new Guid(str6); traceSource.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_COMMAND_ACK received, psGuid : " + guid4.ToString()); callbacks.CommandCreationAckReceived(guid4); return; } case "Close": { if (xmlReader.AttributeCount != 1) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement, RemotingErrorIdStrings.IPCWrongAttributeCountForElement, new object[] { "PSGuid", "Close" }); } string str7 = xmlReader.GetAttribute("PSGuid"); Guid guid5 = new Guid(str7); traceSource.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_CLOSE received, psGuid : " + guid5.ToString()); callbacks.ClosePacketReceived(guid5); return; } case "CloseAck": { if (xmlReader.AttributeCount != 1) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement, RemotingErrorIdStrings.IPCWrongAttributeCountForElement, new object[] { "PSGuid", "CloseAck" }); } string str8 = xmlReader.GetAttribute("PSGuid"); Guid guid6 = new Guid(str8); traceSource.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_CLOSE_ACK received, psGuid : " + guid6.ToString()); callbacks.CloseAckPacketReceived(guid6); return; } case "Signal": { if (xmlReader.AttributeCount != 1) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement, RemotingErrorIdStrings.IPCWrongAttributeCountForElement, new object[] { "PSGuid", "Signal" }); } string str9 = xmlReader.GetAttribute("PSGuid"); Guid guid7 = new Guid(str9); traceSource.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_SIGNAL received, psGuid : " + guid7.ToString()); callbacks.SignalPacketReceived(guid7); return; } case "SignalAck": { if (xmlReader.AttributeCount != 1) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement, RemotingErrorIdStrings.IPCWrongAttributeCountForElement, new object[] { "PSGuid", "SignalAck" }); } string str10 = xmlReader.GetAttribute("PSGuid"); Guid guid8 = new Guid(str10); traceSource.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_SIGNAL_ACK received, psGuid : " + guid8.ToString()); callbacks.SignalAckPacketReceived(guid8); return; } } throw new PSRemotingTransportException(PSRemotingErrorId.IPCUnknownElementReceived, RemotingErrorIdStrings.IPCUnknownElementReceived, new object[] { xmlReader.LocalName }); }
/// <summary> /// Process an XmlElement. The element name must be one of the following: /// "Data" /// </summary> /// <param name="xmlReader"></param> /// <param name="callbacks"></param> /// <exception cref="PSRemotingTransportException"> /// 1. Expected only two attributes with names "{0}" and "{1}" in "{2}" element. /// 2. Not enough data available to process "{0}" element. /// 3. Unknown node "{0}" in "{1}" element. Only "{2}" is expected in "{1}" element. /// </exception> private static void ProcessElement(XmlReader xmlReader, DataProcessingDelegates callbacks) { Dbg.Assert(null != xmlReader, "xmlReader cannot be null."); Dbg.Assert(xmlReader.NodeType == XmlNodeType.Element, "xmlReader's NodeType should be of type Element"); PowerShellTraceSource tracer = PowerShellTraceSourceFactory.GetTraceSource(); switch (xmlReader.LocalName) { case OutOfProcessUtils.PS_OUT_OF_PROC_DATA_TAG: { // A <Data> should have 1 attribute identifying the stream if (xmlReader.AttributeCount != 2) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForDataElement, RemotingErrorIdStrings.IPCWrongAttributeCountForDataElement, OutOfProcessUtils.PS_OUT_OF_PROC_STREAM_ATTRIBUTE, OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE, OutOfProcessUtils.PS_OUT_OF_PROC_DATA_TAG); } string stream = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_STREAM_ATTRIBUTE); string psGuidString = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE); Guid psGuid = new Guid(psGuidString); // Now move the reader to the data portion if (!xmlReader.Read()) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCInsufficientDataforElement, RemotingErrorIdStrings.IPCInsufficientDataforElement, OutOfProcessUtils.PS_OUT_OF_PROC_DATA_TAG); } if (xmlReader.NodeType != XmlNodeType.Text) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCOnlyTextExpectedInDataElement, RemotingErrorIdStrings.IPCOnlyTextExpectedInDataElement, xmlReader.NodeType, OutOfProcessUtils.PS_OUT_OF_PROC_DATA_TAG, XmlNodeType.Text); } string data = xmlReader.Value; tracer.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_DATA received, psGuid : " + psGuid.ToString()); byte[] rawData = Convert.FromBase64String(data); callbacks.DataPacketReceived(rawData, stream, psGuid); } break; case OutOfProcessUtils.PS_OUT_OF_PROC_DATA_ACK_TAG: { if (xmlReader.AttributeCount != 1) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement, RemotingErrorIdStrings.IPCWrongAttributeCountForElement, OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE, OutOfProcessUtils.PS_OUT_OF_PROC_DATA_ACK_TAG); } string psGuidString = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE); Guid psGuid = new Guid(psGuidString); tracer.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_DATA_ACK received, psGuid : " + psGuid.ToString()); callbacks.DataAckPacketReceived(psGuid); } break; case OutOfProcessUtils.PS_OUT_OF_PROC_COMMAND_TAG: { if (xmlReader.AttributeCount != 1) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement, RemotingErrorIdStrings.IPCWrongAttributeCountForElement, OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE, OutOfProcessUtils.PS_OUT_OF_PROC_COMMAND_TAG); } string psGuidString = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE); Guid psGuid = new Guid(psGuidString); tracer.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_COMMAND received, psGuid : " + psGuid.ToString()); callbacks.CommandCreationPacketReceived(psGuid); } break; case OutOfProcessUtils.PS_OUT_OF_PROC_COMMAND_ACK_TAG: { if (xmlReader.AttributeCount != 1) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement, RemotingErrorIdStrings.IPCWrongAttributeCountForElement, OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE, OutOfProcessUtils.PS_OUT_OF_PROC_COMMAND_ACK_TAG); } string psGuidString = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE); Guid psGuid = new Guid(psGuidString); tracer.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_COMMAND_ACK received, psGuid : " + psGuid.ToString()); callbacks.CommandCreationAckReceived(psGuid); } break; case OutOfProcessUtils.PS_OUT_OF_PROC_CLOSE_TAG: { if (xmlReader.AttributeCount != 1) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement, RemotingErrorIdStrings.IPCWrongAttributeCountForElement, OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE, OutOfProcessUtils.PS_OUT_OF_PROC_CLOSE_TAG); } string psGuidString = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE); Guid psGuid = new Guid(psGuidString); tracer.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_CLOSE received, psGuid : " + psGuid.ToString()); callbacks.ClosePacketReceived(psGuid); } break; case OutOfProcessUtils.PS_OUT_OF_PROC_CLOSE_ACK_TAG: { if (xmlReader.AttributeCount != 1) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement, RemotingErrorIdStrings.IPCWrongAttributeCountForElement, OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE, OutOfProcessUtils.PS_OUT_OF_PROC_CLOSE_ACK_TAG); } string psGuidString = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE); Guid psGuid = new Guid(psGuidString); tracer.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_CLOSE_ACK received, psGuid : " + psGuid.ToString()); callbacks.CloseAckPacketReceived(psGuid); } break; case OutOfProcessUtils.PS_OUT_OF_PROC_SIGNAL_TAG: { if (xmlReader.AttributeCount != 1) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement, RemotingErrorIdStrings.IPCWrongAttributeCountForElement, OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE, OutOfProcessUtils.PS_OUT_OF_PROC_SIGNAL_TAG); } string psGuidString = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE); Guid psGuid = new Guid(psGuidString); tracer.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_SIGNAL received, psGuid : " + psGuid.ToString()); callbacks.SignalPacketReceived(psGuid); } break; case OutOfProcessUtils.PS_OUT_OF_PROC_SIGNAL_ACK_TAG: { if (xmlReader.AttributeCount != 1) { throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement, RemotingErrorIdStrings.IPCWrongAttributeCountForElement, OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE, OutOfProcessUtils.PS_OUT_OF_PROC_SIGNAL_ACK_TAG); } string psGuidString = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE); Guid psGuid = new Guid(psGuidString); tracer.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_SIGNAL_ACK received, psGuid : " + psGuid.ToString()); callbacks.SignalAckPacketReceived(psGuid); } break; default: throw new PSRemotingTransportException(PSRemotingErrorId.IPCUnknownElementReceived, RemotingErrorIdStrings.IPCUnknownElementReceived, xmlReader.LocalName); } }
/// <summary> /// Process's data. Data should be a valid XML. /// </summary> /// <param name="data"></param> /// <param name="callbacks"></param> /// <exception cref="PSRemotingTransportException"> /// 1. Expected only two attributes with names "{0}" and "{1}" in "{2}" element. /// 2. Not enough data available to process "{0}" element. /// 3. Unknown node "{0}" in "{1}" element. Only "{2}" is expected in "{1}" element. /// </exception> internal static void ProcessData(string data, DataProcessingDelegates callbacks) { if (string.IsNullOrEmpty(data)) { return; } XmlReader reader = XmlReader.Create(new StringReader(data), XmlReaderSettings); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: ProcessElement(reader, callbacks); break; case XmlNodeType.EndElement: break; default: throw new PSRemotingTransportException(PSRemotingErrorId.IPCUnknownNodeType, RemotingErrorIdStrings.IPCUnknownNodeType, reader.NodeType.ToString(), XmlNodeType.Element.ToString(), XmlNodeType.EndElement.ToString()); } } }