Initial commit
This commit is contained in:
89
CardMarketServer/handleClient.cs
Normal file
89
CardMarketServer/handleClient.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user