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; break;
case NetCommand.CommandType.LOGIN: case NetCommand.CommandType.LOGIN:
if(input.IsDoctor) if(! server.CheckPassword(input.DisplayName, input.Password))
{ {
server.ChangeClientToDoctor(client, this); NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.LOGINWRONG, session));
Console.WriteLine("Doctor connected"); loggedin = false;
running = false;
} }
else else
{ {
name = input.DisplayName; NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.LOGINOK, session));
loggedin = true; 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; break;
case NetCommand.CommandType.DATA: case NetCommand.CommandType.DATA:
metingen.Add(input.Meting); if (loggedin)
server.SendToDoctor(input); {
metingen.Add(input.Meting);
server.SendToDoctor(input);
}
else
NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.NOTLOGGEDIN, session));
break; break;
case NetCommand.CommandType.CHAT: case NetCommand.CommandType.CHAT:
chat.Add(new ChatMessage(name, input.ChatMessage)); if (loggedin)
server.SendToDoctor(input); {
Console.WriteLine(name + ": " + input.ChatMessage); 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; break;
case NetCommand.CommandType.LOGOUT: case NetCommand.CommandType.LOGOUT:
loggedin = false; loggedin = false;
+4 -9
View File
@@ -12,33 +12,29 @@ namespace ErgometerServer
TcpClient client; TcpClient client;
Server server; Server server;
string name;
bool running; bool running;
bool loggedin;
public DoctorThread(TcpClient client, Server server) public DoctorThread(TcpClient client, Server server)
{ {
this.client = client; this.client = client;
this.server = server; this.server = server;
this.name = "Unknown";
this.running = false; this.running = false;
this.loggedin = false;
} }
public void run() public void run()
{ {
running = true; running = true;
loggedin = true; while (running)
while (loggedin && running)
{ {
NetCommand input = NetHelper.ReadNetCommand(client); NetCommand input = NetHelper.ReadNetCommand(client);
switch (input.Type) switch (input.Type)
{ {
case NetCommand.CommandType.LOGOUT: case NetCommand.CommandType.LOGOUT:
loggedin = false; running = false;
client.Close();
Console.WriteLine("Doctor logged out"); Console.WriteLine("Doctor logged out");
break; break;
case NetCommand.CommandType.CHAT: case NetCommand.CommandType.CHAT:
@@ -49,7 +45,6 @@ namespace ErgometerServer
throw new FormatException("Unknown Command"); throw new FormatException("Unknown Command");
} }
} }
client.Close();
} }
public void sendToDoctor(NetCommand command) public void sendToDoctor(NetCommand command)
+17 -3
View File
@@ -111,25 +111,39 @@ namespace ErgometerServer
private void AddUser(string name, string password) private void AddUser(string name, string password)
{ {
users.Add(name, password); users.Add(name, password);
FileHandler.SaveUsers(users);
} }
private bool CheckPassword(string name, string password) public bool CheckPassword(string name, string password)
{ {
string pass; string pass;
bool isOk = users.TryGetValue(name, out pass); bool isOk = users.TryGetValue(name, out pass);
Console.WriteLine($"Checking {name}, {password}: found {isOk} with response {pass}");
if (!isOk) return false; if (!isOk) return false;
return pass == password; 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) private static void OnProcessExit(object sender, EventArgs e)
{ {
FileHandler.SaveUsers(users);
Console.WriteLine("Closing server"); Console.WriteLine("Closing server");
} }
} }