private void phoneEventSocketLoop()
{
long lastConnectionAttemptTime = 0;
bool isFirstTry = true;
while (!shouldStop) {
string addr = EmulatorConfig.Instance.PHONE_EVENT_MODE == EmulatorConfig.Mode.USB
? EmulatorConfig.USB_SERVER_IP : EmulatorConfig.WIFI_SERVER_IP;
// Wait a while in order to enforce the minimum time between connection attempts.
if (!isFirstTry) {
TimeSpan elapsed = new TimeSpan(DateTime.Now.Ticks - lastConnectionAttemptTime);
float toWait = kMinReconnectInterval - (float) elapsed.TotalSeconds;
if (toWait > 0) {
Thread.Sleep((int) (toWait * 1000));
}
}
isFirstTry = false;
lastConnectionAttemptTime = DateTime.Now.Ticks;
try {
if (EmulatorConfig.Instance.PHONE_EVENT_MODE == EmulatorConfig.Mode.USB) {
Debug.LogFormat("Attempting to set up port forwarding.");
setupPortForwarding(kPhoneEventPort);
}
Debug.LogFormat("Attempting to connect to {0}:{1}.", addr, kPhoneEventPort);
TcpClient tcpClient = new TcpClient(addr, kPhoneEventPort);
Debug.Log("Connected to phone event socket: " + addr + ":" + kPhoneEventPort);
connected = true;
ProcessConnection(tcpClient);
tcpClient.Close();
Debug.Log("Disconnected.");
connected = false;
continue;
} catch (Exception e) {
Debug.LogWarning("Error connecting to phone event socket: " + addr + ":"
+ kPhoneEventPort + ". " + e);
}
}
}