public void Start() { if (_threads.Count == 0) { _stop = false; for (int i = 0; i < _ports.Count; i++) { try { DcmSocket socket = DcmSocket.Create(_types[i]); socket.Bind(new IPEndPoint(IPAddress.Any, _ports[i])); socket.Listen(5); Thread thread = new Thread(ServerProc); thread.IsBackground = true; thread.Start(socket); _threads.Add(thread); Dicom.Debug.Log.Info("DICOM {0} server listening on port {1}", _types[i].ToString(), _ports[i]); } catch (Exception ex) { Dicom.Debug.Log.Error("Unable to start DICOM {0} server on port {1}; Check to see that no other network services are using this port and try again.", _types[i].ToString(), _ports[i]); throw ex; } } } }
internal void InitializeService(DcmSocket socket) { InitializeNetwork(socket); }
private void ServerProc(object state) { DcmSocket socket = (DcmSocket)state; try { List <DcmSocket> sockets = new List <DcmSocket>(); List <T> clients = new List <T>(); while (!_stop) { if (socket.Poll(250000, SelectMode.SelectRead)) { try { DcmSocket client = socket.Accept(); Debug.Log.Info("Client connecting from {0}", client.RemoteEndPoint); if (client.Type == DcmSocketType.TLS) { Debug.Log.Info("Authenticating SSL/TLS for client: {0}", client.RemoteEndPoint); } T handler = Activator.CreateInstance <T>(); if (OnDicomClientCreated != null) { OnDicomClientCreated(this, handler, client.Type); } handler.InitializeService(client); clients.Add(handler); Interlocked.Increment(ref _clientCount); } catch (Exception e) { Debug.Log.Error(e.Message); } } for (int i = 0; i < clients.Count; i++) { if (clients[i].IsClosed) { clients[i].Close(); if (OnDicomClientClosed != null) { OnDicomClientClosed(this, clients[i]); } clients.RemoveAt(i--); Interlocked.Decrement(ref _clientCount); } } } for (int i = 0; i < clients.Count; i++) { clients[i].Close(); if (OnDicomClientClosed != null) { OnDicomClientClosed(this, clients[i]); } Interlocked.Decrement(ref _clientCount); } foreach (DcmSocket s in sockets) { s.Close(); } } finally { socket.Close(); } }