src/HarmonyHub/Client.cs
/// <summary>
/// Sends a request response message.
/// </summary>
/// <param name="message">The message to be sent.</param>
/// <param name="timeout">Timeout for the response, exception thrown on expiration.</param>
/// <returns>Response message.</returns>
Sends a request response message.
Sends a request response message .
private async Task<XmlElement> RequestResponseAsync(XmlElement message, int timeout = 2000)
        {
            if (!_heartbeat.Enabled)
                throw new ConnectionException("Connection already closed or not authenticated");
            var messageId = message.Attributes["id"].Value;
            var resultTaskCompletionSource = new TaskCompletionSource<XmlElement>();
            _resultTaskCompletionSources[messageId] = resultTaskCompletionSource;
            Action timeoutAction = () =>
            {
                _resultTaskCompletionSources.Remove(messageId);
                resultTaskCompletionSource.TrySetException(new TimeoutException($"Timeout while waiting on response {messageId} after {timeout}"));
            };
            Send(message);
            var cancellationTokenSource = new CancellationTokenSource(timeout);
            using (cancellationTokenSource.Token.Register(timeoutAction))
            {
                return await resultTaskCompletionSource.Task.ConfigureAwait(false);
            }
        }
private async Task < XmlElement > RequestResponseAsync ( XmlElement message , int timeout = 2000 ) { if ( ! _heartbeat . Enabled ) throw new ConnectionException ( " " ) ; var messageId = message . Attributes [ " " ] . Value ; var resultTaskCompletionSource = new TaskCompletionSource < XmlElement > ( ) ; _resultTaskCompletionSources [ messageId ] = resultTaskCompletionSource ; Action timeoutAction = ( ) => { _resultTaskCompletionSources . Remove ( messageId ) ; resultTaskCompletionSource . TrySetException ( new TimeoutException ( $" Timeout while waiting on response  { messageId }  after  { timeout } " ) ) ; } ; Send ( message ) ; var cancellationTokenSource = new CancellationTokenSource ( timeout ) ; using ( cancellationTokenSource . Token . Register ( timeoutAction ) ) { return await resultTaskCompletionSource . Task . ConfigureAwait ( false ) ; } }
Sends a request response message.
Sends a request response message .
// Heartbeat check // Prepare the TaskCompletionSource, which is used to await the result // Create the action which is called when a timeout occurs // Handle timeout // Await until response or timeout
None
message
XmlElement
The message to be sent.
The message to be sent .
timeout
int
Timeout for the response, exception thrown on expiration.
Timeout for the response exception thrown on expiration .