All pastes #911366 Raw Edit

Latheesan

public text v1 · immutable
·published 2008-02-20 17:38 UTC
rendered paste body
using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AscentCP.Properties;

namespace AscentCP
{
    public partial class AscentCP : Form
    {
        const int ERROR_FILE_NOT_FOUND = 2;
        const int ERROR_ACCESS_DENIED = 5;
        Process logonProcess = new Process();
        Process worldProcess = new Process();

        public AscentCP()
        {
            InitializeComponent();
        }

        void logonProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            EventHandler eh = delegate { this.logonServerOutput.AppendText(e.Data + "\n"); }; 
            this.logonServerOutput.Invoke(eh);
        }

        void logonProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            EventHandler eh = delegate { this.logonServerOutput.AppendText(e.Data + "\n"); };
            this.logonServerOutput.Invoke(eh);
        }

        void worldProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            EventHandler eh = delegate { this.worldServerOutput.AppendText(e.Data + "\n"); };
            this.worldServerOutput.Invoke(eh);
        }

        void worldProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            EventHandler eh = delegate { this.worldServerOutput.AppendText(e.Data + "\n"); };
            this.worldServerOutput.Invoke(eh);
        }

        private void startServerButton_Click(object sender, EventArgs e)
        {
            if (!File.Exists(Settings.Default.logonServer))
            {
                MessageBox.Show("Logon Server not found at : " + Settings.Default.logonServer + "\n\nEdit your AscentCP.exe.config file with the correct path!", "Logon Server Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
            else if (!File.Exists(Settings.Default.worldServer))
            {
                MessageBox.Show("World Server not found at : " + Settings.Default.worldServer + "\n\nEdit your AscentCP.exe.config file with the correct path!", "World Server Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
            else
            {
                try
                {
                    logonProcess.StartInfo.FileName = Settings.Default.logonServer;
                    logonProcess.StartInfo.Verb = "Ascent Logon Server";
                    logonProcess.StartInfo.CreateNoWindow = true;
                    logonProcess.StartInfo.UseShellExecute = false;
                    logonProcess.StartInfo.RedirectStandardOutput = true;
                    logonProcess.StartInfo.RedirectStandardError = true;
                    logonProcess.OutputDataReceived += new DataReceivedEventHandler(logonProcess_OutputDataReceived);
                    logonProcess.ErrorDataReceived += new DataReceivedEventHandler(logonProcess_ErrorDataReceived);
                    logonProcess.Start();
                    logonProcess.BeginOutputReadLine();
                    logonProcess.BeginErrorReadLine();
                    
                    worldProcess.StartInfo.FileName = Settings.Default.worldServer;
                    worldProcess.StartInfo.Verb = "Ascent World Server";
                    worldProcess.StartInfo.CreateNoWindow = true;
                    worldProcess.StartInfo.UseShellExecute = false;
                    worldProcess.StartInfo.RedirectStandardOutput = true;
                    worldProcess.StartInfo.RedirectStandardError = true;
                    worldProcess.OutputDataReceived += new DataReceivedEventHandler(worldProcess_OutputDataReceived);
                    worldProcess.ErrorDataReceived += new DataReceivedEventHandler(worldProcess_ErrorDataReceived);
                    worldProcess.Start();
                    worldProcess.BeginOutputReadLine();
                    worldProcess.BeginErrorReadLine();

                    this.statusLabel.Text = "Online";
                    this.statusLabel.ForeColor = System.Drawing.Color.Green;
                    this.startServerButton.Enabled = false;
                    this.stopServerButton.Enabled = true;
                }
                catch (Win32Exception err)
                {
                    if (err.NativeErrorCode == ERROR_FILE_NOT_FOUND)
                    {
                        MessageBox.Show("Server files not found.\n\nEdit your AscentCP.exe.config file with the correct path!", "Server Files Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        Application.Exit();
                    }
                    else if (err.NativeErrorCode == ERROR_ACCESS_DENIED)
                    {
                        MessageBox.Show(err.Message + ".\n\nYou do not have the permission to run the Ascent Logon/World Server.", "Access Denied", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Application.Exit();
                    }
                }
            }
        }

        private void stopServerButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (!logonProcess.HasExited)
                {
                    logonProcess.Kill();
                }
                if (!worldProcess.HasExited)
                {
                    worldProcess.Kill();
                }
                this.statusLabel.Text = "Offline";
                this.statusLabel.ForeColor = System.Drawing.Color.Maroon;
                this.stopServerButton.Enabled = false;
                this.startServerButton.Enabled = true;
                this.logonServerOutput.Text = "Logon server stopped ...";
                this.worldServerOutput.Text = "World server stopped ...";
            }
            catch (Exception)
            {
                Application.Exit();
            }
        }

        private void AscentCP_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (!logonProcess.HasExited)
                {
                    logonProcess.Kill();
                }
                if (!worldProcess.HasExited)
                {
                    worldProcess.Kill();
                }
            }
            catch (Exception)
            {
                Application.Exit();
            }
        }

        private void AscentCP_Load(object sender, EventArgs e)
        {
            this.stopServerButton.Enabled = false;
        }
    }
}