Merge branch 'client' into develop
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using ProftaakRH;
|
||||
|
||||
namespace Client
|
||||
@@ -9,9 +11,9 @@ namespace Client
|
||||
private TcpClient client;
|
||||
private NetworkStream stream;
|
||||
private byte[] buffer = new byte[1024];
|
||||
private int bytesReceived;
|
||||
private bool connected;
|
||||
private byte clientId = 0;
|
||||
private byte[] totalBuffer = new byte[1024];
|
||||
private int totalBufferReceived = 0;
|
||||
|
||||
|
||||
public Client() : this("localhost", 5555)
|
||||
@@ -22,7 +24,6 @@ namespace Client
|
||||
public Client(string adress, int port)
|
||||
{
|
||||
this.client = new TcpClient();
|
||||
this.bytesReceived = 0;
|
||||
this.connected = false;
|
||||
client.BeginConnect(adress, port, new AsyncCallback(OnConnect), null);
|
||||
}
|
||||
@@ -35,63 +36,66 @@ namespace Client
|
||||
|
||||
this.stream = this.client.GetStream();
|
||||
|
||||
//TODO File in lezen
|
||||
Console.WriteLine("enter username");
|
||||
string username = Console.ReadLine();
|
||||
Console.WriteLine("enter password");
|
||||
string password = Console.ReadLine();
|
||||
|
||||
byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password), this.clientId);
|
||||
|
||||
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
|
||||
tryLogin();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private void OnRead(IAsyncResult ar)
|
||||
{
|
||||
int receivedBytes = this.stream.EndRead(ar);
|
||||
byte[] lengthBytes = new byte[4];
|
||||
|
||||
Array.Copy(this.buffer, 0, lengthBytes, 0, 4);
|
||||
|
||||
int expectedMessageLength = BitConverter.ToInt32(lengthBytes);
|
||||
|
||||
if (expectedMessageLength > this.buffer.Length)
|
||||
if (totalBufferReceived + receivedBytes > 1024)
|
||||
{
|
||||
throw new OutOfMemoryException("buffer to small");
|
||||
throw new OutOfMemoryException("buffer too small");
|
||||
}
|
||||
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, receivedBytes);
|
||||
totalBufferReceived += receivedBytes;
|
||||
|
||||
if (expectedMessageLength > this.bytesReceived + receivedBytes)
|
||||
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
while (totalBufferReceived >= expectedMessageLength)
|
||||
{
|
||||
//message hasn't completely arrived yet
|
||||
this.bytesReceived += receivedBytes;
|
||||
this.stream.BeginRead(this.buffer, this.bytesReceived, this.buffer.Length - this.bytesReceived, new AsyncCallback(OnRead), null);
|
||||
//volledig packet binnen
|
||||
byte[] messageBytes = new byte[expectedMessageLength];
|
||||
Array.Copy(totalBuffer, 0, messageBytes, 0, expectedMessageLength);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//message completely arrived
|
||||
if (expectedMessageLength != this.bytesReceived + receivedBytes)
|
||||
{
|
||||
Console.WriteLine("something has gone completely wrong");
|
||||
}
|
||||
|
||||
byte[] payloadbytes = new byte[BitConverter.ToInt32(messageBytes, 0) - 5];
|
||||
|
||||
Array.Copy(messageBytes, 5, payloadbytes, 0, payloadbytes.Length);
|
||||
|
||||
string identifier;
|
||||
bool isJson = DataParser.getJsonIdentifier(this.buffer, out identifier);
|
||||
bool isJson = DataParser.getJsonIdentifier(messageBytes, out identifier);
|
||||
if (isJson)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
switch (identifier)
|
||||
{
|
||||
case DataParser.LOGIN_RESPONSE:
|
||||
string responseStatus = DataParser.getResponseStatus(payloadbytes);
|
||||
if (responseStatus == "OK")
|
||||
{
|
||||
this.connected = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"login failed \"{responseStatus}\"");
|
||||
tryLogin();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (DataParser.isRawData(this.buffer))
|
||||
else if (DataParser.isRawData(messageBytes))
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
Console.WriteLine($"Received data: {BitConverter.ToString(payloadbytes)}");
|
||||
}
|
||||
|
||||
totalBufferReceived -= expectedMessageLength;
|
||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
}
|
||||
|
||||
this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null);
|
||||
|
||||
}
|
||||
@@ -109,7 +113,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 +123,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);
|
||||
}
|
||||
|
||||
@@ -129,5 +133,17 @@ namespace Client
|
||||
{
|
||||
return this.connected;
|
||||
}
|
||||
private void tryLogin()
|
||||
{
|
||||
//TODO File in lezen
|
||||
Console.WriteLine("enter username");
|
||||
string username = Console.ReadLine();
|
||||
Console.WriteLine("enter password");
|
||||
string password = Console.ReadLine();
|
||||
|
||||
byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password));
|
||||
|
||||
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,27 @@
|
||||
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";
|
||||
/// <summary>
|
||||
/// makes the json object with LOGIN identifier and username and password
|
||||
/// </summary>
|
||||
/// <param name="mUsername">username</param>
|
||||
/// <param name="mPassword">password</param>
|
||||
/// <returns>json object to ASCII to bytes</returns>
|
||||
public static byte[] GetLoginJson(string mUsername, string mPassword)
|
||||
{
|
||||
dynamic json = new
|
||||
{
|
||||
identifier = "LOGIN",
|
||||
identifier = LOGIN,
|
||||
data = new
|
||||
{
|
||||
username = mUsername,
|
||||
@@ -22,17 +32,60 @@ 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 });
|
||||
}
|
||||
|
||||
public static string getResponseStatus(byte[] json)
|
||||
{
|
||||
return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.status;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// get the identifier from json
|
||||
/// </summary>
|
||||
/// <param name="bytes">json in ASCII</param>
|
||||
/// <param name="identifier">gets the identifier</param>
|
||||
/// <returns>if it sucseeded</returns>
|
||||
public static bool getJsonIdentifier(byte[] bytes, out string identifier)
|
||||
{
|
||||
if (bytes.Length <= 6)
|
||||
if (bytes.Length <= 5)
|
||||
{
|
||||
throw new ArgumentException("bytes to short");
|
||||
}
|
||||
byte messageId = bytes[4];
|
||||
|
||||
if (messageId == 1)
|
||||
if (messageId == 0x01)
|
||||
{
|
||||
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;
|
||||
}
|
||||
@@ -43,39 +96,69 @@ namespace Client
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// checks if the de message is raw data according to the protocol
|
||||
/// </summary>
|
||||
/// <param name="bytes">message</param>
|
||||
/// <returns>if message contains raw data</returns>
|
||||
public static bool isRawData(byte[] bytes)
|
||||
{
|
||||
if (bytes.Length <= 6)
|
||||
if (bytes.Length <= 5)
|
||||
{
|
||||
throw new ArgumentException("bytes to short");
|
||||
}
|
||||
return bytes[5] == 0x02;
|
||||
return bytes[4] == 0x02;
|
||||
}
|
||||
|
||||
private static byte[] getMessage(byte[] payload, byte messageId, byte clientId)
|
||||
/// <summary>
|
||||
/// constructs a message with the payload, messageId and clientId
|
||||
/// </summary>
|
||||
/// <param name="payload"></param>
|
||||
/// <param name="messageId"></param>
|
||||
/// <param name="clientId"></param>
|
||||
/// <returns>the message ready for sending</returns>
|
||||
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;
|
||||
Array.Copy(payload, 0, res, 6, payload.Length);
|
||||
Array.Copy(payload, 0, res, 5, payload.Length);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public static byte[] GetRawDataMessage(byte[] payload, byte clientId)
|
||||
/// <summary>
|
||||
/// constructs a message with the payload and clientId and assumes the payload is raw data
|
||||
/// </summary>
|
||||
/// <param name="payload"></param>
|
||||
/// <param name="clientId"></param>
|
||||
/// <returns>the message ready for sending</returns>
|
||||
public static byte[] GetRawDataMessage(byte[] payload)
|
||||
{
|
||||
return getMessage(payload, 0x02, clientId);
|
||||
return getMessage(payload, 0x02);
|
||||
}
|
||||
|
||||
public static byte[] getJsonMessage(byte[] payload, byte clientId)
|
||||
/// <summary>
|
||||
/// constructs a message with the payload and clientId and assumes the payload is json
|
||||
/// </summary>
|
||||
/// <param name="payload"></param>
|
||||
/// <param name="clientId"></param>
|
||||
/// <returns>the message ready for sending</returns>
|
||||
public static byte[] getJsonMessage(byte[] payload)
|
||||
{
|
||||
return getMessage(payload, 0x01, clientId);
|
||||
return getMessage(payload, 0x01);
|
||||
}
|
||||
|
||||
public static byte[] getJsonMessage(string message, byte clientId)
|
||||
/// <summary>
|
||||
/// constructs a message with the message and clientId
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="clientId"></param>
|
||||
/// <returns>the message ready for sending</returns>
|
||||
public static byte[] getJsonMessage(string message)
|
||||
{
|
||||
return getJsonMessage(Encoding.ASCII.GetBytes(message), clientId);
|
||||
return getJsonMessage(Encoding.ASCII.GetBytes(message));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using Hardware;
|
||||
using Hardware.Simulators;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
@@ -18,13 +19,13 @@ namespace Client
|
||||
{
|
||||
|
||||
}
|
||||
BLEHandler bLEHandler = new BLEHandler(client);
|
||||
//BLEHandler bLEHandler = new BLEHandler(client);
|
||||
|
||||
bLEHandler.Connect();
|
||||
//bLEHandler.Connect();
|
||||
|
||||
//BikeSimulator bikeSimulator = new BikeSimulator(client);
|
||||
BikeSimulator bikeSimulator = new BikeSimulator(client);
|
||||
|
||||
//bikeSimulator.StartSimulation();
|
||||
bikeSimulator.StartSimulation();
|
||||
|
||||
while (true)
|
||||
{
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace Hardware
|
||||
/// </summary>
|
||||
public class BLEHandler
|
||||
{
|
||||
IDataReceiver dataReceiver;
|
||||
List<IDataReceiver> dataReceivers;
|
||||
private BLE bleBike;
|
||||
private BLE bleHeart;
|
||||
public bool Running { get; set; }
|
||||
@@ -24,7 +24,17 @@ namespace Hardware
|
||||
/// <param name="dataReceiver">the dataconverter object</param>
|
||||
public BLEHandler(IDataReceiver dataReceiver)
|
||||
{
|
||||
this.dataReceiver = dataReceiver;
|
||||
this.dataReceivers = new List<IDataReceiver> { dataReceiver };
|
||||
}
|
||||
|
||||
public BLEHandler(List<IDataReceiver> dataReceivers)
|
||||
{
|
||||
this.dataReceivers = dataReceivers;
|
||||
}
|
||||
|
||||
public void addDataReceiver(IDataReceiver dataReceiver)
|
||||
{
|
||||
this.dataReceivers.Add(dataReceiver);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -125,11 +135,17 @@ namespace Hardware
|
||||
{
|
||||
byte[] payload = new byte[8];
|
||||
Array.Copy(e.Data, 4, payload, 0, 8);
|
||||
this.dataReceiver.Bike(payload);
|
||||
foreach (IDataReceiver dataReceiver in this.dataReceivers)
|
||||
{
|
||||
dataReceiver.Bike(payload);
|
||||
}
|
||||
}
|
||||
else if (e.ServiceName == "00002a37-0000-1000-8000-00805f9b34fb")
|
||||
{
|
||||
this.dataReceiver.BPM(e.Data);
|
||||
foreach (IDataReceiver dataReceiver in this.dataReceivers)
|
||||
{
|
||||
dataReceiver.BPM(e.Data);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace Hardware.Simulators
|
||||
{
|
||||
public class BikeSimulator : IHandler
|
||||
{
|
||||
IDataReceiver dataReceiver;
|
||||
List<IDataReceiver> dataReceivers;
|
||||
private int elapsedTime = 0;
|
||||
private int eventCounter = 0;
|
||||
private double distanceTraveled = 0;
|
||||
@@ -32,7 +32,17 @@ namespace Hardware.Simulators
|
||||
|
||||
public BikeSimulator(IDataReceiver dataReceiver)
|
||||
{
|
||||
this.dataReceiver = dataReceiver;
|
||||
this.dataReceivers = new List<IDataReceiver> { dataReceiver };
|
||||
}
|
||||
|
||||
public BikeSimulator(List<IDataReceiver> dataReceivers)
|
||||
{
|
||||
this.dataReceivers = dataReceivers;
|
||||
}
|
||||
|
||||
public void addDataReceiver(IDataReceiver dataReceiver)
|
||||
{
|
||||
this.dataReceivers.Add(dataReceiver);
|
||||
}
|
||||
|
||||
public void StartSimulation()
|
||||
@@ -50,9 +60,12 @@ namespace Hardware.Simulators
|
||||
CalculateVariables(improvedPerlin.GetValue(x) + 1);
|
||||
|
||||
//Simulate sending data
|
||||
dataReceiver.Bike(GenerateBike0x19());
|
||||
dataReceiver.Bike(GenerateBike0x10());
|
||||
dataReceiver.BPM(GenerateHeart());
|
||||
foreach (IDataReceiver dataReceiver in this.dataReceivers)
|
||||
{
|
||||
dataReceiver.Bike(GenerateBike0x19());
|
||||
dataReceiver.Bike(GenerateBike0x10());
|
||||
dataReceiver.BPM(GenerateHeart());
|
||||
}
|
||||
|
||||
Thread.Sleep(1000);
|
||||
|
||||
|
||||
142
Server/Client.cs
142
Server/Client.cs
@@ -1,7 +1,9 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using Client;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Server
|
||||
@@ -13,13 +15,18 @@ namespace Server
|
||||
private NetworkStream stream;
|
||||
private byte[] buffer = new byte[1024];
|
||||
private byte[] totalBuffer = new byte[1024];
|
||||
private int bytesReceived;
|
||||
private int totalBufferReceived = 0;
|
||||
private SaveData saveData;
|
||||
private string username = null;
|
||||
private DateTime sessionStart;
|
||||
|
||||
|
||||
|
||||
public string Username { get; set; }
|
||||
|
||||
public Client(Communication communication, TcpClient tcpClient)
|
||||
{
|
||||
this.sessionStart = DateTime.Now;
|
||||
this.communication = communication;
|
||||
this.tcpClient = tcpClient;
|
||||
this.stream = this.tcpClient.GetStream();
|
||||
@@ -29,59 +36,118 @@ namespace Server
|
||||
private void OnRead(IAsyncResult ar)
|
||||
{
|
||||
int receivedBytes = this.stream.EndRead(ar);
|
||||
byte[] lengthBytes = new byte[4];
|
||||
|
||||
Array.Copy(this.buffer, 0, lengthBytes, 0, 4);
|
||||
|
||||
int expectedMessageLength = BitConverter.ToInt32(lengthBytes);
|
||||
|
||||
if (expectedMessageLength > this.buffer.Length)
|
||||
if (totalBufferReceived + receivedBytes > 1024)
|
||||
{
|
||||
throw new OutOfMemoryException("buffer to small");
|
||||
throw new OutOfMemoryException("buffer too small");
|
||||
}
|
||||
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, receivedBytes);
|
||||
totalBufferReceived += receivedBytes;
|
||||
|
||||
if (expectedMessageLength > this.bytesReceived + receivedBytes)
|
||||
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
while (totalBufferReceived >= expectedMessageLength)
|
||||
{
|
||||
//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);
|
||||
//volledig packet binnen
|
||||
byte[] messageBytes = new byte[expectedMessageLength];
|
||||
Array.Copy(totalBuffer, 0, messageBytes, 0, expectedMessageLength);
|
||||
HandleData(messageBytes);
|
||||
|
||||
}
|
||||
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())}");
|
||||
Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength)); //maybe unsafe idk
|
||||
|
||||
}
|
||||
else if (buffer[4] == 0x02)
|
||||
totalBufferReceived -= expectedMessageLength;
|
||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
if (expectedMessageLength <= 5)
|
||||
{
|
||||
Console.WriteLine($"received raw data {BitConverter.ToString(buffer.Skip(5).Take(expectedMessageLength).ToArray())}");
|
||||
break;
|
||||
}
|
||||
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 OnWrite(IAsyncResult ar)
|
||||
{
|
||||
Console.WriteLine("Data " + packet);
|
||||
JsonConvert.DeserializeObject(packet);
|
||||
this.stream.EndWrite(ar);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// TODO
|
||||
/// </summary>
|
||||
/// <param name="message">including message length and messageId (can be changed)</param>
|
||||
private void HandleData(byte[] message)
|
||||
{
|
||||
//Console.WriteLine("Data " + packet);
|
||||
//JsonConvert.DeserializeObject(packet);
|
||||
//0x01 Json
|
||||
//0x01 Raw data
|
||||
|
||||
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)
|
||||
{
|
||||
switch (identifier)
|
||||
{
|
||||
case DataParser.LOGIN:
|
||||
string username;
|
||||
string password;
|
||||
bool worked = DataParser.GetUsernamePassword(payloadbytes, out username, out password);
|
||||
if (worked)
|
||||
{
|
||||
if (verifyLogin(username, password))
|
||||
{
|
||||
Console.WriteLine("Log in");
|
||||
this.username = username;
|
||||
byte[] response = DataParser.getLoginResponse("OK");
|
||||
stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null);
|
||||
this.saveData = new SaveData(Directory.GetCurrentDirectory() + "/" + username, sessionStart.ToString("yyyy-MM-dd HH-mm-ss"));
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] response = DataParser.getLoginResponse("wrong username or password");
|
||||
stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] response = DataParser.getLoginResponse("invalid json");
|
||||
stream.BeginWrite(response, 0, response.Length, new AsyncCallback(OnWrite), null);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}");
|
||||
break;
|
||||
}
|
||||
Array.Copy(message, 5, payloadbytes, 0, message.Length - 5);
|
||||
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payloadbytes));
|
||||
saveData.WriteDataJSON(Encoding.ASCII.GetString(payloadbytes));
|
||||
|
||||
}
|
||||
else if (DataParser.isRawData(message))
|
||||
{
|
||||
Console.WriteLine(BitConverter.ToString(message));
|
||||
saveData.WriteDataRAW(ByteArrayToString(message));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private bool verifyLogin(string username, string password)
|
||||
{
|
||||
return username == password;
|
||||
}
|
||||
|
||||
public static string ByteArrayToString(byte[] ba)
|
||||
{
|
||||
StringBuilder hex = new StringBuilder(ba.Length * 2);
|
||||
foreach (byte b in ba)
|
||||
hex.AppendFormat("{0:x2}", b);
|
||||
return hex.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,9 @@ namespace Server
|
||||
public void Start()
|
||||
{
|
||||
listener.Start();
|
||||
Console.WriteLine($"==========================================================================\n" +
|
||||
$"\tstarted accepting clients at {DateTime.Now}\n" +
|
||||
$"==========================================================================");
|
||||
listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null);
|
||||
}
|
||||
|
||||
|
||||
42
Server/SaveData.cs
Normal file
42
Server/SaveData.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
class SaveData
|
||||
{
|
||||
private string path;
|
||||
private string filename;
|
||||
public SaveData(string path, string filename)
|
||||
{
|
||||
this.path = path;
|
||||
this.filename = filename;
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
Directory.CreateDirectory(path);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Every line is a new data entry
|
||||
/// </summary>
|
||||
|
||||
public void WriteDataJSON(string data)
|
||||
{
|
||||
using (StreamWriter sw = File.AppendText(this.path + "/json"+filename+".txt"))
|
||||
{
|
||||
sw.WriteLine(data);
|
||||
}
|
||||
}
|
||||
|
||||
public void WriteDataRAW(string data)
|
||||
{
|
||||
using (StreamWriter sw = File.AppendText(this.path + "/raw" + filename + ".txt"))
|
||||
{
|
||||
sw.WriteLine(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Client\Client.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user