Files
ErgometerServer/ErgometerServer/ClientThread.cs
T
2015-10-17 21:48:20 +02:00

130 lines
4.6 KiB
C#

using ErgometerLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ErgometerServer
{
class ClientThread
{
TcpClient client;
Server server;
public string name;
public int session { get; }
bool running;
bool loggedin;
List<Meting> metingen;
List<ChatMessage> chat;
public ClientThread(TcpClient client, Server server)
{
this.client = client;
this.server = server;
this.name = "Unknown";
this.session = 0;
this.running = false;
this.loggedin = false;
metingen = new List<Meting>();
chat = new List<ChatMessage>();
session = FileHandler.GenerateSession();
Console.WriteLine("Generated new session: " + session);
}
public void run()
{
running = true;
NetHelper.SendNetCommand(client, new NetCommand(session));
while (running)
{
NetCommand input = NetHelper.ReadNetCommand(client);
switch(input.Type)
{
case NetCommand.CommandType.SESSION:
break;
case NetCommand.CommandType.LOGIN:
if(! server.CheckPassword(input.DisplayName, input.Password))
{
NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.LOGINWRONG, session));
loggedin = false;
}
else
{
NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.LOGINOK, session));
loggedin = true;
if (input.IsDoctor)
{
server.ChangeClientToDoctor(client, this);
Console.WriteLine("Doctor connected");
running = false;
}
else
{
name = input.DisplayName;
loggedin = true;
Console.WriteLine(name + " (client) connected with password: " + input.Password);
FileHandler.CreateSession(session, name);
}
}
break;
case NetCommand.CommandType.DATA:
if (loggedin)
{
metingen.Add(input.Meting);
server.SendToDoctor(input);
}
else
NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.NOTLOGGEDIN, session));
break;
case NetCommand.CommandType.CHAT:
if (loggedin)
{
chat.Add(new ChatMessage(name, input.ChatMessage, false));
server.SendToDoctor(input);
NetHelper.SendNetCommand(client, new NetCommand("shut up", session));
Console.WriteLine(name + ": " + input.ChatMessage);
}
else
NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.NOTLOGGEDIN, session));
break;
case NetCommand.CommandType.LOGOUT:
loggedin = false;
running = false;
Console.WriteLine(name + " logged out");
FileHandler.WriteMetingen(session, metingen);
FileHandler.WriteChat(session, chat);
client.Close();
break;
default:
throw new FormatException("Unknown command");
}
}
server.RemoveActiveSession(this);
}
public void SendToClient(NetCommand command)
{
NetHelper.SendNetCommand(client, command);
if (command.Type == NetCommand.CommandType.CHAT)
{
chat.Add(new ChatMessage("Doctor", command.ChatMessage, true));
}
}
}
}