Compare commits
5 Commits
connect-vr
...
write
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b9ad2540c | ||
|
|
5a425bf19b | ||
|
|
8e24274261 | ||
|
|
8c45d5eccd | ||
|
|
5db3f28deb |
121
Client/Client.cs
121
Client/Client.cs
@@ -1,7 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using ProftaakRH;
|
||||
|
||||
namespace Client
|
||||
@@ -11,10 +9,9 @@ namespace Client
|
||||
private TcpClient client;
|
||||
private NetworkStream stream;
|
||||
private byte[] buffer = new byte[1024];
|
||||
private int bytesReceived;
|
||||
private bool connected;
|
||||
private byte[] totalBuffer = new byte[1024];
|
||||
private int totalBufferReceived = 0;
|
||||
private EngineConnection engineConnection;
|
||||
private byte clientId = 0;
|
||||
|
||||
|
||||
public Client() : this("localhost", 5555)
|
||||
@@ -25,26 +22,11 @@ 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);
|
||||
}
|
||||
|
||||
private void initEngine()
|
||||
{
|
||||
engineConnection = EngineConnection.INSTANCE;
|
||||
engineConnection.OnNoTunnelId = retryEngineConnection;
|
||||
if (!engineConnection.Connected) engineConnection.Connect();
|
||||
}
|
||||
|
||||
private void retryEngineConnection()
|
||||
{
|
||||
Console.WriteLine("-- Could not connect to the VR engine. Please make sure you are running the simulation!");
|
||||
Console.WriteLine("-- Press any key to retry connecting to the VR engine.");
|
||||
Console.ReadKey();
|
||||
|
||||
engineConnection.CreateConnection();
|
||||
}
|
||||
|
||||
private void OnConnect(IAsyncResult ar)
|
||||
{
|
||||
this.client.EndConnect(ar);
|
||||
@@ -53,67 +35,64 @@ namespace Client
|
||||
|
||||
this.stream = this.client.GetStream();
|
||||
|
||||
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.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;
|
||||
}
|
||||
|
||||
private void OnRead(IAsyncResult ar)
|
||||
{
|
||||
int receivedBytes = this.stream.EndRead(ar);
|
||||
byte[] lengthBytes = new byte[4];
|
||||
|
||||
if (totalBufferReceived + receivedBytes > 1024)
|
||||
Array.Copy(this.buffer, 0, lengthBytes, 0, 4);
|
||||
|
||||
int expectedMessageLength = BitConverter.ToInt32(lengthBytes);
|
||||
|
||||
if (expectedMessageLength > this.buffer.Length)
|
||||
{
|
||||
throw new OutOfMemoryException("buffer too small");
|
||||
throw new OutOfMemoryException("buffer to small");
|
||||
}
|
||||
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, receivedBytes);
|
||||
totalBufferReceived += receivedBytes;
|
||||
|
||||
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
while (totalBufferReceived >= expectedMessageLength)
|
||||
if (expectedMessageLength > this.bytesReceived + receivedBytes)
|
||||
{
|
||||
//volledig packet binnen
|
||||
byte[] messageBytes = new byte[expectedMessageLength];
|
||||
Array.Copy(totalBuffer, 0, messageBytes, 0, 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);
|
||||
|
||||
|
||||
byte[] payloadbytes = new byte[BitConverter.ToInt32(messageBytes, 0) - 5];
|
||||
|
||||
Array.Copy(messageBytes, 5, payloadbytes, 0, payloadbytes.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
//message completely arrived
|
||||
if (expectedMessageLength != this.bytesReceived + receivedBytes)
|
||||
{
|
||||
Console.WriteLine("something has gone completely wrong");
|
||||
}
|
||||
|
||||
string identifier;
|
||||
bool isJson = DataParser.getJsonIdentifier(messageBytes, out identifier);
|
||||
bool isJson = DataParser.getJsonIdentifier(this.buffer, out identifier);
|
||||
if (isJson)
|
||||
{
|
||||
switch (identifier)
|
||||
{
|
||||
case DataParser.LOGIN_RESPONSE:
|
||||
string responseStatus = DataParser.getResponseStatus(payloadbytes);
|
||||
if (responseStatus == "OK")
|
||||
{
|
||||
this.connected = true;
|
||||
initEngine();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"login failed \"{responseStatus}\"");
|
||||
tryLogin();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}");
|
||||
break;
|
||||
}
|
||||
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
else if (DataParser.isRawData(messageBytes))
|
||||
else if (DataParser.isRawData(this.buffer))
|
||||
{
|
||||
Console.WriteLine($"Received data: {BitConverter.ToString(payloadbytes)}");
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
totalBufferReceived -= expectedMessageLength;
|
||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
}
|
||||
|
||||
this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null);
|
||||
|
||||
}
|
||||
@@ -131,7 +110,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);
|
||||
}
|
||||
|
||||
@@ -141,7 +120,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);
|
||||
}
|
||||
|
||||
@@ -151,21 +130,5 @@ 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();
|
||||
|
||||
string hashUser = Hashing.Hasher.HashString(username);
|
||||
string hashPassword = Hashing.Hasher.HashString(password);
|
||||
|
||||
byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(hashUser, hashPassword));
|
||||
|
||||
|
||||
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,9 +12,6 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Message\Message.csproj" />
|
||||
<ProjectReference Include="..\ProftaakRH\ProftaakRH.csproj" />
|
||||
<ProjectReference Include="..\RH-Engine\RH-Engine.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<Import Project="..\Hashing\Hashing.projitems" Label="Shared" />
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
@@ -9,8 +7,6 @@ namespace Client
|
||||
{
|
||||
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>
|
||||
@@ -21,7 +17,7 @@ namespace Client
|
||||
{
|
||||
dynamic json = new
|
||||
{
|
||||
identifier = LOGIN,
|
||||
identifier = "LOGIN",
|
||||
data = new
|
||||
{
|
||||
username = mUsername,
|
||||
@@ -32,43 +28,6 @@ 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>
|
||||
@@ -77,15 +36,15 @@ namespace Client
|
||||
/// <returns>if it sucseeded</returns>
|
||||
public static bool getJsonIdentifier(byte[] bytes, out string identifier)
|
||||
{
|
||||
if (bytes.Length <= 5)
|
||||
if (bytes.Length <= 6)
|
||||
{
|
||||
throw new ArgumentException("bytes to short");
|
||||
}
|
||||
byte messageId = bytes[4];
|
||||
|
||||
if (messageId == 0x01)
|
||||
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;
|
||||
}
|
||||
@@ -103,7 +62,7 @@ namespace Client
|
||||
/// <returns>if message contains raw data</returns>
|
||||
public static bool isRawData(byte[] bytes)
|
||||
{
|
||||
if (bytes.Length <= 5)
|
||||
if (bytes.Length <= 6)
|
||||
{
|
||||
throw new ArgumentException("bytes to short");
|
||||
}
|
||||
@@ -117,13 +76,14 @@ namespace Client
|
||||
/// <param name="messageId"></param>
|
||||
/// <param name="clientId"></param>
|
||||
/// <returns>the message ready for sending</returns>
|
||||
private static byte[] getMessage(byte[] payload, byte messageId)
|
||||
public 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);
|
||||
res[5] = clientId;
|
||||
Array.Copy(payload, 0, res, 6, payload.Length);
|
||||
|
||||
return res;
|
||||
}
|
||||
@@ -134,9 +94,9 @@ namespace Client
|
||||
/// <param name="payload"></param>
|
||||
/// <param name="clientId"></param>
|
||||
/// <returns>the message ready for sending</returns>
|
||||
public static byte[] GetRawDataMessage(byte[] payload)
|
||||
public static byte[] GetRawDataMessage(byte[] payload, byte clientId)
|
||||
{
|
||||
return getMessage(payload, 0x02);
|
||||
return getMessage(payload, 0x02, clientId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -145,9 +105,9 @@ namespace Client
|
||||
/// <param name="payload"></param>
|
||||
/// <param name="clientId"></param>
|
||||
/// <returns>the message ready for sending</returns>
|
||||
public static byte[] getJsonMessage(byte[] payload)
|
||||
public static byte[] getJsonMessage(byte[] payload, byte clientId)
|
||||
{
|
||||
return getMessage(payload, 0x01);
|
||||
return getMessage(payload, 0x01, clientId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -156,9 +116,9 @@ namespace Client
|
||||
/// <param name="message"></param>
|
||||
/// <param name="clientId"></param>
|
||||
/// <returns>the message ready for sending</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,180 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using RH_Engine;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
public delegate void HandleSerial(string message);
|
||||
public delegate void HandleNoTunnelId();
|
||||
|
||||
public sealed class EngineConnection
|
||||
{
|
||||
private static EngineConnection instance = null;
|
||||
private static readonly object padlock = new object();
|
||||
public HandleNoTunnelId OnNoTunnelId;
|
||||
|
||||
|
||||
private static PC[] PCs = {
|
||||
//new PC("DESKTOP-M2CIH87", "Fabian"),
|
||||
//new PC("T470S", "Shinichi"),
|
||||
//new PC("DESKTOP-DHS478C", "semme"),
|
||||
new PC("HP-ZBOOK-SEM", "Sem")
|
||||
//new PC("DESKTOP-TV73FKO", "Wouter"),
|
||||
//new PC("DESKTOP-SINMKT1", "Ralf van Aert"),
|
||||
//new PC("NA", "Bart")
|
||||
};
|
||||
|
||||
private static ServerResponseReader serverResponseReader;
|
||||
private static string sessionId = string.Empty;
|
||||
private static string tunnelId = string.Empty;
|
||||
private static string routeId = string.Empty;
|
||||
private static string panelId = string.Empty;
|
||||
private static string bikeId = string.Empty;
|
||||
|
||||
private static NetworkStream stream;
|
||||
|
||||
private static Dictionary<string, HandleSerial> serialResponses = new Dictionary<string, HandleSerial>();
|
||||
private Command mainCommand;
|
||||
|
||||
public bool Connected = false;
|
||||
|
||||
EngineConnection()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static EngineConnection INSTANCE
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (padlock)
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = new EngineConnection();
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
TcpClient client = new TcpClient("145.48.6.10", 6666);
|
||||
stream = client.GetStream();
|
||||
initReader();
|
||||
CreateConnection();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// connects to the server and creates the tunnel
|
||||
/// </summary>
|
||||
/// <param name="stream">the network stream to use</param>
|
||||
public void CreateConnection()
|
||||
{
|
||||
|
||||
WriteTextMessage( "{\r\n\"id\" : \"session/list\",\r\n\"serial\" : \"list\"\r\n}");
|
||||
|
||||
// wait until we have got a sessionId
|
||||
while (sessionId == string.Empty) { }
|
||||
|
||||
string tunnelCreate = "{\"id\" : \"tunnel/create\", \"data\" : {\"session\" : \"" + sessionId + "\"}}";
|
||||
|
||||
WriteTextMessage(tunnelCreate);
|
||||
|
||||
// wait until we have a tunnel id
|
||||
while (tunnelId == string.Empty) { }
|
||||
if (tunnelId != null)
|
||||
{
|
||||
Write("got tunnel id! " + tunnelId);
|
||||
}
|
||||
mainCommand = new Command(tunnelId);
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// initializes and starts the reading of the responses from the vr server
|
||||
/// </summary>
|
||||
/// <param name="stream">the networkstream</param>
|
||||
private void initReader()
|
||||
{
|
||||
serverResponseReader = new ServerResponseReader(stream);
|
||||
serverResponseReader.callback = HandleResponse;
|
||||
serverResponseReader.StartRead();
|
||||
Connected = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// callback method that handles responses from the server
|
||||
/// </summary>
|
||||
/// <param name="message">the response message from the server</param>
|
||||
public void HandleResponse(string message)
|
||||
{
|
||||
string id = JSONParser.GetID(message);
|
||||
|
||||
// because the first messages don't have a serial, we need to check on the id
|
||||
if (id == "session/list")
|
||||
{
|
||||
sessionId = JSONParser.GetSessionID(message, PCs);
|
||||
}
|
||||
else if (id == "tunnel/create")
|
||||
{
|
||||
tunnelId = JSONParser.GetTunnelID(message);
|
||||
if (tunnelId == null)
|
||||
{
|
||||
Write("could not find a valid tunnel id!");
|
||||
OnNoTunnelId?.Invoke();
|
||||
Connected = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (message.Contains("serial"))
|
||||
{
|
||||
//Console.WriteLine("GOT MESSAGE WITH SERIAL: " + message + "\n\n\n");
|
||||
string serial = JSONParser.GetSerial(message);
|
||||
//Console.WriteLine("Got serial " + serial);
|
||||
if (serialResponses.ContainsKey(serial)) serialResponses[serial].Invoke(message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// method that sends the speciefied message with the specified serial, and executes the given action upon receivind a reply from the server with this serial.
|
||||
/// </summary>
|
||||
/// <param name="stream">the networkstream to use</param>
|
||||
/// <param name="message">the message to send</param>
|
||||
/// <param name="serial">the serial to check for</param>
|
||||
/// <param name="action">the code to be executed upon reveiving a reply from the server with the specified serial</param>
|
||||
public void SendMessageAndOnResponse(string message, string serial, HandleSerial action)
|
||||
{
|
||||
serialResponses.Add(serial, action);
|
||||
WriteTextMessage(message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// writes a message to the server
|
||||
/// </summary>
|
||||
/// <param name="stream">the network stream to use</param>
|
||||
/// <param name="message">the message to send</param>
|
||||
public void WriteTextMessage(string message)
|
||||
{
|
||||
byte[] msg = Encoding.ASCII.GetBytes(message);
|
||||
byte[] res = new byte[msg.Length + 4];
|
||||
|
||||
Array.Copy(BitConverter.GetBytes(msg.Length), 0, res, 0, 4);
|
||||
Array.Copy(msg, 0, res, 4, msg.Length);
|
||||
|
||||
stream.Write(res);
|
||||
|
||||
//Write("sent message " + message);
|
||||
}
|
||||
public void Write(string msg)
|
||||
{
|
||||
Console.WriteLine( "[ENGINECONNECT] " + msg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,9 +1,5 @@
|
||||
using System;
|
||||
using Hardware;
|
||||
using Hardware.Simulators;
|
||||
using RH_Engine;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
@@ -14,6 +10,7 @@ namespace Client
|
||||
Console.WriteLine("Hello World!");
|
||||
//connect fiets?
|
||||
|
||||
|
||||
Client client = new Client();
|
||||
|
||||
|
||||
@@ -21,13 +18,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)
|
||||
{
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
<Application x:Class="DokterApp.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:DokterApp"
|
||||
StartupUri="MainWindow.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
@@ -1,17 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace DokterApp
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
||||
@@ -1,9 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,12 +0,0 @@
|
||||
<Window x:Class="DokterApp.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:DokterApp"
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="450" Width="800">
|
||||
<Grid>
|
||||
|
||||
</Grid>
|
||||
</Window>
|
||||
@@ -1,28 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace DokterApp
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Hashing
|
||||
{
|
||||
class Hasher
|
||||
{
|
||||
public static byte[] GetHash(string input)
|
||||
{
|
||||
using (HashAlgorithm algorithm = SHA256.Create())
|
||||
{
|
||||
return algorithm.ComputeHash(Encoding.UTF8.GetBytes(input));
|
||||
}
|
||||
}
|
||||
|
||||
public static string HashString(string input)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
foreach (byte b in GetHash(input)) {
|
||||
sb.Append(b.ToString("X2"));
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
|
||||
<HasSharedItems>true</HasSharedItems>
|
||||
<SharedGUID>70277749-d423-4871-b692-2efc5a6ed932</SharedGUID>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Label="Configuration">
|
||||
<Import_RootNamespace>Hashing</Import_RootNamespace>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Hasher.cs" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>70277749-d423-4871-b692-2efc5a6ed932</ProjectGuid>
|
||||
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
|
||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
|
||||
<PropertyGroup />
|
||||
<Import Project="Hashing.projitems" Label="Shared" />
|
||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
|
||||
</Project>
|
||||
@@ -60,9 +60,6 @@ namespace Message
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Identifier enum for the Message objects
|
||||
/// </summary>
|
||||
public enum Identifier
|
||||
{
|
||||
LOGIN,
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace Hardware
|
||||
/// </summary>
|
||||
public class BLEHandler
|
||||
{
|
||||
List<IDataReceiver> dataReceivers;
|
||||
IDataReceiver dataReceiver;
|
||||
private BLE bleBike;
|
||||
private BLE bleHeart;
|
||||
public bool Running { get; set; }
|
||||
@@ -24,17 +24,7 @@ namespace Hardware
|
||||
/// <param name="dataReceiver">the dataconverter object</param>
|
||||
public BLEHandler(IDataReceiver dataReceiver)
|
||||
{
|
||||
this.dataReceivers = new List<IDataReceiver> { dataReceiver };
|
||||
}
|
||||
|
||||
public BLEHandler(List<IDataReceiver> dataReceivers)
|
||||
{
|
||||
this.dataReceivers = dataReceivers;
|
||||
}
|
||||
|
||||
public void addDataReceiver(IDataReceiver dataReceiver)
|
||||
{
|
||||
this.dataReceivers.Add(dataReceiver);
|
||||
this.dataReceiver = dataReceiver;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -135,17 +125,11 @@ namespace Hardware
|
||||
{
|
||||
byte[] payload = new byte[8];
|
||||
Array.Copy(e.Data, 4, payload, 0, 8);
|
||||
foreach (IDataReceiver dataReceiver in this.dataReceivers)
|
||||
{
|
||||
dataReceiver.Bike(payload);
|
||||
}
|
||||
this.dataReceiver.Bike(payload);
|
||||
}
|
||||
else if (e.ServiceName == "00002a37-0000-1000-8000-00805f9b34fb")
|
||||
{
|
||||
foreach (IDataReceiver dataReceiver in this.dataReceivers)
|
||||
{
|
||||
dataReceiver.BPM(e.Data);
|
||||
}
|
||||
this.dataReceiver.BPM(e.Data);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace Hardware.Simulators
|
||||
{
|
||||
public class BikeSimulator : IHandler
|
||||
{
|
||||
List<IDataReceiver> dataReceivers;
|
||||
IDataReceiver dataReceiver;
|
||||
private int elapsedTime = 0;
|
||||
private int eventCounter = 0;
|
||||
private double distanceTraveled = 0;
|
||||
@@ -32,17 +32,7 @@ namespace Hardware.Simulators
|
||||
|
||||
public BikeSimulator(IDataReceiver dataReceiver)
|
||||
{
|
||||
this.dataReceivers = new List<IDataReceiver> { dataReceiver };
|
||||
}
|
||||
|
||||
public BikeSimulator(List<IDataReceiver> dataReceivers)
|
||||
{
|
||||
this.dataReceivers = dataReceivers;
|
||||
}
|
||||
|
||||
public void addDataReceiver(IDataReceiver dataReceiver)
|
||||
{
|
||||
this.dataReceivers.Add(dataReceiver);
|
||||
this.dataReceiver = dataReceiver;
|
||||
}
|
||||
|
||||
public void StartSimulation()
|
||||
@@ -60,12 +50,9 @@ namespace Hardware.Simulators
|
||||
CalculateVariables(improvedPerlin.GetValue(x) + 1);
|
||||
|
||||
//Simulate sending data
|
||||
foreach (IDataReceiver dataReceiver in this.dataReceivers)
|
||||
{
|
||||
dataReceiver.Bike(GenerateBike0x19());
|
||||
dataReceiver.Bike(GenerateBike0x10());
|
||||
dataReceiver.BPM(GenerateHeart());
|
||||
}
|
||||
dataReceiver.Bike(GenerateBike0x19());
|
||||
dataReceiver.Bike(GenerateBike0x10());
|
||||
dataReceiver.BPM(GenerateHeart());
|
||||
|
||||
Thread.Sleep(1000);
|
||||
|
||||
|
||||
@@ -13,9 +13,9 @@ namespace ProftaakRH
|
||||
{
|
||||
IDataReceiver dataReceiver = new DataConverter();
|
||||
BLEHandler bLEHandler = new BLEHandler(dataReceiver);
|
||||
BikeSimulator bikeSimulator = new BikeSimulator(dataReceiver);
|
||||
bikeSimulator.setResistance(bikeSimulator.GenerateResistance(1f));
|
||||
bikeSimulator.StartSimulation();
|
||||
//BikeSimulator bikeSimulator = new BikeSimulator(dataConverter);
|
||||
//bikeSimulator.setResistance(bikeSimulator.GenerateResistance(1f));
|
||||
//bikeSimulator.StartSimulation();
|
||||
|
||||
|
||||
bool running = true;
|
||||
|
||||
@@ -7,22 +7,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProftaakRH", "ProftaakRH.cs
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RH-Engine", "..\RH-Engine\RH-Engine.csproj", "{984E295E-47A2-41E7-90E5-50FDB9E67694}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server", "..\Server\Server.csproj", "{B1AB6F51-A20D-4162-9A7F-B3350B7510FD}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "..\Server\Server.csproj", "{B1AB6F51-A20D-4162-9A7F-B3350B7510FD}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "..\Client\Client.csproj", "{5759DD20-7A4F-4D8D-B986-A70A7818C112}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "..\Client\Client.csproj", "{5759DD20-7A4F-4D8D-B986-A70A7818C112}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Message", "..\Message\Message.csproj", "{9ED6832D-B0FB-4460-9BCD-FAA58863B0CE}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DokterApp", "..\DokterApp\DokterApp.csproj", "{B150F08B-13DA-4D17-BD96-7E89F52727C6}"
|
||||
EndProject
|
||||
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Hashing", "..\Hashing\Hashing.shproj", "{70277749-D423-4871-B692-2EFC5A6ED932}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Message", "..\Message\Message.csproj", "{9ED6832D-B0FB-4460-9BCD-FAA58863B0CE}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SharedMSBuildProjectFiles) = preSolution
|
||||
..\Hashing\Hashing.projitems*{5759dd20-7a4f-4d8d-b986-a70a7818c112}*SharedItemsImports = 5
|
||||
..\Hashing\Hashing.projitems*{70277749-d423-4871-b692-2efc5a6ed932}*SharedItemsImports = 13
|
||||
..\Hashing\Hashing.projitems*{b1ab6f51-a20d-4162-9a7f-b3350b7510fd}*SharedItemsImports = 5
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
@@ -48,10 +39,6 @@ Global
|
||||
{9ED6832D-B0FB-4460-9BCD-FAA58863B0CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9ED6832D-B0FB-4460-9BCD-FAA58863B0CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9ED6832D-B0FB-4460-9BCD-FAA58863B0CE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{B150F08B-13DA-4D17-BD96-7E89F52727C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{B150F08B-13DA-4D17-BD96-7E89F52727C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B150F08B-13DA-4D17-BD96-7E89F52727C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B150F08B-13DA-4D17-BD96-7E89F52727C6}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
using LibNoise.Primitive;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace RH_Engine
|
||||
{
|
||||
public class Command
|
||||
class Command
|
||||
{
|
||||
public const string STANDARD_HEAD = "Head";
|
||||
public const string STANDARD_GROUND = "GroundPlane";
|
||||
@@ -12,7 +16,9 @@ namespace RH_Engine
|
||||
public const string STANDARD_LEFTHAND = "LeftHand";
|
||||
public const string STANDARD_RIGHTHAND = "RightHand";
|
||||
|
||||
private string tunnelID;
|
||||
|
||||
|
||||
string tunnelID;
|
||||
|
||||
public Command(string tunnelID)
|
||||
{
|
||||
@@ -29,10 +35,10 @@ namespace RH_Engine
|
||||
size = sizeArray,
|
||||
heights = heightsArray
|
||||
}
|
||||
|
||||
};
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
|
||||
public string AddLayer(string uid, string texture)
|
||||
{
|
||||
dynamic payload = new
|
||||
@@ -50,7 +56,6 @@ namespace RH_Engine
|
||||
};
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
|
||||
public string UpdateTerrain()
|
||||
{
|
||||
dynamic payload = new
|
||||
@@ -58,6 +63,7 @@ namespace RH_Engine
|
||||
id = "scene/terrain/update",
|
||||
data = new
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
@@ -85,135 +91,37 @@ namespace RH_Engine
|
||||
|
||||
public string DeleteNode(string uuid)
|
||||
{
|
||||
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/node/delete",
|
||||
data = new
|
||||
{
|
||||
id = uuid,
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
|
||||
}
|
||||
|
||||
public string addPanel(string serialToSend, string uuidBike)
|
||||
public string AddBikeModel()
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/node/add",
|
||||
serial = serialToSend,
|
||||
data = new
|
||||
{
|
||||
name = "dashboard",
|
||||
parent = uuidBike,
|
||||
components = new
|
||||
{
|
||||
panel = new
|
||||
{
|
||||
size = new int[] { 1, 1 },
|
||||
resolution = new int[] { 512, 512 },
|
||||
background = new int[] { 1, 0, 0, 0 },
|
||||
castShadow = false
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
return AddModel("bike", "data\\NetworkEngine\\models\\bike\\bike.fbx");
|
||||
}
|
||||
|
||||
public string ColorPanel(string uuidPanel)
|
||||
public string AddModel(string nodeName, string fileLocation)
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/panel/setclearcolor",
|
||||
data = new
|
||||
{
|
||||
id = uuidPanel,
|
||||
color = new int[] { 1, 1, 1, 1 }
|
||||
}
|
||||
};
|
||||
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
return AddModel(nodeName, fileLocation, null, new float[] { 0, 0, 0 }, 1, new float[] { 0, 0, 0 });
|
||||
}
|
||||
|
||||
public string SwapPanel(string uuid)
|
||||
public string AddModel(string nodeName, string fileLocation, float[] positionVector, float scalar, float[] rotationVector)
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/panel/swap",
|
||||
data = new
|
||||
{
|
||||
id = uuid
|
||||
}
|
||||
};
|
||||
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
return AddModel(nodeName, fileLocation, null, positionVector, scalar, rotationVector);
|
||||
}
|
||||
|
||||
public string bikeSpeed(string uuidPanel, double speed)
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/panel/drawtext",
|
||||
data = new
|
||||
{
|
||||
id = uuidPanel,
|
||||
text = "Bike speed placeholder",
|
||||
position = new int[] { 0, 0 },
|
||||
size = 32.0,
|
||||
color = new int[] { 0, 0, 0, 1 },
|
||||
font = "segoeui"
|
||||
}
|
||||
};
|
||||
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
|
||||
public string SwapPanelCommand(string uuid)
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/panel/swap",
|
||||
data = new
|
||||
{
|
||||
id = uuid
|
||||
}
|
||||
};
|
||||
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
|
||||
public string ClearPanel(string uuid)
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/panel/clear",
|
||||
data = new
|
||||
{
|
||||
id = uuid
|
||||
}
|
||||
};
|
||||
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
|
||||
public string AddBikeModel(string serial)
|
||||
{
|
||||
return AddModel("bike", serial, "data\\NetworkEngine\\models\\bike\\bike.fbx");
|
||||
}
|
||||
|
||||
public string AddModel(string nodeName, string serial, string fileLocation)
|
||||
{
|
||||
return AddModel(nodeName, serial, fileLocation, null, new float[] { 0, 0, 0 }, 1, new float[] { 0, 0, 0 });
|
||||
}
|
||||
|
||||
public string AddModel(string nodeName, string serial, string fileLocation, float[] positionVector, float scalar, float[] rotationVector)
|
||||
{
|
||||
return AddModel(nodeName, serial, fileLocation, null, positionVector, scalar, rotationVector);
|
||||
}
|
||||
|
||||
public string AddModel(string nodeName, string serialToSend, string fileLocation, string animationLocation, float[] positionVector, float scalar, float[] rotationVector)
|
||||
public string AddModel(string nodeName, string fileLocation, string animationLocation, float[] positionVector, float scalar, float[] rotationVector)
|
||||
{
|
||||
string namename = nodeName;
|
||||
bool animatedBool = false;
|
||||
@@ -225,7 +133,6 @@ namespace RH_Engine
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/node/add",
|
||||
serial = serialToSend,
|
||||
data = new
|
||||
{
|
||||
name = namename,
|
||||
@@ -236,6 +143,7 @@ namespace RH_Engine
|
||||
position = positionVector,
|
||||
scale = scalar,
|
||||
rotation = rotationVector
|
||||
|
||||
},
|
||||
model = new
|
||||
{
|
||||
@@ -246,6 +154,7 @@ namespace RH_Engine
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
@@ -275,14 +184,14 @@ namespace RH_Engine
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
|
||||
public string RouteCommand(string serialToSend)
|
||||
|
||||
public string RouteCommand()
|
||||
{
|
||||
ImprovedPerlin improvedPerlin = new ImprovedPerlin(4325, LibNoise.NoiseQuality.Best);
|
||||
Random r = new Random();
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "route/add",
|
||||
serial = serialToSend,
|
||||
data = new
|
||||
{
|
||||
nodes = new dynamic[]
|
||||
@@ -332,44 +241,13 @@ namespace RH_Engine
|
||||
private int[] GetDir()
|
||||
{
|
||||
Random rng = new Random();
|
||||
int[] dir = { rng.Next(50), 0, rng.Next(50) };
|
||||
int[] dir = {rng.Next(50), 0, rng.Next(50)};
|
||||
return dir;
|
||||
}
|
||||
|
||||
public string RouteFollow(string routeID, string nodeID, float speedValue)
|
||||
public string FollowRouteCommand()
|
||||
{
|
||||
return RouteFollow(routeID, nodeID, speedValue, new float[] { 0, 0, 0 });
|
||||
}
|
||||
|
||||
public string RouteFollow(string routeID, string nodeID, float speedValue, float[] rotateOffsetVector, float[] positionOffsetVector)
|
||||
{
|
||||
return RouteFollow(routeID, nodeID, speedValue, 0, "XYZ", 1, true, rotateOffsetVector, positionOffsetVector);
|
||||
}
|
||||
|
||||
public string RouteFollow(string routeID, string nodeID, float speedValue, float[] positionOffsetVector)
|
||||
{
|
||||
return RouteFollow(routeID, nodeID, speedValue, 0, "XYZ", 1, true, new float[] { 0, 0, 0 }, positionOffsetVector);
|
||||
}
|
||||
private string RouteFollow(string routeID, string nodeID, float speedValue, float offsetValue, string rotateValue, float smoothingValue, bool followHeightValue, float[] rotateOffsetVector, float[] positionOffsetVector)
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "route/follow",
|
||||
data = new
|
||||
{
|
||||
route = routeID,
|
||||
node = nodeID,
|
||||
speed = speedValue,
|
||||
offset = offsetValue,
|
||||
rotate = rotateValue,
|
||||
smoothing = smoothingValue,
|
||||
followHeight = followHeightValue,
|
||||
rotateOffset = rotateOffsetVector,
|
||||
positionOffset = positionOffsetVector
|
||||
|
||||
}
|
||||
};
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
return "";
|
||||
}
|
||||
|
||||
public string RoadCommand(string uuid_route)
|
||||
@@ -390,12 +268,11 @@ namespace RH_Engine
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
|
||||
public string GetSceneInfoCommand(string serialToSend)
|
||||
public string GetSceneInfoCommand()
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/get",
|
||||
serial = serialToSend
|
||||
id = "scene/get"
|
||||
};
|
||||
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
@@ -405,8 +282,7 @@ namespace RH_Engine
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/reset",
|
||||
serial = "reset"
|
||||
id = "scene/reset"
|
||||
};
|
||||
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
@@ -419,6 +295,7 @@ namespace RH_Engine
|
||||
throw new Exception("The time must be between 0 and 24!");
|
||||
}
|
||||
|
||||
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/skybox/settime",
|
||||
@@ -426,10 +303,13 @@ namespace RH_Engine
|
||||
{
|
||||
time = timeToSet
|
||||
}
|
||||
|
||||
};
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
|
||||
}
|
||||
|
||||
|
||||
private object Payload(dynamic message)
|
||||
{
|
||||
return new
|
||||
@@ -442,5 +322,8 @@ namespace RH_Engine
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace RH_Engine
|
||||
{
|
||||
public class JSONParser
|
||||
class JSONParser
|
||||
{
|
||||
/// <summary>
|
||||
/// returns all the users from the given response
|
||||
@@ -23,20 +27,21 @@ namespace RH_Engine
|
||||
}
|
||||
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
public static string GetSessionID(string msg, PC[] PCs)
|
||||
{
|
||||
dynamic jsonData = JsonConvert.DeserializeObject(msg);
|
||||
Newtonsoft.Json.Linq.JArray data = jsonData.data;
|
||||
for (int i = data.Count - 1; i >= 0; i--)
|
||||
for (int i = data.Count-1; i >= 0; i--)
|
||||
{
|
||||
dynamic d = data[i];
|
||||
foreach (PC pc in PCs)
|
||||
{
|
||||
if (d.clientinfo.host == pc.host && d.clientinfo.user == pc.user)
|
||||
{
|
||||
Console.WriteLine("[JSONPARSER] connecting to {0}, on {1} with id {2}", pc.user, pc.host, d.id);
|
||||
Console.WriteLine("connecting to {0}, on {1} with id {2}", pc.user, pc.host, d.id);
|
||||
return d.id;
|
||||
}
|
||||
}
|
||||
@@ -45,18 +50,6 @@ namespace RH_Engine
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string GetSerial(string json)
|
||||
{
|
||||
dynamic jsonData = JsonConvert.DeserializeObject(json);
|
||||
return jsonData.data.data.serial;
|
||||
}
|
||||
|
||||
public static string GetID(string json)
|
||||
{
|
||||
dynamic d = JsonConvert.DeserializeObject(json);
|
||||
return d.id;
|
||||
}
|
||||
|
||||
public static string GetTunnelID(string json)
|
||||
{
|
||||
dynamic jsonData = JsonConvert.DeserializeObject(json);
|
||||
@@ -67,30 +60,15 @@ namespace RH_Engine
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// method to get the uuid from requests for adding a node,route or road
|
||||
/// </summary>
|
||||
/// <param name="json">the json response froo the server</param>
|
||||
/// <returns>the uuid of the created object</returns>
|
||||
public static string GetResponseUuid(string json)
|
||||
public static string GetRouteID(string json)
|
||||
{
|
||||
dynamic jsonData = JsonConvert.DeserializeObject(json);
|
||||
if (jsonData.data.data.status == "ok")
|
||||
if (jsonData.data.status == "ok")
|
||||
{
|
||||
return jsonData.data.data.data.uuid;
|
||||
return jsonData.data.uuid;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string getPanelID(string json)
|
||||
{
|
||||
dynamic jsonData = JsonConvert.DeserializeObject(json);
|
||||
if (jsonData.data.data.data.name == "dashboard")
|
||||
{
|
||||
Console.WriteLine(jsonData.data.data.data.uuid);
|
||||
return jsonData.data.data.data.uuid;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,97 +1,35 @@
|
||||
using LibNoise.Primitive;
|
||||
using LibNoise.Primitive;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Net.Sockets;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace RH_Engine
|
||||
{
|
||||
public delegate void HandleSerial(string message);
|
||||
|
||||
public class Program
|
||||
internal class Program
|
||||
{
|
||||
private static PC[] PCs = {
|
||||
//new PC("DESKTOP-M2CIH87", "Fabian"),
|
||||
//new PC("T470S", "Shinichi"),
|
||||
new PC("T470S", "Shinichi"),
|
||||
//new PC("DESKTOP-DHS478C", "semme"),
|
||||
new PC("HP-ZBOOK-SEM", "Sem"),
|
||||
//new PC("DESKTOP-TV73FKO", "Wouter"),
|
||||
new PC("DESKTOP-SINMKT1", "Ralf van Aert"),
|
||||
//new PC("DESKTOP-SINMKT1", "Ralf"),
|
||||
//new PC("NA", "Bart")
|
||||
};
|
||||
|
||||
private static ServerResponseReader serverResponseReader;
|
||||
private static string sessionId = string.Empty;
|
||||
private static string tunnelId = string.Empty;
|
||||
private static string routeId = string.Empty;
|
||||
private static string panelId = string.Empty;
|
||||
private static string bikeId = string.Empty;
|
||||
|
||||
private static Dictionary<string, HandleSerial> serialResponses = new Dictionary<string, HandleSerial>();
|
||||
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
TcpClient client = new TcpClient("145.48.6.10", 6666);
|
||||
|
||||
CreateConnection(client.GetStream());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// initializes and starts the reading of the responses from the vr server
|
||||
/// </summary>
|
||||
/// <param name="stream">the networkstream</param>
|
||||
private static void initReader(NetworkStream stream)
|
||||
{
|
||||
serverResponseReader = new ServerResponseReader(stream);
|
||||
serverResponseReader.callback = HandleResponse;
|
||||
serverResponseReader.StartRead();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// callback method that handles responses from the server
|
||||
/// </summary>
|
||||
/// <param name="message">the response message from the server</param>
|
||||
public static void HandleResponse(string message)
|
||||
{
|
||||
string id = JSONParser.GetID(message);
|
||||
|
||||
// because the first messages don't have a serial, we need to check on the id
|
||||
if (id == "session/list")
|
||||
{
|
||||
sessionId = JSONParser.GetSessionID(message, PCs);
|
||||
}
|
||||
else if (id == "tunnel/create")
|
||||
{
|
||||
tunnelId = JSONParser.GetTunnelID(message);
|
||||
if (tunnelId == null)
|
||||
{
|
||||
Console.WriteLine("could not find a valid tunnel id!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (message.Contains("serial"))
|
||||
{
|
||||
//Console.WriteLine("GOT MESSAGE WITH SERIAL: " + message + "\n\n\n");
|
||||
string serial = JSONParser.GetSerial(message);
|
||||
//Console.WriteLine("Got serial " + serial);
|
||||
if (serialResponses.ContainsKey(serial)) serialResponses[serial].Invoke(message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// method that sends the speciefied message with the specified serial, and executes the given action upon receivind a reply from the server with this serial.
|
||||
/// </summary>
|
||||
/// <param name="stream">the networkstream to use</param>
|
||||
/// <param name="message">the message to send</param>
|
||||
/// <param name="serial">the serial to check for</param>
|
||||
/// <param name="action">the code to be executed upon reveiving a reply from the server with the specified serial</param>
|
||||
public static void SendMessageAndOnResponse(NetworkStream stream, string message, string serial, HandleSerial action)
|
||||
{
|
||||
serialResponses.Add(serial, action);
|
||||
WriteTextMessage(stream, message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -109,30 +47,65 @@ namespace RH_Engine
|
||||
|
||||
stream.Write(res);
|
||||
|
||||
Console.WriteLine("sent message " + message);
|
||||
//Console.WriteLine("sent message " + message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// reads a response from the server
|
||||
/// </summary>
|
||||
/// <param name="stream">the network stream to use</param>
|
||||
/// <returns>the returned message from the server</returns>
|
||||
public static string ReadPrefMessage(NetworkStream stream)
|
||||
{
|
||||
byte[] lengthBytes = new byte[4];
|
||||
|
||||
stream.Read(lengthBytes, 0, 4);
|
||||
Console.WriteLine("read message..");
|
||||
|
||||
int length = BitConverter.ToInt32(lengthBytes);
|
||||
|
||||
//Console.WriteLine("length is: " + length);
|
||||
|
||||
byte[] buffer = new byte[length];
|
||||
int totalRead = 0;
|
||||
|
||||
//read bytes until stream indicates there are no more
|
||||
do
|
||||
{
|
||||
int read = stream.Read(buffer, totalRead, buffer.Length - totalRead);
|
||||
totalRead += read;
|
||||
//Console.WriteLine("ReadMessage: " + read);
|
||||
} while (totalRead < length);
|
||||
|
||||
return Encoding.UTF8.GetString(buffer, 0, totalRead);
|
||||
}
|
||||
/// <summary>
|
||||
/// connects to the server and creates the tunnel
|
||||
/// </summary>
|
||||
/// <param name="stream">the network stream to use</param>
|
||||
private static void CreateConnection(NetworkStream stream)
|
||||
{
|
||||
initReader(stream);
|
||||
WriteTextMessage(stream, "{\r\n\"id\" : \"session/list\"\r\n}");
|
||||
string id = JSONParser.GetSessionID(ReadPrefMessage(stream), PCs);
|
||||
|
||||
WriteTextMessage(stream, "{\r\n\"id\" : \"session/list\",\r\n\"serial\" : \"list\"\r\n}");
|
||||
|
||||
// wait until we have got a sessionId
|
||||
while (sessionId == string.Empty) { }
|
||||
|
||||
string tunnelCreate = "{\"id\" : \"tunnel/create\", \"data\" : {\"session\" : \"" + sessionId + "\"}}";
|
||||
string tunnelCreate = "{\"id\" : \"tunnel/create\", \"data\" : {\"session\" : \"" + id + "\"}}";
|
||||
|
||||
WriteTextMessage(stream, tunnelCreate);
|
||||
|
||||
// wait until we have a tunnel id
|
||||
while (tunnelId == string.Empty) { }
|
||||
Console.WriteLine("got tunnel id! sending commands...");
|
||||
sendCommands(stream, tunnelId);
|
||||
string tunnelResponse = ReadPrefMessage(stream);
|
||||
|
||||
Console.WriteLine(tunnelResponse);
|
||||
|
||||
string tunnelID = JSONParser.GetTunnelID(tunnelResponse);
|
||||
if (tunnelID == null)
|
||||
{
|
||||
Console.WriteLine("could not find a valid tunnel id!");
|
||||
return;
|
||||
}
|
||||
|
||||
sendCommands(stream, tunnelID);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -144,23 +117,27 @@ namespace RH_Engine
|
||||
{
|
||||
Command mainCommand = new Command(tunnelID);
|
||||
|
||||
|
||||
WriteTextMessage(stream, mainCommand.ResetScene());
|
||||
SendMessageAndOnResponse(stream, mainCommand.RouteCommand("routeID"), "routeID", (message) => routeId = JSONParser.GetResponseUuid(message));
|
||||
ReadPrefMessage(stream);
|
||||
string routeid = CreateRoute(stream, mainCommand);
|
||||
|
||||
//WriteTextMessage(stream, mainCommand.TerrainCommand(new int[] { 256, 256 }, null));
|
||||
//string command;
|
||||
WriteTextMessage(stream, mainCommand.TerrainCommand(new int[] { 256, 256 }, null));
|
||||
Console.WriteLine(ReadPrefMessage(stream));
|
||||
string command;
|
||||
|
||||
SendMessageAndOnResponse(stream, mainCommand.AddBikeModel("bikeID"), "bikeID", (message) => bikeId = JSONParser.GetResponseUuid(message));
|
||||
command = mainCommand.AddBikeModel();
|
||||
|
||||
SendMessageAndOnResponse(stream, mainCommand.addPanel("panelID", bikeId), "panelID",
|
||||
(message) =>
|
||||
{
|
||||
panelId = JSONParser.GetResponseUuid(message);
|
||||
while (bikeId == string.Empty) { }
|
||||
WriteTextMessage(stream, mainCommand.RouteFollow(routeId, bikeId, 5, new float[] { 0, -(float)Math.PI / 2f, 0 }, new float[] { 0, 0, 0 }));
|
||||
});
|
||||
WriteTextMessage(stream, command);
|
||||
|
||||
Console.WriteLine(ReadPrefMessage(stream));
|
||||
|
||||
command = mainCommand.AddModel("car", "data\\customModels\\TeslaRoadster.fbx");
|
||||
|
||||
WriteTextMessage(stream, command);
|
||||
|
||||
Console.WriteLine(ReadPrefMessage(stream));
|
||||
|
||||
Console.WriteLine("id of head " + GetId(Command.STANDARD_HEAD, stream, mainCommand));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -183,6 +160,19 @@ namespace RH_Engine
|
||||
}
|
||||
Console.WriteLine("Could not find id of " + name);
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
public static string CreateRoute(NetworkStream stream, Command createGraphics)
|
||||
{
|
||||
WriteTextMessage(stream, createGraphics.RouteCommand());
|
||||
dynamic response = JsonConvert.DeserializeObject(ReadPrefMessage(stream));
|
||||
if (response.data.data.id == "route/add")
|
||||
{
|
||||
return response.data.data.data.uuid;
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
public static void CreateTerrain(NetworkStream stream, Command createGraphics)
|
||||
@@ -195,9 +185,12 @@ namespace RH_Engine
|
||||
height[i] = improvedPerlin.GetValue(x / 10, x / 10, x * 100) + 1;
|
||||
x += 0.001f;
|
||||
}
|
||||
|
||||
WriteTextMessage(stream, createGraphics.TerrainCommand(new int[] { 256, 256 }, height));
|
||||
Console.WriteLine(ReadPrefMessage(stream));
|
||||
|
||||
WriteTextMessage(stream, createGraphics.AddNodeCommand());
|
||||
Console.WriteLine(ReadPrefMessage(stream));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -208,14 +201,9 @@ namespace RH_Engine
|
||||
/// <returns>all the children objects in the current scene</returns>
|
||||
public static JArray GetChildren(NetworkStream stream, Command createGraphics)
|
||||
{
|
||||
JArray res = null;
|
||||
SendMessageAndOnResponse(stream, createGraphics.GetSceneInfoCommand("getChildren"), "getChildren", (message) =>
|
||||
{
|
||||
dynamic response = JsonConvert.DeserializeObject(message);
|
||||
res = response.data.data.data.children;
|
||||
});
|
||||
while (res == null) { }
|
||||
return res;
|
||||
WriteTextMessage(stream, createGraphics.GetSceneInfoCommand());
|
||||
dynamic response = JsonConvert.DeserializeObject(ReadPrefMessage(stream));
|
||||
return response.data.data.data.children;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -237,7 +225,15 @@ namespace RH_Engine
|
||||
}
|
||||
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
public static string getUUIDFromResponse(string response)
|
||||
{
|
||||
dynamic JSON = JsonConvert.DeserializeObject(response);
|
||||
return JSON.data.data.data.uuid;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -250,7 +246,6 @@ namespace RH_Engine
|
||||
this.host = host;
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public string host { get; }
|
||||
public string user { get; }
|
||||
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
using System;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace RH_Engine
|
||||
{
|
||||
public delegate void OnResponse(string response);
|
||||
|
||||
public class ServerResponseReader
|
||||
{
|
||||
public OnResponse callback
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public NetworkStream Stream { get; }
|
||||
|
||||
public ServerResponseReader(NetworkStream stream)
|
||||
{
|
||||
this.Stream = stream;
|
||||
}
|
||||
|
||||
public void StartRead()
|
||||
{
|
||||
Thread t = new Thread(() =>
|
||||
{
|
||||
if (this.callback == null)
|
||||
{
|
||||
throw new Exception("Callback not initialized!");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("[SERVERRESPONSEREADER] Starting loop for reading");
|
||||
while (true)
|
||||
{
|
||||
string res = ReadPrefMessage(Stream);
|
||||
//Console.WriteLine("[SERVERRESPONSEREADER] got message from server: " + res);
|
||||
this.callback(res);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
t.Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// reads a response from the server
|
||||
/// </summary>
|
||||
/// <param name="stream">the network stream to use</param>
|
||||
/// <returns>the returned message from the server</returns>
|
||||
public static string ReadPrefMessage(NetworkStream stream)
|
||||
{
|
||||
byte[] lengthBytes = new byte[4];
|
||||
|
||||
int streamread = stream.Read(lengthBytes, 0, 4);
|
||||
//Console.WriteLine("read message.. " + streamread);
|
||||
|
||||
int length = BitConverter.ToInt32(lengthBytes);
|
||||
|
||||
//Console.WriteLine("length is: " + length);
|
||||
|
||||
byte[] buffer = new byte[length];
|
||||
int totalRead = 0;
|
||||
|
||||
//read bytes until stream indicates there are no more
|
||||
do
|
||||
{
|
||||
int read = stream.Read(buffer, totalRead, buffer.Length - totalRead);
|
||||
totalRead += read;
|
||||
//Console.WriteLine("ReadMessage: " + read);
|
||||
} while (totalRead < length);
|
||||
|
||||
return Encoding.UTF8.GetString(buffer, 0, totalRead);
|
||||
}
|
||||
}
|
||||
}
|
||||
250
Server/Client.cs
250
Server/Client.cs
@@ -1,11 +1,10 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using Client;
|
||||
using Newtonsoft;
|
||||
using Newtonsoft.Json;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
@@ -16,190 +15,141 @@ namespace Server
|
||||
private NetworkStream stream;
|
||||
private byte[] buffer = new byte[1024];
|
||||
private byte[] totalBuffer = new byte[1024];
|
||||
private int totalBufferReceived = 0;
|
||||
private SaveData saveData;
|
||||
private string username = null;
|
||||
private DateTime sessionStart;
|
||||
private const string fileName = "userInfo.dat";
|
||||
|
||||
private int bytesReceived;
|
||||
|
||||
|
||||
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();
|
||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
|
||||
}
|
||||
|
||||
private void OnRead(IAsyncResult ar)
|
||||
/*private void OnRead(IAsyncResult ar)
|
||||
{
|
||||
int receivedBytes = this.stream.EndRead(ar);
|
||||
|
||||
if (totalBufferReceived + receivedBytes > 1024)
|
||||
try
|
||||
{
|
||||
throw new OutOfMemoryException("buffer too small");
|
||||
int receivedBytes = stream.EndRead(ar);
|
||||
}
|
||||
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, receivedBytes);
|
||||
totalBufferReceived += receivedBytes;
|
||||
|
||||
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
while (totalBufferReceived >= expectedMessageLength)
|
||||
catch (IOException)
|
||||
{
|
||||
//volledig packet binnen
|
||||
byte[] messageBytes = new byte[expectedMessageLength];
|
||||
Array.Copy(totalBuffer, 0, messageBytes, 0, expectedMessageLength);
|
||||
HandleData(messageBytes);
|
||||
communication.Disconnect(this);
|
||||
return;
|
||||
}
|
||||
|
||||
Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength)); //maybe unsafe idk
|
||||
int counter = 0;
|
||||
|
||||
totalBufferReceived -= expectedMessageLength;
|
||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
if (expectedMessageLength <= 5)
|
||||
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);
|
||||
byte[] lengthBytes = new byte[4];
|
||||
|
||||
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(6).ToArray(), 16)}");
|
||||
}
|
||||
else if (buffer[4] == 0x01)
|
||||
{
|
||||
byte[] packet = new byte[expectedMessageLength];
|
||||
Console.WriteLine(Encoding.ASCII.GetString(buffer) + " " + expectedMessageLength);
|
||||
Array.Copy(buffer, 6, packet, 0, expectedMessageLength - 6);
|
||||
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 OnWrite(IAsyncResult ar)
|
||||
private void HandleData(string 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)
|
||||
Console.WriteLine("Data " + packet);
|
||||
dynamic json = JsonConvert.DeserializeObject(packet);
|
||||
Console.WriteLine("Name: "+json.data.username + "Password: "+json.data.password);
|
||||
if (json.data.username == json.data.password)
|
||||
{
|
||||
switch (identifier)
|
||||
dynamic payload = new
|
||||
{
|
||||
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)
|
||||
{
|
||||
Console.WriteLine("got hashes " + username + "\n" + password);
|
||||
|
||||
|
||||
if (!File.Exists(fileName))
|
||||
{
|
||||
File.Create(fileName);
|
||||
Console.WriteLine("file doesnt exist");
|
||||
newUsers(username, password);
|
||||
Console.WriteLine("true");
|
||||
return true;
|
||||
} else
|
||||
{
|
||||
Console.WriteLine("file exists, located at " + Path.GetFullPath(fileName));
|
||||
string[] usernamesPasswords = File.ReadAllLines(fileName);
|
||||
if (usernamesPasswords.Length == 0)
|
||||
{
|
||||
newUsers(username, password);
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach (string s in usernamesPasswords)
|
||||
{
|
||||
string[] combo = s.Split(" ");
|
||||
if (combo[0] == username)
|
||||
data = new
|
||||
{
|
||||
Console.WriteLine("correct info");
|
||||
return combo[1] == password;
|
||||
status = "ok"
|
||||
}
|
||||
|
||||
}
|
||||
Console.WriteLine("combo was not found in file");
|
||||
|
||||
}
|
||||
Console.WriteLine("false");
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
private void newUsers(string username, string password)
|
||||
{
|
||||
|
||||
Console.WriteLine("creating new entry in file");
|
||||
using (StreamWriter sw = File.AppendText(fileName))
|
||||
{
|
||||
sw.WriteLine(username + " " + password);
|
||||
};
|
||||
Message.Message message = new Message.Message(Message.Identifier.LOGIN, JsonConvert.SerializeObject(payload));
|
||||
Write(message.Serialize());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static string ByteArrayToString(byte[] ba)
|
||||
private void Write(string data)
|
||||
{
|
||||
StringBuilder hex = new StringBuilder(ba.Length * 2);
|
||||
foreach (byte b in ba)
|
||||
hex.AppendFormat("{0:x2}", b);
|
||||
return hex.ToString();
|
||||
byte[] bytes = DataParser.getMessage(Encoding.ASCII.GetBytes(data), 0x01, 0x01);
|
||||
stream.Write(bytes, 0, bytes.Length);
|
||||
stream.Flush();
|
||||
Console.WriteLine("Wrote message");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,9 +20,6 @@ namespace Server
|
||||
public void Start()
|
||||
{
|
||||
listener.Start();
|
||||
Console.WriteLine($"==========================================================================\n" +
|
||||
$"\tstarted accepting clients at {DateTime.Now}\n" +
|
||||
$"==========================================================================");
|
||||
listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
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,18 +1,16 @@
|
||||
<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>
|
||||
|
||||
<Import Project="..\Hashing\Hashing.projitems" Label="Shared" />
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Client\Client.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user