login implemented

This commit is contained in:
shinichi
2020-09-25 15:33:39 +02:00
parent 9c3b2c3f9b
commit 21203fd3ff
3 changed files with 47 additions and 30 deletions

View File

@@ -36,21 +36,9 @@ namespace Client
this.stream = this.client.GetStream(); this.stream = this.client.GetStream();
//TODO File in lezen tryLogin();
Console.WriteLine("enter username");
string username = Console.ReadLine();
Console.WriteLine("enter password");
string password = Console.ReadLine();
byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password));
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null);
//TODO lees OK message
//temp moet eigenlijk een ok bericht ontvangen
this.connected = true;
} }
private void OnRead(IAsyncResult ar) private void OnRead(IAsyncResult ar)
@@ -71,23 +59,37 @@ namespace Client
byte[] messageBytes = new byte[expectedMessageLength]; byte[] messageBytes = new byte[expectedMessageLength];
Array.Copy(totalBuffer, 0, messageBytes, 0, expectedMessageLength); Array.Copy(totalBuffer, 0, messageBytes, 0, expectedMessageLength);
byte[] payloadbytes = new byte[BitConverter.ToInt32(messageBytes, 0) - 5];
Array.Copy(messageBytes, 5, payloadbytes, 0, payloadbytes.Length);
string identifier; string identifier;
bool isJson = DataParser.getJsonIdentifier(messageBytes, out identifier); bool isJson = DataParser.getJsonIdentifier(messageBytes, out identifier);
if (isJson) if (isJson)
{ {
switch (identifier) switch (identifier)
{ {
case DataParser.LOGIN: case DataParser.LOGIN_RESPONSE:
Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(messageBytes.Skip(5).ToArray())}"); string responseStatus = DataParser.getResponseStatus(payloadbytes);
if (responseStatus == "OK")
{
this.connected = true;
}
else
{
Console.WriteLine($"login failed \"{responseStatus}\"");
tryLogin();
}
break; break;
default: default:
Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(messageBytes.Skip(5).ToArray())}"); Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}");
break; break;
} }
} }
else if (DataParser.isRawData(messageBytes)) else if (DataParser.isRawData(messageBytes))
{ {
Console.WriteLine($"Received data: {BitConverter.ToString(messageBytes.Skip(5).ToArray())}"); Console.WriteLine($"Received data: {BitConverter.ToString(payloadbytes)}");
} }
totalBufferReceived -= expectedMessageLength; totalBufferReceived -= expectedMessageLength;
@@ -131,5 +133,17 @@ namespace Client
{ {
return this.connected; return this.connected;
} }
private void tryLogin()
{
//TODO File in lezen
Console.WriteLine("enter username");
string username = Console.ReadLine();
Console.WriteLine("enter password");
string password = Console.ReadLine();
byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password));
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
}
} }
} }

View File

@@ -64,6 +64,11 @@ namespace Client
return getJsonMessage(LOGIN_RESPONSE, new { status = mStatus }); return getJsonMessage(LOGIN_RESPONSE, new { status = mStatus });
} }
public static string getResponseStatus(byte[] json)
{
return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.status;
}
/// <summary> /// <summary>
/// get the identifier from json /// get the identifier from json
/// </summary> /// </summary>

View File

@@ -25,7 +25,7 @@ namespace Server
public Client(Communication communication, TcpClient tcpClient) public Client(Communication communication, TcpClient tcpClient)
{ {
this.saveData = new SaveData(Directory.GetCurrentDirectory()+$"/test"); this.saveData = new SaveData(Directory.GetCurrentDirectory() + $"/test");
this.communication = communication; this.communication = communication;
this.tcpClient = tcpClient; this.tcpClient = tcpClient;
this.stream = this.tcpClient.GetStream(); this.stream = this.tcpClient.GetStream();
@@ -90,11 +90,9 @@ namespace Server
bool isJson = DataParser.getJsonIdentifier(message, out identifier); bool isJson = DataParser.getJsonIdentifier(message, out identifier);
if (isJson) if (isJson)
{ {
Console.WriteLine($"received json with identifier {identifier} and expecting {DataParser.LOGIN}");
switch (identifier) switch (identifier)
{ {
case DataParser.LOGIN: case DataParser.LOGIN:
Console.WriteLine("LOGIN");
string username; string username;
string password; string password;
bool worked = DataParser.GetUsernamePassword(payloadbytes, out username, out password); bool worked = DataParser.GetUsernamePassword(payloadbytes, out username, out password);
@@ -103,33 +101,33 @@ namespace Server
if (verifyLogin(username, password)) if (verifyLogin(username, password))
{ {
this.username = username; this.username = username;
stream.BeginWrite(DataParser.getLoginResponse("OK"), 0, 0, new AsyncCallback(OnWrite), null); byte[] response = DataParser.getLoginResponse("OK");
stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null);
} }
else else
{ {
stream.BeginWrite(DataParser.getLoginResponse("wrong username or password"), 0, 0, new AsyncCallback(OnWrite), null); byte[] response = DataParser.getLoginResponse("wrong username or password");
stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null);
} }
} }
else else
{ {
stream.BeginWrite(DataParser.getLoginResponse("invalid json"), 0, 0, new AsyncCallback(OnWrite), null); byte[] response = DataParser.getLoginResponse("invalid json");
stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null);
} }
break; break;
default: default:
Console.WriteLine("default");
Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}"); Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}");
break; break;
} }
byte[] jsonArray = new byte[message.Length - 5]; Array.Copy(message, 5, payloadbytes, 0, message.Length - 5);
Array.Copy(message, 5, jsonArray, 0, message.Length - 5); dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payloadbytes));
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonArray)); saveData.WriteDataJSON(Encoding.ASCII.GetString(payloadbytes));
Console.WriteLine(json);
saveData.WriteDataJSON(Encoding.ASCII.GetString(jsonArray));
} }
else if (DataParser.isRawData(message)) else if (DataParser.isRawData(message))
{ {
Console.WriteLine(message); Console.WriteLine(BitConverter.ToString(message));
saveData.WriteDataRAW(Encoding.ASCII.GetString(message)); saveData.WriteDataRAW(Encoding.ASCII.GetString(message));
} }