Auto stash before merge of "client" and "origin/client"

This commit is contained in:
shinichi
2020-09-25 15:00:43 +02:00
parent 6ef5bbfe12
commit 9c3b2c3f9b
4 changed files with 112 additions and 13 deletions

View File

@@ -75,7 +75,15 @@ namespace Client
bool isJson = DataParser.getJsonIdentifier(messageBytes, out identifier); bool isJson = DataParser.getJsonIdentifier(messageBytes, out identifier);
if (isJson) if (isJson)
{ {
Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(messageBytes.Skip(5).ToArray())}"); 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)) else if (DataParser.isRawData(messageBytes))
{ {

View File

@@ -1,12 +1,16 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System; using System;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
namespace Client namespace Client
{ {
class DataParser public class DataParser
{ {
public const string LOGIN = "LOGIN";
public const string LOGIN_RESPONSE = "LOGIN_RESPONSE";
/// <summary> /// <summary>
/// makes the json object with LOGIN identifier and username and password /// makes the json object with LOGIN identifier and username and password
/// </summary> /// </summary>
@@ -17,7 +21,7 @@ namespace Client
{ {
dynamic json = new dynamic json = new
{ {
identifier = "LOGIN", identifier = LOGIN,
data = new data = new
{ {
username = mUsername, username = mUsername,
@@ -28,6 +32,38 @@ namespace Client
return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)); 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> /// <summary>
/// get the identifier from json /// get the identifier from json
/// </summary> /// </summary>

View File

@@ -3,6 +3,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
using Client;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace Server namespace Server
@@ -16,6 +17,8 @@ namespace Server
private byte[] totalBuffer = new byte[1024]; private byte[] totalBuffer = new byte[1024];
private int totalBufferReceived = 0; private int totalBufferReceived = 0;
private SaveData saveData; private SaveData saveData;
private string username = null;
public string Username { get; set; } public string Username { get; set; }
@@ -61,6 +64,13 @@ namespace Server
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);
} }
private void OnWrite(IAsyncResult ar)
{
this.stream.EndWrite(ar);
}
/// <summary> /// <summary>
/// TODO /// TODO
/// </summary> /// </summary>
@@ -72,8 +82,44 @@ namespace Server
//0x01 Json //0x01 Json
//0x01 Raw data //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]; byte[] jsonArray = new byte[message.Length - 5];
Array.Copy(message, 5, jsonArray, 0, message.Length - 5); Array.Copy(message, 5, jsonArray, 0, message.Length - 5);
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonArray)); dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonArray));
@@ -81,7 +127,7 @@ namespace Server
saveData.WriteDataJSON(Encoding.ASCII.GetString(jsonArray)); saveData.WriteDataJSON(Encoding.ASCII.GetString(jsonArray));
} }
else if (message[4] == 0x02) else if (DataParser.isRawData(message))
{ {
Console.WriteLine(message); Console.WriteLine(message);
saveData.WriteDataRAW(Encoding.ASCII.GetString(message)); saveData.WriteDataRAW(Encoding.ASCII.GetString(message));
@@ -89,5 +135,10 @@ namespace Server
} }
private bool verifyLogin(string username, string password)
{
return username == password;
}
} }
} }

View File

@@ -1,12 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Client\Client.csproj" />
</ItemGroup>
</Project> </Project>