added delegate for reading server response

This commit is contained in:
Sem van der Hoeven
2020-09-25 13:26:48 +02:00
parent 64773ffe1c
commit fac3987678
3 changed files with 95 additions and 4 deletions

View File

@@ -119,9 +119,9 @@ namespace RH_Engine
panel = new
{
size = new int[] { 1, 1 },
resolution = new int[] { 256, 256 },
background = new int[] { 1, 1, 1, 1 },
castShadow = true
resolution = new int[] { 512, 512 },
background = new int[] { 1, 0, 0, 0 },
castShadow = false
}
}
@@ -140,7 +140,7 @@ namespace RH_Engine
{
id = uuidPanel,
text = "Bike speed placeholder",
position = new int[] { 0, 0 },
position = new int[] { 100, 100 },
size = 32.0,
color = new int[] { 0, 0, 0, 1 },
font = "segoeui"
@@ -150,6 +150,20 @@ namespace RH_Engine
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

View File

@@ -19,16 +19,27 @@ namespace RH_Engine
//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 void Main(string[] args)
{
TcpClient client = new TcpClient("145.48.6.10", 6666);
CreateConnection(client.GetStream());
serverResponseReader = new ServerResponseReader(client.GetStream());
serverResponseReader.callback = HandleResponse;
}
public static void HandleResponse(string message)
{
}
@@ -146,6 +157,8 @@ namespace RH_Engine
Console.WriteLine(ReadPrefMessage(stream));
WriteTextMessage(stream, mainCommand.bikeSpeed(uuidPanel));
Console.WriteLine(ReadPrefMessage(stream));
WriteTextMessage(stream, mainCommand.SwapPanelCommand(uuidPanel));
}
/// <summary>

View File

@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace RH_Engine
{
public delegate void OnResponse(string response);
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
while (true)
{
string res = ReadPrefMessage(Stream);
this.callback(res);
}
});
}
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);
}
}
}