public PDataTFStream(NetworkBase networkBase, byte pcid, uint max, uint total, bool combineCommandData)
 {
     _command = true;
     _pcid = pcid;
     _max = max;
     _pdu = new PDataTF();
     _buffer = new MemoryStream((int)total + 1024);
     _networkBase = networkBase;
 	_combineCommandData = combineCommandData;
 }
Exemple #2
0
        private bool ProcessPDataTF(PDataTF 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;
                            }
                            if (isLast)
                            {
                                if (_dimse.IsNewDimse)
                                {
                                    OnReceiveDimseBegin(pcid, _dimse.Command, _dimse.Dataset);
                                }
                                OnReceiveDimseProgress(pcid, _dimse.Command, _dimse.Dataset);
                                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();

                        _dimse.DatasetData.AddChunk(pdv.Value);

                        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
                                                   	};
                        }

                        DicomReadStatus stat =
                            _dimse.DatasetReader.Read(null, DicomReadOptions.UseDictionaryForExplicitUN);
                        if (stat == 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);

                        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.DatasetReader.BytesNeeded, _dimse.DatasetReader.LastTagRead.ToString());
                                return false;
                            }
                            _dimse.CommandData = null;
                            _dimse.CommandReader = null;

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

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

                            _dimse = null;
                            return ret;
                        }
                    }
                }

                if (_dimse.IsNewDimse)
                {
                    OnReceiveDimseBegin(pcid, _dimse.Command, _dimse.Dataset);
                    _dimse.IsNewDimse = false;
                }
                else
                {
                    OnReceiveDimseProgress(pcid, _dimse.Command, _dimse.Dataset);
                }

                return true;
            }
            catch (Exception e)
            {
                //do something here!
				Platform.Log(LogLevel.Error, e, "Unexpected exception processing P-DATA PDU");
                return false;
            }
        }
Exemple #3
0
        private bool ProcessRawPDU(RawPDU raw)
        {
            try
            {
                switch (raw.Type)
                {
                    case 0x01:
                        {
                            _assoc = new ServerAssociationParameters();
                            var pdu = new AAssociateRQ(_assoc);
                            pdu.Read(raw);
                            State = DicomAssociationState.Sta3_AwaitingLocalAAssociationResponsePrimative;
                            OnReceiveAssociateRequest(_assoc as ServerAssociationParameters);

                            if (State != DicomAssociationState.Sta13_AwaitingTransportConnectionClose &&
                                State != DicomAssociationState.Sta6_AssociationEstablished)
                            {
                                Platform.Log(LogLevel.Error, "Association incorrectly not accepted or rejected, aborting.");
                                return false;
                            }

                            //if derived class call SendAssociateAccept, it has fired this event
                            //if (AssociationEstablished != null)
                            //    AssociationEstablished(_assoc);

                            return true;
                        }
                    case 0x02:
                        {
                            var pdu = new AAssociateAC(_assoc);
                            pdu.Read(raw);
                            State = DicomAssociationState.Sta6_AssociationEstablished;

                            OnReceiveAssociateAccept(_assoc);

                            if (AssociationEstablished != null)
                                AssociationEstablished(_assoc);

                            return true;
                        }
                    case 0x03:
                        {
                            var pdu = new AAssociateRJ();
                            pdu.Read(raw);
                            State = DicomAssociationState.Sta13_AwaitingTransportConnectionClose;

                            if (AssociationRejected != null)
                                AssociationRejected(pdu.Source, pdu.Reason);

                            OnReceiveAssociateReject(pdu.Result, pdu.Source, pdu.Reason);

                            return true;
                        }
                    case 0x04:
                        {
                            var pdu = new PDataTF();
                            pdu.Read(raw);
                            return ProcessPDataTF(pdu);
                        }
                    case 0x05:
                        {
                            var pdu = new AReleaseRQ();
                            pdu.Read(raw);
                            State = DicomAssociationState.Sta8_AwaitingAReleaseRPLocalUser;

                            OnReceiveReleaseRequest();

                            return true;
                        }
                    case 0x06:
                        {
                            var pdu = new AReleaseRP();
                            pdu.Read(raw);
                            State = DicomAssociationState.Sta13_AwaitingTransportConnectionClose;

                            if (AssociationReleased != null)
                                AssociationReleased(_assoc);

                            OnReceiveReleaseResponse();

                            return true;
                        }
                    case 0x07:
                        {
                            var pdu = new AAbort();
                            pdu.Read(raw);
                            State = DicomAssociationState.Sta1_Idle;

                            if (AssociationAborted != null)
                                AssociationAborted(_assoc, pdu.Reason);

                            OnReceiveAbort(pdu.Source, pdu.Reason);

                            return true;
                        }
                    case 0xFF:
                        {
                            Platform.Log(LogLevel.Error, "Unexpected PDU type: 0xFF.  Potential parsing error.");
                            return false;
                        }
                    default:
                        throw new DicomNetworkException("Unknown PDU type");
                }
            }
            catch (Exception e)
            {
                OnNetworkError(e, true);

                if (NetworkError != null)
                    NetworkError(e);

                Platform.Log(LogLevel.Error, e, "Unexpected exception when processing PDU.");
                return false;
            }
        }
        private void WritePDU(bool last)
        {
            if (_pdu.PDVs.Count == 0 || ((CurrentPduSize() + 6) < _max && GetBufferLength() > 0))
            {
                CreatePDV(last);
            }
            if (_pdu.PDVs.Count > 0)
            {
                if (last)
                {
                    _pdu.PDVs[_pdu.PDVs.Count - 1].IsLastFragment = true;
                }
                RawPDU raw = _pdu.Write();

                _networkBase.EnqueuePdu(raw);
                if (OnTick != null)
                    OnTick();
                _pdu = new PDataTF();
            }
        }