Beispiel #1
0
        /// <summary>
        /// Save the file as a DICOM Part 10 format file.
        /// </summary>
        /// <param name="options">The options to use when saving the file.</param>
        /// <param name="iStream">The <see cref="Stream"/> to Save the DICOM file to.</param>
        /// <returns></returns>
        public bool Save(Stream iStream, DicomWriteOptions options)
        {
            if (iStream == null) throw new ArgumentNullException("iStream");

            // Original code has seek() here, but there may be use cases where
            // a user wants to add the file into a stream (that may contain other data)
            // and the seek would cause the method to not support that.
            byte[] prefix = new byte[128];
            iStream.Write(prefix,0,128);

            iStream.WriteByte((byte) 'D');
            iStream.WriteByte((byte) 'I');
            iStream.WriteByte((byte) 'C');
            iStream.WriteByte((byte) 'M');

            DicomStreamWriter dsw = new DicomStreamWriter(iStream);
            dsw.Write(TransferSyntax.ExplicitVrLittleEndian,
                      MetaInfo, options | DicomWriteOptions.CalculateGroupLengths);

            dsw.Write(TransferSyntax, DataSet, options);

			iStream.Flush();

            return true;
        }
Beispiel #2
0
        /// <summary>
        /// Method for sending a DIMSE mesage.
        /// </summary>
        /// <param name="pcid"></param>
        /// <param name="command"></param>
        /// <param name="dataset"></param>
        private void SendDimse(byte pcid, DicomAttributeCollection command, DicomAttributeCollection dataset)
        {
            try
            {
                TransferSyntax ts = _assoc.GetAcceptedTransferSyntax(pcid);

                uint total =
                    command.CalculateWriteLength(TransferSyntax.ImplicitVrLittleEndian,
                                                 DicomWriteOptions.Default | DicomWriteOptions.CalculateGroupLengths);

                if (dataset != null  && !dataset.IsEmpty())
                    total += dataset.CalculateWriteLength(ts, DicomWriteOptions.Default);

                PDataTFStream pdustream;
                if (_assoc.RemoteMaximumPduLength == 0 || _assoc.RemoteMaximumPduLength > _assoc.LocalMaximumPduLength)
					pdustream = new PDataTFStream(this, pcid, _assoc.LocalMaximumPduLength, total, NetworkSettings.Default.CombineCommandDataPdu);
                else
					pdustream = new PDataTFStream(this, pcid, _assoc.RemoteMaximumPduLength, total, NetworkSettings.Default.CombineCommandDataPdu);
                pdustream.OnTick += delegate
                                        {
                                            OnSendDimseProgress(pcid, command, dataset);

                                            if (DimseMessageSending != null)
                                                DimseMessageSending(_assoc, pcid, command, dataset);
                                        };

                // Introduced lock as risk mitigation for ticket #10147.  Note that a more thorough locking
                // mechanism should be developed to work across PDU types, and also should take into account
                // if we do end up using _multiThreaded = true
                lock (_writeSyncLock)
                {
                    LogSendReceive(false, command, dataset);

                    OnSendDimseBegin(pcid, command, dataset);

                    var dsw = new DicomStreamWriter(pdustream);
                    dsw.Write(TransferSyntax.ImplicitVrLittleEndian,
                              command, DicomWriteOptions.Default | DicomWriteOptions.CalculateGroupLengths);

                    if ((dataset != null) && !dataset.IsEmpty())
                    {
                        pdustream.IsCommand = false;
                        dsw.Write(ts, dataset, DicomWriteOptions.Default);
                    }

                    // flush last pdu
                    pdustream.Flush(true);
                }

                _assoc.TotalBytesSent += total;

                OnDimseSent(pcid, command, dataset);
            }
            catch (Exception e)
            {
                OnNetworkError(e, true);

                // TODO
                // Should we throw another exception here?  Should the user know there's an error?  They'll get
                // the error reported to them through the OnNetworkError routine, and throwing an exception here
                // might cause us to call OnNetworkError a second time, because the exception may be caught at a higher
                // level
                // Note, when fixing defect #8184, realized that throwing an exception here would cause
                // failures in the ImageServer, because there are places where we wouldn't catch the 
                // exception.  Should be careful if this is ever introduced back in.
                //throw new DicomException("Unexpected exception when sending a DIMSE message",e);
            }
        }