/// <summary> /// Expect a DVC Create Response PDU /// </summary> /// <param name="timeout">Timeout</param> /// <param name="channelId">Channel Id</param> /// <param name="transportType">Transport type</param> /// <returns></returns> private CreateRespDvcPdu ExpectDVCCreateResponsePDU(TimeSpan timeout, uint channelId, 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 CreateRespDvcPdu && (unprocessedDVCPacketBuffer[i].PDU as CreateRespDvcPdu).ChannelId == channelId) { CreateRespDvcPdu pdu = unprocessedDVCPacketBuffer[i].PDU as CreateRespDvcPdu; unprocessedDVCPacketBuffer.RemoveAt(i); return(pdu); } } } } Thread.Sleep(this.waitInterval); } return(null); }
/// <summary> /// Create a dynamic virtual channel /// </summary> /// <param name="priority">Priority</param> /// <param name="channelName">Channel name</param> /// <param name="transportType">Transport type</param> /// <param name="receiveCallBack">Callback method called when received data</param> /// <returns>DVC created</returns> public DynamicVirtualChannel CreateChannel(TimeSpan timeout, ushort priority, string channelName, DynamicVC_TransportType transportType, ReceiveData receiveCallBack = null) { if (!transportDic.ContainsKey(transportType)) { throw new InvalidOperationException("Not create DVC transport:" + transportType); } UInt32 channelId = DynamicVirtualChannel.NewChannelId(); DynamicVirtualChannel channel = new DynamicVirtualChannel(channelId, channelName, priority, transportDic[transportType]); if (receiveCallBack != null) { // Add event method here can make sure processing the first DVC data packet channel.Received += receiveCallBack; } channelDicbyId.Add(channelId, channel); this.SendDVCCreateRequestPDU(priority, channelId, channelName, transportType); CreateRespDvcPdu createResp = this.ExpectDVCCreateResponsePDU(timeout, channelId, transportType); if (createResp == null) { throw new System.IO.IOException("Creation of channel: " + channelName + " failed, cannot receive a Create Response PDU"); } if (createResp.CreationStatus < 0) { //Create failed throw new System.IO.IOException("Creation of DVC failed with error code: " + createResp.CreationStatus + ", channel name is " + channelName); } return(channel); }
/// <summary> /// Send a DVC Create Response PDU /// </summary> /// <param name="channelId"></param> /// <param name="creationStatus"></param> /// <param name="transportType"></param> private void SendDVCCreateResponsePDU(uint channelId, int creationStatus, DynamicVC_TransportType transportType) { CreateRespDvcPdu createResp = pduBuilder.CreateCreateRespDvcPdu(channelId, creationStatus); this.Send(createResp, transportType); }