/// <summary> /// Send static virtual channel data /// </summary> /// <param name="channelId">Channel ID</param> /// <param name="channelPduHeader">Channel PDU Header</param> /// <param name="SVCData"></param> public void SendPacket(UInt16 channelId, CHANNEL_PDU_HEADER channelPduHeader, byte[] SVCData) { Virtual_Channel_RAW_Pdu channelPdu = new Virtual_Channel_RAW_Pdu(context); RdpbcgrUtility.FillCommonHeader(ref channelPdu.commonHeader, TS_SECURITY_HEADER_flags_Values.SEC_IGNORE_SEQNO | TS_SECURITY_HEADER_flags_Values.SEC_RESET_SEQNO, context, channelId); channelPdu.virtualChannelData = SVCData; channelPdu.channelPduHeader = channelPduHeader; context.Client.SendPdu(channelPdu); }
private void Listener() { string totalMessage = ""; while (true) { try { IntPtr PduBuffer = Marshal.AllocHGlobal(1600); byte[] MsgBuffer = null; UInt32 BytesRead = 1; UInt32 TotalMsgBytesRead = 0; CHANNEL_PDU_HEADER pdu = new CHANNEL_PDU_HEADER(); while (WTSVirtualChannelRead(hChannelOutputHandle, 5000, PduBuffer, CHANNEL_CHUNK_LENGTH, out BytesRead)) { // copy the PDU header byte[] arrBuffer = new byte[BytesRead]; Marshal.Copy(PduBuffer, arrBuffer, 0, (int)BytesRead); uint[] arrPdu = { 0, 0 }; Buffer.BlockCopy(arrBuffer, 0, arrPdu, 0, 8); pdu.length = arrPdu[0]; pdu.flags = arrPdu[1]; // resize the buffer for the new data Array.Resize(ref MsgBuffer, (int)((MsgBuffer == null) ? pdu.length : MsgBuffer.Length + pdu.length)); Array.Copy(arrBuffer, PDU_HEADER_SIZE, MsgBuffer, TotalMsgBytesRead, pdu.length); TotalMsgBytesRead += BytesRead; // done with this message if ((pdu.flags & CHANNEL_FLAG_LAST) != 0) { string resultXml = System.Text.Encoding.Unicode.GetString(MsgBuffer); Trace.WriteLine(resultXml); ParseXmlDocument(resultXml); // Release the MsgBuffer totalMessage += resultXml; MsgBuffer = null; TotalMsgBytesRead = 0; } } } catch (Exception ex) { Trace.WriteLine(ex.Message); MessageBox.Show(string.Format("{0}: {1}", ex.StackTrace, ex.Message)); break; } } }
private void ClientInfoChannelMonitor() { Trace.WriteLine("Starting ClientInfoChannelMonitor"); IntPtr PduBuffer = Marshal.AllocHGlobal((int)CHANNEL_CHUNK_LENGTH); byte[] MsgBuffer = null; UInt32 BytesRead = 0; UInt32 TotalMsgBytesRead = 0; CHANNEL_PDU_HEADER pdu = new CHANNEL_PDU_HEADER(); while (WTSVirtualChannelRead(hClientInfoChannelHandle, INFINITE, PduBuffer, CHANNEL_CHUNK_LENGTH, out BytesRead)) { // copy the PDU header byte[] arrBuffer = new byte[BytesRead]; Marshal.Copy(PduBuffer, arrBuffer, 0, (int)BytesRead); uint[] arrPdu = { 0, 0 }; Buffer.BlockCopy(arrBuffer, 0, arrPdu, 0, 8); pdu.length = arrPdu[0]; pdu.flags = arrPdu[1]; // resize the buffer for the new data Array.Resize(ref MsgBuffer, (int)((MsgBuffer == null) ? pdu.length : MsgBuffer.Length + pdu.length)); Array.Copy(arrBuffer, PDU_HEADER_SIZE, MsgBuffer, TotalMsgBytesRead, pdu.length); TotalMsgBytesRead += pdu.length; // done with this message if ((pdu.flags & CHANNEL_FLAG_LAST) != 0) { string sClientInfo = System.Text.Encoding.Unicode.GetString(MsgBuffer); Trace.WriteLine(sClientInfo); // Release the MsgBuffer MsgBuffer = null; TotalMsgBytesRead = 0; } } }
private void ImageTransferHelper(int channel) { try { Trace.WriteLine("Starting Image Transfer Channel Monitor for Channel #" + channel.ToString()); IntPtr PduBuffer = Marshal.AllocHGlobal((int)CHANNEL_PDU_LENGTH); byte[] MsgBuffer = null; UInt32 BytesRead = 0; CHANNEL_PDU_HEADER pdu = new CHANNEL_PDU_HEADER(); DateTime dtStart = DateTime.MinValue; DateTime dtFinish; while (true) { MemoryStream msBuffer = new MemoryStream(); do { bool bResult = WTSVirtualChannelRead(ImageXferChannelHandle[channel], INFINITE, PduBuffer, CHANNEL_PDU_LENGTH, out BytesRead); if (bResult && (BytesRead > 0)) { if (dtStart == DateTime.MinValue) { dtStart = DateTime.Now; } // copy the PDU header byte[] arrBuffer = new byte[BytesRead]; Marshal.Copy(PduBuffer, arrBuffer, 0, (int)BytesRead); uint[] arrPdu = { 0, 0 }; Buffer.BlockCopy(arrBuffer, 0, arrPdu, 0, 8); pdu.length = arrPdu[0]; pdu.flags = arrPdu[1]; msBuffer.Write(arrBuffer, 8, (int)(BytesRead - PDU_HEADER_SIZE)); // done with this message if ((pdu.flags & CHANNEL_FLAG_LAST) != 0) { MsgBuffer = msBuffer.ToArray(); // OK, first 4 bytes are the sequence number int[] nSequenceNumber = { 0 }; Buffer.BlockCopy(MsgBuffer, 0, nSequenceNumber, 0, 4); dtFinish = DateTime.Now; TimeSpan ts = dtFinish - dtStart; Trace.WriteLine("Total transfer time: " + ts.TotalMilliseconds + " ms"); byte[] arrFile = new byte[MsgBuffer.Length - 4]; Array.Copy(MsgBuffer, 4, arrFile, 0, MsgBuffer.Length - 4); if (BeforeImageCallback != null) { ImageCallbackEventHandler eh = null; foreach (Delegate del in BeforeImageCallback.GetInvocationList()) { try { eh = (ImageCallbackEventHandler)del; eh(this, new ImageCallbackEventArgs(1)); } catch (Exception ex) { BeforeImageCallback -= eh; } } } if (ImageCallback != null) { ImageCallback(arrFile, arrFile.Length, nSequenceNumber[0]); } if (AfterImageCallback != null) { ImageCallbackEventHandler eh = null; foreach (Delegate del in AfterImageCallback.GetInvocationList()) { try { eh = (ImageCallbackEventHandler)del; eh(this, new ImageCallbackEventArgs(0)); } catch (Exception ex) { AfterImageCallback -= eh; } } } // Release the MsgBuffer MsgBuffer = null; dtStart = DateTime.MinValue; } } } while (BytesRead == CHANNEL_PDU_LENGTH); msBuffer.Dispose(); msBuffer = null; } Marshal.FreeHGlobal(PduBuffer); } catch (Exception ex) { Trace.WriteLine(ex.Message); } }
public async Task <byte[]> ReadMessageAsync(CancellationToken ct) { AssertAlive(); try { var readBuffer = new byte[CHANNEL_PDU_LENGTH]; var readBytes = await Stream.ReadAsync(readBuffer, 0, readBuffer.Length, ct).ConfigureAwait(false); if (readBytes < 1) { throw new DvcChannelDisconnectedException(); } if (readBytes < CHANNEL_PDU_HEADER_SIZE) { throw new ProtocolViolationException($"Read returned buffer that was too short ({readBytes} bytes)"); } var pdu = CHANNEL_PDU_HEADER.FromBuffer(readBuffer, 0); if (!pdu.flags.HasFlag(CHANNEL_FLAG.First)) { throw new ProtocolViolationException($"PDU received with flags {pdu.flags} when FIRST was expected"); } var totalLength = pdu.length; var msResult = new MemoryStream(pdu.length); msResult.Write(readBuffer, CHANNEL_PDU_HEADER_SIZE, readBytes - CHANNEL_PDU_HEADER_SIZE); // 2..n while (msResult.Position < totalLength) { // Allowing cancellation here would result in a protocol violation readBytes = await Stream.ReadAsync(readBuffer, 0, readBuffer.Length).ConfigureAwait(false); if (readBytes < CHANNEL_PDU_HEADER_SIZE) { throw new ProtocolViolationException($"Read returned buffer that was too short ({readBytes} bytes)"); } pdu = CHANNEL_PDU_HEADER.FromBuffer(readBuffer, 0); if (pdu.flags.HasFlag(CHANNEL_FLAG.First)) { throw new ProtocolViolationException($"PDU received with flags {pdu.flags} when MIDDLE/LAST was expected"); } msResult.Write(readBuffer, CHANNEL_PDU_HEADER_SIZE, readBytes - CHANNEL_PDU_HEADER_SIZE); if (pdu.flags.HasFlag(CHANNEL_FLAG.Last)) { break; } } if (msResult.Position != totalLength) { throw new ProtocolViolationException($"PDU declared length {totalLength} but {msResult.Position} bytes were received"); } // ret return(msResult.ToArray()); } catch (IOException ex) when(ex.HResult == unchecked ((int)0x800700e9) /* HR ERROR_PIPE_NOT_CONNECTED */) { throw new DvcChannelDisconnectedException(); } }