Dicom 学习笔记-DICOM C-Get 消息服务

引言


  前篇介绍了 DICOM C-Find 消息服务,本文结合开源 DICOM 库 fo-dicom 详细介绍一下 C-Get 服务。

C-Get 消息服务


  C-Get 服务主要用于获取影像,用于一个 DIMSE-service-user 在同等的DIMSE-service-user 上查询复合 SOP 实例的属性满足查询条件给出的一组属性的复合 SOP 实例,并取回这些符合条件的复合 SOP 实例,同时在这个过程中将触发一个或多个 C-STORE 子操作过程,所有的操作(包含 C-STORE 子操作)均在同一个 association 连接中。
  有关C-Get 服务流程图,我通过不同的 C-Get SCP 的实现,出现了两种流程图,参考如下:

  • 每个 C-Store 子操作完成后都会有一个 C-Get 响应
Dicom 学习笔记-DICOM C-Get 消息服务_第1张图片
C-Get workflow 1

-C-Get 响应只会在 C-Store 子操作全部完成后发送

Dicom 学习笔记-DICOM C-Get 消息服务_第2张图片
C-Get workflow 2

C-Get SCU


  通过结合 DICOM 开源库 fo-dicom 来实现 C-Get SCU,部分代码如下:
需要引用命名空间【Dicom.Network】和【System.Threading】

using Dicom.Network;
using System.Threading;
var client = new DicomClient();

// 添加能够接收的抽象语法 abstract syntax,通过查看 DICOM 文件的 SOPClassUID 可获得,否则不会触发 C-Store 子操作
var pc = DicomPresentationContext.GetScpRolePresentationContext(DicomUID.CTImageStorage);
client.AdditionalPresentationContexts.Add(pc);

var counter = 0;
var locker = new object();
client.OnCStoreRequest = (DicomCStoreRequest request) =>
{
    lock (locker)
    {
        ++counter;
        Console.WriteLine(DateTime.Now.ToString() + " recived, count is " + counter);
    }
    // 可以通过 request.Dataset 获取到 DICOM 文件

    return new DicomCStoreResponse(request, DicomStatus.Success);
};

var get = new DicomCGetRequest({StudyInstanceUID});

var handle = new ManualResetEventSlim();
get.OnResponseReceived = (DicomCGetRequest requ, DicomCGetResponse response) =>
{
    if (response.Remaining == 0)
    {
        handle.Set();
    }
};
client.AddRequest(get);
client.Send({C-Get SCP IP}, {C-Get SCP Port}, false, {C-Get SCU AE Title}, {C-Get SCP AE Title});
handle.Wait();
  • StudyInstanceUID:检查唯一标识;
  • C-Get SCP IP:C-Get 服务端的 IP 地址或机器名;
  • C-Get SCP Port:C-Get 服务端的端口;
  • C-Get SCU AE Title:C-Get 客户端应用实体的名称;
  • C-Get SCP AE Title:C-Get 服务端应用实体的名称;

C-Get SCP


  在本文开头部分给出了两张流程图,这是通过不同 C-Get SCP 的实现进行抓包得到的,针对这块流程中不同的地方在 DICOM 标准中到底是如何定义的,可以在 DICOM 标准的第4部分【C.1.4 Service Definition】 中找到,具体的描述如下:

A C-GET service conveys the following semantics:

The SCU supplies Unique Key values to identify an entity at the level of the retrieval. The SCP generates C-STORE sub-oper- ations for the corresponding storage SOP Instances identified by the Unique Key values. These C-STORE sub-operations occur on the same Association as the C-GET service and the SCU/SCP roles will be reversed for the C-STORE.

The SCP may optionally generate responses to the C-GET with status equal to Pending during the processing of the C-STORE sub-operations. These C-GET responses indicate the number of Remaining C-STORE sub-operations and the number of C- STORE sub-operations returning the status of Success, Warning, and Failed.

When the number of Remaining C-STORE sub-operations reaches zero, the SCP generates a final response with a status equal to Success, Warning, Failed, or Refused. This response may indicate the number of C-STORE sub-operations returning the status of Success, Warning, and Failed. If the status of a C-STORE sub-operation was Failed a UID List will be returned.

