Ejemplo n.º 1
0
        internal CStoreRequestResult Send(CStoreClient client)
        {
            Load(client);

            if (HasError)
            {
                if (client.Associate.FindAbstractSyntax(SOPClassUID) == 0)
                {
                    client.Reassociate();
                    return(CStoreRequestResult.Reassociate);
                }
            }

            if (client.OnCStoreRequestBegin != null)
            {
                client.OnCStoreRequestBegin(client, this);
            }

            if (HasError)
            {
                Status = DcmStatus.UnrecognizedOperation;
                if (client.OnCStoreRequestFailed != null)
                {
                    client.OnCStoreRequestFailed(client, this);
                }
                return(CStoreRequestResult.Failure);
            }

            byte pcid = client.Associate.FindAbstractSyntaxWithTransferSyntax(SOPClassUID, TransferSyntax);

            if (pcid == 0)
            {
                client.Log.Info("{0} -> C-Store request failed: No accepted presentation context for {1}", client.LogID, SOPClassUID.Description);
                Status = DcmStatus.SOPClassNotSupported;
                if (client.OnCStoreRequestFailed != null)
                {
                    client.OnCStoreRequestFailed(client, this);
                }
                return(CStoreRequestResult.Failure);
            }

            if (_dataset != null)
            {
                client.SendCStoreRequest(pcid, SOPInstanceUID, _dataset);
            }
            else
            {
                using (Stream s = DicomFileFormat.GetDatasetStream(FileName)) {
                    client.SendCStoreRequest(pcid, SOPInstanceUID, s);
                    s.Close();
                }
            }

            if (client.OnCStoreRequestComplete != null)
            {
                client.OnCStoreRequestComplete(client, this);
            }

            return(CStoreRequestResult.Success);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads the DICOM file and changes the transfer syntax if needed. (Internal)
        /// </summary>
        /// <param name="client">C-Store Client</param>
        public void Load(CStoreClient client)
        {
            if (_loaded)
            {
                return;
            }

            try {
                DicomTransferSyntax tx = null;

                foreach (DcmPresContext pc in client.Associate.GetPresentationContexts())
                {
                    if (pc.Result == DcmPresContextResult.Accept && pc.AbstractSyntax == _sopClass)
                    {
                        tx = pc.AcceptedTransferSyntax;
                        break;
                    }
                }

                if (tx == null)
                {
                    throw new DicomNetworkException("No accepted presentation contexts for abstract syntax: " + _sopClass.Description);
                }

                // Possible to stream from file?
                if (!client.DisableFileStreaming && tx == TransferSyntax)
                {
                    using (FileStream fs = DicomFileFormat.GetDatasetStream(_fileName)) {
                        _datasetSize = Convert.ToUInt32(fs.Length - fs.Position);
                        fs.Close();
                    }
                    return;
                }

                DcmCodecParameters codecParams = null;
                if (tx == client.PreferredTransferSyntax)
                {
                    codecParams = client.PreferredTransferSyntaxParams;
                }

                DicomFileFormat ff = new DicomFileFormat();
                ff.Load(FileName, DicomReadOptions.DefaultWithoutDeferredLoading);

                if (_originalTransferSyntax != tx)
                {
                    if (_originalTransferSyntax.IsEncapsulated)
                    {
                        // Dataset is compressed... decompress
                        try {
                            ff.ChangeTransferSytnax(DicomTransferSyntax.ExplicitVRLittleEndian, null);
                        }
                        catch {
                            client.Log.Error("{0} -> Unable to change transfer syntax:\n\tclass: {1}\n\told: {2}\n\tnew: {3}\n\treason: {4}\n\tcodecs: {5} - {6}",
                                             client.LogID, SOPClassUID.Description, _originalTransferSyntax, DicomTransferSyntax.ExplicitVRLittleEndian,
#if DEBUG
                                             HasError ? "Unknown" : Error.ToString(),
#else
                                             HasError ? "Unknown" : Error.Message,
#endif
                                             DicomCodec.HasCodec(_originalTransferSyntax), DicomCodec.HasCodec(DicomTransferSyntax.ExplicitVRLittleEndian));
                            throw;
                        }
                    }

                    if (tx.IsEncapsulated)
                    {
                        // Dataset needs to be compressed
                        try {
                            ff.ChangeTransferSytnax(tx, codecParams);
                        }
                        catch {
                            client.Log.Error("{0} -> Unable to change transfer syntax:\n\tclass: {1}\n\told: {2}\n\tnew: {3}\n\treason: {4}\n\tcodecs: {5} - {6}",
                                             client.LogID, SOPClassUID.Description, ff.Dataset.InternalTransferSyntax, tx,
#if DEBUG
                                             HasError ? "Unknown" : Error.ToString(),
#else
                                             HasError ? "Unknown" : Error.Message,
#endif
                                             DicomCodec.HasCodec(ff.Dataset.InternalTransferSyntax), DicomCodec.HasCodec(tx));
                            throw;
                        }
                    }
                }

                _dataset        = ff.Dataset;
                _datasetSize    = _dataset.CalculateWriteLength(tx, DicomWriteOptions.Default);
                _transferSyntax = tx;
            }
            catch (Exception e) {
                _dataset        = null;
                _transferSyntax = _originalTransferSyntax;
                _status         = DcmStatus.ProcessingFailure;
                _exception      = e;
            }
            finally {
                _loaded = true;
            }
        }