using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Threading;

using EA.GameTalkRouter;

namespace EA.GameTalkServer
{
	/// <summary>
	/// Summary description for GameServerConsole.
	/// </summary>
	public class GameServerConsole : System.Windows.Forms.Form
	{
		private TcpServer mTcpServer = null;
		TraceListener traceListener;

		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.TextBox textBox1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public GameServerConsole()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			traceListener = new TextBoxTraceListener(textBox1);
			// Comment out the following line if you don't want to output all of the messages from the server 
			// to the server console.  Not commenting out this line can cause considerable slow-down depending on the 
			// volume of data
			Trace.Listeners.Add(traceListener);

			int port = GameServer.GetOrSetPortNumInRegistry();
			mTcpServer = new GameTalkTcpServer(port);
			mTcpServer.StartServer();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose(bool disposing)
		{
			Trace.Listeners.Remove(traceListener);
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.panel1 = new System.Windows.Forms.Panel();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.textBox1);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(1192, 262);
			this.panel1.TabIndex = 0;
			// 
			// textBox1
			// 
			this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.textBox1.Location = new System.Drawing.Point(0, 0);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.textBox1.Size = new System.Drawing.Size(1192, 262);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "";
			// 
			// GameServerConsole
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(1192, 262);
			this.Controls.Add(this.panel1);
			this.Name = "GameServerConsole";
			this.Text = "GameServerConsole";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.GameServerConsole_Closing);
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void GameServerConsole_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (mTcpServer != null)
				mTcpServer.Shutdown();

			// TcpServer's (specifically GameTalkTcpServer's) finalizer should get trigger because of this
			mTcpServer = null;
		}
	}
}
