public async Task PartwiseStreamReaderExceptionTest(string jsonContent)
        {
            var buffer = Encoding.UTF8.GetBytes(jsonContent);

            using (var stream = new MemoryStream(buffer, false))
            {
                using (var reader = new PartwiseStreamMessageReader(stream))
                {
                    var ex = await Assert.ThrowsAsync <MessageReaderException>(() =>
                                                                               reader.ReadAsync(m => true, CancellationToken.None));

                    Output.WriteLine(ex.Message);
                }
            }
        }
Exemplo n.º 2
0
 /// <param name="manualIo">Simply read messages from console, line by line,
 /// instead of using language server protocol's specification.</param>
 public ConsoleIoService(bool manualIo)
 {
     if (manualIo)
     {
         ConsoleMessageReader = new ByLineTextMessageReader(Console.In);
         ConsoleMessageWriter = new ByLineTextMessageWriter(Console.Out);
     }
     else
     {
         var cin  = Console.OpenStandardInput();
         var bcin = new BufferedStream(cin);
         var cout = Console.OpenStandardOutput();
         ConsoleMessageReader = new PartwiseStreamMessageReader(bcin);
         ConsoleMessageWriter = new PartwiseStreamMessageWriter(cout);
     }
 }
Exemplo n.º 3
0
 static void Main(string[] args)
 {
     using (Stream cin = Console.OpenStandardInput())
         using (BufferedStream bcin = new BufferedStream(cin))
             using (Stream cout = Console.OpenStandardOutput())
                 using (PartwiseStreamMessageReader reader = new PartwiseStreamMessageReader(bcin))
                     using (PartwiseStreamMessageWriter writer = new PartwiseStreamMessageWriter(cout))
                     {
                         JsonRpcContractResolver contractResolver = new JsonRpcContractResolver
                         {
                             NamingStrategy          = new CamelCaseJsonRpcNamingStrategy(),
                             ParameterValueConverter = new CamelCaseJsonValueConverter(),
                         };
                         StreamRpcClientHandler clientHandler = new StreamRpcClientHandler();
                         JsonRpcClient          client        = new JsonRpcClient(clientHandler);
                         clientHandler.MessageSending += (_, e) =>
                         {
                             Console.Error.WriteLine("Sending: " + e.Message);
                         };
                         clientHandler.MessageReceiving += (_, e) =>
                         {
                             Console.Error.WriteLine("Receiving: " + e.Message);
                         };
                         LanguageServerSession     session = new LanguageServerSession(client, contractResolver);
                         JsonRpcServiceHostBuilder builder = new JsonRpcServiceHostBuilder {
                             ContractResolver = contractResolver
                         };
                         builder.UseCancellationHandling();
                         builder.Register(typeof(Program).GetTypeInfo().Assembly);
                         builder.Intercept(async(context, next) =>
                         {
                             Console.Error.WriteLine("Request: " + context.Request);
                             await next();
                             Console.Error.WriteLine("Response: " + context.Response);
                         });
                         IJsonRpcServiceHost    host          = builder.Build();
                         StreamRpcServerHandler serverHandler = new StreamRpcServerHandler(host,
                                                                                           StreamRpcServerHandlerOptions.ConsistentResponseSequence | StreamRpcServerHandlerOptions.SupportsRequestCancellation);
                         serverHandler.DefaultFeatures.Set(session);
                         using (serverHandler.Attach(reader, writer))
                             using (clientHandler.Attach(reader, writer))
                             {
                                 // Wait for the "stop" request.
                                 session.CancellationToken.WaitHandle.WaitOne();
                             }
                     }
 }
        public async Task PartwiseStreamReaderTest()
        {
            var buffer = TestMessagePartwiseStreamContent.Concat(TestMessagePartwiseStreamContent).ToArray();

            using (var stream = new MemoryStream(buffer, false))
            {
                using (var reader = new PartwiseStreamMessageReader(stream))
                {
                    var message1 = await reader.ReadAsync(m => true, CancellationToken.None);

                    Output.WriteLine(message1.ToString());
                    var message2 = await reader.ReadAsync(m => true, CancellationToken.None);

                    Output.WriteLine(message2.ToString());
                    Assert.Null(await reader.ReadAsync(m => true, CancellationToken.None));
                    Assert.Equal(JsonConvert.SerializeObject(TestMessage), JsonConvert.SerializeObject(message1));
                    Assert.Equal(JsonConvert.SerializeObject(TestMessage), JsonConvert.SerializeObject(message2));
                }
            }
        }
Exemplo n.º 5
0
        public static void Main(string[] args)
        {
            var debugMode = args.Any(a => a.Equals("--debug", StringComparison.OrdinalIgnoreCase));

#if WAIT_FOR_DEBUGGER
            while (!Debugger.IsAttached)
            {
                Thread.Sleep(5000);
            }
            Debugger.Break();
#endif
            StreamWriter logWriter = null;
            if (debugMode)
            {
                logWriter           = File.CreateText("messages-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".log");
                logWriter.AutoFlush = true;
            }

            using (logWriter)
                using (var cin = Console.OpenStandardInput())
                    using (var bcin = new BufferedStream(cin))
                        using (var cout = Console.OpenStandardOutput())
                            using (var reader = new PartwiseStreamMessageReader(bcin))
                                using (var writer = new PartwiseStreamMessageWriter(cout))
                                {
                                    var contractResolver = new JsonRpcContractResolver
                                    {
                                        NamingStrategy          = new CamelCaseJsonRpcNamingStrategy(),
                                        ParameterValueConverter = new CamelCaseJsonValueConverter(),
                                    };
                                    var clientHandler = new StreamRpcClientHandler();
                                    var client        = new JsonRpcClient(clientHandler);
                                    if (debugMode)
                                    {
                                        // We want to capture log all the LSP server-to-client calls as well
                                        clientHandler.MessageSending += (_, e) =>
                                        {
                                            lock (logWriter) logWriter.WriteLine("<C{0}", e.Message);
                                        };
                                        clientHandler.MessageReceiving += (_, e) =>
                                        {
                                            lock (logWriter) logWriter.WriteLine(">C{0}", e.Message);
                                        };
                                    }

                                    // Configure & build service host
                                    var session       = new SessionState(client, contractResolver);
                                    var host          = BuildServiceHost(logWriter, contractResolver, debugMode);
                                    var serverHandler = new StreamRpcServerHandler(host,
                                                                                   StreamRpcServerHandlerOptions.ConsistentResponseSequence |
                                                                                   StreamRpcServerHandlerOptions.SupportsRequestCancellation);
                                    serverHandler.DefaultFeatures.Set(session);
                                    // If we want server to stop, just stop the "source"
                                    using (serverHandler.Attach(reader, writer))
                                        using (clientHandler.Attach(reader, writer))
                                        {
                                            // Wait for the "stop" request.
                                            session.CancellationToken.WaitHandle.WaitOne();
                                        }

                                    logWriter?.WriteLine("Exited");
                                }
        }