Fo-dicom是如何實現(xiàn)DICOM 的網(wǎng)絡(luò)通信功能
DICOM3.0標準的通用通信模型
下圖顯示了DICOM3.0標準的通用通信模型,該模型跨越了 網(wǎng)絡(luò)(在線)和媒體存儲交換(離線)通信。應(yīng)用程序可利用以下任一傳輸機制:
- DICOM 消息服務(wù)和上層服務(wù),它們獨立于特定的物理網(wǎng)絡(luò)通信支持和協(xié)議(如 TCP/IP)。
- DICOM Web 服務(wù) API 和 HTTP 服務(wù),允許使用通用超文本和相關(guān)協(xié)議來傳輸 DICOM 服務(wù)
- 基本 DICOM 文件服務(wù),它提供獨立于特定媒體存儲格式和文件結(jié)構(gòu)的存儲介質(zhì)訪問
- DICOM 實時通信,提供基于 SMPTE 和 RTP 的 DICOM 元數(shù)據(jù)的實時傳輸。
DICOM的通用通信模型旨在為醫(yī)療圖像和相關(guān)數(shù)據(jù)的傳輸和存儲提供靈活和多樣化的解決方案。DICOM的通用通信模型不僅僅是簡單地傳輸和存儲醫(yī)療圖像和相關(guān)數(shù)據(jù),而是提供了一種多層次、多種方式的靈活解決方案,以滿足不同場景下的需求和要求。這種多樣化的傳輸和存儲機制使得DICOM成為醫(yī)療行業(yè)中不可或缺的通信標準,為醫(yī)療圖像和相關(guān)數(shù)據(jù)的交換和共享提供了可靠和高效的技術(shù)支持。
DICOM的通用通信模型的重要性和價值在于其能夠滿足醫(yī)療行業(yè)不同方面的需求,為醫(yī)療圖像和相關(guān)數(shù)據(jù)的傳輸和存儲提供了全面而可靠的解決方案,從而推動了醫(yī)療信息技術(shù)的發(fā)展和應(yīng)用。
fo-dicom基于.NET 平臺的網(wǎng)絡(luò)通信庫來實現(xiàn) DICOM 的網(wǎng)絡(luò)通信功能
`fo-dicom` 使用了Socket和TcpClient等底層網(wǎng)絡(luò)通信類來與DICOM服務(wù)器進行連接和通信,從而實現(xiàn) DICOM 的網(wǎng)絡(luò)通信功能。下面是 `fo-dicom` 網(wǎng)絡(luò)通信的實現(xiàn)基本原理:
- 傳輸協(xié)議選擇:`fo-dicom` 支持多種傳輸協(xié)議,如 TCP/IP、UDP 和 WebSocket。用戶可以根據(jù)需要選擇適合的傳輸協(xié)議。
- 連接建立:對于服務(wù)器端應(yīng)用程序,使用 `DicomServer` 類監(jiān)聽指定的端口號,等待客戶端連接請求。一旦有客戶端連接請求到達,服務(wù)器將建立一個與客戶端的網(wǎng)絡(luò)連接。
- 數(shù)據(jù)傳輸:在 DICOM 通信中,數(shù)據(jù)通過 DIMSE(DICOM Message Service Element)進行傳輸。DIMSE 是基于消息的通信模式,包括 C-STORE(存儲服務(wù))、C-FIND(查詢服務(wù))、C-MOVE(移動服務(wù))等。
- 數(shù)據(jù)編碼:`fo-dicom` 使用 DICOM 標準定義的數(shù)據(jù)格式和編碼規(guī)則對數(shù)據(jù)進行編碼。DICOM 數(shù)據(jù)集使用一系列的標簽(Tag)來組織和描述不同的信息,例如患者姓名、圖像序列等。`fo-dicom` 將數(shù)據(jù)集編碼為字節(jié)流以進行傳輸。
- 數(shù)據(jù)解碼:在接收方,`fo-dicom` 將接收到的字節(jié)流解碼為 DICOM 數(shù)據(jù)集,以便進行后續(xù)的處理和分析。
- 數(shù)據(jù)處理:根據(jù)具體的應(yīng)用需求,可以對 DICOM 數(shù)據(jù)集進行查詢、存儲、檢索等操作。`fo-dicom` 提供了一組 API 來處理 DICOM 數(shù)據(jù)集,以便用戶能夠方便地訪問和操作數(shù)據(jù)。
- 響應(yīng)發(fā)送:在服務(wù)器端應(yīng)用程序中,一旦完成對 DICOM 請求的處理,將向客戶端發(fā)送一個響應(yīng)。響應(yīng)中包含請求的執(zhí)行結(jié)果、狀態(tài)信息等。
- 連接斷開:通信完成后,可以關(guān)閉服務(wù)器端的監(jiān)聽或斷開客戶端與服務(wù)器的連接。
`fo-dicom` 通過使用 .NET 平臺的網(wǎng)絡(luò)通信庫來實現(xiàn)底層的網(wǎng)絡(luò)傳輸,并且遵循 DICOM 標準的數(shù)據(jù)格式和編碼規(guī)則。它提供了一組簡潔而強大的 API,使得用戶可以方便地進行 DICOM 數(shù)據(jù)的傳輸和處理。
案例說明
以下是一個使用 `fo-dicom` 進行C-STORE命令的網(wǎng)絡(luò)通信的簡單案例:
案例來源于官網(wǎng)的示例:https://github.com/fo-dicom/fo-dicom-samples
假設(shè)我們有一個服務(wù)器端應(yīng)用程序,它監(jiān)聽在本地的端口號 11112上,等待客戶端的連接請求。一旦接收到來自客戶端的 C-STORE 請求,服務(wù)器將把接收到的 DICOM 圖像數(shù)據(jù)保存到本地磁盤。
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using FellowOakDicom.Network;
using Microsoft.Extensions.Logging;
namespace Samples
{
internal class Program
{
private const string _storagePath = @".\DICOM";
private static void Main(string[] args)
{
// start DICOM server on port from command line argument or 11112
var port = args != null && args.Length > 0 && int.TryParse(args[0], out int tmp) ? tmp : 11112;
Console.WriteLine($"Starting C-Store SCP server on port {port}");
using (var server = DicomServerFactory.Create<CStoreSCP>(port))
{
// end process
Console.WriteLine("Press <return> to end...");
Console.ReadLine();
}
}
private class CStoreSCP : DicomService, IDicomServiceProvider, IDicomCStoreProvider, IDicomCEchoProvider
{
private static readonly DicomTransferSyntax[] _acceptedTransferSyntaxes = new DicomTransferSyntax[]
{
DicomTransferSyntax.ExplicitVRLittleEndian,
DicomTransferSyntax.ExplicitVRBigEndian,
DicomTransferSyntax.ImplicitVRLittleEndian
};
private static readonly DicomTransferSyntax[] _acceptedImageTransferSyntaxes = new DicomTransferSyntax[]
{
// Lossless
DicomTransferSyntax.JPEGLSLossless,
DicomTransferSyntax.JPEG2000Lossless,
DicomTransferSyntax.JPEGProcess14SV1,
DicomTransferSyntax.JPEGProcess14,
DicomTransferSyntax.RLELossless,
// Lossy
DicomTransferSyntax.JPEGLSNearLossless,
DicomTransferSyntax.JPEG2000Lossy,
DicomTransferSyntax.JPEGProcess1,
DicomTransferSyntax.JPEGProcess2_4,
// Uncompressed
DicomTransferSyntax.ExplicitVRLittleEndian,
DicomTransferSyntax.ExplicitVRBigEndian,
DicomTransferSyntax.ImplicitVRLittleEndian
};
public CStoreSCP(INetworkStream stream, Encoding fallbackEncoding, ILogger log, DicomServiceDependencies dependencies)
: base(stream, fallbackEncoding, log, dependencies)
{
}
public Task OnReceiveAssociationRequestAsync(DicomAssociation association)
{
if (association.CalledAE != "STORESCP")
{
return SendAssociationRejectAsync(
DicomRejectResult.Permanent,
DicomRejectSource.ServiceUser,
DicomRejectReason.CalledAENotRecognized);
}
foreach (var pc in association.PresentationContexts)
{
if (pc.AbstractSyntax == DicomUID.Verification)
{
pc.AcceptTransferSyntaxes(_acceptedTransferSyntaxes);
}
else if (pc.AbstractSyntax.StorageCategory != DicomStorageCategory.None)
{
pc.AcceptTransferSyntaxes(_acceptedImageTransferSyntaxes);
}
}
return SendAssociationAcceptAsync(association);
}
public Task OnReceiveAssociationReleaseRequestAsync()
{
return SendAssociationReleaseResponseAsync();
}
public void OnReceiveAbort(DicomAbortSource source, DicomAbortReason reason)
{
/* nothing to do here */
}
public void OnConnectionClosed(Exception exception)
{
/* nothing to do here */
}
public async Task<DicomCStoreResponse> OnCStoreRequestAsync(DicomCStoreRequest request)
{
var studyUid = request.Dataset.GetSingleValue<string>(DicomTag.StudyInstanceUID).Trim();
var instUid = request.SOPInstanceUID.UID;
var path = Path.GetFullPath(Program._storagePath);
path = Path.Combine(path, studyUid);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = Path.Combine(path, instUid) + ".dcm";
await request.File.SaveAsync(path);
return new DicomCStoreResponse(request, DicomStatus.Success);
}
public Task OnCStoreRequestExceptionAsync(string tempFileName, Exception e)
{
// let library handle logging and error response
return Task.CompletedTask;
}
public Task<DicomCEchoResponse> OnCEchoRequestAsync(DicomCEchoRequest request)
{
return Task.FromResult(new DicomCEchoResponse(request, DicomStatus.Success));
}
}
}
}
在上面的代碼中,首先從命令行參數(shù)中獲取端口號,然后創(chuàng)建一個 CStoreSCP 對象作為 DICOM 服務(wù)器,并將其綁定到指定的端口。在 CStoreSCP 類中,實現(xiàn)了 IDicomServiceProvider、IDicomCStoreProvider 和 IDicomCEchoProvider 接口,分別處理 DICOM 關(guān)聯(lián)請求、C-Store 請求和 C-Echo 請求。其中,
OnReceiveAssociationRequestAsync() 方法會檢查 Called AE 是否為 STORESCP,如果不是則拒絕關(guān)聯(lián)請求。OnCStoreRequestAsync() 方法則會將接收到的 DICOM 數(shù)據(jù)保存到本地文件系統(tǒng)中。其他的方法實現(xiàn)通常為空實現(xiàn),因為并不需要對其進行特殊處理。
對于客戶端應(yīng)用程序,我們可以使用 `DicomClient` 類來發(fā)送 C-STORE 請求到服務(wù)器。以下是一個簡單的客戶端示例:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using FellowOakDicom.Network;
using FellowOakDicom.Network.Client;
namespace Samples
{
internal static class Program
{
private static string _storeServerHost = "127.0.0.1";
private static int _storeServerPort = 11112;
private const string _storeServerAET = "STORESCP";
private const string _aet = "FODICOMSCU";
static async Task Main(string[] args)
{
var storeMore = "";
_storeServerHost = GetServerHost();
_storeServerPort = GetServerPort();
Console.WriteLine("***************************************************");
Console.WriteLine("Server AE Title: " + _storeServerAET);
Console.WriteLine("Server Host Address: " + _storeServerHost);
Console.WriteLine("Server Port: " + _storeServerPort);
Console.WriteLine("Client AE Title: " + _aet);
Console.WriteLine("***************************************************");
var client = DicomClientFactory.Create(_storeServerHost, _storeServerPort, false, _aet, _storeServerAET);
client.NegotiateAsyncOps();
do
{
try
{
Console.WriteLine();
Console.WriteLine("Enter the path for a DICOM file:");
Console.Write(">>>");
string dicomFile = Console.ReadLine();
while (!File.Exists(dicomFile))
{
Console.WriteLine("Invalid file path, enter the path for a DICOM file or press Enter to Exit:");
dicomFile = Console.ReadLine();
if (string.IsNullOrWhiteSpace(dicomFile))
{
return;
}
}
var request = new DicomCStoreRequest(dicomFile);
request.OnResponseReceived += (req, response) => Console.WriteLine("C-Store Response Received, Status: " + response.Status);
await client.AddRequestAsync(request);
await client.SendAsync();
}
catch (Exception exception)
{
Console.WriteLine();
Console.WriteLine("----------------------------------------------------");
Console.WriteLine("Error storing file. Exception Details:");
Console.WriteLine(exception.ToString());
Console.WriteLine("----------------------------------------------------");
Console.WriteLine();
}
Console.WriteLine("To store another file, enter \"y\"; Othersie, press enter to exit: ");
Console.Write(">>>");
storeMore = Console.ReadLine().Trim();
} while (storeMore.Length > 0 && storeMore.ToLower()[0] == 'y');
}
private static string GetServerHost()
{
var hostAddress = "";
var localIP = GetLocalIPAddress();
do
{
Console.WriteLine("Your local IP is: " + localIP);
Console.WriteLine("Enter \"1\" to use your local IP Address: " + localIP);
Console.WriteLine("Enter \"2\" to use defult: " + _storeServerHost);
Console.WriteLine("Enter \"3\" to enter custom");
Console.Write(">>>");
string input = Console.ReadLine().Trim().ToLower();
if (input.Length > 0)
{
if (input[0] == '1')
{
hostAddress = localIP;
}
else if (input[0] == '2')
{
hostAddress = _storeServerHost;
}
else if (input[0] == '3')
{
Console.WriteLine("Enter Server Host Address:");
Console.Write(">>>");
hostAddress = Console.ReadLine();
}
}
} while (hostAddress.Length == 0);
return hostAddress;
}
private static int GetServerPort()
{
Console.WriteLine("Enter Server port, or \"Enter\" for default \"" + _storeServerPort + "\":");
Console.Write(">>>");
var input = Console.ReadLine().Trim();
return string.IsNullOrEmpty(input) ? _storeServerPort : int.Parse(input);
}
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
return "";
}
}
}
首先定義了一些變量,包括存儲服務(wù)器的主機地址、端口號,以及客戶端和服務(wù)器的 AE(Application Entity)標題。然后,在 Main 方法中創(chuàng)建了一個 DicomClient 對象,并通過調(diào)用 NegotiateAsyncOps 方法進行異步操作的協(xié)商。接下來,進入一個循環(huán),用戶可以輸入要發(fā)送的 DICOM 文件的路徑。程序會檢查路徑是否有效,如果無效則提示用戶重新輸入,直到輸入為空或用戶選擇退出。然后,創(chuàng)建一個 DicomCStoreRequest 對象,傳入要發(fā)送的 DICOM 文件路徑作為參數(shù)。并通過訂閱 OnResponseReceived 事件來處理響應(yīng)。最后,調(diào)用 AddRequestAsync 方法將請求添加到客戶端的請求隊列中,并調(diào)用 SendAsync 方法發(fā)送請求。
其中GetServerHost 方法用于獲取服務(wù)器主機地址,它會提示用戶選擇使用本地 IP 地址、默認地址還是自定義地址。GetServerPort 方法用于獲取服務(wù)器端口號,用戶可以輸入自定義端口號,或者直接回車使用默認端口號。GetLocalIPAddress 方法用于獲取本地 IP 地址。
這個案例展示了一個簡單的基于 `fo-dicom` 的 DICOM 網(wǎng)絡(luò)通信示例,即SCU和SCP對CStore的通信的簡單處理,服務(wù)器接收到客戶端發(fā)送的 C-STORE 請求并保存圖像到本地磁盤。