commit d64e49ce1976588d0d3d7c7f32d67df3913305dc Author: Damian Wessels Date: Tue Aug 1 14:04:38 2023 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b22aadf --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.vs/ +/CardMarketServer/bin +/CardMarketServer/obj/ diff --git a/CardMarketServer.sln b/CardMarketServer.sln new file mode 100644 index 0000000..f07a730 --- /dev/null +++ b/CardMarketServer.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.6.33829.357 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CardMarketServer", "CardMarketServer\CardMarketServer.csproj", "{1A5EEA3B-ED7F-4D5F-91D5-74501DD74E1E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1A5EEA3B-ED7F-4D5F-91D5-74501DD74E1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1A5EEA3B-ED7F-4D5F-91D5-74501DD74E1E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1A5EEA3B-ED7F-4D5F-91D5-74501DD74E1E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1A5EEA3B-ED7F-4D5F-91D5-74501DD74E1E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {66DB3E2E-EAA6-4E84-AF2B-E1C9D1CAC340} + EndGlobalSection +EndGlobal diff --git a/CardMarketServer/CardMarketServer.csproj b/CardMarketServer/CardMarketServer.csproj new file mode 100644 index 0000000..2cda4e8 --- /dev/null +++ b/CardMarketServer/CardMarketServer.csproj @@ -0,0 +1,16 @@ + + + + Exe + net7.0 + enable + enable + + + + + PreserveNewest + + + + diff --git a/CardMarketServer/IUsedRepository.cs b/CardMarketServer/IUsedRepository.cs new file mode 100644 index 0000000..9fb366e --- /dev/null +++ b/CardMarketServer/IUsedRepository.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CardMarketServer +{ + internal interface IUsedRepository + { + List Query { get; } + void Insert(string bestellungid); + } +} diff --git a/CardMarketServer/Program.cs b/CardMarketServer/Program.cs new file mode 100644 index 0000000..6d416d7 --- /dev/null +++ b/CardMarketServer/Program.cs @@ -0,0 +1,48 @@ +using System.Net; +using System.Net.Sockets; + +namespace CardMarketServer +{ + internal class Program + { + private static object _lock = new object(); + private static readonly List _clients = new List(); + + public static TcpClient[] GetClients() + { + lock(_lock ) return _clients.ToArray(); + } + + public static int GetClientCount() + { + lock(_lock) return _clients.Count; + } + + public static void RemoveClient(TcpClient client) + { + lock(_lock) _clients.Remove(client); + } + + public static void AddClient(TcpClient client) + { + lock(_lock) _clients.Add(client); + } + static void Main(string[] args) + { + IPAddress ip = IPAddress.Parse("0.0.0.0"); + TcpListener ServerSocket = new TcpListener(ip, 4000); + ServerSocket.Start(); + Console.WriteLine("Server started"); + while(true) + { + TcpClient clientSocket = ServerSocket.AcceptTcpClient(); + Console.WriteLine($"Client connected: {clientSocket.Client.RemoteEndPoint}"); + AddClient(clientSocket); + handleClient client = new handleClient(); + client.startClient(clientSocket); + + Console.WriteLine($"{GetClientCount()} clients connected"); + } + } + } +} \ No newline at end of file diff --git a/CardMarketServer/UsedRepository.cs b/CardMarketServer/UsedRepository.cs new file mode 100644 index 0000000..4029dce --- /dev/null +++ b/CardMarketServer/UsedRepository.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CardMarketServer +{ + internal class UsedRepository : IUsedRepository + { + const string FILENAME = "usedList.csv"; + public List Query => File + .ReadLines(FILENAME) + .Select(l => l.Split(',')) + .Select(p => new string(p[0])) + .ToList(); + + void insert(string key) + { + key += Environment.NewLine; + File.AppendAllText(FILENAME,key); + } + + public void Insert(string bestellungid) + { + insert(bestellungid); + } + } +} diff --git a/CardMarketServer/handleClient.cs b/CardMarketServer/handleClient.cs new file mode 100644 index 0000000..b63cbd1 --- /dev/null +++ b/CardMarketServer/handleClient.cs @@ -0,0 +1,89 @@ +using System.Diagnostics; +using System.Net.Sockets; + +namespace CardMarketServer +{ + internal class handleClient + { + TcpClient clientSocket; + List bereitsBearbeitet= new List(); + IUsedRepository repository; + public handleClient() + { + repository = new UsedRepository(); + bereitsBearbeitet = repository.Query; + } + + internal void startClient(TcpClient clientSocket) + { + this.clientSocket = clientSocket; + Thread ctThread = new Thread(Chat); + ctThread.Start(); + } + enum EACTION + { + NONE, + TEST, + WRITE + } + private void Chat() + { + BinaryReader reader = new BinaryReader(clientSocket.GetStream()); + try + { + while (true) + { + string message = reader.ReadString(); + + if (message != null) + { + EACTION action = EACTION.NONE; + string[] parts = message.Split('#'); + if(parts.Length < 3) + { + Console.WriteLine("fehler"); + } + if (parts[1].Equals("TEST")) action = EACTION.TEST; + if (parts[1].Equals("WRITE")) action = EACTION.WRITE; + + BinaryWriter writer = new BinaryWriter(clientSocket.GetStream()); + if (action == EACTION.TEST) + { + Console.Write($"Test for {parts[2]} : "); + if (bereitsBearbeitet.Find(x => x.Equals(parts[2])) != null) + { + Console.WriteLine("Eintrag bereits vorhanden"); + writer.Write("FAILED"); + } + else + { + Console.WriteLine("Eintrag noch nicht vorhanden"); + writer.Write("OK"); + } + } + if(action == EACTION.WRITE) + { + repository.Insert(parts[2]); + writer.Write("OK"); + } + Console.WriteLine(message); + } + + } + } + catch(EndOfStreamException) + { + Console.WriteLine("Client diconnecting"); + clientSocket.Client.Shutdown(SocketShutdown.Both); + } + catch(IOException e) + { + Console.WriteLine($"IOException reading from {clientSocket.Client.RemoteEndPoint}: {e.Message}"); + } + + clientSocket.Close(); + Program.RemoveClient(clientSocket); + Console.WriteLine($"{Program.GetClientCount()} clients connected"); + } + } +} \ No newline at end of file diff --git a/CardMarketServer/usedList.csv b/CardMarketServer/usedList.csv new file mode 100644 index 0000000..5f28270 --- /dev/null +++ b/CardMarketServer/usedList.csv @@ -0,0 +1 @@ + \ No newline at end of file