The SCU may cancel the C-GET service by issuing a C-GET-CANCEL request at any time during the processing of the C- GET. The SCP terminates all incomplete C-STORE sub-operations and returns a status of Canceled.

这里已经说明了是可选的,更详细的 C-Get 过程在 DICOM 标准第7部分【9.1.3.2】有描述,不过针对这块没有描述,所以才出现如文章开头两种流程图。C-Get SCP 可以通过派生 DicomService 服务类来实现 Dicom 服务的基本框架,然后实现 IDicomServiceProvider 和 IDicomCGetProvider 接口来实现,部分代码可以参考这里,核心部分是实现 OnCGetRequest 方法。

C-Get 过程分析


  我这里选择本文开头第一种流程图的抓包数据进行分析,由于包的数据量比较大,我这里过滤掉不能被解码成 DICOM 协议的包,只分析能被解码成 DICOM 协议的包,先看前面部分:

C-Get pcap

红色框内的两行是两个 AE 建立 association 的过程:

  1. C-Get SCU(10.3.13.202)向 C-Get SCP(10.3.2.209) 发送 A-ASSOCIATE 请求;
  2. C-Get SCP(10.3.2.209)响应 C-Get SCU(10.3.13.202)的 A-ASSOCIATE 请求,然后两个 AE 就建立了一个 association;

接着蓝色框第一行就是 C-Get SCU(10.3.13.202)向 C-Get SCP(10.3.2.209) 发送 C-Get 请求了;
蓝色框第二行是 C-Get SCP(10.3.2.209)经过查找,找到满足条件的复合 SOP 实例对象,返回找到的信息,从下图红色框可以看到有3个子操作待执行,这里表示找到了3个满足条件的复合 SOP 实例对象,接下来会触发3个 C-Store 子操作。

Dicom 学习笔记-DICOM C-Get 消息服务_第3张图片
C-Get Response

接着就是 C-Store 归档数据的操作了

C-Store

一个复合 SOP 实例对象数据归档的最后一个包是一个畸形数据包【Malformed packet】:

Dicom 学习笔记-DICOM C-Get 消息服务_第4张图片
Malformed packet

从上图可以看出,前面传输的数据包都将在收到 Frame 609后进行重组,Frame 609就在 Malformed Packet 这个数据包,下面的蓝色框是这个数据包将前面所有收到的数据包重组之后,解析 SOP 对象得到的 TAG 值;
然后 C-Get SCU(10.3.13.202)向 C-Get SCP(10.3.2.209) 发送 C-Store 响应,并告知状态为 Success;

Dicom 学习笔记-DICOM C-Get 消息服务_第5张图片
C-Store Response

接着 C-Get SCP(10.3.2.209)向 C-Get SCU(10.3.13.202)发送 C-Get 请求的响应,内容包括当前的状态、剩下多少个子操作待执行、已完成多少个子操作和失败的子操作个数:

Dicom 学习笔记-DICOM C-Get 消息服务_第6张图片
C-Get Response

这个数据包结束后就又是下一个 SOP 实例对象的归档数据包了,和上面一样,这里不再继续分析。
当所有的 C-Store 子操作结束后 C-Get SCP(10.3.2.209)会向 C-Get SCU(10.3.13.202)发送一个 C-Get 请求的响应,这里会返回 C-Get 请求的状态,如果前面的子操作都成功的话,这里会返回状态为 Success:

Dicom 学习笔记-DICOM C-Get 消息服务_第7张图片
C-Get Response

最后 C-Get SCU(10.3.13.202)向 C-Get SCP(10.3.2.209) 发送 A-RELEASE 请求断开 association;
C-Get SCP(10.3.2.209)响应 C-Get SCU(10.3.13.202)的 A-RELEASE 请求,然后断开两个 AE 之间的 association;
至此,整个 DICOM 协议相关的包就全部发送完毕。

你可能感兴趣的:(Dicom 学习笔记-DICOM C-Get 消息服务)