2 using System.Collections;
3 using System.Collections.Generic;
5 using Newtonsoft.Json.Linq;
6 using System.Net.Sockets;
8 using System.Threading;
10 using Newtonsoft.Json;
24 #region public members
27 #region private members
28 private TcpListener tcpListener;
36 private Thread tcpListenerThread;
40 private TcpClient connectedTcpClient;
42 [Header(
"Airplane Input Required")]
43 [Tooltip(
"Drag and drop here the AC_BaseAirplane_Input.cs OR AC_XboxAirplane_Input.cs")]
52 #region Default Methods
57 tcpListenerThread.IsBackground =
true;
58 tcpListenerThread.Start();
62 #region Custom Methods
69 tcpListener =
new TcpListener(IPAddress.Parse(
"0.0.0.0"), CommonFunctions.ServerPort);
72 Debug.Log(
"Started Server At Port "+CommonFunctions.ServerPort);
73 Byte[] bytes =
new Byte[1024];
77 using (connectedTcpClient = tcpListener.AcceptTcpClient()) {
80 using (NetworkStream stream = connectedTcpClient.GetStream()) {
83 while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
87 var incommingData =
new byte[length];
88 Array.Copy(bytes, 0, incommingData, 0, length);
90 string clientMessage = Encoding.ASCII.GetString(incommingData);
91 clientMessage = clientMessage.Replace(
"}{",
"} | {");
92 string [] inputArray = clientMessage.Split(
'|');
93 foreach(
string eachInput
in inputArray)
97 var inputJson = JObject.Parse(eachInput);
99 isOutput =
bool.Parse(inputJson[
"IsOutput"].ToString());
101 catch (SocketException e){
102 Debug.LogError($
"JsonReaderException : { e.Source}");
105 catch (JsonReaderException e){
106 Debug.LogError($
"JsonReaderException : { e.Source}");
116 string logOutput = outputHandle.
LogOutput();
123 Debug.LogWarning(
"Socket exception: " + ex.ToString());
134 Debug.Log(
"InputHandle is detached in from Network manager. Go to Unity Hierarchy, look at inspector, drag and drop InputHandle onto Network communicator");
137 catch (SocketException ex) {
138 Debug.LogWarning(
"Socket exception: " + ex.ToString());
146 public void ResetThings()
148 if(StaticOutputSchema.IfCollision)
150 StaticOutputSchema.IfCollision =
false;
160 if (connectedTcpClient ==
null) {
164 NetworkStream stream = connectedTcpClient.GetStream();
165 if (stream.CanWrite) {
168 byte[] serverMessageAsByteArray = Encoding.ASCII.GetBytes(outStructSerialized);
170 stream.Write(serverMessageAsByteArray, 0, serverMessageAsByteArray.Length);
173 catch (SocketException socketException) {
174 Debug.LogWarning(
"Socket exception: " + socketException);