public void Listen(TimeSpan?maxWaitTime = null) { maxWaitTime = maxWaitTime ?? TimeSpan.FromSeconds(25); var clock = new Stopwatch(); clock.Start(); while (State != NetworkState.CLOSING_ASSOCIATION && clock.Elapsed < maxWaitTime) { if (_abortRequested) { Logger.Log("Abort requested...aborting."); HandleAbort(); break; } if (_cancelRequested) { Logger.Log("Cancellation requested...cancelling."); HandleCancel(); } if (State != NetworkState.CLOSING_ASSOCIATION && State != NetworkState.TRANSPORT_CONNECTION_OPEN) { var message = Read(); if (message != null) { clock.Restart(); Process(message); Stream.Flush(); clock.Restart(); } } if (State == NetworkState.TRANSPORT_CONNECTION_OPEN && !OutboundMessages.IsEmpty) { while (OutboundMessages.Any()) { if (State == NetworkState.TRANSPORT_CONNECTION_OPEN) { AbstractDIMSEBase dimse; if (OutboundMessages.TryDequeue(out dimse)) { PDataMessenger.Send(dimse, this); } } } } if (!IsClientConnected) { Logger.Log("Connection closed - ending association."); break; } } if (State != NetworkState.CLOSING_ASSOCIATION) { Logger.Log("Network timeout - closing association."); } }
private void HandleCancel() { AbstractDIMSEBase cancel; OutboundMessages.TryPeek(out cancel); if (cancel is CCancel) { OutboundMessages.TryDequeue(out cancel); Stream.Flush(); PDataMessenger.Send(cancel, this); } }
public void Listen(TimeSpan?maxWaitTime = null) { maxWaitTime = maxWaitTime ?? TimeSpan.FromSeconds(25); IdleClock = IdleClock ?? new Stopwatch(); IdleClock.Reset(); IdleClock.Start(); while (State != NetworkState.CLOSING_ASSOCIATION && IdleClock.Elapsed < maxWaitTime) { if (_abortRequested) { Logger.Log("Abort requested...aborting."); HandleAbort(); break; } if (_cancelRequested) { Logger.Log("Cancellation requested...cancelling."); HandleCancel(); } if (State != NetworkState.CLOSING_ASSOCIATION && State != NetworkState.TRANSPORT_CONNECTION_OPEN) { var message = Read(maxWaitTime.Value.TotalMilliseconds - IdleClock.ElapsedMilliseconds); if (message != null) { try { IdleClock.Restart(); Process(message); Stream.Flush(); IdleClock.Restart(); } catch (IOException e) { Logger.Log($"Network connection was lost. {e.Message}", LogPriority.ERROR); break;//Connection was lost } } } if (State == NetworkState.TRANSPORT_CONNECTION_OPEN && !OutboundMessages.IsEmpty) { while (!OutboundMessages.IsEmpty) { if (State == NetworkState.TRANSPORT_CONNECTION_OPEN) { AbstractDIMSEBase dimse; if (OutboundMessages.TryDequeue(out dimse)) { PDataMessenger.Send(dimse, this); } } } } if (!IsClientConnected) { Logger.Log("Connection closed - ending association."); break; } } if (State != NetworkState.CLOSING_ASSOCIATION) { Logger.Log("Network timeout - closing association."); } }