From 3ca3c1ad78b732f3645eae38fec377c2bd378d3a Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 30 Sep 2020 12:17:57 +0200 Subject: [PATCH] added engineconnection to client --- Client/Client.cs | 5 ++ Client/EngineConnection.cs | 171 +++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 Client/EngineConnection.cs diff --git a/Client/Client.cs b/Client/Client.cs index 123d222..3701599 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -14,6 +14,7 @@ namespace Client private bool connected; private byte[] totalBuffer = new byte[1024]; private int totalBufferReceived = 0; + private EngineConnection engineConnection; public Client() : this("localhost", 5555) @@ -26,6 +27,10 @@ namespace Client this.client = new TcpClient(); this.connected = false; client.BeginConnect(adress, port, new AsyncCallback(OnConnect), null); + + engineConnection = EngineConnection.INSTANCE; + engineConnection.Connect(); + } private void OnConnect(IAsyncResult ar) diff --git a/Client/EngineConnection.cs b/Client/EngineConnection.cs new file mode 100644 index 0000000..463dabc --- /dev/null +++ b/Client/EngineConnection.cs @@ -0,0 +1,171 @@ +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 EngineConnection + { + private static EngineConnection 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 NetworkStream stream; + + private static Dictionary serialResponses = new Dictionary(); + + 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(); + CreateConnection(); + } + + /// + /// connects to the server and creates the tunnel + /// + /// the network stream to use + private void CreateConnection() + { + initReader(); + + 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) { } + Write("got tunnel id! sending commands..."); + + } + /// + /// initializes and starts the reading of the responses from the vr server + /// + /// the networkstream + private void initReader() + { + serverResponseReader = new ServerResponseReader(stream); + serverResponseReader.callback = HandleResponse; + serverResponseReader.StartRead(); + Connected = true; + } + + /// + /// callback method that handles responses from the server + /// + /// the response message from the server + 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!"); + 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); + } + } + + /// + /// 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. + /// + /// the networkstream to use + /// the message to send + /// the serial to check for + /// the code to be executed upon reveiving a reply from the server with the specified serial + public void SendMessageAndOnResponse(string message, string serial, HandleSerial action) + { + serialResponses.Add(serial, action); + WriteTextMessage(message); + } + + /// + /// writes a message to the server + /// + /// the network stream to use + /// the message to send + 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); + } + + } + + +}