From 2139fcf2b2d29590aafbc39cd979fdeb12d87d01 Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 25 Sep 2020 12:51:54 +0200 Subject: [PATCH] removed clientId --- Client/Client.cs | 7 +++--- Client/DataParser.cs | 27 +++++++++++----------- Server/Client.cs | 53 ++++---------------------------------------- 3 files changed, 20 insertions(+), 67 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index e6727d3..64b66cb 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -11,7 +11,6 @@ namespace Client private byte[] buffer = new byte[1024]; private int bytesReceived; private bool connected; - private byte clientId = 0; public Client() : this("localhost", 5555) @@ -41,7 +40,7 @@ namespace Client Console.WriteLine("enter password"); string password = Console.ReadLine(); - byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password), this.clientId); + byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password)); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); @@ -109,7 +108,7 @@ namespace Client { throw new ArgumentNullException("no bytes"); } - byte[] message = DataParser.GetRawDataMessage(bytes, clientId); + byte[] message = DataParser.GetRawDataMessage(bytes); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } @@ -119,7 +118,7 @@ namespace Client { throw new ArgumentNullException("no bytes"); } - byte[] message = DataParser.GetRawDataMessage(bytes, clientId); + byte[] message = DataParser.GetRawDataMessage(bytes); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } diff --git a/Client/DataParser.cs b/Client/DataParser.cs index e9051f3..5300b79 100644 --- a/Client/DataParser.cs +++ b/Client/DataParser.cs @@ -36,7 +36,7 @@ namespace Client /// if it sucseeded public static bool getJsonIdentifier(byte[] bytes, out string identifier) { - if (bytes.Length <= 6) + if (bytes.Length <= 5) { throw new ArgumentException("bytes to short"); } @@ -44,7 +44,7 @@ namespace Client if (messageId == 1) { - dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(6).ToArray())); + dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(5).ToArray())); identifier = json.identifier; return true; } @@ -62,7 +62,7 @@ namespace Client /// if message contains raw data public static bool isRawData(byte[] bytes) { - if (bytes.Length <= 6) + if (bytes.Length <= 5) { throw new ArgumentException("bytes to short"); } @@ -76,14 +76,13 @@ namespace Client /// /// /// the message ready for sending - private static byte[] getMessage(byte[] payload, byte messageId, byte clientId) + private static byte[] getMessage(byte[] payload, byte messageId) { - byte[] res = new byte[payload.Length + 6]; + byte[] res = new byte[payload.Length + 5]; - Array.Copy(BitConverter.GetBytes(payload.Length + 6), 0, res, 0, 4); + Array.Copy(BitConverter.GetBytes(payload.Length + 5), 0, res, 0, 4); res[4] = messageId; - res[5] = clientId; - Array.Copy(payload, 0, res, 6, payload.Length); + Array.Copy(payload, 0, res, 5, payload.Length); return res; } @@ -94,9 +93,9 @@ namespace Client /// /// /// the message ready for sending - public static byte[] GetRawDataMessage(byte[] payload, byte clientId) + public static byte[] GetRawDataMessage(byte[] payload) { - return getMessage(payload, 0x02, clientId); + return getMessage(payload, 0x02); } /// @@ -105,9 +104,9 @@ namespace Client /// /// /// the message ready for sending - public static byte[] getJsonMessage(byte[] payload, byte clientId) + public static byte[] getJsonMessage(byte[] payload) { - return getMessage(payload, 0x01, clientId); + return getMessage(payload, 0x01); } /// @@ -116,9 +115,9 @@ namespace Client /// /// /// the message ready for sending - public static byte[] getJsonMessage(string message, byte clientId) + public static byte[] getJsonMessage(string message) { - return getJsonMessage(Encoding.ASCII.GetBytes(message), clientId); + return getJsonMessage(Encoding.ASCII.GetBytes(message)); } diff --git a/Server/Client.cs b/Server/Client.cs index 37a6800..c836f72 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -13,7 +13,6 @@ namespace Server private NetworkStream stream; private byte[] buffer = new byte[1024]; private byte[] totalBuffer = new byte[1024]; - private int bytesReceived; private int totalBufferReceived = 0; @@ -30,7 +29,6 @@ namespace Server private void OnRead(IAsyncResult ar) { int receivedBytes = this.stream.EndRead(ar); - byte[] lengthBytes = new byte[4]; if (totalBufferReceived + receivedBytes > 1024) { @@ -45,7 +43,7 @@ namespace Server { //volledig packet binnen byte[] packetBytes = new byte[expectedMessageLength]; - Array.Copy(totalBuffer, 6, packetBytes, 0, expectedMessageLength - 6); + Array.Copy(totalBuffer, 5, packetBytes, 0, expectedMessageLength - 5); Console.WriteLine(Encoding.ASCII.GetString(packetBytes) + " " + expectedMessageLength); @@ -57,59 +55,16 @@ namespace Server expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); } - /* - Array.Copy(this.buffer, 0, lengthBytes, 0, 4); - int expectedMessageLength = BitConverter.ToInt32(lengthBytes); - - if (expectedMessageLength > this.buffer.Length) - { - throw new OutOfMemoryException("buffer to small"); - } - - if (expectedMessageLength > this.bytesReceived + receivedBytes) - { - //message hasn't completely arrived yet - this.bytesReceived += receivedBytes; - Console.WriteLine("segmented message, {0} arrived", receivedBytes); - this.stream.BeginRead(this.buffer, this.bytesReceived, this.buffer.Length - this.bytesReceived, new AsyncCallback(OnRead), null); - - } - else - { - //message completely arrived - if (expectedMessageLength != this.bytesReceived + receivedBytes) - { - Console.WriteLine("something has gone completely wrong"); - Console.WriteLine($"expected: {expectedMessageLength} bytesReceive: {bytesReceived} receivedBytes: {receivedBytes}"); - Console.WriteLine($"received WEIRD data {BitConverter.ToString(buffer.Take(receivedBytes).ToArray())} string {Encoding.ASCII.GetString(buffer.Take(receivedBytes).ToArray())}"); - - } - else if (buffer[4] == 0x02) - { - Console.WriteLine($"received raw data {BitConverter.ToString(buffer.Skip(5).Take(expectedMessageLength).ToArray())}"); - } - else if (buffer[4] == 0x01) - { - byte[] packet = new byte[expectedMessageLength]; - Console.WriteLine(Encoding.ASCII.GetString(buffer) + " " + expectedMessageLength); - Array.Copy(buffer, 5, packet, 0, expectedMessageLength - 5); - Console.WriteLine(Encoding.ASCII.GetString(packet)); - HandleData(Encoding.ASCII.GetString(packet)); - } - this.bytesReceived = 0; - - } - */ this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); } - private void HandleData(string packet) + private void HandleData(byte[] message) { - Console.WriteLine("Data " + packet); - JsonConvert.DeserializeObject(packet); + //Console.WriteLine("Data " + packet); + //JsonConvert.DeserializeObject(packet); } } }