Exemple #1
0
        private void PduDataReceived(byte[] pdu)
        {
            //Logging.Log("PduDataReceived {0}", pdu);
            //Dump("<< P-DATA-TF", pdu);

            // TODO this must be able to handle multiple pdvs

            string       syntax  = Syntax.ImplicitVrLittleEndian;
            ServiceClass service = FindServiceClass(pdu[10]);

            if (service != null)
            {
                syntax = service.Syntaxes[0];
            }

            // TODO currently assuming that a Command comes in a single pdu
            // TODO all DataSet fragments are combined into a single Message

            if (MessageControl.IsDataSet((MessageType)pdu[11]))
            {
                //Logging.Log("dataset");
                if (MessageControl.IsNotLast((MessageType)pdu[11]))
                {
                    //Logging.Log("not last fragment");
                    if (file == null)
                    {
                        file = new FileStream(Guid.NewGuid().ToString() + ".tmp", FileMode.Create, FileAccess.ReadWrite);
                        //Dump("PduDataReceived1 ...", pdu);
                    }
                    else
                    {
                        //Logging.Log("Receiving {0} bytes", pdu.Length - 12);
                    }
                    file.Write(pdu, 12, pdu.Length - 12);
                    return;
                }
                else
                {
                    //Logging.Log("last fragment");
                    if (file != null)
                    {
                        Dump("PduDataReceived2 ...", pdu);

                        file.Write(pdu, 12, pdu.Length - 12);
                        file.Flush();

                        DataSet dicom = new DataSet();
                        dicom.TransferSyntaxUID = syntax;

                        file.Seek(0, SeekOrigin.Begin);
                        dicom.Read(file);

                        if (service != null)
                        {
                            try
                            {
                                service.LastMessage = new Message(dicom);
                                ((IPresentationDataSink)service).OnData(MessageType.LastDataSet, service.LastMessage);
                            }
                            catch (Exception ex)
                            {
                                Logging.Log(LogLevel.Error, "Exception in OnData for SOPClassUId={0}, {1}", service.SOPClassUId, ex.Message);
                            }
                        }

                        file.Close();
                        File.Delete(file.Name);
                        file = null;
                        return;
                    }
                }
            }

            // parse response
            MemoryStream        memory   = new MemoryStream(pdu, 0, pdu.Length);
            PresentationDataPdu response = new PresentationDataPdu(syntax);

            response.Read(memory);

            response.Dump();

            foreach (PresentationDataValue pdv in response.Values)
            {
                service = FindServiceClass(pdv.context);
                if (service != null && service is IPresentationDataSink)
                {
                    service.LastMessage = new Message(pdv.Dicom);
                    ((IPresentationDataSink)service).OnData(pdv.control, service.LastMessage);
                }
            }
        }