From 9c3b2c3f9b9855aebb7df63414949714f3c6f01c Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 25 Sep 2020 15:00:43 +0200 Subject: [PATCH] Auto stash before merge of "client" and "origin/client" --- Client/Client.cs | 10 +++++++- Client/DataParser.cs | 40 ++++++++++++++++++++++++++++++-- Server/Client.cs | 55 ++++++++++++++++++++++++++++++++++++++++++-- Server/Server.csproj | 20 +++++++++------- 4 files changed, 112 insertions(+), 13 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 12acd78..023b806 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -75,7 +75,15 @@ namespace Client bool isJson = DataParser.getJsonIdentifier(messageBytes, out identifier); 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)) { diff --git a/Client/DataParser.cs b/Client/DataParser.cs index 57a9b1c..2bc16e5 100644 --- a/Client/DataParser.cs +++ b/Client/DataParser.cs @@ -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"; /// /// makes the json object with LOGIN identifier and username and password /// @@ -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 }); + } + /// /// get the identifier from json /// diff --git a/Server/Client.cs b/Server/Client.cs index 9ff610e..df16fa2 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -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); + } + + /// /// TODO /// @@ -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; + } } } diff --git a/Server/Server.csproj b/Server/Server.csproj index 0eaee1b..a91c176 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -1,12 +1,16 @@ - + - - Exe - netcoreapp3.1 - + + Exe + netcoreapp3.1 + - - - + + + + + + +