Merge remote-tracking branch 'origin/client' into write

This commit is contained in:
fabjuuuh
2020-09-23 15:38:16 +02:00
6 changed files with 31 additions and 28 deletions

View File

@@ -1,6 +1,4 @@
using System; using System;
using System.Globalization;
using System.Linq;
using System.Net.Sockets; using System.Net.Sockets;
using ProftaakRH; using ProftaakRH;
@@ -13,6 +11,7 @@ namespace Client
private byte[] buffer = new byte[1024]; private byte[] buffer = new byte[1024];
private int bytesReceived; private int bytesReceived;
private bool connected; private bool connected;
private byte clientId = 0;
public Client() : this("localhost", 5555) public Client() : this("localhost", 5555)
@@ -42,12 +41,13 @@ namespace Client
Console.WriteLine("enter password"); Console.WriteLine("enter password");
string password = Console.ReadLine(); 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.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
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);
//TODO lees OK message
//temp moet eigenlijk een ok bericht ontvangen //temp moet eigenlijk een ok bericht ontvangen
this.connected = true; this.connected = true;
} }
@@ -92,12 +92,13 @@ namespace Client
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null);
} }
private void OnWrite(IAsyncResult ar) private void OnWrite(IAsyncResult ar)
{ {
this.stream.EndWrite(ar); this.stream.EndWrite(ar);
Console.WriteLine("wrote some stuff");
} }
#region interface #region interface
@@ -108,7 +109,7 @@ namespace Client
{ {
throw new ArgumentNullException("no bytes"); 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); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
} }
@@ -118,7 +119,7 @@ namespace Client
{ {
throw new ArgumentNullException("no bytes"); 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); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
} }

View File

@@ -1,10 +1,6 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Security.Cryptography;
using System.Text; using System.Text;
namespace Client namespace Client
@@ -28,7 +24,7 @@ namespace Client
public static bool getJsonIdentifier(byte[] bytes, out string identifier) public static bool getJsonIdentifier(byte[] bytes, out string identifier)
{ {
if (bytes.Length <= 5) if (bytes.Length <= 6)
{ {
throw new ArgumentException("bytes to short"); throw new ArgumentException("bytes to short");
} }
@@ -36,7 +32,7 @@ namespace Client
if (messageId == 1) 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; identifier = json.identifier;
return true; return true;
} }
@@ -49,7 +45,7 @@ namespace Client
public static bool isRawData(byte[] bytes) public static bool isRawData(byte[] bytes)
{ {
if (bytes.Length <= 5) if (bytes.Length <= 6)
{ {
throw new ArgumentException("bytes to short"); throw new ArgumentException("bytes to short");
} }
@@ -67,19 +63,19 @@ namespace Client
return res; 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);
} }

View File

@@ -1,8 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Text;
using Hardware; using Hardware;
using ProftaakRH;
namespace Client namespace Client
{ {
@@ -24,6 +21,11 @@ namespace Client
BLEHandler bLEHandler = new BLEHandler(client); BLEHandler bLEHandler = new BLEHandler(client);
bLEHandler.Connect(); bLEHandler.Connect();
//BikeSimulator bikeSimulator = new BikeSimulator(client);
//bikeSimulator.StartSimulation();
while (true) while (true)
{ {
} }

View File

@@ -10,7 +10,7 @@ using System.Threading;
namespace Hardware.Simulators namespace Hardware.Simulators
{ {
class BikeSimulator : IHandler public class BikeSimulator : IHandler
{ {
IDataReceiver dataReceiver; IDataReceiver dataReceiver;
private int elapsedTime = 0; private int elapsedTime = 0;
@@ -34,6 +34,7 @@ namespace Hardware.Simulators
{ {
this.dataReceiver = dataReceiver; this.dataReceiver = dataReceiver;
} }
public void StartSimulation() public void StartSimulation()
{ {
//Example BLE Message //Example BLE Message

View File

@@ -1,8 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
using Client; using Client;
@@ -94,6 +91,7 @@ namespace Server
{ {
//message hasn't completely arrived yet //message hasn't completely arrived yet
this.bytesReceived += receivedBytes; 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); this.stream.BeginRead(this.buffer, this.bytesReceived, this.buffer.Length - this.bytesReceived, new AsyncCallback(OnRead), null);
} }
@@ -104,6 +102,8 @@ namespace Server
{ {
Console.WriteLine("something has gone completely wrong"); Console.WriteLine("something has gone completely wrong");
Console.WriteLine($"expected: {expectedMessageLength} bytesReceive: {bytesReceived} receivedBytes: {receivedBytes}"); 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) else if (buffer[4] == 0x02)
{ {
@@ -117,8 +117,12 @@ namespace Server
Console.WriteLine(Encoding.ASCII.GetString(packet)); Console.WriteLine(Encoding.ASCII.GetString(packet));
HandleData(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(string packet)

View File

@@ -1,5 +1,4 @@
using System; using System.Net;
using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
namespace Server namespace Server