Esempio n. 1
0
 private void OnSetUpdateIntervalPacket(SensorUpdateInterval interval)
 {
     _transmitIndex = 0;
     _deviceProvider.SetSensorUpdateInterval(interval);
     WearableProxyServerProtocol.EncodeUpdateIntervalValue(_transmitBuffer, ref _transmitIndex, interval);
     SendTransmitBuffer();
 }
Esempio n. 2
0
        private void Awake()
        {
            _protocol                        = new WearableProxyServerProtocol();
            _protocol.KeepAlive             += OnKeepAlivePacket;
            _protocol.SensorControl         += OnSensorControlPacket;
            _protocol.GestureControl        += OnGestureControlPacket;
            _protocol.ConnectToDevice       += OnConnectToDevicePacket;
            _protocol.DisconnectFromDevice  += OnDisconnectFromDevicePacket;
            _protocol.InitiateDeviceSearch  += OnInitiateDeviceSearchPacket;
            _protocol.StopDeviceSearch      += OnStopDeviceSearchPacket;
            _protocol.QueryConnectionStatus += OnQueryConnectionStatusPacket;
            _protocol.QueryUpdateInterval   += OnQueryUpdateIntervalPacket;
            _protocol.RSSIFilterValueChange += OnRSSIFilterValueChangePacket;
            _protocol.SetUpdateInterval     += OnSetUpdateIntervalPacket;
            _protocol.QuerySensorStatus     += OnQuerySensorStatusPacket;
            _protocol.QueryGestureStatus    += OnQueryGestureStatusPacket;
            _protocol.PingQuery             += OnPingQuery;

            _listener       = new TcpListener(IPAddress.Any, _port);
            _receiveBuffer  = new byte[WearableProxyProtocolBase.SuggestedClientToServerBufferSize];
            _receiveIndex   = 0;
            _transmitBuffer = new byte[WearableProxyProtocolBase.SuggestedServerToClientBufferSize];
            _transmitIndex  = 0;

            _networkTimeout = 0.5f;

            _running = false;

            _wearableControl = WearableControl.Instance;
            _wearableControl.DeviceConnecting   += OnDeviceConnecting;
            _wearableControl.DeviceConnected    += OnDeviceConnected;
            _wearableControl.DeviceDisconnected += OnDeviceDisconnected;
            _deviceProvider = (WearableDeviceProvider)_wearableControl.GetOrCreateProvider <WearableDeviceProvider>();
        }
Esempio n. 3
0
        private void Update()
        {
            if (!_running)
            {
                return;
            }

            // Accept new clients
            // TODO: Allow multiple clients
            if (_clientSlot == null && _listener.Pending())
            {
                _clientSlot = _listener.AcceptTcpClient();
                SendWelcomePackets();
            }

            // Scan for incoming data
            if (_clientSlot == null)
            {
                return;
            }

            try
            {
                NetworkStream stream = _clientSlot.GetStream();
                while (stream.DataAvailable)
                {
                    int bufferSpaceRemaining = _receiveBuffer.Length - _receiveIndex;
                    if (bufferSpaceRemaining <= 0)
                    {
                        // Can't fit any more packets or consume any more of the buffer; dump buffer to free space.
                        Debug.LogWarning(WearableConstants.ProxyProviderBufferFullWarning);
                        _receiveIndex        = 0;
                        bufferSpaceRemaining = _receiveBuffer.Length;
                    }

                    int actualBytesRead = stream.Read(_receiveBuffer, _receiveIndex, bufferSpaceRemaining);
                    _receiveIndex += actualBytesRead;

                    ProcessReceiveBuffer();
                }
            }
            catch (Exception)
            {
                // The client has disconnected, or some other error.
                _clientSlot.Close();
                _clientSlot = null;
                return;
            }

            if (_deviceProvider.CurrentSensorFrames.Count > 0)
            {
                _transmitIndex = 0;
                for (int i = 0; i < _deviceProvider.CurrentSensorFrames.Count; i++)
                {
                    WearableProxyServerProtocol.EncodeSensorFrame(_transmitBuffer, ref _transmitIndex, _deviceProvider.CurrentSensorFrames[i]);
                }

                SendTransmitBuffer();
            }
        }
