Initial commit

This commit is contained in:
2023-08-01 14:04:38 +02:00
commit d64e49ce19
8 changed files with 225 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Update="usedList.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -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<string> Query { get; }
void Insert(string bestellungid);
}
}

View File

@@ -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<TcpClient> _clients = new List<TcpClient>();
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");
}
}
}
}

View File

@@ -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<string> 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);
}
}
}

View File

@@ -0,0 +1,89 @@
using System.Diagnostics;
using System.Net.Sockets;
namespace CardMarketServer
{
internal class handleClient
{
TcpClient clientSocket;
List<string> bereitsBearbeitet= new List<string>();
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");
}
}
}

View File

@@ -0,0 +1 @@