/// <summary> /// Main processing routine for processing a network connection. /// </summary> private void RunRead() { try { ResetDimseTimeout(); while (!_stop) { if (NetworkHasData()) { ResetDimseTimeout(); bool success = ProcessNextPDU(); if (!success) { // Start the Abort process, not much else we can do Platform.Log(LogLevel.Error, "Unexpected error processing PDU. Aborting Association from {0} to {1}", _assoc.CallingAE, _assoc.CalledAE); SendAssociateAbort(DicomAbortSource.ServiceProvider, DicomAbortReason.InvalidPDUParameter); } } else if (DateTime.Now > GetDimseTimeout()) { string errorMessage; switch (State) { case DicomAssociationState.Sta6_AssociationEstablished: OnDimseTimeout(); ResetDimseTimeout(); break; case DicomAssociationState.Sta2_TransportConnectionOpen: errorMessage = "ARTIM timeout when waiting for AAssociate Request PDU, closing connection."; Platform.Log(LogLevel.Error, errorMessage); State = DicomAssociationState.Sta13_AwaitingTransportConnectionClose; OnNetworkError(new DicomNetworkException(errorMessage), true); if (NetworkClosed != null) NetworkClosed(errorMessage); break; case DicomAssociationState.Sta5_AwaitingAAssociationACOrReject: errorMessage = "ARTIM timeout when waiting for AAssociate AC or RJ PDU, closing connection."; Platform.Log(LogLevel.Error,errorMessage ); State = DicomAssociationState.Sta13_AwaitingTransportConnectionClose; OnNetworkError(new DicomNetworkException(errorMessage), true); if (NetworkClosed != null) NetworkClosed(errorMessage); break; case DicomAssociationState.Sta13_AwaitingTransportConnectionClose: errorMessage = string.Format( "Timeout when waiting for transport connection to close from {0} to {1}. Dropping Connection.", _assoc.CallingAE, _assoc.CalledAE); Platform.Log(LogLevel.Error, errorMessage); OnNetworkError(new DicomNetworkException(errorMessage), true); if (NetworkClosed != null) NetworkClosed(errorMessage); break; default: Platform.Log(LogLevel.Error, "DIMSE timeout in unexpected state: {0}", State.ToString()); OnDimseTimeout(); ResetDimseTimeout(); break; } } //else //{ // Thread.Sleep(0); //} } _network.Close(); _network.Dispose(); _network = null; } catch (Exception e) { OnNetworkError(e, true); if (NetworkError != null) NetworkError(e); } }
/// <summary> /// Main processing routine for processing a network connection. /// </summary> private void Process() { try { DateTime timeout = _assoc != null ? DateTime.Now.AddMilliseconds(_assoc.ReadTimeout) : DateTime.Now.AddSeconds(Timeout); while (!_stop) { if (NetworkHasData()) { timeout = _assoc != null ? DateTime.Now.AddMilliseconds(_assoc.ReadTimeout) : DateTime.Now.AddSeconds(Timeout); bool success = ProcessNextPDU(); if (!success) { // Start the Abort process, not much else we can do Platform.Log(LogLevel.Error, "Unexpected error processing PDU. Aborting Association from {0} to {1}", _assoc.CallingAE, _assoc.CalledAE); SendAssociateAbort(DicomAbortSource.ServiceProvider, DicomAbortReason.InvalidPDUParameter); } } else if (_pduQueue.Count > 0) { //SendRawPDU(DequeuePdu()); // Note, if this is ever enabled, it would make sense to reset the timeout at this point. // So that the timeout is really based on the last data read or written to the network, instead of // from the last time data was read from the network. } else if (DateTime.Now > timeout) { string errorMessage; switch (State) { case DicomAssociationState.Sta6_AssociationEstablished: OnDimseTimeout(); timeout = DateTime.Now.AddMilliseconds(_assoc.ReadTimeout); break; case DicomAssociationState.Sta2_TransportConnectionOpen: errorMessage = "ARTIM timeout when waiting for AAssociate Request PDU, closing connection."; Platform.Log(LogLevel.Error, errorMessage); State = DicomAssociationState.Sta13_AwaitingTransportConnectionClose; OnNetworkError(new DicomNetworkException(errorMessage), true); if (NetworkClosed != null) NetworkClosed(errorMessage); break; case DicomAssociationState.Sta5_AwaitingAAssociationACOrReject: errorMessage = "ARTIM timeout when waiting for AAssociate AC or RJ PDU, closing connection."; Platform.Log(LogLevel.Error,errorMessage ); State = DicomAssociationState.Sta13_AwaitingTransportConnectionClose; OnNetworkError(new DicomNetworkException(errorMessage), true); if (NetworkClosed != null) NetworkClosed(errorMessage); break; case DicomAssociationState.Sta13_AwaitingTransportConnectionClose: errorMessage = string.Format( "Timeout when waiting for transport connection to close from {0} to {1}. Dropping Connection.", _assoc.CallingAE, _assoc.CalledAE); Platform.Log(LogLevel.Error, errorMessage); OnNetworkError(new DicomNetworkException(errorMessage), true); if (NetworkClosed != null) NetworkClosed(errorMessage); break; default: Platform.Log(LogLevel.Error, "DIMSE timeout in unexpected state: {0}", State.ToString()); OnDimseTimeout(); timeout = _assoc != null ? DateTime.Now.AddMilliseconds(_assoc.ReadTimeout) : DateTime.Now.AddSeconds(Timeout); break; } } //else //{ // Thread.Sleep(0); //} } _network.Close(); _network.Dispose(); _network = null; } catch (Exception e) { OnNetworkError(e, true); if (NetworkError != null) NetworkError(e); } }