login implemented
This commit is contained in:
@@ -36,21 +36,9 @@ namespace Client
|
||||
|
||||
this.stream = this.client.GetStream();
|
||||
|
||||
//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);
|
||||
tryLogin();
|
||||
|
||||
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)
|
||||
@@ -71,23 +59,37 @@ namespace Client
|
||||
byte[] messageBytes = new byte[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;
|
||||
bool isJson = DataParser.getJsonIdentifier(messageBytes, out identifier);
|
||||
if (isJson)
|
||||
{
|
||||
switch (identifier)
|
||||
{
|
||||
case DataParser.LOGIN:
|
||||
Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(messageBytes.Skip(5).ToArray())}");
|
||||
case DataParser.LOGIN_RESPONSE:
|
||||
string responseStatus = DataParser.getResponseStatus(payloadbytes);
|
||||
if (responseStatus == "OK")
|
||||
{
|
||||
this.connected = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"login failed \"{responseStatus}\"");
|
||||
tryLogin();
|
||||
}
|
||||
break;
|
||||
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;
|
||||
}
|
||||
}
|
||||
else if (DataParser.isRawData(messageBytes))
|
||||
{
|
||||
Console.WriteLine($"Received data: {BitConverter.ToString(messageBytes.Skip(5).ToArray())}");
|
||||
Console.WriteLine($"Received data: {BitConverter.ToString(payloadbytes)}");
|
||||
}
|
||||
|
||||
totalBufferReceived -= expectedMessageLength;
|
||||
@@ -131,5 +133,17 @@ namespace Client
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,6 +64,11 @@ namespace Client
|
||||
return getJsonMessage(LOGIN_RESPONSE, new { status = mStatus });
|
||||
}
|
||||
|
||||
public static string getResponseStatus(byte[] json)
|
||||
{
|
||||
return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.status;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// get the identifier from json
|
||||
/// </summary>
|
||||
|
||||
@@ -90,11 +90,9 @@ namespace Server
|
||||
bool isJson = DataParser.getJsonIdentifier(message, out identifier);
|
||||
if (isJson)
|
||||
{
|
||||
Console.WriteLine($"received json with identifier {identifier} and expecting {DataParser.LOGIN}");
|
||||
switch (identifier)
|
||||
{
|
||||
case DataParser.LOGIN:
|
||||
Console.WriteLine("LOGIN");
|
||||
string username;
|
||||
string password;
|
||||
bool worked = DataParser.GetUsernamePassword(payloadbytes, out username, out password);
|
||||
@@ -103,33 +101,33 @@ namespace Server
|
||||
if (verifyLogin(username, password))
|
||||
{
|
||||
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
|
||||
{
|
||||
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
|
||||
{
|
||||
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;
|
||||
default:
|
||||
Console.WriteLine("default");
|
||||
Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}");
|
||||
break;
|
||||
}
|
||||
byte[] jsonArray = new byte[message.Length - 5];
|
||||
Array.Copy(message, 5, jsonArray, 0, message.Length - 5);
|
||||
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonArray));
|
||||
Console.WriteLine(json);
|
||||
saveData.WriteDataJSON(Encoding.ASCII.GetString(jsonArray));
|
||||
Array.Copy(message, 5, payloadbytes, 0, message.Length - 5);
|
||||
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payloadbytes));
|
||||
saveData.WriteDataJSON(Encoding.ASCII.GetString(payloadbytes));
|
||||
|
||||
}
|
||||
else if (DataParser.isRawData(message))
|
||||
{
|
||||
Console.WriteLine(message);
|
||||
Console.WriteLine(BitConverter.ToString(message));
|
||||
saveData.WriteDataRAW(Encoding.ASCII.GetString(message));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user