Ejemplo n.º 1
0
        CompilerData CreateExecutable(InputData input)
        {
            CompilerData cdata = new CompilerData();
            string ext = "";
            string rand = Utils.RandomString();
            cdata.Rand = rand;
            string dir = rand + @"\";
            switch (input.Lang)
            {
                case Languages.VCPP:
                    ext = ".cpp";
                    break;
                case Languages.VC:
                    ext = ".c";
                    break;
                default:
                    ext = ".unknown";
                    break;
            }
            string PathToSource = RootPath + dir + rand + ext;
            input.PathToSource = PathToSource;
            input.BaseDir = RootPath + dir;
            input.Rand = rand;
            Directory.CreateDirectory(RootPath + dir);

            Directory.SetCurrentDirectory(RootPath + dir);
            //DirectorySecurity sec = Directory.GetAccessControl(RootPath + dir);
            //SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
            //sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
            //Directory.SetAccessControl(RootPath + dir, sec);

            using (TextWriter sw = new StreamWriter(PathToSource))
            {
                sw.Write(input.Program);
            }

            cdata.CleanThis = RootPath + dir;

            var comp = ICompilerFactory.GetICompiler(input.Lang);
            if (comp != null)
                return comp.Compile(input, cdata);

            cdata.Success = false;
            return cdata;
        }
Ejemplo n.º 2
0
        OutputData RunVC(InputData idata)
        {
            CompilerData cdata = null;

            try
            {
                OutputData odata = new OutputData();
                cdata = CreateExecutable(idata);
                if (!cdata.Success)
                {
                    odata.Errors   = cdata.Error;
                    odata.Warnings = cdata.Warning;
                    odata.Stats    = string.Format("Compilation time: {0} sec", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2));
                    return(odata);
                }
                if (!string.IsNullOrEmpty(cdata.Warning))
                {
                    odata.Warnings = cdata.Warning;
                }

                Stopwatch watch = new Stopwatch();
                watch.Start();
                string nr = cdata.Rand;
                if (!string.IsNullOrEmpty(idata.Input))
                {
                    RedisConnection.Strings.Set(1, nr, Encoding.UTF8.GetBytes(idata.Input));
                }

                RedisConnection.Strings.Set(0, nr, new byte[] { (byte)1 });

                for (int i = 400; i > 0; i--)
                {
                    Thread.Sleep(100);
                    bool _break = false;
                    var  res    = RedisConnection.Strings.Get(4, nr).Result;
                    if (res != null)
                    {
                        _break = true;
                        var output = RedisConnection.Strings.Get(2, nr).Result;
                        if (output != null)
                        {
                            odata.Output = Encoding.UTF8.GetString(output);
                            var a = RedisConnection.Keys.Remove(2, nr).Result;
                        }
                        var errors = RedisConnection.Strings.Get(3, nr).Result;
                        if (errors != null)
                        {
                            odata.Errors = Encoding.UTF8.GetString(errors);
                            var a = RedisConnection.Keys.Remove(3, nr).Result;
                        }
                    }
                    if (_break)
                    {
                        break;
                    }
                }
                watch.Stop();


                if (Utils.IsCompiled(idata.Lang))
                {
                    //odata.Stats = string.Format("Compilation time: {0} sec, absolute running time: {1} sec, cpu time: {2} sec, memory peak: {3} Mb", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2), Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2), Math.Round(CpuTimeInSec, 2), MemoryPickInKilobytes / 1024);
                    odata.Stats = string.Format("Compilation time: {0} sec, absolute running time: {1} sec", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2), Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2));
                }
                else
                {
                    //odata.Stats = string.Format("Absolute running time: {0} sec, cpu time: {1} sec, memory peak: {2} Mb", Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2), Math.Round(CpuTimeInSec, 2), MemoryPickInKilobytes / 1024);
                    odata.Stats = string.Format("Absolute running time: {0} sec", Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2));
                }
                return(odata);
            }
            catch (Exception ex)
            {
                return(new OutputData()
                {
                    System_Error = ex.Message
                });
            }
            finally
            {
                if (cdata != null)
                {
                    Cleanup(cdata.CleanThis);
                }
            }
        }
