Merge remote-tracking branch 'origin/develop' into develop

This commit is contained in:
shinichi
2020-09-16 14:26:51 +02:00
2 changed files with 105 additions and 4 deletions

View File

@@ -10,20 +10,23 @@ namespace RH_Engine
class JSONParser class JSONParser
{ {
/// <summary> /// <summary>
/// parses the given response from the server into strings /// returns all the users from the given response
/// </summary> /// </summary>
/// <param name="msg">the message gotten from the server, without the length prefix</param> /// <param name="msg">the message gotten from the server, without the length prefix</param>
/// <returns></returns> /// <returns></returns>
public static string[] Parse(string msg) public static PC[] GetUsers(string msg)
{ {
dynamic jsonData = JsonConvert.DeserializeObject(msg); dynamic jsonData = JsonConvert.DeserializeObject(msg);
Newtonsoft.Json.Linq.JArray data = jsonData.data; Newtonsoft.Json.Linq.JArray data = jsonData.data;
PC[] res = new PC[data.Count];
int counter = 0;
foreach (dynamic d in data) foreach (dynamic d in data)
{ {
Console.WriteLine(d.clientinfo.host); res[counter] = new PC((string)d.clientinfo.host, (string)d.clientinfo.user);
counter++;
} }
return null; return res;
} }

98
RH-Engine/Program.cs Normal file
View File

@@ -0,0 +1,98 @@
using System;
using System.IO;
using System.Net.Sockets;
using System.Runtime.Intrinsics.X86;
using System.Text;
namespace RH_Engine
{
internal class Program
{
private static PC[] PCs = {
new PC("DESKTOP-M2CIH87", "Fabian"),
new PC("T470S", "Shinichi"),
new PC("NA", "Sem"),
new PC("NA", "Wouter"),
new PC("NA", "Ralf"),
new PC("NA", "Bart") };
private static void Main(string[] args)
{
TcpClient client = new TcpClient("145.48.6.10", 6666);
CreateConnection(client.GetStream());
}
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);
}
public static string ReadPrefMessage(NetworkStream stream)
{
byte[] lengthBytes = new byte[4];
stream.Read(lengthBytes, 0, 4);
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);
}
private static void CreateConnection(NetworkStream stream)
{
//WriteTextMessage(stream, "{\r\n\"id\" : \"session/list\"\r\n}");
//string msg = ReadPrefMessage(stream);
//Console.WriteLine(msg);
//string id = JSONParser.GetSessionID(msg, PCs);
//Console.WriteLine(id);
WriteTextMessage(stream, "{\r\n\"id\" : \"session/list\"\r\n}");
string result = ReadPrefMessage(stream);
//Console.WriteLine(result);
foreach (var s in JSONParser.GetUsers(result))
{
Console.WriteLine(s);
}
}
}
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;
}
}
}