public static int Check(Answer thisAnswer, Problem thisProblem) { string stdData = thisProblem.StandardOutput.Replace("\r", ""); string data = thisAnswer.OutputData.Replace("\r", ""); string stdDataNp = stdData.Replace("\n", "").Replace("\t", "").Replace(" ", ""); string dataNp = data.Replace("\n", "").Replace("\t", "").Replace(" ", ""); if (stdData.Length < data.Length / 2) { StatusCode = Const.StatusCodeOutputLimitExceeded; } else if (stdData == data) { StatusCode = Const.StatusCodeAccepted; } else if (stdDataNp == dataNp) { StatusCode = Const.StatusCodePresentationError; } else { StatusCode = Const.StatusCodeWrongAnswer; } return StatusCode; }
public static void Compile(string workingDirectory, Answer thatAnswer) { SourceFilePath = ""; OutputStringBuilder = new StringBuilder(); ErrorStringBuilder = new StringBuilder(); CompilerProcess = new Process { StartInfo = { CreateNoWindow = true, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true } }; switch (thatAnswer.LanguageCode) { case Const.LanguageCodeC: SourceFilePath = workingDirectory + "Main.c"; CompilerProcess.StartInfo.FileName = "gcc"; CompilerProcess.StartInfo.Arguments = SourceFilePath + " -o " + workingDirectory + "Main.exe" + " -O2 -Wall -lm --static -std=c99 -DONLINE_JUDGE"; break; case Const.LanguageCodeCpp: SourceFilePath = workingDirectory + "Main.cpp"; CompilerProcess.StartInfo.FileName = "g++"; CompilerProcess.StartInfo.Arguments = SourceFilePath + " -o " + workingDirectory + "Main.exe" + " -O2 -Wall -lm --static -DONLINE_JUDGE"; break; case Const.LanguageCodeJava: SourceFilePath = workingDirectory + "Main.java"; CompilerProcess.StartInfo.FileName = "javac"; CompilerProcess.StartInfo.Arguments = "-J-Xms32m -J-Xmx256m " + SourceFilePath; break; default: throw new Exception("不支持的语言类型"); } //写入源文件 var thatSourceFileStreamWriter = new StreamWriter(File.Create(SourceFilePath)); thatSourceFileStreamWriter.Write(thatAnswer.SourceCode); thatSourceFileStreamWriter.Close(); //启动编译器 CompilerProcess.Start(); //启动输出流流监控线程 var threadWatchOutputStream = new Thread(WatchOutputStream); threadWatchOutputStream.Start(); //启动错误流监控线程 var threadWatchErrorStream = new Thread(WatchErrorStream); threadWatchErrorStream.Start(); //等待编译器编译完成 CompilerProcess.WaitForExit(); }
public static Answer GetEarliestPendingAnswer() { Answer thisAnswer = null; var thisMySqlCommand = TheMySqlConnection.CreateCommand(); thisMySqlCommand.CommandText = "SELECT `answer`.* FROM `answer` WHERE `answer`.StatusCode = 1 LIMIT " + Program.Offset + ",1"; if (TheMySqlConnection.State == ConnectionState.Closed) TheMySqlConnection.Open(); var thisMySqlDataReader = thisMySqlCommand.ExecuteReader(); try { while (thisMySqlDataReader.Read()) { thisAnswer = new Answer( thisMySqlDataReader.GetInt64("ID"), thisMySqlDataReader.GetInt64("ProblemID"), thisMySqlDataReader.GetInt64("UserID"), thisMySqlDataReader.GetInt16("LanguageCode"), thisMySqlDataReader.GetString("SourceCode"), "", "", thisMySqlDataReader.GetInt64("UsedTime"), thisMySqlDataReader.GetInt64("UsedMemory"), thisMySqlDataReader.GetInt16("StatusCode"), thisMySqlDataReader.GetString("Info"), thisMySqlDataReader.GetMySqlDateTime("SubmitTime").GetDateTime(), thisMySqlDataReader.GetMySqlDateTime("MarkedTime").GetDateTime() ); } } catch (MySqlConversionException) { throw; } catch (Exception) { throw; } finally { thisMySqlDataReader.Close(); } return thisAnswer; }
public static void Run(string workingDirectory, Answer thatAnswer, Problem thatProblem) { StatusCode = Const.StatusCodeAccepted; OutputStringBuilder = new StringBuilder(); ErrorStringBuilder = new StringBuilder(); OutputLimit = thatProblem.StandardOutput.Length * 2; ThatProgramProcess = new Process { StartInfo = { CreateNoWindow = true, UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true } }; switch (thatAnswer.LanguageCode) { case Const.LanguageCodeC: case Const.LanguageCodeCpp: ThatProgramProcess.StartInfo.FileName = workingDirectory + "Main.exe"; TimeLimit = thatProblem.TimeLimitNormal; MemoryLimit = thatProblem.MemoryLimitNormal * 1000; break; case Const.LanguageCodeJava: ThatProgramProcess.StartInfo.FileName = "java"; ThatProgramProcess.StartInfo.Arguments = "-cp " + workingDirectory + " " + "Main"; TimeLimit = thatProblem.TimeLimitJava * 1000; MemoryLimit = thatProblem.MemoryLimitJava * 1000; break; default: throw new Exception("不支持的语言类型"); } try { //启动进程 ThatProgramProcess.Start(); //启动时间内存监控线程 var threadWatchTimeAndMemory = new Thread(WatchTimeAndMemory); threadWatchTimeAndMemory.Start(); //启动输出流监控线程 var threadWatchOutputStream = new Thread(WatchOutputStream); threadWatchOutputStream.Start(); //启动错误流监控线程 var threadWatchErrorStream = new Thread(WatchErrorStream); threadWatchErrorStream.Start(); //设置最大使用内存 if (ThatProgramProcess.MaxWorkingSet.ToInt64() < MemoryLimit) { ThatProgramProcess.MaxWorkingSet = new IntPtr(MemoryLimit); } else { throw new OutOfMemoryException(); } //输入数据 ThatProgramProcess.StandardInput.Write(thatProblem.StandardInput); ThatProgramProcess.StandardInput.Close(); //等待进程结束 ThatProgramProcess.WaitForExit(); } catch (OutOfMemoryException) { StatusCode = Const.StatusCodeMemoryLimitExceeded; } catch (IOException) { } catch (Exception) { StatusCode = Const.StatusCodeSystemError; } finally { //强制结束进程 try { ThatProgramProcess.Kill(); } catch (Exception) { } switch (StatusCode) { case Const.StatusCodeAccepted: if (ThatProgramProcess.ExitCode != 0) { StatusCode = Const.StatusCodeRuntimeError; Info = ExitCodeDictionary.ContainsKey(ThatProgramProcess.ExitCode) ? ExitCodeDictionary[ThatProgramProcess.ExitCode] : ErrorStringBuilder.ToString(); } else if (ErrorStringBuilder.ToString() != "") { StatusCode = Const.StatusCodeRuntimeError; Info = ErrorStringBuilder.ToString(); } break; case Const.StatusCodeMemoryLimitExceeded: UsedMemory = MemoryLimit; Info = ""; break; case Const.StatusCodeTimeLimitExceeded: UsedTime = TimeLimit; Info = ""; break; case Const.StatusCodeOutputLimitExceeded: Info = ""; break; default: throw new Exception("状态码错误"); } } }
public static int UpdateAnswer(Answer thatAnswer) { try { var command = TheMySqlConnection.CreateCommand(); command.CommandText = "UPDATE answer " + "SET " + "answer.UsedTime = @UsedTime, " + "answer.UsedMemory = @UsedMemory, " + "answer.StatusCode = @StatusCode, " + "answer.Info = @Info, " + "answer.MarkedTime = @MarkedTime " + "WHERE answer.ID = @ID"; command.Parameters.AddWithValue("@ID", thatAnswer.Id); command.Parameters.AddWithValue("@UsedTime", thatAnswer.UsedTime); command.Parameters.AddWithValue("@UsedMemory", thatAnswer.UsedMemory); command.Parameters.AddWithValue("@StatusCode", thatAnswer.StatusCode); command.Parameters.AddWithValue("@Info", thatAnswer.Info); command.Parameters.AddWithValue("@MarkedTime", new MySqlDateTime(thatAnswer.MarkedTime)); if (TheMySqlConnection.State == ConnectionState.Closed) TheMySqlConnection.Open(); var result = command.ExecuteNonQuery(); return result; } catch (MySqlConversionException) { throw; } catch (Exception) { throw; } }
public static void Run(string workingDirectory, Answer thatAnswer, Problem thatProblem) { StatusCode = Const.StatusCodeAccepted; OutputStringBuilder = new StringBuilder(); ErrorStringBuilder = new StringBuilder(); OutputLimit = thatProblem.StandardOutput.Length * 2; ThatProgramProcess = new Process { StartInfo = { CreateNoWindow = true, UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true } }; switch (thatAnswer.LanguageCode) { case Const.LanguageCodeC: case Const.LanguageCodeCpp: ThatProgramProcess.StartInfo.FileName = workingDirectory + "Main.exe"; TimeLimit = thatProblem.TimeLimitNormal; MemoryLimit = thatProblem.MemoryLimitNormal * 1000; break; case Const.LanguageCodeJava: ThatProgramProcess.StartInfo.FileName = "java"; ThatProgramProcess.StartInfo.Arguments = "-cp " + workingDirectory + " " + "Main"; TimeLimit = thatProblem.TimeLimitJava * 1000; MemoryLimit = thatProblem.MemoryLimitJava * 1000; break; default: throw new Exception("不支持的语言类型"); } try { //启动进程 ThatProgramProcess.Start(); //启动时间内存监控线程 var threadWatchTimeAndMemory = new Thread(WatchTimeAndMemory); threadWatchTimeAndMemory.Start(); //启动输出流监控线程 var threadWatchOutputStream = new Thread(WatchOutputStream); threadWatchOutputStream.Start(); //启动错误流监控线程 var threadWatchErrorStream = new Thread(WatchErrorStream); threadWatchErrorStream.Start(); //设置最大使用内存 if (ThatProgramProcess.MaxWorkingSet.ToInt64() < MemoryLimit) ThatProgramProcess.MaxWorkingSet = new IntPtr(MemoryLimit); else throw new OutOfMemoryException(); //输入数据 ThatProgramProcess.StandardInput.Write(thatProblem.StandardInput); ThatProgramProcess.StandardInput.Close(); //等待进程结束 ThatProgramProcess.WaitForExit(); } catch (OutOfMemoryException) { StatusCode = Const.StatusCodeMemoryLimitExceeded; } catch (IOException) { } catch (Exception) { StatusCode = Const.StatusCodeSystemError; } finally { //强制结束进程 try { ThatProgramProcess.Kill(); } catch (Exception) { } switch (StatusCode) { case Const.StatusCodeAccepted: if (ThatProgramProcess.ExitCode != 0) { StatusCode = Const.StatusCodeRuntimeError; Info = ExitCodeDictionary.ContainsKey(ThatProgramProcess.ExitCode) ? ExitCodeDictionary[ThatProgramProcess.ExitCode] : ErrorStringBuilder.ToString(); } else if (ErrorStringBuilder.ToString() != "") { StatusCode = Const.StatusCodeRuntimeError; Info = ErrorStringBuilder.ToString(); } break; case Const.StatusCodeMemoryLimitExceeded: UsedMemory = MemoryLimit; Info = ""; break; case Const.StatusCodeTimeLimitExceeded: UsedTime = TimeLimit; Info = ""; break; case Const.StatusCodeOutputLimitExceeded: Info = ""; break; default: throw new Exception("状态码错误"); } } }