From 7b05fcc8982fa5349a645b1294c09446223bf0ad Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 30 Sep 2020 12:01:53 +0200 Subject: [PATCH] added engineconnect as singleton --- Client/EngineConnect.cs | 97 +++++++++++++++++++++++++++++++ RH-Engine/ServerResponseReader.cs | 2 +- 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 Client/EngineConnect.cs diff --git a/Client/EngineConnect.cs b/Client/EngineConnect.cs new file mode 100644 index 0000000..78dc20c --- /dev/null +++ b/Client/EngineConnect.cs @@ -0,0 +1,97 @@ +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 sealed class EngineConnect + { + private static EngineConnect instance = null; + private static readonly object padlock = new object(); + + + 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 Dictionary serialResponses = new Dictionary(); + + + EngineConnect() + { + + } + + public static EngineConnect INSTANCE + { + get + { + lock (padlock) + { + if (instance == null) + { + instance = new EngineConnect(); + } + }return instance; + } + } + + public static void Connect() + { + TcpClient client = new TcpClient("145.48.6.10", 6666); + + CreateConnection(client.GetStream()); + } + + /// + /// connects to the server and creates the tunnel + /// + /// the network stream to use + private static void CreateConnection(NetworkStream stream) + { + initReader(stream); + + 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 + "\"}}"; + + WriteTextMessage(stream, tunnelCreate); + + // wait until we have a tunnel id + while (tunnelId == string.Empty) { } + Console.WriteLine("got tunnel id! sending commands..."); + sendCommands(stream, tunnelId); + } + } + + /// + /// initializes and starts the reading of the responses from the vr server + /// + /// the networkstream + private static void initReader(NetworkStream stream) + { + serverResponseReader = new ServerResponseReader(stream); + serverResponseReader.callback = HandleResponse; + serverResponseReader.StartRead(); + } +} diff --git a/RH-Engine/ServerResponseReader.cs b/RH-Engine/ServerResponseReader.cs index 85e7f00..feeef05 100644 --- a/RH-Engine/ServerResponseReader.cs +++ b/RH-Engine/ServerResponseReader.cs @@ -7,7 +7,7 @@ namespace RH_Engine { public delegate void OnResponse(string response); - class ServerResponseReader + public class ServerResponseReader { public OnResponse callback {