/// <summary> /// Expect a Create Request PDU with specific channel name and using specific transport /// </summary> /// <param name="timeout">Timeout</param> /// <param name="channelName">Channel name</param> /// <param name="transportType">Transport type</param> /// <returns></returns> private CreateReqDvcPdu ExpectDVCCreateRequestPDU(TimeSpan timeout, string channelName, DynamicVC_TransportType transportType) { DateTime endTime = DateTime.Now + timeout; while (DateTime.Now < endTime) { if (unprocessedDVCPacketBuffer.Count > 0) { lock (unprocessedDVCPacketBuffer) { for (int i = 0; i < unprocessedDVCPacketBuffer.Count; i++) { if (transportType == unprocessedDVCPacketBuffer[i].TransportType && unprocessedDVCPacketBuffer[i].PDU is CreateReqDvcPdu && (string.Equals((unprocessedDVCPacketBuffer[i].PDU as CreateReqDvcPdu).ChannelName.Trim('\0'), channelName, StringComparison.OrdinalIgnoreCase))) { CreateReqDvcPdu pdu = unprocessedDVCPacketBuffer[i].PDU as CreateReqDvcPdu; unprocessedDVCPacketBuffer.RemoveAt(i); return(pdu); } } } } Thread.Sleep(this.waitInterval); } return(null); }
/// <summary> /// Expect a Create Request PDU /// </summary> /// <param name="timeout">Timeout</param> /// <param name="transportType">out parameter: indicate which transport received this packet</param> /// <returns></returns> private CreateReqDvcPdu ExpectDVCCreateRequestPDU(TimeSpan timeout, out DynamicVC_TransportType transportType) { transportType = DynamicVC_TransportType.RDP_TCP; { DateTime endTime = DateTime.Now + timeout; while (DateTime.Now < endTime) { if (unprocessedDVCPacketBuffer.Count > 0) { lock (unprocessedDVCPacketBuffer) { for (int i = 0; i < unprocessedDVCPacketBuffer.Count; i++) { if (unprocessedDVCPacketBuffer[i].PDU is CreateReqDvcPdu) { CreateReqDvcPdu pdu = unprocessedDVCPacketBuffer[i].PDU as CreateReqDvcPdu; transportType = unprocessedDVCPacketBuffer[i].TransportType; unprocessedDVCPacketBuffer.RemoveAt(i); return(pdu); } } } } Thread.Sleep(this.waitInterval); } return(null); } }
/// <summary> /// Expect server send a create SVC request /// </summary> /// <param name="timeout">Timeout</param> /// <param name="channelName">Channel Name</param> /// <param name="transportType">Transport Type</param> /// <returns></returns> public DynamicVirtualChannel ExpectChannel(TimeSpan timeout, string channelName, DynamicVC_TransportType transportType) { if (autoCreateChannel) { throw new InvalidOperationException("Cannot establish a DVC manually if autoCreateChannel is true!"); } if (!transportDic.ContainsKey(transportType)) { throw new InvalidOperationException("DVC transport:" + transportType + " does not exist."); } CreateReqDvcPdu createReq = this.ExpectDVCCreateRequestPDU(timeout, channelName, transportType); if (createReq == null) { throw new System.IO.IOException("Creation of channel: " + channelName + " failed, cannot receive a Create Request PDU"); } DynamicVirtualChannel channel = new DynamicVirtualChannel(createReq.ChannelId, channelName, (ushort)createReq.HeaderBits.Sp, transportDic[transportType]); channelDicbyId.Add(createReq.ChannelId, channel); this.SendDVCCreateResponsePDU(createReq.ChannelId, 0, transportType); return(channel); }
/// <summary> /// Establish a DVC after received a Create Request PDU /// </summary> /// <param name="createReq"></param> /// <param name="transportType"></param> /// <param name="receiveCallBack"></param> private void EstablishChannel(CreateReqDvcPdu createReq, DynamicVC_TransportType transportType, ReceiveData receiveCallBack = null) { if (channelDicbyId.ContainsKey(createReq.ChannelId)) { throw new InvalidOperationException("Cannot establish the DVC, since a channel with same channelId have been established. Channel ID is " + createReq.ChannelId); } DynamicVirtualChannel channel = new DynamicVirtualChannel(createReq.ChannelId, createReq.ChannelName, (ushort)createReq.HeaderBits.Sp, transportDic[transportType]); if (receiveCallBack != null) { // Add event method here can make sure processing the first DVC data packet channel.Received += receiveCallBack; } else { if (callBackMethodsDic != null && callBackMethodsDic.ContainsKey(createReq.ChannelName)) { channel.Received += callBackMethodsDic[createReq.ChannelName]; } } channelDicbyId.Add(createReq.ChannelId, channel); this.SendDVCCreateResponsePDU(createReq.ChannelId, 0, transportType); }
/// <summary> /// Establish a DVC after received a Create Request PDU /// </summary> /// <param name="createReq"></param> /// <param name="transportType"></param> /// <param name="receiveCallBack"></param> private void EstablishChannel(CreateReqDvcPdu createReq, DynamicVC_TransportType transportType, ReceiveData receiveCallBack = null) { if (channelDicbyId.ContainsKey(createReq.ChannelId)) { throw new InvalidOperationException("Cannot establish the DVC, since a channel with same channelId have been established. Channel ID is " + createReq.ChannelId); } DynamicVirtualChannel channel = new DynamicVirtualChannel(createReq.ChannelId, createReq.ChannelName, (ushort)createReq.HeaderBits.Sp, transportDic[transportType]); channelDicbyId.Add(createReq.ChannelId, channel); this.SendDVCCreateResponsePDU(createReq.ChannelId, 0, transportType); }
/// <summary> /// Expect to create a SVC /// </summary> /// <param name="timeout">Timeout</param> /// <param name="channelName">Channel Name</param> /// <param name="transportType">Transport Type</param> /// <returns></returns> public DynamicVirtualChannel ExpectChannel(TimeSpan timeout, string channelName, DynamicVC_TransportType transportType, ReceiveData receiveCallBack = null) { if (autoCreateChannel) { throw new InvalidOperationException("Cannot establish a DVC manually if autoCreateChannel is true!"); } if (!transportDic.ContainsKey(transportType)) { throw new InvalidOperationException("Not create DVC transport:" + transportType); } CreateReqDvcPdu createReq = this.ExpectDVCCreateRequestPDU(timeout, channelName, transportType); if (createReq == null) { throw new System.IO.IOException("Creation of channel: " + channelName + " failed, cannot receive a Create Request PDU"); } DynamicVirtualChannel channel = new DynamicVirtualChannel(createReq.ChannelId, channelName, (ushort)createReq.HeaderBits.Sp, transportDic[transportType]); if (receiveCallBack != null) { // Add event method here can make sure processing the first DVC data packet channel.Received += receiveCallBack; } else { if (callBackMethodsDic != null && callBackMethodsDic.ContainsKey(channelName)) { channel.Received += callBackMethodsDic[channelName]; } } channelDicbyId.Add(createReq.ChannelId, channel); this.SendDVCCreateResponsePDU(createReq.ChannelId, 0, transportType); return(channel); }
/// <summary> /// Send a Create Request PDU /// </summary> /// <param name="priority">Priority</param> /// <param name="channelId">Channel ID</param> /// <param name="channelName">Channel Name</param> /// <param name="transportType">Transport type</param> private void SendDVCCreateRequestPDU(ushort priority, uint channelId, string channelName, DynamicVC_TransportType transportType) { CreateReqDvcPdu pdu = pduBuilder.CreateCreateReqDvcPdu(priority, channelId, channelName); this.Send(pdu, transportType); }
/// <summary> /// Establish a DVC after received a Create Request PDU /// </summary> /// <param name="createReq"></param> /// <param name="transportType"></param> /// <param name="receiveCallBack"></param> private void EstablishChannel(CreateReqDvcPdu createReq, DynamicVC_TransportType transportType, ReceiveData receiveCallBack = null) { if (channelDicbyId.ContainsKey(createReq.ChannelId)) { throw new InvalidOperationException("Cannot establish the DVC, since a channel with same channelId have been established. Channel ID is " + createReq.ChannelId); } DynamicVirtualChannel channel = new DynamicVirtualChannel(createReq.ChannelId, createReq.ChannelName, (ushort)createReq.HeaderBits.Sp, transportDic[transportType]); if (receiveCallBack != null) { // Add event method here can make sure processing the first DVC data packet channel.Received += receiveCallBack; } else { if (callBackMethodsDic != null && callBackMethodsDic.ContainsKey(createReq.ChannelName)) { channel.Received += callBackMethodsDic[createReq.ChannelName]; } } channelDicbyId.Add(createReq.ChannelId, channel); this.SendDVCCreateResponsePDU(createReq.ChannelId, 0, transportType); }