Program.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (c) 2012-2020 fo-dicom contributors.
  2. // Licensed under the Microsoft Public License (MS-PL).
  3. using System;
  4. using System.IO;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Dicom.Log;
  8. using Dicom.Network;
  9. namespace Dicom.CStoreSCP
  10. {
  11. internal class Program
  12. {
  13. private static string StoragePath = @".\DICOM";
  14. private static void Main(string[] args)
  15. {
  16. // preload dictionary to prevent timeouts
  17. var dict = DicomDictionary.Default;
  18. // start DICOM server on port from command line argument or 11112
  19. var port = args != null && args.Length > 0 && int.TryParse(args[0], out int tmp) ? tmp : 11112;
  20. Console.WriteLine($"Starting C-Store SCP server on port {port}");
  21. using (var server = DicomServer.Create<CStoreSCP>(port))
  22. {
  23. // end process
  24. Console.WriteLine("Press <return> to end...");
  25. Console.ReadLine();
  26. }
  27. }
  28. private class CStoreSCP : DicomService, IDicomServiceProvider, IDicomCStoreProvider, IDicomCEchoProvider
  29. {
  30. private static readonly DicomTransferSyntax[] AcceptedTransferSyntaxes = new DicomTransferSyntax[]
  31. {
  32. DicomTransferSyntax.ExplicitVRLittleEndian,
  33. DicomTransferSyntax.ExplicitVRBigEndian,
  34. DicomTransferSyntax.ImplicitVRLittleEndian
  35. };
  36. private static readonly DicomTransferSyntax[] AcceptedImageTransferSyntaxes = new DicomTransferSyntax[]
  37. {
  38. // Lossless
  39. DicomTransferSyntax.JPEGLSLossless,
  40. DicomTransferSyntax.JPEG2000Lossless,
  41. DicomTransferSyntax.JPEGProcess14SV1,
  42. DicomTransferSyntax.JPEGProcess14,
  43. DicomTransferSyntax.RLELossless,
  44. // Lossy
  45. DicomTransferSyntax.JPEGLSNearLossless,
  46. DicomTransferSyntax.JPEG2000Lossy,
  47. DicomTransferSyntax.JPEGProcess1,
  48. DicomTransferSyntax.JPEGProcess2_4,
  49. // Uncompressed
  50. DicomTransferSyntax.ExplicitVRLittleEndian,
  51. DicomTransferSyntax.ExplicitVRBigEndian,
  52. DicomTransferSyntax.ImplicitVRLittleEndian
  53. };
  54. public CStoreSCP(INetworkStream stream, Encoding fallbackEncoding, Logger log)
  55. : base(stream, fallbackEncoding, log)
  56. {
  57. }
  58. public Task OnReceiveAssociationRequestAsync(DicomAssociation association)
  59. {
  60. if (association.CalledAE != "STORESCP")
  61. {
  62. return SendAssociationRejectAsync(
  63. DicomRejectResult.Permanent,
  64. DicomRejectSource.ServiceUser,
  65. DicomRejectReason.CalledAENotRecognized);
  66. }
  67. foreach (var pc in association.PresentationContexts)
  68. {
  69. if (pc.AbstractSyntax == DicomUID.Verification) pc.AcceptTransferSyntaxes(AcceptedTransferSyntaxes);
  70. else if (pc.AbstractSyntax.StorageCategory != DicomStorageCategory.None) pc.AcceptTransferSyntaxes(AcceptedImageTransferSyntaxes);
  71. }
  72. return SendAssociationAcceptAsync(association);
  73. }
  74. public Task OnReceiveAssociationReleaseRequestAsync()
  75. {
  76. return SendAssociationReleaseResponseAsync();
  77. }
  78. public void OnReceiveAbort(DicomAbortSource source, DicomAbortReason reason)
  79. {
  80. }
  81. public void OnConnectionClosed(Exception exception)
  82. {
  83. }
  84. public DicomCStoreResponse OnCStoreRequest(DicomCStoreRequest request)
  85. {
  86. var studyUid = request.Dataset.GetSingleValue<string>(DicomTag.StudyInstanceUID);
  87. var instUid = request.SOPInstanceUID.UID;
  88. var path = Path.GetFullPath(Program.StoragePath);
  89. path = Path.Combine(path, studyUid);
  90. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  91. path = Path.Combine(path, instUid) + ".dcm";
  92. request.File.Save(path);
  93. return new DicomCStoreResponse(request, DicomStatus.Success);
  94. }
  95. public void OnCStoreRequestException(string tempFileName, Exception e)
  96. {
  97. // let library handle logging and error response
  98. }
  99. public DicomCEchoResponse OnCEchoRequest(DicomCEchoRequest request)
  100. {
  101. return new DicomCEchoResponse(request, DicomStatus.Success);
  102. }
  103. }
  104. }
  105. }