using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace WorldOfPeacecraft
{
	public class Sender
	{
		private TcpClient Client;
		private Buffer Buffer;
		private Thread SenderThread;

		public Sender (TcpClient client, Buffer buffer)
		{
			this.Client = client;
			this.Buffer = buffer;
			this.SenderThread = new Thread(new ThreadStart(this.threadStart));
			this.SenderThread.IsBackground = true;
		}

		public void Send (String message)
		{
			StreamWriter writer = new StreamWriter (Client.GetStream ());
			writer.WriteLine (message);
			writer.Flush ();
		}

		private void threadStart ()
		{
			while (true) {
				Send (Buffer.NextLine());
			}
		}

		public void Start()
		{
			SenderThread.Start();
		}

		public void Stop()
		{
			SenderThread.Abort();
			SenderThread.Join();
		}
	}
}