-
Notifications
You must be signed in to change notification settings - Fork 108
Add ServerCloseMessage to protocol #236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e6c6b1b
5aa99da
98d071a
accae6d
f290a3e
c19e4e8
50c2be4
7d702b0
6f7dee7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ internal abstract class ServiceConnectionBase : IServiceConnection | |
| private bool _isStopped; | ||
| private long _lastReceiveTimestamp; | ||
| protected ConnectionContext _connection; | ||
| protected string ErrorMessage; | ||
|
|
||
| public Task WaitForConnectionStart => _serviceConnectionStartTcs.Task; | ||
|
|
||
|
|
@@ -85,6 +86,13 @@ public async virtual Task WriteAsync(ServiceMessage serviceMessage) | |
| // The lock is per serviceConnection | ||
| await _serviceConnectionLock.WaitAsync(); | ||
|
|
||
| var errorMessage = ErrorMessage; | ||
| if (!string.IsNullOrEmpty(errorMessage)) | ||
| { | ||
| _serviceConnectionLock.Release(); | ||
| throw new InvalidOperationException(errorMessage); | ||
| } | ||
|
|
||
| if (_connection == null) | ||
| { | ||
| _serviceConnectionLock.Release(); | ||
|
|
@@ -119,6 +127,21 @@ public async virtual Task WriteAsync(ServiceMessage serviceMessage) | |
|
|
||
| protected abstract Task OnMessageAsync(ConnectionDataMessage connectionDataMessage); | ||
|
|
||
| protected Task OnServiceErrorAsync(ServiceErrorMessage serviceErrorMessage) | ||
| { | ||
| if (!string.IsNullOrEmpty(serviceErrorMessage.ErrorMessage)) | ||
| { | ||
| // When receives service error message, we suppose server -> service connection doesn't work, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @davidfowl After discussed offline, we renamed the message name and changed to log rather than throw exceptions in |
||
| // and set ErrorMessage to prevent sending message from server to service | ||
| // But messages in the pipe from service -> server should be processed as usual. Just log without | ||
| // throw exception here. | ||
| ErrorMessage = serviceErrorMessage.ErrorMessage; | ||
| Log.ReceivedServiceErrorMessage(_logger, _connectionId, serviceErrorMessage.ErrorMessage); | ||
| } | ||
|
|
||
| return Task.CompletedTask; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
shall we close the server connection with this message? |
||
| } | ||
|
|
||
| private async Task<bool> StartAsyncCore() | ||
| { | ||
| // Always try until connected | ||
|
|
@@ -130,6 +153,7 @@ private async Task<bool> StartAsyncCore() | |
| try | ||
| { | ||
| _connection = await CreateConnection(); | ||
| ErrorMessage = null; | ||
|
|
||
| if (await HandshakeAsync()) | ||
| { | ||
|
|
@@ -343,6 +367,8 @@ private Task DispatchMessageAsync(ServiceMessage message) | |
| return OnDisconnectedAsync(closeConnectionMessage); | ||
| case ConnectionDataMessage connectionDataMessage: | ||
| return OnMessageAsync(connectionDataMessage); | ||
| case ServiceErrorMessage serviceErrorMessage: | ||
| return OnServiceErrorAsync(serviceErrorMessage); | ||
| case PingMessage _: | ||
| // ignore ping | ||
| break; | ||
|
|
@@ -514,6 +540,9 @@ private static class Log | |
| private static readonly Action<ILogger, Exception> _failedSendingPing = | ||
| LoggerMessage.Define(LogLevel.Warning, new EventId(26, "FailedSendingPing"), "Failed sending a ping message to service."); | ||
|
|
||
| private static readonly Action<ILogger, string, string, Exception> _receivedServiceErrorMessage = | ||
| LoggerMessage.Define<string, string>(LogLevel.Warning, new EventId(27, "ReceivedServiceErrorMessage"), "Connection {ServiceConnectionId} received error message from service: {Error}"); | ||
|
|
||
| public static void FailedToWrite(ILogger logger, Exception exception) | ||
| { | ||
| _failedToWrite(logger, exception); | ||
|
|
@@ -643,6 +672,11 @@ public static void FailedSendingPing(ILogger logger, Exception exception) | |
| { | ||
| _failedSendingPing(logger, exception); | ||
| } | ||
|
|
||
| public static void ReceivedServiceErrorMessage(ILogger logger, string connectionId, string errorMessage) | ||
| { | ||
| _receivedServiceErrorMessage(logger, connectionId, errorMessage, null); | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use a local
errorMessage=ErrorMessagefor throw,ErrorMessagecan be reset in betweenifandthrow