Class to manage the TCP network function
More...
Class to manage the TCP network function
Definition at line 21 of file NetworkCommunicator.cs.
◆ ListenForIncommingRequests()
void Communicator.NetworkCommunicator.ListenForIncommingRequests |
( |
| ) |
|
|
inline |
Runs in background TcpServerThread; Handles incomming TcpClient requests
Definition at line 66 of file NetworkCommunicator.cs.
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());
◆ SendMessage()
void Communicator.NetworkCommunicator.SendMessage |
( |
ref String |
outStructSerialized | ) |
|
|
inline |
Send message to client using socket connection.
Definition at line 159 of file NetworkCommunicator.cs.
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);
The documentation for this class was generated from the following file: