Exemple #1
0
 private void HandleFreeMessage(NamedPipe pipe, string msg)
 {
     string[] parts = msg.Split(':');
     if (parts.Length == 2 && compilerBusy.ContainsKey(parts[1]))
     {
         string id     = parts[1];
         var    result = "";
         lock (compilerBusy)
         {
             if (compilerBusy.ContainsKey(id))
             {
                 Compiler c = compilerBusy[id];
                 compilerBusy.Remove(id);
                 compilerFree[id] = c;
                 result           = CompilerServiceClient.JobFinishedMessage;
             }
             else
             {
                 result = $"Error: '<free>guid', specified compiler not found with id='{id}'";
             }
         }
         pipe.WriteMessage(result);
     }
     else
     {
         pipe.WriteMessage("Protocol error: expecting '<free>guid', to free the specified compiler");
     }
 }
Exemple #2
0
        private void HandleLockMessage(NamedPipe pipe)
        {
            Tuple <string, Compiler> pair = GetFreeCompiler();

            // send the id to the client for use in subsequent jobs.
            pipe.WriteMessage(pair.Item1);
        }
Exemple #3
0
 public void Dispose()
 {
     if (service != null && !service.IsClosed)
     {
         // now free the Compiler object
         service.WriteMessage(CompilerFreeMessage + ":" + id);
         string handshake = service.ReadMessage();
         if (handshake != JobFinishedMessage)
         {
             DebugWriteLine("Job Error: " + handshake);
         }
         else
         {
             DebugWriteLine("Job Terminated: " + handshake);
         }
     }
     service?.Close(); // we can only write one message at a time.
 }
Exemple #4
0
        public bool Compile(CommandLineOptions options, TextWriter log)
        {
            bool finished = false;
            bool result   = false;

            NamedPipe service = Connect(log);

            CompilerOutputStream output = new CompilerOutputStream(log);

            options.compilerId = id;
            StringWriter  writer     = new StringWriter();
            XmlSerializer serializer = new XmlSerializer(typeof(CommandLineOptions));

            serializer.Serialize(writer, options);
            service.WriteMessage(writer.ToString());

            try
            {
                while (!finished && !service.IsClosed)
                {
                    string msg = service.ReadMessage();
                    DebugWriteLine(msg);
                    int i = msg.IndexOf(':');
                    if (i > 0)
                    {
                        string       sev      = msg.Substring(0, i);
                        SeverityKind severity = SeverityKind.Info;
                        Enum.TryParse <SeverityKind>(sev, out severity);
                        msg = msg.Substring(i + 2);

                        if (msg.StartsWith(JobFinishedMessage))
                        {
                            string tail = msg.Substring(JobFinishedMessage.Length);
                            finished = true;
                            bool.TryParse(tail, out result);
                        }
                        else
                        {
                            output.WriteMessage(msg, severity);
                        }
                    }
                    else
                    {
                        log.WriteLine(msg);
                    }
                }
            }
            catch (Exception)
            {
                result = false;
                output.WriteMessage("PCompilerService is gone, did someone kill it?  Perhaps the P build is happening in parallel?", SeverityKind.Error);
                finished = true;
            }

            return(result);
        }
Exemple #5
0
        private NamedPipe Connect(TextWriter log)
        {
            Mutex processLock = new Mutex(false, "PCompilerService");

            processLock.WaitOne();
            try
            {
                if (service == null)
                {
                    service = new NamedPipe(ServerPipeName);

                    if (!service.Connect())
                    {
                        ProcessStartInfo info = new ProcessStartInfo();
                        info.FileName    = typeof(CompilerServiceClient).Assembly.Location;
                        info.WindowStyle = ProcessWindowStyle.Hidden;
                        Process p = Process.Start(info);
                        if (!service.Connect())
                        {
                            log.WriteLine("Cannot start the CompilerService?");
                            service = null;
                            return(null);
                        }
                        else
                        {
                            // now lock a Compiler object until we re disposed so we can get better
                            // performance by sharing the same Compiler across compile, link and test.
                            service.WriteMessage(CompilerLockMessage);
                            this.id = service.ReadMessage();
                        }
                    }
                }
            }
            finally
            {
                processLock.ReleaseMutex();
            }
            return(service);
        }
Exemple #6
0
 public void WriteMessage(string msg, SeverityKind severity)
 {
     // send this back to the command line process that invoked this compiler.
     DebugWriteLine("WriteMessage: " + msg);
     pipe.WriteMessage(severity + ": " + msg);
 }
Exemple #7
0
        public bool Compile(CommandLineOptions options, TextWriter log)
        {
            service = new NamedPipe(ServerPipeName, false);
            Mutex processLock = new Mutex(false, "PCompilerService");

            processLock.WaitOne();
            try
            {
                if (!service.Connect())
                {
                    ProcessStartInfo info = new ProcessStartInfo();
                    info.FileName    = typeof(CompilerServiceClient).Assembly.Location;
                    info.WindowStyle = ProcessWindowStyle.Hidden;
                    Process p = Process.Start(info);
                    if (!service.Connect())
                    {
                        log.WriteLine("Cannot start the CompilerService?");
                        return(false);
                    }
                }
            }
            finally
            {
                processLock.ReleaseMutex();
            }
            Guid   clientPipe     = Guid.NewGuid();
            string clientPipeName = clientPipe.ToString() + "-CompilerServiceClient";

            client = new NamedPipe(clientPipeName, true);
            if (!client.Connect())
            {
                log.WriteLine("weird, the process that launched this job is gone?");
                return(false);
            }
            AutoResetEvent msgEvent = new AutoResetEvent(false);
            bool           finished = false;
            bool           result   = false;

            CompilerOutputStream output = new CompilerOutputStream(log);

            client.MessageArrived += (s2, e2) =>
            {
                string msg = e2.Message;
                int    i   = msg.IndexOf(':');
                if (i > 0)
                {
                    string sev = msg.Substring(0, i);
                    msg = msg.Substring(i + 2);
                    if (msg.StartsWith("finished:"))
                    {
                        i = msg.IndexOf(':');
                        string tail = msg.Substring(i + 1);
                        finished = true;
                        bool.TryParse(tail, out result);
                        msgEvent.Set();
                    }
                    else
                    {
                        SeverityKind severity = SeverityKind.Info;
                        Enum.TryParse <SeverityKind>(sev, out severity);
                        output.WriteMessage(msg, severity);
                    }
                }
                else
                {
                    log.WriteLine(e2.Message);
                }
            };

            options.pipeName = clientPipeName;

            StringWriter  writer     = new StringWriter();
            XmlSerializer serializer = new XmlSerializer(typeof(CommandLineOptions));

            serializer.Serialize(writer, options);
            service.WriteMessage(writer.ToString());

            while (!finished)
            {
                msgEvent.WaitOne(1000);
                if (client.IsClosed)
                {
                    result = false;
                    output.WriteMessage("PCompilerService is gone, did someone kill it?  Perhaps the P build is happening in parallel?", SeverityKind.Error);
                    finished = true;
                }
            }
            service.Close();
            client.Close();
            return(result);
        }