ClearCanvas.Dicom.Network.NetworkBase.ProcessPDataTF C# (CSharp) Method

ProcessPDataTF() private method

private ProcessPDataTF ( ClearCanvas.Dicom.Network.PDataTFRead pdu ) : bool
pdu ClearCanvas.Dicom.Network.PDataTFRead
return bool
        private bool ProcessPDataTF(PDataTFRead pdu)
        {
        	try
            {
            	byte pcid = 0;
                foreach (PDV pdv in pdu.PDVs)
                {
                    pcid = pdv.PCID;
	                if (pdv.IsCommand)
	                {
		                if (_dimse.CommandData == null)
			                _dimse.CommandData = new ChunkStream();

		                _dimse.CommandData.AddChunk(pdv.Value);

		                if (_dimse.Command == null)
		                {
			                _dimse.Command = new DicomAttributeCollection(0x00000000, 0x0000FFFF);
		                }

		                if (_dimse.CommandReader == null)
		                {
			                _dimse.CommandReader = new DicomStreamReader(_dimse.CommandData)
				                {
					                TransferSyntax = TransferSyntax.ImplicitVrLittleEndian,
					                Dataset = _dimse.Command
				                };
		                }

		                DicomReadStatus stat =
			                _dimse.CommandReader.Read(null, DicomReadOptions.UseDictionaryForExplicitUN);
		                if (stat == DicomReadStatus.UnknownError)
		                {
			                Platform.Log(LogLevel.Error, "Unexpected parsing error when reading command group elements.");
			                return false;
		                }
		                _assoc.TotalBytesRead += (UInt64) pdv.PDVLength - 6;
		                if (DimseMessageReceiving != null)
			                DimseMessageReceiving(_assoc, pcid);

		                if (pdv.IsLastFragment)
		                {
			                if (stat == DicomReadStatus.NeedMoreData)
			                {
				                Platform.Log(LogLevel.Error,
				                             "Unexpected end of StreamReader.  More data needed ({0} bytes, last tag read {1}) after reading last PDV fragment.",
				                             _dimse.CommandReader.BytesNeeded, _dimse.CommandReader.LastTagRead.ToString());
				                return false;
			                }
			                _dimse.CommandData = null;
			                _dimse.CommandReader = null;

			                bool isLast = true;
			                if (_dimse.Command.Contains(DicomTags.DataSetType))
			                {
				                if (_dimse.Command[DicomTags.DataSetType].GetUInt16(0, 0x0) != 0x0101)
					                isLast = false;
			                }

			                OnReceiveDimseCommand(pcid, _dimse.Command);

			                if (isLast)
			                {
				                bool ret = OnReceiveDimse(pcid, _dimse.Command, _dimse.Dataset);
				                if (!ret)
					                Platform.Log(LogLevel.Error, "Error with OnReceiveDimse");

				                LogSendReceive(true, _dimse.Command, _dimse.Dataset);

				                //_assoc.TotalBytesRead += (UInt64)total;

				                _dimse = null;
				                return ret;
			                }
		                }
	                }
	                else
	                {
		                if (_dimse.DatasetData == null)
			                _dimse.DatasetData = new ChunkStream();

		                if (_dimse.Dataset == null)
			                _dimse.Dataset = new DicomAttributeCollection(0x00040000, 0xFFFFFFFF);

		                if (_dimse.DatasetReader == null)
		                {
			                _dimse.DatasetReader = new DicomStreamReader(_dimse.DatasetData)
				                {
					                TransferSyntax = _assoc.GetAcceptedTransferSyntax(pdv.PCID),
					                Dataset = _dimse.Dataset
				                };
		                }

						if (!_dimse.DatasetReader.EncounteredStopTag)
		                {
			                _dimse.DatasetData.AddChunk(pdv.Value);

			                _dimse.ParseDatasetStatus = _dimse.DatasetReader.Read(_dimse.DatasetStopTag, DicomReadOptions.UseDictionaryForExplicitUN);
			                if (_dimse.ParseDatasetStatus == DicomReadStatus.UnknownError)
			                {
				                Platform.Log(LogLevel.Error, "Unexpected parsing error when reading DataSet.");
				                return false;
			                }
		                }

		                _assoc.TotalBytesRead += (UInt64) pdv.PDVLength - 6;
                        if (DimseMessageReceiving != null)
                            DimseMessageReceiving(_assoc, pcid);

		                bool ret = true;
						if (_dimse.StreamMessage)
						{
							if (_dimse.IsNewDimse)
							{
								byte[] fileGroup2 = CreateFileHeader(pcid, _dimse.Command);
								ret = OnReceiveFileStream(pcid, _dimse.Command, _dimse.Dataset, fileGroup2, 0, fileGroup2.Length, _dimse.DatasetReader.EncounteredStopTag, _dimse.IsNewDimse, false);
								_dimse.IsNewDimse = false;
							}

							ret = ret && OnReceiveFileStream(pcid, _dimse.Command, _dimse.Dataset, pdv.Value.Array, pdv.Value.Index, pdv.Value.Count, _dimse.DatasetReader.EncounteredStopTag, false, pdv.IsLastFragment);
							if (!ret)
								Platform.Log(LogLevel.Error, "Error with OnReceiveFileStream");
						}

                        if (pdv.IsLastFragment)
                        {
							if (_dimse.ParseDatasetStatus == DicomReadStatus.NeedMoreData)
                            {
                            	Platform.Log(LogLevel.Error,
                            	             "Unexpected end of StreamReader.  More data needed ({0} bytes, last tag read {1}) after reading last PDV fragment.",
											 _dimse.DatasetReader.BytesNeeded, _dimse.DatasetReader.LastTagRead.ToString());
                                return false;
                            }
                            _dimse.CommandData = null;
                            _dimse.CommandReader = null;

                            LogSendReceive(true, _dimse.Command, _dimse.Dataset);

	                        if (!_dimse.StreamMessage)
	                        {
		                        ret = OnReceiveDimse(pcid, _dimse.Command, _dimse.Dataset);
		                        if (!ret)
			                        Platform.Log(LogLevel.Error, "Error with OnReceiveDimse");
	                        }
	                        else
	                        {
								if (MessageReceived != null)
									MessageReceived(_assoc, new DicomMessage(_dimse.Command, _dimse.Dataset));
	                        }

							_dimse = null;
							return ret;
                        }

						if (!ret) return false;
	                }
                }

                return true;
            }
            catch (Exception e)
            {
                //do something here!
				Platform.Log(LogLevel.Error, e, "Unexpected exception processing P-DATA PDU");
                return false;
            }
        }