Ejemplo n.º 3
0
        public OutputData DoWork(InputData idata)
        {
            CompilerData cdata = null;

            try
            {
                OutputData odata = new OutputData();
                cdata = CreateExecutable(idata);
                if (!cdata.Success)
                {
                    odata.Errors   = cdata.Error;
                    odata.Warnings = cdata.Warning;
                    odata.Stats    = string.Format("Compilation time: {0} sec", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2));
                    return(odata);
                }
                if (!string.IsNullOrEmpty(cdata.Warning))
                {
                    odata.Warnings = cdata.Warning;
                }

                Stopwatch watch = new Stopwatch();
                watch.Start();
                using (Process process = new Process())
                    using (var job = new Job())
                    {
                        process.StartInfo.FileName               = cdata.Executor + (string.IsNullOrEmpty(cdata.Executor) ? "" : " ") + cdata.ExecuteThis;
                        process.StartInfo.UseShellExecute        = false;
                        process.StartInfo.CreateNoWindow         = true;
                        process.StartInfo.RedirectStandardError  = true;
                        process.StartInfo.RedirectStandardOutput = true;
                        process.StartInfo.RedirectStandardInput  = true;

                        process.Start();
                        job.AddProcess(process.Handle);

                        if (!string.IsNullOrEmpty(idata.Input))
                        {
                            InputWriter input       = new InputWriter(process.StandardInput, idata.Input);
                            Thread      inputWriter = new Thread(new ThreadStart(input.Writeinput));
                            inputWriter.Start();
                        }

                        OutputReader output       = new OutputReader(process.StandardOutput);
                        Thread       outputReader = new Thread(new ThreadStart(output.ReadOutput));
                        outputReader.Start();
                        OutputReader error       = new OutputReader(process.StandardError);
                        Thread       errorReader = new Thread(new ThreadStart(error.ReadOutput));
                        errorReader.Start();

                        var  start  = DateTime.Now;
                        bool killed = false;
                        do
                        {
                            // Refresh the current process property values.
                            process.Refresh();
                            if (!process.HasExited)
                            {
                                try
                                {
                                    if (start + TimeSpan.FromSeconds(10) < DateTime.Now)
                                    {
                                        process.Kill();
                                        var res = string.Format("Process killed because it ran longer than 10 seconds");
                                        odata.Errors = res;
                                        odata.Output = output.Builder.ToString();
                                        killed       = true;
                                    }
                                }
                                catch (InvalidOperationException)
                                {
                                    break;
                                }
                            }
                        }while (!process.WaitForExit(10));
                        process.WaitForExit();

                        if (!killed)
                        {
                            errorReader.Join(5000);
                            outputReader.Join(5000);

                            if (process.ExitCode != 0)
                            {
                                error.Output = string.Format("Process exit code is not 0: {0}\n", process.ExitCode) + error.Output;
                            }

                            odata.Errors = error.Output;
                            odata.Output = output.Output;
                        }
                    }
                watch.Stop();


                if (Utils.IsCompiled(idata.Lang))
                {
                    //odata.Stats = string.Format("Compilation time: {0} sec, absolute running time: {1} sec, cpu time: {2} sec, memory peak: {3} Mb", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2), Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2), Math.Round(CpuTimeInSec, 2), MemoryPickInKilobytes / 1024);
                    odata.Stats = string.Format("Compilation time: {0} sec, absolute running time: {1} sec", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2), Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2));
                }
                else
                {
                    //odata.Stats = string.Format("Absolute running time: {0} sec, cpu time: {1} sec, memory peak: {2} Mb", Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2), Math.Round(CpuTimeInSec, 2), MemoryPickInKilobytes / 1024);
                    odata.Stats = string.Format("Absolute running time: {0} sec", Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2));
                }
                return(odata);
            }
            catch (Exception ex)
            {
                return(new OutputData()
                {
                    System_Error = ex.Message
                });
            }
            finally
            {
                if (cdata != null)
                {
                    Cleanup(cdata.CleanThis);
                }
            }
        }