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); }
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 OutgoingMessage(Guid id, Stream data, Action messageCompletedAction, NamedPipeCommunicationProtocol communicationProtocol, ILog log, PipeStream writeStream) : base(id, data, log) { this.messageCompletedAction = messageCompletedAction; this.communicationProtocol = communicationProtocol; this.writeStream = writeStream; confirmationTimer.Elapsed += ConfirmationTimerOnElapsed; }
public OutgoingMessageQueue(ILog log, PipeStream writeStream, NamedPipeCommunicationProtocol communicationProtocol, CancellationToken cancellationToken) { this.log = log; this.cancellationToken = cancellationToken; this.writeStream = writeStream; this.communicationProtocol = communicationProtocol; }
public IncomingMessageQueue(StreamFactory streamFactory, ILog log, PipeStream readStream, NamedPipeCommunicationProtocol communicationProtocol, OutgoingMessageQueue outgoingMessageQueue, CancellationToken cancellationToken) { this.streamFactory = streamFactory; this.log = log; this.readStream = readStream; this.communicationProtocol = communicationProtocol; this.cancellationToken = cancellationToken; this.outgoingMessageQueue = outgoingMessageQueue; }
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)); }
public static async Task <ICommunicationProtocol> Connect(string address, StreamFactory streamFactory, ILog log, bool actAsClient = false, CancellationToken cancellationToken = default(CancellationToken)) { NamedPipeCommunicationProtocol result; if (!actAsClient) { NamedPipeServerStream writeServer = new NamedPipeServerStream(Path.Combine(address, "server-output"), PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.None); NamedPipeServerStream readServer = new NamedPipeServerStream(Path.Combine(address, "server-input"), PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.None); await Task.WhenAll(readServer.WaitForConnectionAsync(cancellationToken), writeServer.WaitForConnectionAsync(cancellationToken)) .ConfigureAwait(false); result = new NamedPipeCommunicationProtocol(readServer, writeServer, streamFactory, new ServerNameLogDecorator(log, address, false)); } else { NamedPipeClientStream writeClient = new NamedPipeClientStream(".", Path.Combine(address, "server-input"), PipeDirection.InOut, PipeOptions.None); NamedPipeClientStream readClient = new NamedPipeClientStream(".", Path.Combine(address, "server-output"), PipeDirection.InOut, PipeOptions.None); await Task.WhenAll(readClient.ConnectAsync(cancellationToken), writeClient.ConnectAsync(cancellationToken)) .ConfigureAwait(false); result = new NamedPipeCommunicationProtocol(readClient, writeClient, streamFactory, new ServerNameLogDecorator(log, address, true)); } return(result); }