private void ProcessConnection(TcpClient tcpClient)
        {
            byte[]        buffer = new byte[4];
            NetworkStream stream = tcpClient.GetStream();

            stream.ReadTimeout       = kSocketReadTimeoutMillis;
            tcpClient.ReceiveTimeout = kSocketReadTimeoutMillis;
            while (!shouldStop)
            {
                int bytesRead = blockingRead(stream, buffer, 0, 4);
                if (bytesRead < 4)
                {
                    Debug.LogWarning("Socket read failed, considering socket to have been disconnected.");
                    return;
                }
                int msgLen = unpack32bits(correctEndianness(buffer), 0);

                byte[] dataBuffer = new byte[msgLen];
                bytesRead = blockingRead(stream, dataBuffer, 0, msgLen);
                if (bytesRead < msgLen)
                {
                    Debug.LogWarning("Socket read failed, considering socket to have been disconnected.");
                    return;
                }

                PhoneEvent proto =
                    PhoneEvent.CreateBuilder().MergeFrom(dataBuffer).Build();
                phoneRemote.OnPhoneEvent(proto);

                // Debug.Log(BitConverter.ToString(dataBuffer));
                // Debug.Log(proto.Type);
            }
        }
Пример #2
0
        private void ProcessConnection(TcpClient tcpClient)
        {
            byte[]        buffer = new byte[4];
            NetworkStream stream = tcpClient.GetStream();

            stream.ReadTimeout       = kSocketReadTimeoutMillis;
            tcpClient.ReceiveTimeout = kSocketReadTimeoutMillis;
            while (!shouldStop)
            {
                int bytesRead = blockingRead(stream, buffer, 0, 4);
                if (bytesRead < 4)
                {
                    // Caught by phoneEventSocketLoop.
                    throw new Exception(
                              "Failed to read from controller emulator app event socket." +
                              "\nVerify that the controller emulator app is running.");
                }

                int msgLen = unpack32bits(correctEndianness(buffer), 0);

                byte[] dataBuffer = new byte[msgLen];
                bytesRead = blockingRead(stream, dataBuffer, 0, msgLen);
                if (bytesRead < msgLen)
                {
                    // Caught by phoneEventSocketLoop.
                    throw new Exception(
                              "Failed to read from controller emulator app event socket." +
                              "\nVerify that the controller emulator app is running.");
                }

                PhoneEvent proto =
                    PhoneEvent.CreateBuilder().MergeFrom(dataBuffer).Build();
                phoneRemote.OnPhoneEvent(proto);

                connected = EmulatorClientSocketConnectionState.Connected;

                if (!lastConnectionAttemptWasSuccessful)
                {
                    Debug.Log("Successfully connected to controller emulator app.");

                    // Log first failure after after successful read from event socket.
                    lastConnectionAttemptWasSuccessful = true;
                }
            }
        }