コード例 #1
0
        public void TestClientToServer()
        {
            WearableProxyServerProtocol serverProtocol = new WearableProxyServerProtocol();

            int serverIndex = 0;

            byte[] serverBuffer = new byte[1024];

            // Register callbacks for packet processing
            serverProtocol.KeepAlive    += () => { _lastPacketName = "KeepAlive"; };
            serverProtocol.PingQuery    += () => { _lastPacketName = "PingQuery"; };
            serverProtocol.PingResponse += () => { _lastPacketName = "PingResponse"; };
            serverProtocol.SetNewConfig += deviceConfig =>
            {
                _lastPacketName = "SetConfig";
                Assert.AreEqual(SensorUpdateInterval.ThreeHundredTwentyMs, deviceConfig.updateInterval);
                Assert.AreEqual(false, deviceConfig.rotationSixDof.isEnabled);
                Assert.AreEqual(true, deviceConfig.gyroscope.isEnabled);
                Assert.AreEqual(false, deviceConfig.accelerometer.isEnabled);
                Assert.AreEqual(false, deviceConfig.headNodGesture.isEnabled);
                Assert.AreEqual(true, deviceConfig.doubleTapGesture.isEnabled);
            };
            serverProtocol.QueryConfigStatus     += () => { _lastPacketName = "QueryConfig"; };
            serverProtocol.RSSIFilterValueChange += value =>
            {
                _lastPacketName = "SetRSSI";
                Assert.AreEqual(-40, value);
            };
            serverProtocol.InitiateDeviceSearch += () => { _lastPacketName = "StartSearch"; };
            serverProtocol.StopDeviceSearch     += () => { _lastPacketName = "StopSearch"; };
            serverProtocol.ConnectToDevice      += uid =>
            {
                _lastPacketName = "ConnectToDevice";
                Assert.AreEqual("00000000-0000-0000-0000-000000000000", uid);
            };
            serverProtocol.DisconnectFromDevice  += () => { _lastPacketName = "DisconnectFromDevice"; };
            serverProtocol.QueryConnectionStatus += () => { _lastPacketName = "QueryConnection"; };

            // Encode
            WearableProxyProtocolBase.EncodeKeepAlive(serverBuffer, ref serverIndex);
            WearableProxyProtocolBase.EncodePingQuery(serverBuffer, ref serverIndex);
            WearableProxyProtocolBase.EncodePingResponse(serverBuffer, ref serverIndex);

            WearableDeviceConfig config = new WearableDeviceConfig
            {
                updateInterval   = SensorUpdateInterval.ThreeHundredTwentyMs,
                gyroscope        = { isEnabled = true },
                doubleTapGesture = { isEnabled = true }
            };

            WearableProxyClientProtocol.EncodeSetNewConfig(serverBuffer, ref serverIndex, config);
            WearableProxyClientProtocol.EncodeQueryConfig(serverBuffer, ref serverIndex);

            WearableProxyClientProtocol.EncodeRSSIFilterControl(serverBuffer, ref serverIndex, -40);
            WearableProxyClientProtocol.EncodeInitiateDeviceSearch(serverBuffer, ref serverIndex);
            WearableProxyClientProtocol.EncodeStopDeviceSearch(serverBuffer, ref serverIndex);
            WearableProxyClientProtocol.EncodeConnectToDevice(serverBuffer, ref serverIndex, "00000000-0000-0000-0000-000000000000");
            WearableProxyClientProtocol.EncodeDisconnectFromDevice(serverBuffer, ref serverIndex);
            WearableProxyClientProtocol.EncodeQueryConnectionStatus(serverBuffer, ref serverIndex);
            WearableProxyProtocolBase.EncodeKeepAlive(serverBuffer, ref serverIndex);

            // Decode
            serverIndex = 0;

            serverProtocol.ProcessPacket(serverBuffer, ref serverIndex);
            Assert.AreEqual("KeepAlive", _lastPacketName);

            serverProtocol.ProcessPacket(serverBuffer, ref serverIndex);
            Assert.AreEqual("PingQuery", _lastPacketName);

            serverProtocol.ProcessPacket(serverBuffer, ref serverIndex);
            Assert.AreEqual("PingResponse", _lastPacketName);

            serverProtocol.ProcessPacket(serverBuffer, ref serverIndex);
            Assert.AreEqual("SetConfig", _lastPacketName);

            serverProtocol.ProcessPacket(serverBuffer, ref serverIndex);
            Assert.AreEqual("QueryConfig", _lastPacketName);

            serverProtocol.ProcessPacket(serverBuffer, ref serverIndex);
            Assert.AreEqual("SetRSSI", _lastPacketName);

            serverProtocol.ProcessPacket(serverBuffer, ref serverIndex);
            Assert.AreEqual("StartSearch", _lastPacketName);

            serverProtocol.ProcessPacket(serverBuffer, ref serverIndex);
            Assert.AreEqual("StopSearch", _lastPacketName);

            serverProtocol.ProcessPacket(serverBuffer, ref serverIndex);
            Assert.AreEqual("ConnectToDevice", _lastPacketName);

            serverProtocol.ProcessPacket(serverBuffer, ref serverIndex);
            Assert.AreEqual("DisconnectFromDevice", _lastPacketName);

            serverProtocol.ProcessPacket(serverBuffer, ref serverIndex);
            Assert.AreEqual("QueryConnection", _lastPacketName);

            serverProtocol.ProcessPacket(serverBuffer, ref serverIndex);
            Assert.AreEqual("KeepAlive", _lastPacketName);
        }