AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
Communicator.NetworkCommunicator Class Reference

Class to manage the TCP network function More...

Inheritance diagram for Communicator.NetworkCommunicator:
Collaboration diagram for Communicator.NetworkCommunicator:

Public Member Functions

void ListenForIncommingRequests ()
 Runs in background TcpServerThread; Handles incomming TcpClient requests More...
 
void ResetThings ()
 
void SendMessage (ref String outStructSerialized)
 
Send message to client using socket connection.
More...
 

Public Attributes

AC_BaseAirplane_Input currentReadings
 
InputHandle inputHandle
 
OutputHandle outputHandle
 

Detailed Description

Class to manage the TCP network function

Definition at line 21 of file NetworkCommunicator.cs.

Member Function Documentation

◆ ListenForIncommingRequests()

void Communicator.NetworkCommunicator.ListenForIncommingRequests ( )
inline

Runs in background TcpServerThread; Handles incomming TcpClient requests

Definition at line 66 of file NetworkCommunicator.cs.

66  {
67  try {
68  // Create listener on localhost port 8052.
69  tcpListener = new TcpListener(IPAddress.Parse("0.0.0.0"), CommonFunctions.ServerPort);
70 
71  tcpListener.Start();
72  Debug.Log("Started Server At Port "+CommonFunctions.ServerPort);
73  Byte[] bytes = new Byte[1024];
74  if(inputHandle)
75  {
76  while (true) {
77  using (connectedTcpClient = tcpListener.AcceptTcpClient()) {
78  // Get a stream object for reading
79 
80  using (NetworkStream stream = connectedTcpClient.GetStream()) {
81  int length;
82  // Read incomming stream into byte arrary.
83  while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
84  {
85  try
86  {
87  var incommingData = new byte[length];
88  Array.Copy(bytes, 0, incommingData, 0, length);
89  // Convert byte array to string message.
90  string clientMessage = Encoding.ASCII.GetString(incommingData);
91  clientMessage = clientMessage.Replace("}{", "} | {");
92  string [] inputArray = clientMessage.Split('|');
93  foreach(string eachInput in inputArray)
94  {
95  isOutput = false;
96  try{
97  var inputJson = JObject.Parse(eachInput);
98  inputHandle.ParseInput(inputJson);
99  isOutput = bool.Parse(inputJson["IsOutput"].ToString());
100  }
101  catch (SocketException e){
102  Debug.LogError($"JsonReaderException : { e.Source}");
103  isOutput = true;
104  }
105  catch (JsonReaderException e){
106  Debug.LogError($"JsonReaderException : { e.Source}");
107  isOutput = true;
108  }
109  // once received the message, send message in return
110  if(isOutput){
111 
112  outputHandle.ParseOutput(ref outputmsg);
113  SendMessage(ref outputmsg);
114  }
115  else{
116  string logOutput = outputHandle.LogOutput();
117  SendMessage(ref logOutput);
118  }
119  }
120  }
121  catch(Exception ex)
122  {
123  Debug.LogWarning("Socket exception: " + ex.ToString());
124  isOutput = true;
125  }
126  ResetThings();
127  }
128  }
129  }
130  }
131  }
132  else
133  {
134  Debug.Log("InputHandle is detached in from Network manager. Go to Unity Hierarchy, look at inspector, drag and drop InputHandle onto Network communicator");
135  }
136  }
137  catch (SocketException ex) {
138  Debug.LogWarning("Socket exception: " + ex.ToString());
139  // tcpListener.Stop();
140  isOutput = true;
141  }
142  }

◆ SendMessage()

void Communicator.NetworkCommunicator.SendMessage ( ref String  outStructSerialized)
inline


Send message to client using socket connection.


Definition at line 159 of file NetworkCommunicator.cs.

159  {
160  if (connectedTcpClient == null) {
161  return;
162  }
163  try {
164  NetworkStream stream = connectedTcpClient.GetStream();
165  if (stream.CanWrite) {
166  // string serverMessage = "This is a message from your server.";
167  // Convert string message to byte array.
168  byte[] serverMessageAsByteArray = Encoding.ASCII.GetBytes(outStructSerialized);
169  // Write byte array to socketConnection stream.
170  stream.Write(serverMessageAsByteArray, 0, serverMessageAsByteArray.Length);
171  }
172  }
173  catch (SocketException socketException) {
174  Debug.LogWarning("Socket exception: " + socketException);
175  }
176  }

The documentation for this class was generated from the following file:
Communicator.OutputHandle.ParseOutput
void ParseOutput(ref string outputmsg)
Prepare output object and return json string to be dispatched
Definition: OutputHandle.cs:24
Communicator.InputHandle.ParseInput
void ParseInput(JObject inputJson)
Parse input coming from network
Definition: InputHandle.cs:26
Communicator.NetworkCommunicator.SendMessage
void SendMessage(ref String outStructSerialized)
Send message to client using socket connection.
Definition: NetworkCommunicator.cs:159
Communicator.OutputHandle.LogOutput
string LogOutput()
Just Log output if the entire output is not required
Definition: OutputHandle.cs:94