Esempio n. 4
0
        private void SendWelcomePackets()
        {
            // Prepare to transmit
            _transmitIndex = 0;

            // Device connection info
            if (_deviceProvider.ConnectedDevice == null)
            {
                WearableProxyServerProtocol.EncodeConnectionStatus(
                    _transmitBuffer,
                    ref _transmitIndex,
                    WearableProxyProtocolBase.ConnectionState.Disconnected,
                    new Device {
                    name = string.Empty, uid = WearableConstants.EmptyUID
                });
            }
            else
            {
                _transmitIndex = 0;
                WearableProxyServerProtocol.EncodeConnectionStatus(
                    _transmitBuffer,
                    ref _transmitIndex,
                    WearableProxyProtocolBase.ConnectionState.Connected,
                    _deviceProvider.ConnectedDevice.Value);
            }

            // Send config packet
            WearableProxyServerProtocol.EncodeConfigStatus(
                _transmitBuffer,
                ref _transmitIndex,
                _deviceProvider.GetCachedDeviceConfiguration());

            // Transmit
            SendTransmitBuffer();
        }
Esempio n. 5
0
 private void OnSetRotationSourcePacket(RotationSensorSource source)
 {
     _transmitIndex = 0;
     _deviceProvider.SetRotationSource(source);
     WearableProxyServerProtocol.EncodeRotationSourceValue(_transmitBuffer, ref _transmitIndex, source);
     SendTransmitBuffer();
 }
Esempio n. 6
0
 private void OnDeviceDisconnected(Device device)
 {
     _transmitIndex = 0;
     WearableProxyServerProtocol.EncodeConnectionStatus(
         _transmitBuffer,
         ref _transmitIndex,
         WearableProxyProtocolBase.ConnectionState.Disconnected,
         device);
     SendTransmitBuffer();
 }
Esempio n. 7
0
        private void OnQueryConfigPacket()
        {
            _transmitIndex = 0;

            WearableProxyServerProtocol.EncodeConfigStatus(
                _transmitBuffer,
                ref _transmitIndex,
                _deviceProvider.GetCachedDeviceConfiguration());

            SendTransmitBuffer();
        }
Esempio n. 8
0
        private void SendWelcomePackets()
        {
            // Prepare to transmit
            _transmitIndex = 0;

            // Device connection info
            if (_deviceProvider.ConnectedDevice == null)
            {
                WearableProxyServerProtocol.EncodeConnectionStatus(
                    _transmitBuffer,
                    ref _transmitIndex,
                    WearableProxyProtocolBase.ConnectionState.Disconnected,
                    new Device {
                    name = String.Empty, uid = WearableConstants.EmptyUID
                });
            }
            else
            {
                _transmitIndex = 0;
                WearableProxyServerProtocol.EncodeConnectionStatus(
                    _transmitBuffer,
                    ref _transmitIndex,
                    WearableProxyProtocolBase.ConnectionState.Connected,
                    _deviceProvider.ConnectedDevice.Value);
            }

            // Update interval value
            WearableProxyServerProtocol.EncodeUpdateIntervalValue(_transmitBuffer, ref _transmitIndex, _deviceProvider.GetSensorUpdateInterval());

            // Rotation source
            WearableProxyServerProtocol.EncodeRotationSourceValue(_transmitBuffer, ref _transmitIndex, _deviceProvider.GetRotationSource());

            // Sensor status
            SensorId[] sensors = (SensorId[])Enum.GetValues(typeof(SensorId));
            for (int i = 0; i < sensors.Length; i++)
            {
                SensorId sensor = sensors[i];
                WearableProxyServerProtocol.EncodeSensorStatus(_transmitBuffer, ref _transmitIndex, sensor, _deviceProvider.GetSensorActive(sensor));
            }

            // Gesture status
            GestureId[] gestures = WearableConstants.GestureIds;
            for (int i = 0; i < gestures.Length; i++)
            {
                GestureId gestureId = gestures[i];
                if (gestureId != GestureId.None)
                {
                    WearableProxyServerProtocol.EncodeGestureStatus(_transmitBuffer, ref _transmitIndex, gestureId, _deviceProvider.GetGestureEnabled(gestureId));
                }
            }

            // Transmit
            SendTransmitBuffer();
        }
