Beispiel #1
0
        /// <summary>
        /// Initializes an instance of <see cref="DicomServer{T}"/>, that starts listening for connections in the background.
        /// </summary>
        /// <param name="port">Port to listen to.</param>
        /// <param name="certificateName">Certificate name for authenticated connections.</param>
        /// <param name="options">Service options.</param>
        /// <param name="logger">Logger.</param>
        public DicomServer(int port, string certificateName = null, DicomServiceOptions options = null, Logger logger = null)
        {
            this.port               = port;
            this.certificateName    = certificateName;
            this.cancellationSource = new CancellationTokenSource();
            this.clients            = new List <T>();

            this.Options     = options;
            this.Logger      = logger ?? LogManager.GetLogger("Dicom.Network");
            this.IsListening = false;
            this.Exception   = null;

            Task.Factory.StartNew(
                this.OnTimerTickAsync,
                this.cancellationSource.Token,
                TaskCreationOptions.LongRunning,
                TaskScheduler.Default);

            Task.Factory.StartNew(
                this.ListenAsync,
                this.cancellationSource.Token,
                TaskCreationOptions.LongRunning,
                TaskScheduler.Default);

            this.disposed = false;
        }
Beispiel #2
0
        private TServer CreateServer <TProvider, TServer>(int port)
            where TProvider : DicomService, IDicomServiceProvider
            where TServer : class, IDicomServer <TProvider>, new()
        {
            var logger  = _logger.IncludePrefix(nameof(IDicomServer));
            var options = new DicomServiceOptions
            {
                LogDimseDatasets = false,
                LogDataPDUs      = false,
            };
            var ipAddress = NetworkManager.IPv4Any;
            var server    = DicomServer.Create <TProvider, TServer>(ipAddress, port, logger: logger, options: options);

            return(server as TServer);
        }
Beispiel #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DicomService"/> class.
        /// </summary>
        /// <param name="stream">Network stream.</param>
        /// <param name="fallbackEncoding">Fallback encoding.</param>
        /// <param name="log">Logger</param>
        protected DicomService(INetworkStream stream, Encoding fallbackEncoding, Logger log)
        {
            _network = stream;
            _lock = new object();
            _pduQueue = new Queue<PDU>();
            MaximumPDUsInQueue = 16;
            _msgQueue = new Queue<DicomMessage>();
            _pending = new List<DicomRequest>();
            _isConnected = true;
            _fallbackEncoding = fallbackEncoding ?? DicomEncoding.Default;
            Logger = log ?? LogManager.GetLogger("Dicom.Network");
            Options = new DicomServiceOptions();

            BackgroundWorker = ReadAndProcessPDUAsync();
        }
Beispiel #4
0
 internal DicomServiceUser(
     DicomClient client,
     Stream stream,
     DicomAssociation association,
     DicomServiceOptions options,
     Logger log)
     : base(stream, log)
 {
     this.client      = client;
     this.isLingering = false;
     if (options != null)
     {
         this.Options = options;
     }
     this.SendAssociationRequest(association);
 }
Beispiel #5
0
        /// <inheritdoc />
        public virtual Task StartAsync(string ipAddress, int port, string certificateName, Encoding fallbackEncoding,
                                       DicomServiceOptions options, object userState)
        {
            if (_wasStarted)
            {
                throw new DicomNetworkException("Server has already been started once, cannot be started again.");
            }
            _wasStarted = true;

            IPAddress = string.IsNullOrEmpty(ipAddress?.Trim()) ? NetworkManager.IPv4Any : ipAddress;
            Port      = port;

            Options = options;

            _userState        = userState;
            _certificateName  = certificateName;
            _fallbackEncoding = fallbackEncoding;

            return(Task.WhenAll(ListenForConnectionsAsync(), RemoveUnusedServicesAsync()));
        }
