Auto stash before merge of "client" and "origin/client"
This commit is contained in:
@@ -75,7 +75,15 @@ namespace Client
|
||||
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())}");
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(messageBytes.Skip(5).ToArray())}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (DataParser.isRawData(messageBytes))
|
||||
{
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
class DataParser
|
||||
public class DataParser
|
||||
{
|
||||
public const string LOGIN = "LOGIN";
|
||||
public const string LOGIN_RESPONSE = "LOGIN_RESPONSE";
|
||||
/// <summary>
|
||||
/// makes the json object with LOGIN identifier and username and password
|
||||
/// </summary>
|
||||
@@ -17,7 +21,7 @@ namespace Client
|
||||
{
|
||||
dynamic json = new
|
||||
{
|
||||
identifier = "LOGIN",
|
||||
identifier = LOGIN,
|
||||
data = new
|
||||
{
|
||||
username = mUsername,
|
||||
@@ -28,6 +32,38 @@ namespace Client
|
||||
return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json));
|
||||
}
|
||||
|
||||
public static bool GetUsernamePassword(byte[] jsonbytes, out string username, out string password)
|
||||
{
|
||||
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonbytes));
|
||||
try
|
||||
{
|
||||
username = json.data.username;
|
||||
password = json.data.password;
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
username = null;
|
||||
password = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] getJsonMessage(string mIdentifier, dynamic data)
|
||||
{
|
||||
dynamic json = new
|
||||
{
|
||||
identifier = mIdentifier,
|
||||
data
|
||||
};
|
||||
return getMessage(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)), 0x01);
|
||||
}
|
||||
|
||||
public static byte[] getLoginResponse(string mStatus)
|
||||
{
|
||||
return getJsonMessage(LOGIN_RESPONSE, new { status = mStatus });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// get the identifier from json
|
||||
/// </summary>
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using Client;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Server
|
||||
@@ -16,6 +17,8 @@ namespace Server
|
||||
private byte[] totalBuffer = new byte[1024];
|
||||
private int totalBufferReceived = 0;
|
||||
private SaveData saveData;
|
||||
private string username = null;
|
||||
|
||||
|
||||
|
||||
public string Username { get; set; }
|
||||
@@ -61,6 +64,13 @@ namespace Server
|
||||
this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null);
|
||||
|
||||
}
|
||||
|
||||
private void OnWrite(IAsyncResult ar)
|
||||
{
|
||||
this.stream.EndWrite(ar);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// TODO
|
||||
/// </summary>
|
||||
@@ -72,8 +82,44 @@ namespace Server
|
||||
//0x01 Json
|
||||
//0x01 Raw data
|
||||
|
||||
if (message[4] == 0x01)
|
||||
byte[] payloadbytes = new byte[BitConverter.ToInt32(message, 0) - 5];
|
||||
|
||||
Array.Copy(message, 5, payloadbytes, 0, payloadbytes.Length);
|
||||
|
||||
string identifier;
|
||||
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);
|
||||
if (worked)
|
||||
{
|
||||
if (verifyLogin(username, password))
|
||||
{
|
||||
this.username = username;
|
||||
stream.BeginWrite(DataParser.getLoginResponse("OK"), 0, 0, new AsyncCallback(OnWrite), null);
|
||||
}
|
||||
else
|
||||
{
|
||||
stream.BeginWrite(DataParser.getLoginResponse("wrong username or password"), 0, 0, new AsyncCallback(OnWrite), null);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
stream.BeginWrite(DataParser.getLoginResponse("invalid json"), 0, 0, 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));
|
||||
@@ -81,7 +127,7 @@ namespace Server
|
||||
saveData.WriteDataJSON(Encoding.ASCII.GetString(jsonArray));
|
||||
|
||||
}
|
||||
else if (message[4] == 0x02)
|
||||
else if (DataParser.isRawData(message))
|
||||
{
|
||||
Console.WriteLine(message);
|
||||
saveData.WriteDataRAW(Encoding.ASCII.GetString(message));
|
||||
@@ -89,5 +135,10 @@ namespace Server
|
||||
|
||||
|
||||
}
|
||||
|
||||
private bool verifyLogin(string username, string password)
|
||||
{
|
||||
return username == password;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
@@ -9,4 +9,8 @@
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Client\Client.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user