private static void ListenForClients() { tcpListener.Start(); if (Database.GetSetting("EnableOpenFirewall", "YAMS") == "true") { Networking.OpenFirewallPort(Convert.ToInt32(YAMS.Database.GetSetting("TelnetPort", "YAMS")), "Telnet"); } if (Database.GetSetting("EnablePortForwarding", "YAMS") == "true") { Networking.OpenUPnP(Convert.ToInt32(YAMS.Database.GetSetting("TelnetPort", "YAMS")), "Telnet", YAMS.Database.GetSetting("YAMSListenIP", "YAMS")); } while (true && stoppingServer == false) { //blocks until a client has connected to the server TcpClient client = tcpListener.AcceptTcpClient(); //create a thread to handle communication //with connected client TelnetClient myClient = new TelnetClient(client); } }
public void Start() { if (this.Running) { return; } //Refresh Variables this.bolEnableJavaOptimisations = Convert.ToBoolean(Database.GetSetting(this.ServerID, "ServerEnableOptimisations")); this.intAssignedMem = Convert.ToInt32(Database.GetSetting(this.ServerID, "ServerAssignedMemory")); this.ServerTitle = Convert.ToString(Database.GetSetting(this.ServerID, "ServerTitle")); this.ServerType = Convert.ToString(Database.GetSetting(this.ServerID, "ServerType")); this.RestartWhenFree = false; this.RestartNeeded = false; //What type of server are we running? string strFile = ""; switch (this.ServerType) { case "vanilla": strFile = "minecraft_server.jar"; break; case "bukkit": strFile = "craftbukkit.jar"; break; case "pre": strFile = "minecraft_server_pre.jar"; break; default: strFile = "minecraft_server.jar"; break; } //First check if an update is waiting to be applied if (!Util.ReplaceFile(Core.RootFolder + "\\lib\\" + strFile, Core.RootFolder + "\\lib\\" + strFile + ".UPDATE")) { return; } //Also check if a new properties file is to be applied if (!Util.ReplaceFile(this.strWorkingDir + "server.properties", this.strWorkingDir + "server.properties.UPDATE")) { return; } this.prcMinecraft = new Process(); try { var strArgs = ""; var strFileName = YAMS.Util.JavaPath() + "java.exe"; if (YAMS.Util.HasJDK()) { strFileName = YAMS.Util.JavaPath("jdk") + "java.exe"; } if (File.Exists(this.strWorkingDir + "args.txt")) { StreamReader reader = new StreamReader(this.strWorkingDir + "args.txt"); String text = reader.ReadToEnd(); reader.Close(); strArgs = text; } else { //If we have enabled the java optimisations add the additional //arguments. See http://www.minecraftforum.net/viewtopic.php?f=1012&t=68128 if (bolEnableJavaOptimisations) { var intGCCores = Environment.ProcessorCount - 1; if (intGCCores == 0) { intGCCores = 1; } strArgs += "-server -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=" + intGCCores + " -XX:+AggressiveOpts"; } //Some specials for bukkit if (this.ServerType == "bukkit") { strArgs += " -Djline.terminal=jline.UnsupportedTerminal"; } //Basic arguments in all circumstances strArgs += " -Xmx" + intAssignedMem + "M -Xms" + intAssignedMem + @"M -jar " + "\"" + Core.RootFolder + "\\lib\\"; strArgs += strFile; strArgs += "\" nogui"; } this.prcMinecraft.StartInfo.UseShellExecute = false; this.prcMinecraft.StartInfo.FileName = strFileName; this.prcMinecraft.StartInfo.Arguments = strArgs; this.prcMinecraft.StartInfo.CreateNoWindow = true; this.prcMinecraft.StartInfo.RedirectStandardError = true; this.prcMinecraft.StartInfo.RedirectStandardInput = true; this.prcMinecraft.StartInfo.RedirectStandardOutput = true; this.prcMinecraft.StartInfo.WorkingDirectory = this.strWorkingDir; //Set up events this.prcMinecraft.OutputDataReceived += new DataReceivedEventHandler(ServerOutput); this.prcMinecraft.ErrorDataReceived += new DataReceivedEventHandler(ServerError); this.prcMinecraft.EnableRaisingEvents = true; this.prcMinecraft.Exited += new EventHandler(ServerExited); //Finally start the thing this.prcMinecraft.Start(); this.prcMinecraft.BeginOutputReadLine(); this.prcMinecraft.BeginErrorReadLine(); this.Running = true; this.SafeStop = false; Database.AddLog("Server Started: " + strArgs, "server", "info", false, this.ServerID); //Try and open the firewall port if (Database.GetSetting("EnableOpenFirewall", "YAMS") == "true") { Networking.OpenFirewallPort(this.Port, this.ServerTitle); } if (Database.GetSetting("EnablePortForwarding", "YAMS") == "true") { Networking.OpenUPnP(this.Port, this.ServerTitle, this.ListenIP); } //Save the process ID so we can kill if there is a crash this.PID = this.prcMinecraft.Id; Util.AddPID(this.prcMinecraft.Id); } catch (Exception e) { Database.AddLog("Failed to start Server: " + e.Message, "library", "error", false, this.ServerID); } }
//Control public static void Init() { //See if there is a new version of the web files waiting before we start the server if (File.Exists(Core.RootFolder + @"\web.zip")) { if (Directory.Exists(Core.RootFolder + @"\web\")) { Directory.Delete(Core.RootFolder + @"\web\", true); } Directory.CreateDirectory(YAMS.Core.RootFolder + @"\web\"); AutoUpdate.ExtractZip(YAMS.Core.RootFolder + @"\web.zip", YAMS.Core.RootFolder + @"\web\"); File.Delete(Core.RootFolder + @"\web.zip"); } adminServer = new Server(); publicServer = new Server(); //Handle the requests for static files var adminModule = new FileModule(); adminModule.Resources.Add(new FileResources("/assets/", YAMS.Core.RootFolder + "\\web\\assets\\")); adminServer.Add(adminModule); //Add any server specific folders var publicModule = new FileModule(); publicModule.Resources.Add(new FileResources("/assets/", YAMS.Core.RootFolder + "\\web\\assets\\")); SqlCeDataReader readerServers = YAMS.Database.GetServers(); while (readerServers.Read()) { var intServerID = readerServers["ServerID"].ToString(); if (!Directory.Exists(Core.StoragePath + intServerID + "\\renders\\")) { Directory.CreateDirectory(Core.StoragePath + intServerID + "\\renders\\"); } publicModule.Resources.Add(new FileResources("/servers/" + intServerID + "/renders/", Core.StoragePath + intServerID + "\\renders\\")); if (!Directory.Exists(Core.StoragePath + intServerID + "\\backups\\")) { Directory.CreateDirectory(Core.StoragePath + intServerID + "\\backups\\"); } publicModule.Resources.Add(new FileResources("/servers/" + intServerID + "/backups/", Core.StoragePath + intServerID + "\\backups\\")); } publicServer.Add(publicModule); //Handle requests to API adminServer.Add(new Web.AdminAPI()); publicServer.Add(new Web.PublicAPI()); adminServer.Add(HttpListener.Create(IPAddress.Any, Convert.ToInt32(YAMS.Database.GetSetting("AdminListenPort", "YAMS")))); publicServer.Add(HttpListener.Create(IPAddress.Any, Convert.ToInt32(YAMS.Database.GetSetting("PublicListenPort", "YAMS")))); adminServer.ErrorPageRequested += new EventHandler <ErrorPageEventArgs>(myServer_ErrorPageRequested); publicServer.ErrorPageRequested += new EventHandler <ErrorPageEventArgs>(myServer_ErrorPageRequested); adminServerThread = new Thread(new ThreadStart(StartAdmin)); publicServerThread = new Thread(new ThreadStart(StartPublic)); adminServerThread.Start(); publicServerThread.Start(); //Open firewall ports Networking.OpenFirewallPort(Convert.ToInt32(YAMS.Database.GetSetting("AdminListenPort", "YAMS")), "Admin website"); Networking.OpenFirewallPort(Convert.ToInt32(YAMS.Database.GetSetting("PublicListenPort", "YAMS")), "Public website"); Networking.OpenUPnP(Convert.ToInt32(YAMS.Database.GetSetting("AdminListenPort", "YAMS")), "Admin website"); Networking.OpenUPnP(Convert.ToInt32(YAMS.Database.GetSetting("PublicListenPort", "YAMS")), "Public website"); }