private void ReadStream() { while (this._streamWrapper.CanRead) { TRead tRead = this._streamWrapper.ReadObject(); ConnectionMessageEventHandler <TRead, TWrite> connectionMessageEventHandler = this.ReceiveMessage; if (connectionMessageEventHandler != null) { connectionMessageEventHandler(this, tRead); } else { } if (tRead != null) { continue; } this.CloseImpl(); return; } }
public Task <object> Get(string varName, string property) { TaskCompletionSource <object> tcs = new TaskCompletionSource <object>(); Guid replyId = Guid.NewGuid(); ConnectionEventHandler <Message, Message> disconnection = null; disconnection = (connection) => { Disconnected -= disconnection; tcs.TrySetCanceled(); }; ConnectionMessageEventHandler <Message, Message> fn = null; fn = (connection, msg) => { if (msg.Id.Equals(replyId)) { MessageReceived -= fn; tcs.SetResult(msg.Data); } }; MessageReceived += fn; Disconnected += disconnection; PushMessage(new Message(replyId) { Command = Command.Get, VariableName = varName, Property = property, Data = null }); return(tcs.Task); }
private bool TryAttachDebugger(int processId) { var stopWatch = Stopwatch.StartNew(); var resetEvent = new ManualResetEventSlim(false); bool debuggerAttachedSuccessfully = false; string errorMessage = null; ConnectionMessageEventHandler <AttachDebuggerMessage, AttachDebuggerMessage> onServerMessage = (connection, message) => { if (message.ProcessId != processId) { return; } debuggerAttachedSuccessfully = message.DebuggerAttachedSuccessfully; errorMessage = message.ErrorMessage; resetEvent.Set(); }; var client = CreateAndStartPipeClient(_debuggingNamedPipeId, onServerMessage, _logger); if (client == null) { return(false); } client.PushMessage(new AttachDebuggerMessage { ProcessId = processId }); if (!resetEvent.Wait(_timeout)) { errorMessage = $"Could not attach debugger to process {processId} since attaching timed out after {_timeout.TotalSeconds}s"; } stopWatch.Stop(); if (debuggerAttachedSuccessfully) { _logger.DebugInfo($"Debugger attached to process {processId}, took {stopWatch.ElapsedMilliseconds}ms"); } else { if (string.IsNullOrWhiteSpace(errorMessage)) { errorMessage = $"Could not attach debugger to process {processId}, no error message available"; } errorMessage += $"{Environment.NewLine}There might be more information on the problem in Visual Studio's ActivityLog.xml (see e.g. https://blogs.msdn.microsoft.com/visualstudio/2010/02/24/troubleshooting-extensions-with-the-activity-log/)"; _logger.LogError(errorMessage); } client.Stop(); return(debuggerAttachedSuccessfully); }
public static NamedPipeClient <AttachDebuggerMessage> CreateAndStartPipeClient(string pipeId, ConnectionMessageEventHandler <AttachDebuggerMessage, AttachDebuggerMessage> onServerMessage, ILogger logger) { var client = new NamedPipeClient <AttachDebuggerMessage>(GetPipeName(pipeId)); client.Error += exception => { logger.DebugError( $"Named pipe error: Named pipe: {GetPipeName(pipeId)}, exception:{Environment.NewLine}{exception}"); }; client.ServerMessage += onServerMessage; client.Start(); // workaround for NamedPipeClient not telling if connection has been established :-) var stopwatch = Stopwatch.StartNew(); client.WaitForConnection(TimeSpan.FromSeconds(3)); stopwatch.Stop(); if (stopwatch.Elapsed > TimeSpan.FromSeconds(2.5)) { logger.LogError($"Could not connect to NamedPipe {GetPipeName(pipeId)} - not able to attach debugger"); client.Stop(); return(null); } return(client); }