Ejemplo n.º 1
0
        /// <summary>
        /// Server loop
        /// </summary>
        public async Task LoopAsync()
        {
            await JoinableTaskFactory.SwitchToMainThreadAsync(Loop.Token);

            var DTE = await Package.GetServiceAsync(typeof(DTE)) as DTE2;

            await TaskScheduler.Default;

            var pipeName = string.Format("QtVSTest_{0}", Process.GetCurrentProcess().Id);

            while (!Loop.Token.IsCancellationRequested)
            {
                using (var pipe = new NamedPipeServerStream(pipeName, PipeDirection.InOut)) {
                    await pipe.WaitForConnectionAsync(Loop.Token);

                    if (Loop.Token.IsCancellationRequested)
                    {
                        break;
                    }

                    while (!Loop.Token.IsCancellationRequested && pipe.IsConnected)
                    {
                        byte[] data = new byte[4];
                        await pipe.ReadAsync(data, 0, 4, Loop.Token);

                        if (Loop.Token.IsCancellationRequested)
                        {
                            break;
                        }

                        if (pipe.IsConnected)
                        {
                            int size = BitConverter.ToInt32(data, 0);
                            data = new byte[size];

                            await pipe.ReadAsync(data, 0, size, Loop.Token);

                            if (Loop.Token.IsCancellationRequested)
                            {
                                break;
                            }

                            var macro = new Macro(Package, DTE, JoinableTaskFactory, Loop.Token);
                            await macro.CompileAsync(Encoding.UTF8.GetString(data));

                            if (macro.AutoRun)
                            {
                                await macro.RunAsync();
                            }

                            data = Encoding.UTF8.GetBytes(macro.Result);
                            size = data.Length;

                            await pipe.WriteAsync(BitConverter.GetBytes(size), 0, 4, Loop.Token);

                            if (Loop.Token.IsCancellationRequested)
                            {
                                break;
                            }

                            await pipe.WriteAsync(data, 0, size, Loop.Token);

                            if (Loop.Token.IsCancellationRequested)
                            {
                                break;
                            }

                            await pipe.FlushAsync(Loop.Token);

                            if (Loop.Token.IsCancellationRequested)
                            {
                                break;
                            }

                            pipe.WaitForPipeDrain();

                            if (macro != null && macro.Ok && macro.AutoRun && macro.QuitWhenDone)
                            {
                                await JoinableTaskFactory.SwitchToMainThreadAsync(Loop.Token);

                                if (DTE != null)
                                {
                                    DTE.Solution.Close(false);
                                    DTE.Quit();
                                }
                                await TaskScheduler.Default;
                                Loop.Cancel();
                            }
                        }
                    }
                }
            }
        }