private async void StartAndWaitForInterProcessUpdateInternal() { try { while (!CancellationToken.IsCancellationRequested) { protocol?.Dispose(); log.LogVerbose($"Start other instance update receiver {serverName}."); protocol = await NamedPipeCommunicationProtocol.Connect(serverName, streamFactory, log, cancellationToken : CancellationToken).ConfigureAwait(false); log.LogVerbose($"Other instance connected to the update receiver {serverName}."); AsyncAutoResetEvent waitEvent = new AsyncAutoResetEvent(false); protocol.CommunicationError += ProtocolOnError; protocol.MessageReceived += ProtocolOnMessageReceived; protocol.Start(); await waitEvent.WaitAsync(CancellationToken).ConfigureAwait(false); void ProtocolOnError(object sender, EventArgs e) { protocol.FlushReceivedMessages(); protocol.CommunicationError -= ProtocolOnError; protocol.MessageReceived -= ProtocolOnMessageReceived; waitEvent.Set(); } } } catch (Exception) { //Do not log anything as any log will lead to another exception } }
public async Task <bool> Start(string serverName, bool heartbeat) { if (communicationProtocol != null) { StopServer(); } try { startCancellationTokenSource = new CancellationTokenSource(); communicationProtocol = await NamedPipeCommunicationProtocol.Connect(serverName, streamFactory, log, cancellationToken : startCancellationTokenSource.Token).ConfigureAwait(false); } catch (TaskCanceledException e) { log.LogError($"Connection to server was canceled.{Environment.NewLine}{e}"); communicationProtocol = null; return(false); } finally { startCancellationTokenSource.Dispose(); startCancellationTokenSource = null; } if (communicationProtocol != null) { communicationProtocol.CommunicationError += CommunicationProtocolOnCommunicationError; } OnConnected(new ServerConnectedEventArgs(communicationProtocol, heartbeat)); communicationProtocol.Start(); return(true); }
public static async Task <NamedPipeInstanceCommunicationChannel> OpenChannel( string serverAddress, StreamFactory streamFactory, IContainer parentContainer, ILog channelLog, CancellationToken cancellationToken) { ICommunicationProtocol protocol; using (CancellationTokenSource timeout = new CancellationTokenSource(NamedPipeCommunicationProtocol.MaxConfirmationResponseTime)) using (CancellationTokenSource linkedSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeout.Token)) { protocol = await NamedPipeCommunicationProtocol.Connect(serverAddress, streamFactory, channelLog, actAsClient : true, cancellationToken : linkedSource.Token).ConfigureAwait(false); protocol.Start(); } channelLog.LogVerbose($"Successfully connected to other instance server {serverAddress}."); ILifetimeScope lifetimeScope = parentContainer.BeginLifetimeScope(builder => { builder.RegisterInstance(channelLog).As <ILog>().SingleInstance(); builder.RegisterInstance(protocol).As <ICommunicationProtocol>().SingleInstance(); builder.RegisterInstance(new CommunicationSettings(false)).AsSelf(); }); return(new NamedPipeInstanceCommunicationChannel(lifetimeScope)); }