Пример #1
0
        public async Task ConnectTask()
        {
            Dispatcher.Invoke(() => { ConnectionStatus.Content = "Connecting"; });
            IButtplugClientConnector connector;
            //if (_useEmbeddedServer)
            {
                var embeddedConnector = new ButtplugEmbeddedConnector("VaMLaunch Embedded Server", 0, _deviceManager);
                if (_deviceManager == null)
                {
                    _deviceManager = embeddedConnector.Server.DeviceManager;
                }
                connector = embeddedConnector;
            }

            var client = new ButtplugClient("VaMLaunch Client", connector);

            while (!_quitting)
            {
                try
                {
                    client.DeviceAdded      += OnDeviceAdded;
                    client.DeviceRemoved    += OnDeviceRemoved;
                    client.Log              += OnLogMessage;
                    client.ServerDisconnect += OnDisconnect;
                    await client.ConnectAsync();

                    await client.RequestLogAsync(ButtplugLogLevel.Debug);

                    _client = client;
                    await Dispatcher.Invoke(async() =>
                    {
                        ConnectedHandler?.Invoke(this, new EventArgs());
                        ConnectionStatus.Content = "Connected";
                        await StartScanning();
                        _scanningButton.Visibility = Visibility.Visible;
                    });

                    break;
                }
                catch (ButtplugClientConnectorException)
                {
                    Debug.WriteLine("Retrying");
                    // Just keep trying to connect.
                    // If the exception was thrown after connect, make sure we disconnect.
                    if (_client != null && _client.Connected)
                    {
                        await _client.DisconnectAsync();
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Did something else fail? {ex})");
                    // If the exception was thrown after connect, make sure we disconnect.
                    if (_client != null && _client.Connected)
                    {
                        await _client.DisconnectAsync();
                    }
                }
            }
        }
Пример #2
0
        public async Task TestLogEvent()
        {
            var signal = new SemaphoreSlim(1, 1);
            await _client.ConnectAsync();

            _client.Log += (aObj, aLogEvent) =>
            {
                if (signal.CurrentCount == 0)
                {
                    signal.Release(1);
                }
            };
            await _client.RequestLogAsync(ButtplugLogLevel.Debug);

            await _client.StartScanningAsync();

            await _client.StopScanningAsync();

            await signal.WaitAsync();
        }
Пример #3
0
        private static async Task RunExample()
        {
            // Now that we've gone through all of the things B******g can do, let's cover what
            // happens when you try something it can't do.
            //
            // Using the B******g Client API should, in most cases not involving connector setup,
            // look exactly the same. This mean that you should get the same exceptions either using
            // embedded connectors or remote connectors. Your application should "just work"
            // regardless of how client and server are connected, though it may be a bit slower if
            // there's a remote client/server setup.
            //
            // Let's see what accidentally screwing up in B******g looks like.
            //
            // First off, all B******g specific exceptions derive from ButtplugException, so if you
            // want to catch everything that could come out of the library, that's the base class to
            // start catching with.

            // Let's go ahead, put our client/server together, and get connected.
            var connector = new ButtplugEmbeddedConnector("Example Server");
            var client    = new ButtplugClient("Example Client", connector);

            // We've set up our client and connector as usual, but we haven't called ConnectAsync()
            // yet. Let's call StartScanningAsync() and see what happens.
            try
            {
                await client.StartScanningAsync();
            }
            catch (ButtplugClientConnectorException ex)
            {
                // Sure enough, we get an exception. In this case, it's a
                // ButtplugClientConnectorException. B******g itself doesn't really have an idea of
                // connection states (since it's basically communication agnostic), so we handle
                // connection errors via ButtplugClientConnectorExceptions.
                Console.WriteLine($"Can't scan because we're not connected yet! Message: {ex.Message}");
            }

            // Let's go ahead, set up our test device, and actually connect now.
            var server     = connector.Server;
            var testDevice = new TestDevice(new ButtplugLogManager(), "Test Device");

            server.AddDeviceSubtypeManager(
                aLogManager => new TestDeviceSubtypeManager(testDevice));
            await client.ConnectAsync();

            Console.WriteLine("Connected!");

            // Getting errors is certainly helpful, but sometimes it'd also be nice to know what's
            // going on between the errors, right? That's where logging comes in. Let's sign up for
            // some logs.
            //
            // First off, we'll want to assign a Log event handler to the client, to make sure we do
            // something when we get log messages.
            void HandleLogMessage(object aObj, LogEventArgs aArgs)
            {
                Console.WriteLine($"LOG: {aArgs.Message.LogMessage}");
            }

            client.Log += HandleLogMessage;
            // Calling RequestLogAsync tells the server to send us all log messages at the requested
            // level and below. The log levels are:
            //
            // - Off
            // - Fatal
            // - Error
            // - Warn
            // - Info
            // - Debug
            // - Trace
            //
            // If we set to Debug, this means we'll get all messages that aren't Trace.
            await client.RequestLogAsync(ButtplugLogLevel.Debug);

            // Now that we've registered, whenever log messages are generated, our log handler should
            // fire and print them to the console.

            // You usually shouldn't run Start/Stop scanning back-to-back like this, but with
            // TestDevice we know our device will be found when we call StartScanning, so we can get
            // away with it.
            await client.StartScanningAsync();

            await client.StopScanningAsync();

            Console.WriteLine("Client currently knows about these devices:");
            foreach (var device in client.Devices)
            {
                Console.WriteLine($"- {device.Name}");
            }

            await WaitForKey();

            // Thanks to the last tutorials, we know we've got a test device now. Let's send it a
            // weird message it doesn't expect and see what happens.
            try
            {
                await client.Devices[0].SendLovenseCmd("Vibrate:20;");
            }
            catch (ButtplugDeviceException e)
            {
                // Sure enough, it's going to fail again, this time with a Device Exception, because,
                // well, this has to do with a device this time.
                Console.WriteLine(e);
            }

            // There are 5 different types of B******g Exceptions:
            //
            // - ButtplugHandshakeException - Something went wrong while the client was connecting to
            //   the server
            // - ButtplugDeviceException - Something went wrong with a device.
            // - ButtplugMessageException - Something went wrong while forming a message.
            // - ButtplugPingException - Client missed pinging the server and a ping timeout happened.
            // - ButtplugUnknownException - Something went wrong. Somewhere. YOLO. (This should
            //   rarely if ever get thrown)
            //
            // All of these exceptions are translations of B******g Error messages, meaning if a
            // remote server sends an error message, we can still turn it into an Exception on the
            // Client side, to make it look local.
            await WaitForKey();

            // And that's it! Now you know how B******g API error handling works, and how to interact
            // with the internal logging utilities.
        }