Added login, bugfixes

This commit is contained in:
2015-10-08 22:41:51 +02:00
parent c6735fadc3
commit 1744fdb74f
3 changed files with 55 additions and 24 deletions
+34 -12
View File
@@ -55,28 +55,50 @@ namespace ErgometerServer
break;
case NetCommand.CommandType.LOGIN:
if(input.IsDoctor)
if(! server.CheckPassword(input.DisplayName, input.Password))
{
server.ChangeClientToDoctor(client, this);
Console.WriteLine("Doctor connected");
running = false;
NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.LOGINWRONG, session));
loggedin = false;
}
else
{
name = input.DisplayName;
NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.LOGINOK, session));
loggedin = true;
Console.WriteLine(name + " (client) connected with password: " + input.Password);
FileHandler.CreateSession(session, name);
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:
metingen.Add(input.Meting);
server.SendToDoctor(input);
if (loggedin)
{
metingen.Add(input.Meting);
server.SendToDoctor(input);
}
else
NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.NOTLOGGEDIN, session));
break;
case NetCommand.CommandType.CHAT:
chat.Add(new ChatMessage(name, input.ChatMessage));
server.SendToDoctor(input);
Console.WriteLine(name + ": " + input.ChatMessage);
if (loggedin)
{
chat.Add(new ChatMessage(name, input.ChatMessage));
server.SendToDoctor(input);
Console.WriteLine(name + ": " + input.ChatMessage);
}
else
NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.NOTLOGGEDIN, session));
break;
case NetCommand.CommandType.LOGOUT:
loggedin = false;
+4 -9
View File
@@ -12,33 +12,29 @@ namespace ErgometerServer
TcpClient client;
Server server;
string name;
bool running;
bool loggedin;
public DoctorThread(TcpClient client, Server server)
{
this.client = client;
this.server = server;
this.name = "Unknown";
this.running = false;
this.loggedin = false;
}
public void run()
{
running = true;
loggedin = true;
while (loggedin && running)
while (running)
{
NetCommand input = NetHelper.ReadNetCommand(client);
switch (input.Type)
{
case NetCommand.CommandType.LOGOUT:
loggedin = false;
running = false;
client.Close();
Console.WriteLine("Doctor logged out");
break;
case NetCommand.CommandType.CHAT:
@@ -49,7 +45,6 @@ namespace ErgometerServer
throw new FormatException("Unknown Command");
}
}
client.Close();
}
public void sendToDoctor(NetCommand command)
+17 -3
View File
@@ -111,25 +111,39 @@ namespace ErgometerServer
private void AddUser(string name, string password)
{
users.Add(name, password);
FileHandler.SaveUsers(users);
}
private bool CheckPassword(string name, string password)
public bool CheckPassword(string name, string password)
{
string pass;
bool isOk = users.TryGetValue(name, out pass);
Console.WriteLine($"Checking {name}, {password}: found {isOk} with response {pass}");
if (!isOk) return false;
return pass == password;
}
private void ChangePassword(string name, string newpassword)
private string GeneratePassword(int len = 8)
{
string pass = "";
char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray();
Random rand = new Random();
for (int i=0; i<=len; i++)
{
pass += chars[rand.Next(0, chars.Length - 1)];
}
return pass;
}
private static void OnProcessExit(object sender, EventArgs e)
{
FileHandler.SaveUsers(users);
Console.WriteLine("Closing server");
}
}