Esempio n. 9
0
        private void OnDeviceConnectionFailed()
        {
            Device emptyDevice = new Device {
                uid = WearableConstants.EmptyUID, name = string.Empty, productId = ProductId.Undefined
            };

            _transmitIndex = 0;
            WearableProxyServerProtocol.EncodeConnectionStatus(
                _transmitBuffer,
                ref _transmitIndex,
                WearableProxyProtocolBase.ConnectionState.Failed,
                emptyDevice);
            SendTransmitBuffer();
        }
Esempio n. 10
0
 private void OnQuerySensorStatusPacket()
 {
     _transmitIndex = 0;
     SensorId[] sensors = (SensorId[])Enum.GetValues(typeof(SensorId));
     for (int i = 0; i < sensors.Length; i++)
     {
         SensorId sensor = sensors[i];
         WearableProxyServerProtocol.EncodeSensorStatus(
             _transmitBuffer,
             ref _transmitIndex,
             sensor,
             _deviceProvider.GetSensorActive(sensor));
     }
     SendTransmitBuffer();
 }
Esempio n. 11
0
 private void OnQueryGestureStatusPacket()
 {
     _transmitIndex = 0;
     GestureId[] gestures = WearableConstants.GestureIds;
     for (int i = 0; i < gestures.Length; i++)
     {
         GestureId gestureId = gestures[i];
         if (gestureId != GestureId.None)
         {
             WearableProxyServerProtocol.EncodeGestureStatus(
                 _transmitBuffer,
                 ref _transmitIndex,
                 gestureId,
                 _deviceProvider.GetGestureEnabled(gestureId));
         }
     }
     SendTransmitBuffer();
 }
Esempio n. 12
0
        private void OnQueryConnectionStatusPacket()
        {
            _transmitIndex = 0;

            if (_deviceProvider.ConnectedDevice == null)
            {
                WearableProxyServerProtocol.EncodeConnectionStatus(
                    _transmitBuffer,
                    ref _transmitIndex,
                    WearableProxyProtocolBase.ConnectionState.Disconnected,
                    new Device());
            }
            else
            {
                _transmitIndex = 0;
                WearableProxyServerProtocol.EncodeConnectionStatus(
                    _transmitBuffer,
                    ref _transmitIndex,
                    WearableProxyProtocolBase.ConnectionState.Connected,
                    _deviceProvider.ConnectedDevice.Value);
            }

            SendTransmitBuffer();
        }
Esempio n. 13
0
 private void OnQueryUpdateIntervalPacket()
 {
     _transmitIndex = 0;
     WearableProxyServerProtocol.EncodeUpdateIntervalValue(_transmitBuffer, ref _transmitIndex, _deviceProvider.GetSensorUpdateInterval());
     SendTransmitBuffer();
 }
Esempio n. 14
0
 private void OnDevicesUpdated(Device[] devices)
 {
     _transmitIndex = 0;
     WearableProxyServerProtocol.EncodeDeviceList(_transmitBuffer, ref _transmitIndex, devices);
     SendTransmitBuffer();
 }
Esempio n. 15
0
 private void OnQueryRotationSourcePacket()
 {
     _transmitIndex = 0;
     WearableProxyServerProtocol.EncodeRotationSourceValue(_transmitBuffer, ref _receiveIndex, _deviceProvider.GetRotationSource());
     SendTransmitBuffer();
 }