Exemplo n.º 1
0
 private void DisconnectAllEndpoints()
 {
     controller?.Dispose();
     head?.Dispose();
     broadcasts?.Dispose();
     controller = null;
     head       = null;
     broadcasts = null;
 }
Exemplo n.º 2
0
        // Sets up connection and returns true if all links are established.
        private bool TrySettingUpConnection()
        {
            lastConnectionAttempt = DateTime.Now;

            // Make sure API server is alive
            var status = api.GetStatus(timeoutThresholdMs);

            if (status == null)
            {
                return(false);
            }

            // Reset current connections, if exist
            DisconnectAllEndpoints();

            try
            {
                if (capabilities.HasFlag(Capabilities.HeadTracking))
                {
                    var endpointStatus = status.Endpoints.FirstOrDefault(x => x.Name == EndpointNames.HeadTracking);
                    if (endpointStatus == null || endpointStatus.Code != (int)ControlResponseCode.OK)
                    {
                        return(false);
                    }

                    head = new HeadRemote(api.CreateProxy <HeadTrackingProxy>());
                }

                if (capabilities.HasFlag(Capabilities.Controllers))
                {
                    var endpointStatus = status.Endpoints.FirstOrDefault(x => x.Name == EndpointNames.Controller);
                    if (endpointStatus == null || endpointStatus.Code != (int)ControlResponseCode.OK)
                    {
                        return(false);
                    }

                    controller = new ControllerRemote(api.CreateProxy <ControllerProxy>());
                }

                // Subscribe to haptic pulse broadcasts, if controller proxy is in use
                if (controller != null)
                {
                    broadcasts = api.CreateProxy <BroadcastProxy>();

                    // Forward the events to long-lived event so API user doesn't
                    // have to resubscribe on reconnect
                    broadcasts.HapticPulseReceived += (s, e) => HapticPulse?.Invoke(this, e);
                }

                return(true);
            }
            catch (Exception x)
            {
                Logger.Error("Error during API connection: " + x);

                // Cleanup possibly connected endpoints
                DisconnectAllEndpoints();
            }

            return(false);
        }