Пример #1
0
        private void Run()
        {
            Task.Factory.StartNew(async() =>
            {
                _writer.WriteLine(new EventPacket()
                {
                    Event = "started"
                });

                while (!_cancellation.IsCancellationRequested)
                {
                    var line = await _input.ReadLineAsync();
                    if (line == null)
                    {
                        break;
                    }

                    var ignored = Task.Factory.StartNew(async() =>
                    {
                        try
                        {
                            await HandleRequest(line);
                        }
                        catch (Exception e)
                        {
                            _writer.WriteLine(new EventPacket()
                            {
                                Event = "error",
                                Body  = JsonConvert.ToString(e.ToString(), '"', StringEscapeHandling.Default)
                            });
                        }
                    });
                }
            });
        }
Пример #2
0
        private async Task Run()
        {
            while (!_cancellation.IsCancellationRequested)
            {
                var line = await _process.StandardOutput.ReadLineAsync();

                if (line == null)
                {
                    break;
                }

                var ignored = Task.Factory.StartNew(() =>
                {
                    try
                    {
                        var response = PluginResponse.Parse(line);

                        if (!response.Success)
                        {
                            _writer.WriteLine(new EventPacket()
                            {
                                Event = "error",
                                Body  = response.Message,
                            });
                            return;
                        }

                        Action <string> requestHandler = null;
                        if (!_requests.TryGetValue(response.Request_seq, out requestHandler))
                        {
                            throw new ArgumentException("invalid seq-value");
                        }

                        requestHandler((string)response.BodyJson);
                    }
                    catch (Exception e)
                    {
                        _writer.WriteLine(new EventPacket()
                        {
                            Event = "error",
                            Body  = e.ToString()
                        });
                    }
                });
            }
        }
Пример #3
0
        public void Emit(string kind, object args)
        {
            var packet = new EventPacket
            {
                Event = kind,
                Body  = args
            };

            _writer.WriteLine(packet);
        }
Пример #4
0
        protected override void WriteMessage(LogLevel logLevel, string message)
        {
            var packet = new EventPacket()
            {
                Event = "log",
                Body  = new
                {
                    LogLevel = logLevel.ToString().ToUpperInvariant(),
                    Name     = this.CategoryName,
                    Message  = message
                }
            };

            _writer.WriteLine(packet);
        }
Пример #5
0
        public void Start()
        {
            WorkspaceInitializer.Initialize(_serviceProvider, _compositionHost, _configuration, _logger);

            Task.Factory.StartNew(async() =>
            {
                _writer.WriteLine(new EventPacket()
                {
                    Event = "started"
                });

                while (!_cancellationTokenSource.IsCancellationRequested)
                {
                    var line = await _input.ReadLineAsync();
                    if (line == null)
                    {
                        break;
                    }

                    var ignored = Task.Factory.StartNew(async() =>
                    {
                        try
                        {
                            await HandleRequest(line, _logger);
                        }
                        catch (Exception e)
                        {
                            if (e is AggregateException aggregateEx)
                            {
                                e = aggregateEx.Flatten().InnerException;
                            }

                            _writer.WriteLine(new EventPacket()
                            {
                                Event = "error",
                                Body  = JsonConvert.ToString(e.ToString(), '"', StringEscapeHandling.Default)
                            });
                        }
                    });
                }
            });

            _logger.LogInformation($"Omnisharp server running using {nameof(TransportType.Stdio)} at location '{_environment.TargetDirectory}' on host {_environment.HostProcessId}.");

            Console.CancelKeyPress += (sender, e) =>
            {
                _cancellationTokenSource.Cancel();
                e.Cancel = true;
            };

            if (_environment.HostProcessId != -1)
            {
                try
                {
                    var hostProcess = Process.GetProcessById(_environment.HostProcessId);
                    hostProcess.EnableRaisingEvents = true;
                    hostProcess.OnExit(() => _cancellationTokenSource.Cancel());
                }
                catch
                {
                    // If the process dies before we get here then request shutdown
                    // immediately
                    _cancellationTokenSource.Cancel();
                }
            }
        }