コード例 #1
0
ファイル: DicomServer.cs プロジェクト: fo-dicom/fo-dicom
        /// <summary>
        /// Listen indefinitely for network connections on the specified port.
        /// </summary>
        private async Task ListenForConnectionsAsync()
        {
            INetworkListener listener = null;

            try
            {
                var noDelay = Options?.TcpNoDelay ?? DicomServiceOptions.Default.TcpNoDelay;

                listener = NetworkManager.CreateNetworkListener(IPAddress, Port);
                await listener.StartAsync().ConfigureAwait(false);

                IsListening = true;

                while (!_cancellationSource.IsCancellationRequested)
                {
                    await _hasNonMaxServicesFlag.WaitAsync().ConfigureAwait(false);

                    var networkStream = await listener
                                        .AcceptNetworkStreamAsync(_certificateName, noDelay, _cancellationSource.Token)
                                        .ConfigureAwait(false);

                    if (networkStream != null)
                    {
                        var scp = CreateScp(networkStream);
                        if (Options != null)
                        {
                            scp.Options = Options;
                        }

                        var serviceTask = scp.RunAsync();
                        lock (_services)
                        {
                            _services.Add(new RunningDicomService(scp, serviceTask));
                        }

                        _hasServicesFlag.Set();
                        if (IsServicesAtMax)
                        {
                            _hasNonMaxServicesFlag.Reset();
                        }
                    }
                }
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception e)
            {
                Logger.Error("Exception listening for DICOM services, {@error}", e);

                Stop();
                Exception = e;
            }
            finally
            {
                listener?.Stop();
                IsListening = false;
            }
        }