summaryrefslogtreecommitdiff
blob: f2d8976be61c2aa322aad58f9cab5d85773bc676 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.IO;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace Server
{
	public class ClientConnection
	{
		public StreamReader StreamReader { get; set; }
		public StreamWriter StreamWriter { get; set; }

		public ClientConnection(TcpListener listener)
		{
			TcpClient connectionSocket = listener.AcceptTcpClient();
			NetworkStream ns = connectionSocket.GetStream();
			StreamReader = new StreamReader(ns);
			StreamWriter = new StreamWriter(ns);
			StreamWriter.AutoFlush = true;
			Task.Run(() => EchoService(StreamReader, StreamWriter));
		}

		private async Task EchoService(StreamReader sr, StreamWriter sw)
		{
			while (true)
			{
				string message = await sr.ReadLineAsync();
				LogSingleton.Instance.Log += $"{message}\n";
				foreach (ClientConnection client in LogSingleton.Instance.ConnectedClients) client.StreamWriter.WriteLine(message);
				message = "";
			}
		}
	}
}