Compare commits
146 Commits
testResist
...
write
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b9ad2540c | ||
|
|
5a425bf19b | ||
|
|
8e24274261 | ||
|
|
fb57a1779c | ||
|
|
8c45d5eccd | ||
|
|
5db3f28deb | ||
|
|
8c6cb443e3 | ||
|
|
81dcebb5e8 | ||
|
|
c782301cf2 | ||
|
|
d5eadbe529 | ||
|
|
d67c8448ad | ||
|
|
8e45c9a435 | ||
|
|
cd5f23d3e4 | ||
|
|
5f7f1c8510 | ||
|
|
cdaac839b9 | ||
|
|
f899c90ff7 | ||
|
|
c62b0224e1 | ||
|
|
be9553cc51 | ||
|
|
40cbff209b | ||
|
|
fd36e420d1 | ||
|
|
e315e1cf3f | ||
|
|
e0e910ac25 | ||
|
|
0bf41b5c07 | ||
|
|
de18ddc0f0 | ||
|
|
748f7eed46 | ||
|
|
674a10a779 | ||
|
|
797fb16465 | ||
|
|
60cf23d980 | ||
|
|
31ef1beb82 | ||
|
|
603703b3f6 | ||
|
|
1a67e97f75 | ||
|
|
28242ff052 | ||
|
|
a52f1ea7ed | ||
|
|
dcae307754 | ||
|
|
1041aa0391 | ||
|
|
b391583107 | ||
|
|
c63b51c22b | ||
|
|
1ce519f661 | ||
|
|
7df8d98ed9 | ||
|
|
fce99ff9ec | ||
|
|
a4bfac3de1 | ||
|
|
5762989f93 | ||
|
|
fe977deb46 | ||
|
|
541538d50a | ||
|
|
8f8490fe2a | ||
|
|
47cfe4efe6 | ||
|
|
5cd4e61194 | ||
|
|
84a80df9ca | ||
|
|
3f3a388ec1 | ||
|
|
360767b6a5 | ||
|
|
ddcc2171e0 | ||
|
|
b6be86b811 | ||
|
|
109b982931 | ||
|
|
688509dc66 | ||
|
|
02b180c246 | ||
|
|
d912f2dd31 | ||
|
|
6d0439fab3 | ||
|
|
6b78cf12a2 | ||
|
|
9f675844ed | ||
|
|
92d95eb6a6 | ||
|
|
b2163b0459 | ||
|
|
c837af0392 | ||
|
|
de7f667dd0 | ||
|
|
7842baafb4 | ||
|
|
12b4b90bc5 | ||
|
|
4a745ace75 | ||
|
|
f1d543dafc | ||
|
|
c69c3193d4 | ||
|
|
17eb3b1845 | ||
|
|
6eb2ac1a82 | ||
|
|
36a0177270 | ||
|
|
a5a61a8a83 | ||
|
|
b7f3f3542f | ||
|
|
f1066a365e | ||
|
|
99b4e7f426 | ||
|
|
3d72ed9694 | ||
|
|
037bb8a6cf | ||
|
|
1e7d679fc2 | ||
|
|
0a2a0405bf | ||
|
|
6e09b87f60 | ||
|
|
0d107a7170 | ||
|
|
1cda955254 | ||
|
|
ef0b293373 | ||
|
|
bc99bfd46b | ||
|
|
8fd3322c63 | ||
|
|
c82f1e9537 | ||
|
|
178bbb030c | ||
|
|
bb70d80409 | ||
|
|
e54cca454b | ||
|
|
e98bf569f9 | ||
|
|
a655646e80 | ||
|
|
0e2ee807e1 | ||
|
|
9ff2eb4e68 | ||
|
|
a8494bc4d2 | ||
|
|
2b80dbad47 | ||
|
|
7c7b64094b | ||
|
|
e32d103e61 | ||
|
|
4782ecf53a | ||
|
|
a9036e6fc2 | ||
|
|
189fc18448 | ||
|
|
2e19b4af92 | ||
|
|
fe05f7e71d | ||
|
|
5f65eb32fd | ||
|
|
c171f15316 | ||
|
|
0264370b38 | ||
|
|
39e4f655b1 | ||
|
|
87a370ee76 | ||
|
|
b9dc2b82a6 | ||
|
|
7b94cd77f9 | ||
|
|
ac45f1b454 | ||
|
|
2adfce27b0 | ||
|
|
bdbda2b570 | ||
|
|
5f18b3e772 | ||
|
|
631342ba04 | ||
|
|
cb1ca0b671 | ||
|
|
119f6384b6 | ||
|
|
f292310775 | ||
|
|
60ae9a3716 | ||
|
|
3307f57f36 | ||
|
|
359484baa3 | ||
|
|
4854dcb759 | ||
|
|
ae4fd1f093 | ||
|
|
ececffef3f | ||
|
|
5699f8678e | ||
|
|
758fef325e | ||
|
|
b0dc258455 | ||
|
|
d197d01971 | ||
|
|
86a5ddfbc4 | ||
|
|
7edb0ceded | ||
|
|
b70da1148e | ||
|
|
934507dd36 | ||
|
|
03dcd4c8b0 | ||
|
|
9dd292dafc | ||
|
|
34a3e409ab | ||
|
|
6beb031907 | ||
|
|
72c369a578 | ||
|
|
1c0958f33c | ||
|
|
dee370a4cb | ||
|
|
40f70a42d4 | ||
|
|
4ab46d94db | ||
|
|
e77372c913 | ||
|
|
a471854d71 | ||
|
|
6e07f3b33a | ||
|
|
45096558fb | ||
|
|
8dac69ebf9 | ||
|
|
5c29a9c064 |
134
Client/Client.cs
Normal file
134
Client/Client.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
using System;
|
||||
using System.Net.Sockets;
|
||||
using ProftaakRH;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
class Client : IDataReceiver
|
||||
{
|
||||
private TcpClient client;
|
||||
private NetworkStream stream;
|
||||
private byte[] buffer = new byte[1024];
|
||||
private int bytesReceived;
|
||||
private bool connected;
|
||||
private byte clientId = 0;
|
||||
|
||||
|
||||
public Client() : this("localhost", 5555)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
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 OnConnect(IAsyncResult ar)
|
||||
{
|
||||
this.client.EndConnect(ar);
|
||||
Console.WriteLine("Verbonden!");
|
||||
|
||||
|
||||
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);
|
||||
|
||||
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)
|
||||
{
|
||||
throw new OutOfMemoryException("buffer to small");
|
||||
}
|
||||
|
||||
if (expectedMessageLength > this.bytesReceived + receivedBytes)
|
||||
{
|
||||
//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);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//message completely arrived
|
||||
if (expectedMessageLength != this.bytesReceived + receivedBytes)
|
||||
{
|
||||
Console.WriteLine("something has gone completely wrong");
|
||||
}
|
||||
|
||||
string identifier;
|
||||
bool isJson = DataParser.getJsonIdentifier(this.buffer, out identifier);
|
||||
if (isJson)
|
||||
{
|
||||
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
else if (DataParser.isRawData(this.buffer))
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null);
|
||||
|
||||
}
|
||||
|
||||
private void OnWrite(IAsyncResult ar)
|
||||
{
|
||||
this.stream.EndWrite(ar);
|
||||
}
|
||||
|
||||
#region interface
|
||||
//maybe move this to other place
|
||||
public void BPM(byte[] bytes)
|
||||
{
|
||||
if (bytes == null)
|
||||
{
|
||||
throw new ArgumentNullException("no bytes");
|
||||
}
|
||||
byte[] message = DataParser.GetRawDataMessage(bytes, clientId);
|
||||
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
|
||||
}
|
||||
|
||||
public void Bike(byte[] bytes)
|
||||
{
|
||||
if (bytes == null)
|
||||
{
|
||||
throw new ArgumentNullException("no bytes");
|
||||
}
|
||||
byte[] message = DataParser.GetRawDataMessage(bytes, clientId);
|
||||
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public bool IsConnected()
|
||||
{
|
||||
return this.connected;
|
||||
}
|
||||
}
|
||||
}
|
||||
17
Client/Client.csproj
Normal file
17
Client/Client.csproj
Normal file
@@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Message\Message.csproj" />
|
||||
<ProjectReference Include="..\ProftaakRH\ProftaakRH.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
126
Client/DataParser.cs
Normal file
126
Client/DataParser.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
public class DataParser
|
||||
{
|
||||
/// <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",
|
||||
data = new
|
||||
{
|
||||
username = mUsername,
|
||||
password = mPassword,
|
||||
}
|
||||
};
|
||||
|
||||
return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json));
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
throw new ArgumentException("bytes to short");
|
||||
}
|
||||
byte messageId = bytes[4];
|
||||
|
||||
if (messageId == 1)
|
||||
{
|
||||
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(6).ToArray()));
|
||||
identifier = json.identifier;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
identifier = "";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
throw new ArgumentException("bytes to short");
|
||||
}
|
||||
return bytes[4] == 0x02;
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public static byte[] getMessage(byte[] payload, byte messageId, byte clientId)
|
||||
{
|
||||
byte[] res = new byte[payload.Length + 6];
|
||||
|
||||
Array.Copy(BitConverter.GetBytes(payload.Length + 6), 0, res, 0, 4);
|
||||
res[4] = messageId;
|
||||
res[5] = clientId;
|
||||
Array.Copy(payload, 0, res, 6, payload.Length);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/// <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, byte clientId)
|
||||
{
|
||||
return getMessage(payload, 0x02, 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, byte clientId)
|
||||
{
|
||||
return getMessage(payload, 0x01, 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, byte clientId)
|
||||
{
|
||||
return getJsonMessage(Encoding.ASCII.GetBytes(message), clientId);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
34
Client/Program.cs
Normal file
34
Client/Program.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using Hardware;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello World!");
|
||||
//connect fiets?
|
||||
|
||||
|
||||
Client client = new Client();
|
||||
|
||||
|
||||
while (!client.IsConnected())
|
||||
{
|
||||
|
||||
}
|
||||
BLEHandler bLEHandler = new BLEHandler(client);
|
||||
|
||||
bLEHandler.Connect();
|
||||
|
||||
//BikeSimulator bikeSimulator = new BikeSimulator(client);
|
||||
|
||||
//bikeSimulator.StartSimulation();
|
||||
|
||||
while (true)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
68
Message/Message.cs
Normal file
68
Message/Message.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
|
||||
namespace Message
|
||||
{
|
||||
/// <summary>
|
||||
/// Message class to handle traffic between clients and server
|
||||
/// </summary>
|
||||
public class Message
|
||||
{
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// identifier for the message
|
||||
/// </summary>
|
||||
public Identifier Identifier
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// payload of the message, the actual text
|
||||
/// </summary>
|
||||
public string Payload
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// constructs a new message with the given parameters
|
||||
/// </summary>
|
||||
/// <param name="identifier">the identifier</param>
|
||||
/// <param name="payload">the payload</param>
|
||||
public Message(Identifier identifier, string payload)
|
||||
{
|
||||
this.Identifier = identifier;
|
||||
this.Payload = payload;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// serializes this object to a JSON string
|
||||
/// </summary>
|
||||
/// <returns>a JSON representation of this object</returns>
|
||||
public string Serialize()
|
||||
{
|
||||
return JsonConvert.SerializeObject(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// deserializes a JSON string into a new Message object
|
||||
/// </summary>
|
||||
/// <param name="json">the JSON string to deserialize</param>
|
||||
/// <returns>a new <c>Message</c> object from the JSON string</returns>
|
||||
public static Message Deserialize(string json)
|
||||
{
|
||||
return (Message)JsonConvert.DeserializeObject(json);
|
||||
}
|
||||
}
|
||||
|
||||
public enum Identifier
|
||||
{
|
||||
LOGIN,
|
||||
CHAT,
|
||||
}
|
||||
}
|
||||
12
Message/Message.csproj
Normal file
12
Message/Message.csproj
Normal file
@@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -4,22 +4,32 @@ using System.Text;
|
||||
using Avans.TI.BLE;
|
||||
using System.Threading;
|
||||
using System.Security.Cryptography;
|
||||
using ProftaakRH;
|
||||
|
||||
namespace Hardware
|
||||
{
|
||||
class BLEHandler
|
||||
/// <summary>
|
||||
/// <c>BLEHandler</c> class that handles connection and traffic to and from the bike
|
||||
/// </summary>
|
||||
public class BLEHandler
|
||||
{
|
||||
IDataConverter dataConverter;
|
||||
IDataReceiver dataReceiver;
|
||||
private BLE bleBike;
|
||||
private BLE bleHeart;
|
||||
public bool Running { get; set; }
|
||||
|
||||
public BLEHandler(IDataConverter dataConverter)
|
||||
/// <summary>
|
||||
/// Makes a new BLEHandler object
|
||||
/// </summary>
|
||||
/// <param name="dataReceiver">the dataconverter object</param>
|
||||
public BLEHandler(IDataReceiver dataReceiver)
|
||||
{
|
||||
this.dataConverter = dataConverter;
|
||||
bool running = false;
|
||||
this.dataReceiver = dataReceiver;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks for available devices to connect to, and if one is found, it connects to it
|
||||
/// </summary>
|
||||
public void Connect()
|
||||
{
|
||||
BLE bleBike = new BLE();
|
||||
@@ -40,6 +50,11 @@ namespace Hardware
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Connects to the device with the given name
|
||||
/// </summary>
|
||||
/// <param name="deviceName">The name of the device to connect to</param>
|
||||
public async void Connect(string deviceName)
|
||||
{
|
||||
int errorCode = 0;
|
||||
@@ -95,23 +110,26 @@ namespace Hardware
|
||||
|
||||
Console.WriteLine("connected to BLE");
|
||||
this.Running = true;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Callback for when the subscription value of the ble bike has changed
|
||||
/// </summary>
|
||||
/// <param name="sender"> the sender object</param>
|
||||
/// <param name="e">the value changed event</param>
|
||||
private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
|
||||
{
|
||||
//Console.WriteLine("Received from {0}: {1}", e.ServiceName,
|
||||
// BitConverter.ToString(e.Data).Replace("-", " "));
|
||||
//send to dataconverter
|
||||
|
||||
if (e.ServiceName == "6e40fec2-b5a3-f393-e0a9-e50e24dcca9e")
|
||||
{
|
||||
byte[] payload = new byte[8];
|
||||
Array.Copy(e.Data, 4, payload, 0, 8);
|
||||
this.dataConverter.Bike(payload);
|
||||
this.dataReceiver.Bike(payload);
|
||||
}
|
||||
else if (e.ServiceName == "00002a37-0000-1000-8000-00805f9b34fb")
|
||||
{
|
||||
this.dataConverter.BPM(e.Data);
|
||||
this.dataReceiver.BPM(e.Data);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -120,6 +138,9 @@ namespace Hardware
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes of the current BLE object, if it exists.
|
||||
/// </summary>
|
||||
private void disposeBLE()
|
||||
{
|
||||
this.bleBike?.Dispose();
|
||||
@@ -127,6 +148,10 @@ namespace Hardware
|
||||
this.Running = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method <c>setResistance</c> converts the input percentage to bytes and sends it to the bike.
|
||||
/// </summary>
|
||||
/// <param name="percentage">The precentage of resistance to set</param>
|
||||
public void setResistance(float percentage)
|
||||
{
|
||||
byte[] antMessage = new byte[13];
|
||||
@@ -140,7 +165,7 @@ namespace Hardware
|
||||
antMessage[i] = 0xFF;
|
||||
}
|
||||
antMessage[11] = (byte)Math.Max(Math.Min(Math.Round(percentage / 0.5), 255), 0);
|
||||
//antMessage[11] = 50; //hardcoded for testing
|
||||
|
||||
|
||||
byte checksum = 0;
|
||||
for (int i = 0; i < 12; i++)
|
||||
@@ -151,7 +176,7 @@ namespace Hardware
|
||||
antMessage[12] = checksum;//reminder that i am dumb :P
|
||||
|
||||
|
||||
bleBike.WriteCharacteristic("6E40FEC3-B5A3-F393-E0A9-E50E24DCCA9E", antMessage);
|
||||
bleBike.WriteCharacteristic("6e40fec3-b5a3-f393-e0a9-e50e24dcca9e", antMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using LibNoise.Primitive;
|
||||
using ProftaakRH;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -9,9 +10,9 @@ using System.Threading;
|
||||
|
||||
namespace Hardware.Simulators
|
||||
{
|
||||
class BikeSimulator : IHandler
|
||||
public class BikeSimulator : IHandler
|
||||
{
|
||||
IDataConverter dataConverter;
|
||||
IDataReceiver dataReceiver;
|
||||
private int elapsedTime = 0;
|
||||
private int eventCounter = 0;
|
||||
private double distanceTraveled = 0;
|
||||
@@ -20,16 +21,20 @@ namespace Hardware.Simulators
|
||||
private int BPM = 0;
|
||||
private int cadence = 0;
|
||||
private double resistance = 0;
|
||||
private double power;
|
||||
private double accPower;
|
||||
|
||||
//Array for the speed bytes
|
||||
byte[] array;
|
||||
byte[] speedArray;
|
||||
byte[] powerArray;
|
||||
byte[] accPowerArray;
|
||||
|
||||
|
||||
|
||||
public BikeSimulator(IDataConverter dataConverter)
|
||||
public BikeSimulator(IDataReceiver dataReceiver)
|
||||
{
|
||||
this.dataConverter = dataConverter;
|
||||
this.dataReceiver = dataReceiver;
|
||||
}
|
||||
|
||||
public void StartSimulation()
|
||||
{
|
||||
//Example BLE Message
|
||||
@@ -38,20 +43,16 @@ namespace Hardware.Simulators
|
||||
float x = 0.0f;
|
||||
|
||||
//Perlin for Random values
|
||||
ImprovedPerlin improvedPerlin = new ImprovedPerlin(0,LibNoise.NoiseQuality.Best);
|
||||
|
||||
ImprovedPerlin improvedPerlin = new ImprovedPerlin(0, LibNoise.NoiseQuality.Best);
|
||||
|
||||
while (true)
|
||||
{
|
||||
CalculateVariables(improvedPerlin.GetValue(x)+1);
|
||||
|
||||
Console.WriteLine("BikeSimulation:\nSpeed: " + this.speed / 100 + "m/s\t" + this.BPM + "BPM\n"+"Resis: "+ this.resistance+"%\n");
|
||||
CalculateVariables(improvedPerlin.GetValue(x) + 1);
|
||||
|
||||
//Simulate sending data
|
||||
dataConverter.Bike(GenerateBike0x19());
|
||||
dataConverter.Bike(GenerateBike0x10());
|
||||
dataConverter.BPM(GenerateHeart());
|
||||
|
||||
|
||||
dataReceiver.Bike(GenerateBike0x19());
|
||||
dataReceiver.Bike(GenerateBike0x10());
|
||||
dataReceiver.BPM(GenerateHeart());
|
||||
|
||||
Thread.Sleep(1000);
|
||||
|
||||
@@ -65,21 +66,22 @@ namespace Hardware.Simulators
|
||||
//Generate an ANT message for page 0x19
|
||||
private byte[] GenerateBike0x19()
|
||||
{
|
||||
byte[] bikeByte = { 0x19, Convert.ToByte(eventCounter%256), 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
byte statByte = (byte)(powerArray[1] >> 4);
|
||||
byte[] bikeByte = { 0x19, Convert.ToByte(eventCounter % 256), Convert.ToByte(cadence % 254), accPowerArray[0], accPowerArray[1], powerArray[0], statByte, 0x20 };
|
||||
return bikeByte;
|
||||
}
|
||||
|
||||
//Generate an ANT message for page 0x10
|
||||
private byte[] GenerateBike0x10()
|
||||
{
|
||||
byte[] bikeByte = { 0x10, Convert.ToByte(equipmentType), Convert.ToByte(elapsedTime*4%64), Convert.ToByte(distanceTraveled), array[0], array[1], Convert.ToByte(BPM), 0xFF };
|
||||
byte[] bikeByte = { 0x10, Convert.ToByte(equipmentType), Convert.ToByte(elapsedTime * 4 % 64), Convert.ToByte(distanceTraveled), speedArray[0], speedArray[1], Convert.ToByte(BPM), 0xFF };
|
||||
return bikeByte;
|
||||
}
|
||||
|
||||
//Generate an ANT message for BPM
|
||||
private byte[] GenerateHeart()
|
||||
{
|
||||
byte[] hartByte = { 0x00, Convert.ToByte(BPM)};
|
||||
byte[] hartByte = { 0x00, Convert.ToByte(BPM) };
|
||||
return hartByte;
|
||||
}
|
||||
|
||||
@@ -114,22 +116,26 @@ namespace Hardware.Simulators
|
||||
//Input perlin value
|
||||
private void CalculateVariables(float perlin)
|
||||
{
|
||||
this.speed = perlin * 5 / 0.01 ;
|
||||
this.speed = perlin * 5 / 0.01;
|
||||
short sped = (short)speed;
|
||||
array = BitConverter.GetBytes(sped);
|
||||
this.distanceTraveled = (distanceTraveled+(speed*0.01)) % 256;
|
||||
this.BPM = (int) (perlin * 80);
|
||||
this.cadence = (int)speed * 4;
|
||||
|
||||
speedArray = BitConverter.GetBytes(sped);
|
||||
this.distanceTraveled = (distanceTraveled + (speed * 0.01)) % 256;
|
||||
this.BPM = (int)(perlin * 80);
|
||||
this.cadence = (int)speed / 6;
|
||||
this.power = ((1 + resistance) * speed) / 14 % 4094;
|
||||
this.accPower = (this.accPower + this.power) % 65536;
|
||||
// TO DO power to power LSB & MSN
|
||||
powerArray = BitConverter.GetBytes((short)this.power);
|
||||
accPowerArray = BitConverter.GetBytes((short)accPower);
|
||||
}
|
||||
|
||||
//Set resistance in simulated bike
|
||||
public void setResistance(byte[] bytes)
|
||||
{
|
||||
//TODO check if message is correct
|
||||
if(bytes.Length == 13)
|
||||
if (bytes.Length == 13)
|
||||
{
|
||||
this.resistance = Convert.ToDouble(bytes[11])/2;
|
||||
this.resistance = Convert.ToDouble(bytes[11]) / 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
using System;
|
||||
using ProftaakRH;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Hardware
|
||||
{
|
||||
class DataConverter : IDataConverter
|
||||
/// <summary>
|
||||
/// DataConverter class that handles all conversion of received data from the BLE bike.
|
||||
/// </summary>
|
||||
class DataConverter : IDataReceiver
|
||||
{
|
||||
/// <summary>
|
||||
/// Receives, parses and displays any incoming data from the bike.
|
||||
/// </summary>
|
||||
/// <param name="bytes">the array of bytes that was received</param>
|
||||
public void Bike(byte[] bytes)
|
||||
{
|
||||
if (bytes == null)
|
||||
@@ -15,7 +23,7 @@ namespace Hardware
|
||||
else
|
||||
if (bytes.Length == 8)
|
||||
{
|
||||
|
||||
|
||||
switch (bytes[0])
|
||||
{
|
||||
case 0x10:
|
||||
@@ -30,7 +38,7 @@ namespace Hardware
|
||||
Console.WriteLine($"Speed is : {input * 0.01}m/s (Range 65.534m/4)");
|
||||
if (bytes[6] != 0xFF)
|
||||
{
|
||||
Console.WriteLine("Heart rate byte: {0}", Convert.ToString(bytes[6],2));
|
||||
Console.WriteLine("Heart rate byte: {0}", Convert.ToString(bytes[6], 2));
|
||||
}
|
||||
break;
|
||||
case 0x19:
|
||||
@@ -38,22 +46,23 @@ namespace Hardware
|
||||
if (bytes[2] != 0xFF)
|
||||
{
|
||||
Console.WriteLine($"Instantaneous cadence: {bytes[2]} RPM (Range 0-254)");
|
||||
|
||||
|
||||
}
|
||||
int accumPower = bytes[3] | (bytes[4] << 8);
|
||||
|
||||
|
||||
Console.WriteLine($"Accumulated power: {accumPower} watt (Rollover 65536)");
|
||||
|
||||
int instantPower = (bytes[5]) | (bytes[6]>>4)<<8;
|
||||
|
||||
int instantPower = (bytes[5]) | (bytes[6] & 0b00001111) << 8;
|
||||
|
||||
|
||||
if (instantPower != 0xFFF)
|
||||
Console.WriteLine($"Instant power: {instantPower} watt (Range 0-4094)");
|
||||
Console.WriteLine($"Instant power: {instantPower} watt (Range 0-4094)");
|
||||
|
||||
int trainerStatus = bytes[6] & 0b00001111; // bit 4-7
|
||||
int trainerStatus = bytes[6] & 0b11110000; // bit 4-7
|
||||
int flags = bytes[7] >> 4;
|
||||
int FEState = bytes[7] & 0b00001111;
|
||||
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -68,6 +77,10 @@ namespace Hardware
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets and prints the BPM from the message received from the bike.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The array with bytes that was received</param>
|
||||
public void BPM(byte[] bytes)
|
||||
{
|
||||
if (bytes == null)
|
||||
@@ -91,10 +104,4 @@ namespace Hardware
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
interface IDataConverter
|
||||
{
|
||||
void BPM(byte[] bytes);
|
||||
void Bike(byte[] bytes);
|
||||
}
|
||||
}
|
||||
|
||||
12
ProftaakRH/IDataReceiver.cs
Normal file
12
ProftaakRH/IDataReceiver.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace ProftaakRH
|
||||
{
|
||||
public interface IDataReceiver
|
||||
{
|
||||
void BPM(byte[] bytes);
|
||||
void Bike(byte[] bytes);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Hardware;
|
||||
@@ -7,16 +7,38 @@ using Hardware.Simulators;
|
||||
|
||||
namespace ProftaakRH
|
||||
{
|
||||
class Program
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] agrs)
|
||||
{
|
||||
IDataConverter dataConverter = new DataConverter();
|
||||
BikeSimulator bikeSimulator = new BikeSimulator(dataConverter);
|
||||
bikeSimulator.setResistance(bikeSimulator.GenerateResistance(1f));
|
||||
bikeSimulator.StartSimulation();
|
||||
IDataReceiver dataReceiver = new DataConverter();
|
||||
BLEHandler bLEHandler = new BLEHandler(dataReceiver);
|
||||
//BikeSimulator bikeSimulator = new BikeSimulator(dataConverter);
|
||||
//bikeSimulator.setResistance(bikeSimulator.GenerateResistance(1f));
|
||||
//bikeSimulator.StartSimulation();
|
||||
|
||||
Console.ReadLine();
|
||||
|
||||
bool running = true;
|
||||
while (running)
|
||||
{
|
||||
string input = Console.ReadLine();
|
||||
input.ToLower();
|
||||
input.Trim();
|
||||
if (input == "quit")
|
||||
{
|
||||
running = false;
|
||||
break;
|
||||
}
|
||||
try
|
||||
{
|
||||
int resistance = Int32.Parse(input);
|
||||
bLEHandler.setResistance(resistance);
|
||||
}
|
||||
catch
|
||||
{
|
||||
//do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="LibNoise" Version="0.2.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="BLELibrary">
|
||||
|
||||
@@ -3,7 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30413.136
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProftaakRH", "ProftaakRH.csproj", "{0F053CC5-D969-4970-9501-B3428EA3D777}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProftaakRH", "ProftaakRH.csproj", "{0F053CC5-D969-4970-9501-B3428EA3D777}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RH-Engine", "..\RH-Engine\RH-Engine.csproj", "{984E295E-47A2-41E7-90E5-50FDB9E67694}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "..\Server\Server.csproj", "{B1AB6F51-A20D-4162-9A7F-B3350B7510FD}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "..\Client\Client.csproj", "{5759DD20-7A4F-4D8D-B986-A70A7818C112}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Message", "..\Message\Message.csproj", "{9ED6832D-B0FB-4460-9BCD-FAA58863B0CE}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -15,6 +23,22 @@ Global
|
||||
{0F053CC5-D969-4970-9501-B3428EA3D777}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0F053CC5-D969-4970-9501-B3428EA3D777}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0F053CC5-D969-4970-9501-B3428EA3D777}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{984E295E-47A2-41E7-90E5-50FDB9E67694}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{984E295E-47A2-41E7-90E5-50FDB9E67694}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{984E295E-47A2-41E7-90E5-50FDB9E67694}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{984E295E-47A2-41E7-90E5-50FDB9E67694}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{B1AB6F51-A20D-4162-9A7F-B3350B7510FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{B1AB6F51-A20D-4162-9A7F-B3350B7510FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B1AB6F51-A20D-4162-9A7F-B3350B7510FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B1AB6F51-A20D-4162-9A7F-B3350B7510FD}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{5759DD20-7A4F-4D8D-B986-A70A7818C112}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{5759DD20-7A4F-4D8D-B986-A70A7818C112}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{5759DD20-7A4F-4D8D-B986-A70A7818C112}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{5759DD20-7A4F-4D8D-B986-A70A7818C112}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{9ED6832D-B0FB-4460-9BCD-FAA58863B0CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{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
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
52
ProftaakRH/VRMessage.cs
Normal file
52
ProftaakRH/VRMessage.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace RH_Engine
|
||||
{
|
||||
class VRMessage
|
||||
{
|
||||
public VRMessage(string id, params JObject[] data)
|
||||
{
|
||||
this.Id = id;
|
||||
this.Data = data;
|
||||
}
|
||||
|
||||
public string Id
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public JObject[] Data
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public string GetCommand()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
|
||||
using (JsonWriter writer = new JsonTextWriter(sw))
|
||||
{
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("id");
|
||||
writer.WriteValue(this.Id);
|
||||
writer.WritePropertyName("data");
|
||||
writer.WriteStartArray();
|
||||
foreach (JObject o in Data)
|
||||
{
|
||||
writer.WriteValue(o);
|
||||
}
|
||||
writer.WriteEndArray();
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
329
RH-Engine/Command.cs
Normal file
329
RH-Engine/Command.cs
Normal file
@@ -0,0 +1,329 @@
|
||||
using LibNoise.Primitive;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace RH_Engine
|
||||
{
|
||||
class Command
|
||||
{
|
||||
public const string STANDARD_HEAD = "Head";
|
||||
public const string STANDARD_GROUND = "GroundPlane";
|
||||
public const string STANDARD_SUN = "SunLight";
|
||||
public const string STANDARD_LEFTHAND = "LeftHand";
|
||||
public const string STANDARD_RIGHTHAND = "RightHand";
|
||||
|
||||
|
||||
|
||||
string tunnelID;
|
||||
|
||||
public Command(string tunnelID)
|
||||
{
|
||||
this.tunnelID = tunnelID;
|
||||
}
|
||||
|
||||
public string TerrainCommand(int[] sizeArray, float[] heightsArray)
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/terrain/add",
|
||||
data = new
|
||||
{
|
||||
size = sizeArray,
|
||||
heights = heightsArray
|
||||
}
|
||||
|
||||
};
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
public string AddLayer(string uid, string texture)
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/node/addlayer",
|
||||
data = new
|
||||
{
|
||||
id = uid,
|
||||
diffuse = @"C:\Users\woute\Downloads\NetworkEngine.18.10.10.1\NetworkEngine\data\NetworkEngine\textures\terrain\adesert_cracks_d.jpg",
|
||||
normal = @"C:\Users\woute\Downloads\NetworkEngine.18.10.10.1\NetworkEngine\data\NetworkEngine\textures\terrain\adesert_mntn_d.jpg",
|
||||
minHeight = 0,
|
||||
maxHeight = 10,
|
||||
fadeDist = 1
|
||||
}
|
||||
};
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
public string UpdateTerrain()
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/terrain/update",
|
||||
data = new
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
|
||||
public string AddNodeCommand()
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/node/add",
|
||||
data = new
|
||||
{
|
||||
name = "newNode",
|
||||
components = new
|
||||
{
|
||||
terrain = new
|
||||
{
|
||||
smoothnormals = true
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
|
||||
public string DeleteNode(string uuid)
|
||||
{
|
||||
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/node/delete",
|
||||
data = new
|
||||
{
|
||||
id = uuid,
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
|
||||
}
|
||||
|
||||
public string AddBikeModel()
|
||||
{
|
||||
return AddModel("bike", "data\\NetworkEngine\\models\\bike\\bike.fbx");
|
||||
}
|
||||
|
||||
public string AddModel(string nodeName, string fileLocation)
|
||||
{
|
||||
return AddModel(nodeName, fileLocation, null, new float[] { 0, 0, 0 }, 1, new float[] { 0, 0, 0 });
|
||||
}
|
||||
|
||||
public string AddModel(string nodeName, string fileLocation, float[] positionVector, float scalar, float[] rotationVector)
|
||||
{
|
||||
return AddModel(nodeName, fileLocation, null, positionVector, scalar, rotationVector);
|
||||
}
|
||||
|
||||
public string AddModel(string nodeName, string fileLocation, string animationLocation, float[] positionVector, float scalar, float[] rotationVector)
|
||||
{
|
||||
string namename = nodeName;
|
||||
bool animatedBool = false;
|
||||
if (animationLocation != null)
|
||||
{
|
||||
animatedBool = true;
|
||||
}
|
||||
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/node/add",
|
||||
data = new
|
||||
{
|
||||
name = namename,
|
||||
components = new
|
||||
{
|
||||
transform = new
|
||||
{
|
||||
position = positionVector,
|
||||
scale = scalar,
|
||||
rotation = rotationVector
|
||||
|
||||
},
|
||||
model = new
|
||||
{
|
||||
file = fileLocation,
|
||||
cullbackfaces = true,
|
||||
animated = animatedBool,
|
||||
animation = animationLocation
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
|
||||
public string MoveTo(string uuid, float[] positionVector, float rotateValue, float speedValue, float timeValue)
|
||||
{
|
||||
return MoveTo(uuid, "idk", positionVector, rotateValue, "linear", false, speedValue, timeValue);
|
||||
}
|
||||
|
||||
private string MoveTo(string uuid, string stopValue, float[] positionVector, float rotateValue, string interpolateValue, bool followHeightValue, float speedValue, float timeValue)
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/node/moveto",
|
||||
data = new
|
||||
{
|
||||
id = uuid,
|
||||
stop = stopValue,
|
||||
position = positionVector,
|
||||
rotate = rotateValue,
|
||||
interpolate = interpolateValue,
|
||||
followheight = followHeightValue,
|
||||
speed = speedValue,
|
||||
time = timeValue
|
||||
}
|
||||
};
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
|
||||
|
||||
public string RouteCommand()
|
||||
{
|
||||
ImprovedPerlin improvedPerlin = new ImprovedPerlin(4325, LibNoise.NoiseQuality.Best);
|
||||
Random r = new Random();
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "route/add",
|
||||
data = new
|
||||
{
|
||||
nodes = new dynamic[]
|
||||
{
|
||||
new
|
||||
{
|
||||
/*pos = GetPos(0.6f, improvedPerlin)*/
|
||||
pos = new int[] {0,0,5 },
|
||||
dir = new int[] { r.Next(20,100),0,-r.Next(20, 100) }
|
||||
},
|
||||
new
|
||||
{
|
||||
//pos = GetPos(1.6f, improvedPerlin),
|
||||
pos = new int[] {50,0,0 },
|
||||
dir = new int[] { r.Next(20, 100),0,r.Next(20, 100) }
|
||||
},
|
||||
new
|
||||
{
|
||||
//pos = GetPos(2.654f, improvedPerlin),
|
||||
pos = new int[] {20,0,20 },
|
||||
dir = new int[] { r.Next(20, 100),0,r.Next(20, 100) }
|
||||
},
|
||||
new
|
||||
{
|
||||
//pos = GetPos(3.6543f, improvedPerlin),
|
||||
pos = new int[] {10,0,50 },
|
||||
dir = new int[] { -r.Next(3,7),0,r.Next(3,7) }
|
||||
},
|
||||
new
|
||||
{
|
||||
pos = new int[] {0,0,50 },
|
||||
dir = new int[] { -r.Next(20, 50),0,-r.Next(20, 50) }
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Console.WriteLine("route command: " + JsonConvert.SerializeObject(Payload(payload)));
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
|
||||
private float[] GetPos(float n, ImprovedPerlin improvedPerlin)
|
||||
{
|
||||
float[] res = new float[] { improvedPerlin.GetValue(n) * 50, 0, improvedPerlin.GetValue(n) * 50 };
|
||||
return res;
|
||||
}
|
||||
|
||||
private int[] GetDir()
|
||||
{
|
||||
Random rng = new Random();
|
||||
int[] dir = {rng.Next(50), 0, rng.Next(50)};
|
||||
return dir;
|
||||
}
|
||||
|
||||
public string FollowRouteCommand()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
public string RoadCommand(string uuid_route)
|
||||
{
|
||||
Console.WriteLine("road");
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/road/add",
|
||||
data = new
|
||||
{
|
||||
route = uuid_route,
|
||||
diffuse = "data/NetworkEngine/textures/tarmac_diffuse.png",
|
||||
normal = "data/NetworkEngine/textures/tarmac_normale.png",
|
||||
specular = "data/NetworkEngine/textures/tarmac_specular.png",
|
||||
heightoffset = 1f
|
||||
}
|
||||
};
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
|
||||
public string GetSceneInfoCommand()
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/get"
|
||||
};
|
||||
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
|
||||
public string ResetScene()
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/reset"
|
||||
};
|
||||
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
}
|
||||
|
||||
public string SkyboxCommand(double timeToSet)
|
||||
{
|
||||
if (timeToSet < 0 || timeToSet > 24)
|
||||
{
|
||||
throw new Exception("The time must be between 0 and 24!");
|
||||
}
|
||||
|
||||
|
||||
dynamic payload = new
|
||||
{
|
||||
id = "scene/skybox/settime",
|
||||
data = new
|
||||
{
|
||||
time = timeToSet
|
||||
}
|
||||
|
||||
};
|
||||
return JsonConvert.SerializeObject(Payload(payload));
|
||||
|
||||
}
|
||||
|
||||
|
||||
private object Payload(dynamic message)
|
||||
{
|
||||
return new
|
||||
{
|
||||
id = "tunnel/send",
|
||||
data = new
|
||||
{
|
||||
dest = tunnelID,
|
||||
data = message,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
74
RH-Engine/JSONParser.cs
Normal file
74
RH-Engine/JSONParser.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace RH_Engine
|
||||
{
|
||||
class JSONParser
|
||||
{
|
||||
/// <summary>
|
||||
/// returns all the users from the given response
|
||||
/// </summary>
|
||||
/// <param name="msg">the message gotten from the server, without the length prefix</param>
|
||||
/// <returns></returns>
|
||||
public static PC[] GetUsers(string msg)
|
||||
{
|
||||
dynamic jsonData = JsonConvert.DeserializeObject(msg);
|
||||
Newtonsoft.Json.Linq.JArray data = jsonData.data;
|
||||
PC[] res = new PC[data.Count];
|
||||
int counter = 0;
|
||||
foreach (dynamic d in data)
|
||||
{
|
||||
res[counter] = new PC((string)d.clientinfo.host, (string)d.clientinfo.user);
|
||||
counter++;
|
||||
}
|
||||
|
||||
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--)
|
||||
{
|
||||
dynamic d = data[i];
|
||||
foreach (PC pc in PCs)
|
||||
{
|
||||
if (d.clientinfo.host == pc.host && d.clientinfo.user == pc.user)
|
||||
{
|
||||
Console.WriteLine("connecting to {0}, on {1} with id {2}", pc.user, pc.host, d.id);
|
||||
return d.id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string GetTunnelID(string json)
|
||||
{
|
||||
dynamic jsonData = JsonConvert.DeserializeObject(json);
|
||||
if (jsonData.data.status == "ok")
|
||||
{
|
||||
return jsonData.data.id;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string GetRouteID(string json)
|
||||
{
|
||||
dynamic jsonData = JsonConvert.DeserializeObject(json);
|
||||
if (jsonData.data.status == "ok")
|
||||
{
|
||||
return jsonData.data.uuid;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
257
RH-Engine/Program.cs
Normal file
257
RH-Engine/Program.cs
Normal file
@@ -0,0 +1,257 @@
|
||||
using LibNoise.Primitive;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
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
|
||||
{
|
||||
|
||||
internal class Program
|
||||
{
|
||||
private static PC[] PCs = {
|
||||
//new PC("DESKTOP-M2CIH87", "Fabian"),
|
||||
new PC("T470S", "Shinichi"),
|
||||
//new PC("DESKTOP-DHS478C", "semme"),
|
||||
//new PC("DESKTOP-TV73FKO", "Wouter"),
|
||||
//new PC("DESKTOP-SINMKT1", "Ralf"),
|
||||
//new PC("NA", "Bart")
|
||||
};
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
TcpClient client = new TcpClient("145.48.6.10", 6666);
|
||||
|
||||
CreateConnection(client.GetStream());
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <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 static void WriteTextMessage(NetworkStream stream, 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);
|
||||
|
||||
//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)
|
||||
{
|
||||
WriteTextMessage(stream, "{\r\n\"id\" : \"session/list\"\r\n}");
|
||||
string id = JSONParser.GetSessionID(ReadPrefMessage(stream), PCs);
|
||||
|
||||
string tunnelCreate = "{\"id\" : \"tunnel/create\", \"data\" : {\"session\" : \"" + id + "\"}}";
|
||||
|
||||
WriteTextMessage(stream, tunnelCreate);
|
||||
|
||||
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>
|
||||
/// sends all the commands to the server
|
||||
/// </summary>
|
||||
/// <param name="stream">the network stream to use</param>
|
||||
/// <param name="tunnelID">the tunnel id to use</param>
|
||||
private static void sendCommands(NetworkStream stream, string tunnelID)
|
||||
{
|
||||
Command mainCommand = new Command(tunnelID);
|
||||
|
||||
|
||||
WriteTextMessage(stream, mainCommand.ResetScene());
|
||||
ReadPrefMessage(stream);
|
||||
string routeid = CreateRoute(stream, mainCommand);
|
||||
|
||||
WriteTextMessage(stream, mainCommand.TerrainCommand(new int[] { 256, 256 }, null));
|
||||
Console.WriteLine(ReadPrefMessage(stream));
|
||||
string command;
|
||||
|
||||
command = mainCommand.AddBikeModel();
|
||||
|
||||
WriteTextMessage(stream, command);
|
||||
|
||||
Console.WriteLine(ReadPrefMessage(stream));
|
||||
|
||||
command = mainCommand.AddModel("car", "data\\customModels\\TeslaRoadster.fbx");
|
||||
|
||||
WriteTextMessage(stream, command);
|
||||
|
||||
Console.WriteLine(ReadPrefMessage(stream));
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// gets the id of the object with the given name
|
||||
/// </summary>
|
||||
/// <param name="name">the name of the object</param>
|
||||
/// <param name="stream">the network stream to send requests to</param>
|
||||
/// <param name="createGraphics">the create graphics object to create all the commands</param>
|
||||
/// <returns> the uuid of the object with the given name, <c>null</c> otherwise.</returns>
|
||||
public static string GetId(string name, NetworkStream stream, Command createGraphics)
|
||||
{
|
||||
JArray children = GetChildren(stream, createGraphics);
|
||||
|
||||
foreach (dynamic child in children)
|
||||
{
|
||||
if (child.name == name)
|
||||
{
|
||||
return child.uuid;
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
float x = 0f;
|
||||
float[] height = new float[256 * 256];
|
||||
ImprovedPerlin improvedPerlin = new ImprovedPerlin(0, LibNoise.NoiseQuality.Best);
|
||||
for (int i = 0; i < 256 * 256; i++)
|
||||
{
|
||||
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>
|
||||
/// gets all the children in the current scene
|
||||
/// </summary>
|
||||
/// <param name="stream">the network stream to send requests to</param>
|
||||
/// <param name="createGraphics">the create graphics object to create all the commands</param>
|
||||
/// <returns>all the children objects in the current scene</returns>
|
||||
public static JArray GetChildren(NetworkStream stream, Command createGraphics)
|
||||
{
|
||||
WriteTextMessage(stream, createGraphics.GetSceneInfoCommand());
|
||||
dynamic response = JsonConvert.DeserializeObject(ReadPrefMessage(stream));
|
||||
return response.data.data.data.children;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// returns all objects in the current scene, as name-uuid tuples.
|
||||
/// </summary>
|
||||
/// <param name="stream">the network stream to send requests to</param>
|
||||
/// <param name="createGraphics">the create graphics object to create all the commands</param>
|
||||
/// <returns>an array of name-uuid tuples for each object</returns>
|
||||
public static (string, string)[] GetObjectsInScene(NetworkStream stream, Command createGraphics)
|
||||
{
|
||||
JArray children = GetChildren(stream, createGraphics);
|
||||
(string, string)[] res = new (string, string)[children.Count];
|
||||
|
||||
int i = 0;
|
||||
foreach (dynamic child in children)
|
||||
{
|
||||
res[i] = (child.name, child.uuid);
|
||||
i++;
|
||||
}
|
||||
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
public static string getUUIDFromResponse(string response)
|
||||
{
|
||||
dynamic JSON = JsonConvert.DeserializeObject(response);
|
||||
return JSON.data.data.data.uuid;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// struct used to store the host pc name and user
|
||||
/// </summary>
|
||||
public readonly struct PC
|
||||
{
|
||||
public PC(string host, string user)
|
||||
{
|
||||
this.host = host;
|
||||
this.user = user;
|
||||
}
|
||||
public string host { get; }
|
||||
public string user { get; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "PC - host:" + host + " - user:" + user;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
RH-Engine/RH-Engine.csproj
Normal file
14
RH-Engine/RH-Engine.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<RootNamespace>RH_Engine</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="LibNoise" Version="0.2.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
25
RH-Engine/RH-Engine.sln
Normal file
25
RH-Engine/RH-Engine.sln
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30503.244
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RH-Engine", "RH-Engine.csproj", "{12E8F82B-C464-4152-B4FB-FCB5E1A9FCFA}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{12E8F82B-C464-4152-B4FB-FCB5E1A9FCFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{12E8F82B-C464-4152-B4FB-FCB5E1A9FCFA}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{12E8F82B-C464-4152-B4FB-FCB5E1A9FCFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{12E8F82B-C464-4152-B4FB-FCB5E1A9FCFA}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {C75B8E99-BE3D-496F-B2F0-03C4069493B2}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
155
Server/Client.cs
Normal file
155
Server/Client.cs
Normal file
@@ -0,0 +1,155 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using Client;
|
||||
using Newtonsoft;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
class Client
|
||||
{
|
||||
private Communication communication;
|
||||
private TcpClient tcpClient;
|
||||
private NetworkStream stream;
|
||||
private byte[] buffer = new byte[1024];
|
||||
private byte[] totalBuffer = new byte[1024];
|
||||
private int bytesReceived;
|
||||
|
||||
|
||||
public string Username { get; set; }
|
||||
|
||||
public Client(Communication communication, TcpClient tcpClient)
|
||||
{
|
||||
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)
|
||||
{
|
||||
try
|
||||
{
|
||||
int receivedBytes = stream.EndRead(ar);
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
communication.Disconnect(this);
|
||||
return;
|
||||
}
|
||||
|
||||
int counter = 0;
|
||||
|
||||
while (buffer.Length > counter)
|
||||
{
|
||||
//Console.WriteLine(buffer.Length);
|
||||
byte[] lenghtBytes = new byte[4];
|
||||
Array.Copy(buffer, counter, lenghtBytes, 0, 4);
|
||||
int length = BitConverter.ToInt32(lenghtBytes);
|
||||
Console.WriteLine(buffer[5]);
|
||||
if (length == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if (buffer[counter + 4] == 0x02)
|
||||
{
|
||||
}
|
||||
else if (buffer[counter + 4] == 0x01)
|
||||
{
|
||||
byte[] packet = new byte[length];
|
||||
Console.WriteLine(Encoding.ASCII.GetString(buffer) + " " + length);
|
||||
Array.Copy(buffer, counter + 5, packet, 0, length);
|
||||
Console.WriteLine(Encoding.ASCII.GetString(packet));
|
||||
HandleData(Encoding.ASCII.GetString(packet));
|
||||
}
|
||||
|
||||
counter += length;
|
||||
}
|
||||
|
||||
Console.WriteLine("Done");
|
||||
|
||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
|
||||
}*/
|
||||
|
||||
private void OnRead(IAsyncResult ar)
|
||||
{
|
||||
int receivedBytes = this.stream.EndRead(ar);
|
||||
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 HandleData(string packet)
|
||||
{
|
||||
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)
|
||||
{
|
||||
dynamic payload = new
|
||||
{
|
||||
data = new
|
||||
{
|
||||
status = "ok"
|
||||
}
|
||||
};
|
||||
Message.Message message = new Message.Message(Message.Identifier.LOGIN, JsonConvert.SerializeObject(payload));
|
||||
Write(message.Serialize());
|
||||
}
|
||||
}
|
||||
|
||||
private void Write(string data)
|
||||
{
|
||||
byte[] bytes = DataParser.getMessage(Encoding.ASCII.GetBytes(data), 0x01, 0x01);
|
||||
stream.Write(bytes, 0, bytes.Length);
|
||||
stream.Flush();
|
||||
Console.WriteLine("Wrote message");
|
||||
}
|
||||
}
|
||||
}
|
||||
39
Server/Communication.cs
Normal file
39
Server/Communication.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Pipes;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
class Communication
|
||||
{
|
||||
private TcpListener listener;
|
||||
private List<Client> clients;
|
||||
|
||||
public Communication(TcpListener listener)
|
||||
{
|
||||
this.listener = listener;
|
||||
this.clients = new List<Client>();
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
listener.Start();
|
||||
listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null);
|
||||
}
|
||||
|
||||
private void OnConnect(IAsyncResult ar)
|
||||
{
|
||||
var tcpClient = listener.EndAcceptTcpClient(ar);
|
||||
Console.WriteLine($"Client connected from {tcpClient.Client.RemoteEndPoint}");
|
||||
clients.Add(new Client(this, tcpClient));
|
||||
listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null);
|
||||
}
|
||||
|
||||
internal void Disconnect(Client client)
|
||||
{
|
||||
clients.Remove(client);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
Server/Program.cs
Normal file
19
Server/Program.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Communication communication = new Communication(new TcpListener(IPAddress.Any, 5555));
|
||||
communication.Start();
|
||||
|
||||
while (true)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
16
Server/Server.csproj
Normal file
16
Server/Server.csproj
Normal file
@@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<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