From d5eadbe52936c7b1229584b9f3a9d028281c4ab6 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 14:32:07 +0200 Subject: [PATCH 1/4] forgot to stage --- Server/Client.cs | 46 ---------------------------------------------- 1 file changed, 46 deletions(-) diff --git a/Server/Client.cs b/Server/Client.cs index 56e1685..fc29226 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -30,52 +30,6 @@ namespace Server stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); } - //private void OnRead(IAsyncResult ar) - //{ - // try - // { - // int receivedBytes = stream.EndRead(ar); - // } - // catch (IOException) - // { - // communication.Disconnect(this); - // return; - // } - - // int counter = 0; - - // while (buffer.Length > counter) - // { - // //Console.WriteLine(buffer.Length); - // byte[] lenghtBytes = new byte[4]; - // Array.Copy(buffer, counter, lenghtBytes, 0, 4); - // int length = BitConverter.ToInt32(lenghtBytes); - // Console.WriteLine(buffer[5]); - // if (length == 0) - // { - // break; - // } - // else if(buffer[counter+4]==0x02) - // { - - // } - // else if(buffer[counter+4]==0x01) - // { - // byte[] packet = new byte[length]; - // Console.WriteLine(Encoding.ASCII.GetString(buffer)+" "+length); - // Array.Copy(buffer, counter+5, packet, 0, length); - // Console.WriteLine(Encoding.ASCII.GetString(packet)); - // HandleData(Encoding.ASCII.GetString(packet)); - // } - - // counter += length; - // } - - // Console.WriteLine("Done"); - - // stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); - //} - private void OnRead(IAsyncResult ar) { int receivedBytes = this.stream.EndRead(ar); From c782301cf270d306ee49c8f95a710c11d68ce69f Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 15:05:09 +0200 Subject: [PATCH 2/4] connection from bike/simulator to client to server --- Client/Client.cs | 3 ++- Client/Program.cs | 6 ++++++ ProftaakRH/BikeSimulator.cs | 3 ++- Server/Client.cs | 9 ++++++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 26f27ed..86fba5e 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -92,12 +92,13 @@ namespace Client throw new NotImplementedException(); } } + this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); + } private void OnWrite(IAsyncResult ar) { this.stream.EndWrite(ar); - Console.WriteLine("wrote some stuff"); } #region interface diff --git a/Client/Program.cs b/Client/Program.cs index 9598930..60cf68e 100644 --- a/Client/Program.cs +++ b/Client/Program.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using Hardware; +using Hardware.Simulators; using ProftaakRH; namespace Client @@ -24,6 +25,11 @@ namespace Client BLEHandler bLEHandler = new BLEHandler(client); bLEHandler.Connect(); + + //BikeSimulator bikeSimulator = new BikeSimulator(client); + + //bikeSimulator.StartSimulation(); + while (true) { } diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index 86dddc9..304a00d 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -10,7 +10,7 @@ using System.Threading; namespace Hardware.Simulators { - class BikeSimulator : IHandler + public class BikeSimulator : IHandler { IDataReceiver dataReceiver; private int elapsedTime = 0; @@ -34,6 +34,7 @@ namespace Hardware.Simulators { this.dataReceiver = dataReceiver; } + public void StartSimulation() { //Example BLE Message diff --git a/Server/Client.cs b/Server/Client.cs index fc29226..d1d5599 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -48,6 +48,7 @@ namespace Server { //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); } @@ -58,10 +59,12 @@ namespace Server { 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).ToArray(), 16)}"); + Console.WriteLine($"received raw data {BitConverter.ToString(buffer.Skip(5).Take(expectedMessageLength).ToArray())}"); } else if (buffer[4] == 0x01) { @@ -71,8 +74,12 @@ namespace Server 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) From 81dcebb5e88eb43be2a953ad1b185f41d2a6442e Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 15:11:13 +0200 Subject: [PATCH 3/4] added client id to protocol --- Client/Client.cs | 8 +++++--- Client/DataParser.cs | 26 +++++++++++++------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 86fba5e..9eb787d 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -13,6 +13,7 @@ namespace Client private byte[] buffer = new byte[1024]; private int bytesReceived; private bool connected; + private byte clientId = 0; public Client() : this("localhost", 5555) @@ -42,12 +43,13 @@ namespace Client Console.WriteLine("enter password"); string password = Console.ReadLine(); - byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password)); + byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password), this.clientId); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); 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; } @@ -109,7 +111,7 @@ namespace Client { throw new ArgumentNullException("no bytes"); } - byte[] message = DataParser.GetRawDataMessage(bytes); + byte[] message = DataParser.GetRawDataMessage(bytes, clientId); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } @@ -119,7 +121,7 @@ namespace Client { throw new ArgumentNullException("no bytes"); } - byte[] message = DataParser.GetRawDataMessage(bytes); + byte[] message = DataParser.GetRawDataMessage(bytes, clientId); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } diff --git a/Client/DataParser.cs b/Client/DataParser.cs index 4215cf6..137242e 100644 --- a/Client/DataParser.cs +++ b/Client/DataParser.cs @@ -28,7 +28,7 @@ namespace Client public static bool getJsonIdentifier(byte[] bytes, out string identifier) { - if (bytes.Length <= 5) + if (bytes.Length <= 6) { throw new ArgumentException("bytes to short"); } @@ -36,7 +36,7 @@ namespace Client if (messageId == 1) { - dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(5).ToArray())); + dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(6).ToArray())); identifier = json.identifier; return true; } @@ -49,37 +49,37 @@ namespace Client public static bool isRawData(byte[] bytes) { - if (bytes.Length <= 5) + if (bytes.Length <= 6) { throw new ArgumentException("bytes to short"); } return bytes[5] == 0x02; } - private static byte[] getMessage(byte[] payload, byte messageId) + private static byte[] getMessage(byte[] payload, byte messageId, byte clientId) { - byte[] res = new byte[payload.Length + 5]; + byte[] res = new byte[payload.Length + 6]; - Array.Copy(BitConverter.GetBytes(payload.Length + 5), 0, res, 0, 4); + Array.Copy(BitConverter.GetBytes(payload.Length + 6), 0, res, 0, 4); res[4] = messageId; - Array.Copy(payload, 0, res, 5, payload.Length); + Array.Copy(payload, 0, res, 6, payload.Length); return res; } - public static byte[] GetRawDataMessage(byte[] payload) + public static byte[] GetRawDataMessage(byte[] payload, byte clientId) { - return getMessage(payload, 0x02); + return getMessage(payload, 0x02, clientId); } - public static byte[] getJsonMessage(byte[] payload) + public static byte[] getJsonMessage(byte[] payload, byte clientId) { - return getMessage(payload, 0x01); + return getMessage(payload, 0x01, clientId); } - public static byte[] getJsonMessage(string message) + public static byte[] getJsonMessage(string message, byte clientId) { - return getJsonMessage(Encoding.ASCII.GetBytes(message)); + return getJsonMessage(Encoding.ASCII.GetBytes(message), clientId); } From 8c6cb443e3effd33b086843c835523268ed560de Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 23 Sep 2020 15:22:45 +0200 Subject: [PATCH 4/4] deleted stuff --- Client/Client.cs | 2 -- Client/DataParser.cs | 4 ---- Client/Program.cs | 4 ---- Server/Client.cs | 4 ---- Server/Program.cs | 3 +-- 5 files changed, 1 insertion(+), 16 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 9eb787d..e6727d3 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -1,6 +1,4 @@ using System; -using System.Globalization; -using System.Linq; using System.Net.Sockets; using ProftaakRH; diff --git a/Client/DataParser.cs b/Client/DataParser.cs index 137242e..14014d6 100644 --- a/Client/DataParser.cs +++ b/Client/DataParser.cs @@ -1,10 +1,6 @@ using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using System; -using System.Collections.Generic; -using System.Globalization; using System.Linq; -using System.Security.Cryptography; using System.Text; namespace Client diff --git a/Client/Program.cs b/Client/Program.cs index 60cf68e..e98dfc7 100644 --- a/Client/Program.cs +++ b/Client/Program.cs @@ -1,9 +1,5 @@ using System; -using System.Collections.Generic; -using System.Text; using Hardware; -using Hardware.Simulators; -using ProftaakRH; namespace Client { diff --git a/Server/Client.cs b/Server/Client.cs index d1d5599..9dfc946 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -1,11 +1,7 @@ using System; -using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Net; using System.Net.Sockets; using System.Text; -using Newtonsoft; using Newtonsoft.Json; namespace Server diff --git a/Server/Program.cs b/Server/Program.cs index bf66448..cd980a4 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -1,5 +1,4 @@ -using System; -using System.Net; +using System.Net; using System.Net.Sockets; namespace Server