C# Client 0.0.0.7
C# Library to interface with Corelink
Loading...
Searching...
No Matches
CorelinkTCP.cs
Go to the documentation of this file.
1using UnityEngine;
2using System.Net.Sockets;
3using System.Threading;
4using System;
5using System.Text;
6
7namespace CoreLink
8{
12 {
13 private TcpClient tcpClient;
14 private string response = null;
15 private Thread clientReceiveThread;
16 // Boolean flag to tell that thread to stop listening
17 private bool shouldExit;
18 private Byte[] bytes;
19 private int MAX_BUF_SIZE = 65536; // 2^16
20 private bool isTCPDone = false;
21 // Get a stream object for reading
22 private NetworkStream stream;
23 public CorelinkTCP(Config config)
24 {
25 // Initialize TCP Connection
26 Control.Print("Initializing TCP Connection");
27 tcpClient = new TcpClient(config.dataIP, config.controlPort);
28 stream = tcpClient.GetStream();
29 // TODO: Figure out how to use TCP KeepAlives instead of a perpetual while loop
30 //tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
31
32 // Start a separate thread to continue listen to TCP to keep the connection alive
33 StartTCPListenThread();
34 }
39 void StartTCPListenThread()
40 {
41 Debug.Log("StartTCPListenThread");
42 try
43 {
44 clientReceiveThread = new Thread(new ThreadStart(ListenForTCPData));
45 //clientReceiveThread.IsBackground = true;
46 clientReceiveThread.Start();
47 }
48 catch (Exception e)
49 {
50 Debug.Log("On client connect exception " + e);
51 }
52 }
53 public string GetResponse()
54 {
55 int iterations = 0;
56 while (string.IsNullOrEmpty(response) && iterations < Control.timeoutIterations)
57 {
58 Thread.Sleep(100);
59 iterations++;
60 }
61 if (iterations == Control.timeoutIterations)
62 {
63 throw new Exception("Timeout error");
64 }
65 string retval = response;
66 response = null;
67 return retval;
68 //string serverMessage = "";
69 //bytes = new Byte[MAX_BUF_SIZE];
70 //try
71 //{
72 // // Get a stream object for reading
73 // using (NetworkStream stream = tcpClient.GetStream())
74 // {
75 // int length;
76 // // Read incoming stream into byte array.
77 // while ((length = stream.Read(bytes, 0, bytes.Length)) == 0)
78 // {
79 // Debug.Log("Waiting for input");
80 // Thread.Sleep(100);
81 // }
82
83 // var incommingData = new byte[length];
84 // Array.Copy(bytes, 0, incommingData, 0, length);
85 // // Convert byte array to string message.
86 // serverMessage = Encoding.ASCII.GetString(incommingData);
87 // //response = serverMessage;
88
89 // }
90 //}
91 //catch (SocketException socketException)
92 //{
93 // Debug.Log("Socket exception: " + socketException);
94 //}
95 //return serverMessage;
96 }
97 void ListenForTCPData()
98 {
99 //Debug.Log("ListenForTCPData");
100 try
101 {
102 // connect to relay first
103 //tcpClient.Connect(IPAddress.Parse(config.ControlIP), config.ControlPort);
104
105 bytes = new Byte[MAX_BUF_SIZE];
106 while (!shouldExit)
107 {
108 int length;
109 try
110 {
111 // Read incoming stream into byte array.
112 while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
113 {
114 if (shouldExit)
115 break;
116 byte[] incomingData = new byte[length];
117 Array.Copy(bytes, 0, incomingData, 0, length);
118 // Convert byte array to string message.
119 string serverMessage = Encoding.ASCII.GetString(incomingData);
120 //Debug.Log("server message received as: " + serverMessage);
121 response = serverMessage;
122 }
123 }
124 catch (System.IO.IOException e)
125 {
126 Control.Print("Catching IO Error from ending the stream and (hopefully) continuing");
127 }
128 }
129 isTCPDone = true;
130 Control.Print("Closing TCP Connection");
131 }
132 catch (SocketException socketException)
133 {
134 Debug.LogException(socketException);
135 }
136 }
137 override public void sendMessage(string request)
138 {
139 if (tcpClient == null)
140 {
141 return;
142 }
143
144 //Debug.Log("SendWS:" + request);
145 try
146 {
147 // Get a stream object for writing.
148 NetworkStream stream = tcpClient.GetStream();
149 if (stream.CanWrite)
150 {
151 // Convert string message to byte array.
152 byte[] clientMessageAsByteArray = Encoding.ASCII.GetBytes(request);
153 // Write byte array to socketConnection stream.
154 stream.Write(clientMessageAsByteArray, 0, clientMessageAsByteArray.Length);
155 }
156 }
157 catch (SocketException socketException)
158 {
159 Debug.LogException(socketException);
160 throw new Exception("Socket exception: " + socketException);
161 }
162 }
163 override public void stopConnection()
164 {
165 shouldExit = true;
166 stream.Close();
167 tcpClient.Close();
168 }
169 }
170
171}