Beispiel #6
0
            internal DicomServiceUser(
                DicomClient client,
                INetworkStream stream,
                DicomAssociation association,
                DicomServiceOptions options,
                Encoding fallbackEncoding,
                Logger log)
                : base(stream, fallbackEncoding, log)
            {
                _client = client;

                if (options != null)
                {
                    Options = options;
                }

                List <DicomRequest> requests;

                lock (_client._lock)
                {
                    requests = _client._requests.Select(s => s.Value).ToList();
                }

                foreach (var request in requests)
                {
                    association.PresentationContexts.AddFromRequest(request);
                }

                foreach (var context in client.AdditionalPresentationContexts)
                {
                    association.PresentationContexts.Add(
                        context.AbstractSyntax,
                        context.UserRole,
                        context.ProviderRole,
                        context.GetTransferSyntaxes().ToArray());
                }

                _association      = association;
                _isInitialized    = false;
                _releaseRequested = 0;
            }
        public Task StartAsync(CancellationToken cancellationToken)
        {
            return(Task.Run(() =>
            {
                _logger.LogInformation("Clara DICOM Adapter (SCP Service) {Version} loading...",
                                       Assembly.GetEntryAssembly().GetCustomAttribute <AssemblyFileVersionAttribute>().Version);

                try
                {
                    _logger.Log(LogLevel.Information, "Starting SCP Service.");
                    var options = new FoDicomNetwork.DicomServiceOptions
                    {
                        IgnoreUnsupportedTransferSyntaxChange = true,
                        LogDimseDatasets = _dicomAdapterConfiguration.Value.Dicom.Scp.LogDimseDatasets,
                        MaxClientsAllowed = _dicomAdapterConfiguration.Value.Dicom.Scp.MaximumNumberOfAssociations
                    };

                    _server = FoDicomNetwork.DicomServer.Create <ScpServiceInternal>(
                        FoDicomNetwork.NetworkManager.IPv4Any,
                        _dicomAdapterConfiguration.Value.Dicom.Scp.Port,
                        options: options,
                        userState: _associationDataProvider);

                    if (_server.Exception != null)
                    {
                        throw _server.Exception;
                    }

                    Status = ServiceStatus.Running;
                    _logger.Log(LogLevel.Information, "SCP listening on port: {0}", _dicomAdapterConfiguration.Value.Dicom.Scp.Port);
                }
                catch (System.Exception ex)
                {
                    Status = ServiceStatus.Cancelled;
                    _logger.Log(LogLevel.Critical, ex, "Failed to start SCP listener.");
                    _appLifetime.StopApplication();
                }
            }));
        }
Beispiel #8
0
            internal DicomServiceUser(
                DicomClient client,
                INetworkStream stream,
                DicomAssociation association,
                DicomServiceOptions options,
                Encoding fallbackEncoding,
                Logger log)
                : base(stream, fallbackEncoding, log)
            {
                this.client = client;

                if (options != null)
                {
                    Options = options;
                }

                List <DicomRequest> requests;

                lock (this.client.locker)
                {
                    requests = new List <DicomRequest>(this.client.requests);
                }

                foreach (var request in requests)
                {
                    association.PresentationContexts.AddFromRequest(request);
                }

                foreach (var context in client.AdditionalPresentationContexts)
                {
                    association.PresentationContexts.Add(
                        context.AbstractSyntax,
                        context.UserRole,
                        context.ProviderRole,
                        context.GetTransferSyntaxes().ToArray());
                }

                SendAssociationRequest(association);
            }
Beispiel #9
0
        public DicomServer(
            int port,
            string certificateName      = null,
            DicomServiceOptions options = null,
            Encoding fallbackEncoding   = null,
            Logger logger = null)
        {
            this.Port               = port;
            this.certificateName    = certificateName;
            this.fallbackEncoding   = fallbackEncoding;
            this.cancellationSource = new CancellationTokenSource();
            this.clients            = new List <T>();

            this.Options     = options;
            this.Logger      = logger ?? LogManager.GetLogger("Dicom.Network");
            this.IsListening = false;
            this.Exception   = null;

            this.BackgroundWorker = Task.WhenAll(OnTimerTickAsync(), ListenAsync());

            this.disposed = false;
            this.Register();
        }
Beispiel #10
0
 internal DicomServiceUser(
     DicomClient client,
     Stream stream,
     DicomAssociation association,
     DicomServiceOptions options,
     Logger log)
     : base(stream, log)
 {
     this.client = client;
     this.isLingering = false;
     if (options != null) this.Options = options;
     this.SendAssociationRequest(association);
 }
Beispiel #11
0
            internal DicomServiceUser(
                DicomClient client,
                INetworkStream stream,
                DicomAssociation association,
                DicomServiceOptions options,
                Encoding fallbackEncoding,
                Logger log)
                : base(stream, fallbackEncoding, log)
            {
                this.client = client;

                if (options != null)
                {
                    Options = options;
                }

                List<DicomRequest> requests;
                lock (this.client.locker)
                {
                    requests = new List<DicomRequest>(this.client.requests);
                }

                foreach (var request in requests)
                {
                    association.PresentationContexts.AddFromRequest(request);
                }

                foreach (var context in client.AdditionalPresentationContexts)
                {
                    association.PresentationContexts.Add(
                        context.AbstractSyntax,
                        context.UserRole,
                        context.ProviderRole,
                        context.GetTransferSyntaxes().ToArray());
                }

                SendAssociationRequest(association);
            }