Exemplo n.º 1
0
        public async Task TestDoNotRequestServerInfoFirst()
        {
            var s = new ButtplugServer("TestServer", 0);

            s.Awaiting(async aServer => await s.SendMessageAsync(new Core.Messages.Test("Test"))).Should().Throw <ButtplugHandshakeException>();

            var msg = await s.SendMessageAsync(new RequestServerInfo("TestClient"));

            msg.Should().BeOfType <ServerInfo>();

            msg = await s.SendMessageAsync(new Core.Messages.Test("Test"));

            msg.Should().BeOfType <Core.Messages.Test>();
        }
        private async Task CheckDeviceCount(ButtplugServer aServer, int aExpectedCount)
        {
            var deviceListMsg = await aServer.SendMessageAsync(new RequestDeviceList());

            deviceListMsg.Should().BeOfType <DeviceList>();
            ((DeviceList)deviceListMsg).Devices.Length.Should().Be(aExpectedCount);
        }
Exemplo n.º 3
0
        public async Task TestRequestServerInfo()
        {
            var s = new ButtplugServer("TestServer", 100);
            var r = await s.SendMessageAsync(new RequestServerInfo("TestClient"));

            r.Should().BeOfType <ServerInfo>();
            var info = r as ServerInfo;

            info.MajorVersion.Should().Be(Assembly.GetAssembly(typeof(ServerInfo)).GetName().Version.Major);
            info.MinorVersion.Should().Be(Assembly.GetAssembly(typeof(ServerInfo)).GetName().Version.Minor);
            info.BuildVersion.Should().Be(Assembly.GetAssembly(typeof(ServerInfo)).GetName().Version.Build);
        }
Exemplo n.º 4
0
 public async Task <ButtplugMessage> SendAsync(ButtplugMessage aMsg, CancellationToken aToken = default(CancellationToken))
 {
     return(await Server.SendMessageAsync(aMsg).ConfigureAwait(false));
 }
Exemplo n.º 5
0
        public async Task RunServerSession()
        {
            try
            {
                var readTask  = _ws.ReadStringAsync(_linkedCancelSource.Token);
                var writeTask = _outgoingMessages.OutputAvailableAsync(_linkedCancelSource.Token);
                while (_ws.IsConnected && !_linkedCancelSource.IsCancellationRequested)
                {
                    var msgTasks = new Task[]
                    {
                        readTask,
                        writeTask,
                    };

                    var completedTaskIndex = Task.WaitAny(msgTasks);

                    if (completedTaskIndex == 0)
                    {
                        var incomingMsg = await((Task <string>)msgTasks[0]).ConfigureAwait(false);
                        if (incomingMsg != null)
                        {
                            ButtplugMessage[] msg;
                            try
                            {
                                msg = await _server.SendMessageAsync(incomingMsg).ConfigureAwait(false);
                            }
                            catch (ButtplugException ex)
                            {
                                msg = new ButtplugMessage[] { ex.ButtplugErrorMessage };
                            }
                            await QueueMessage(msg).ConfigureAwait(false);
                        }

                        readTask = _ws.ReadStringAsync(_linkedCancelSource.Token);
                    }
                    else
                    {
                        try
                        {
                            _outgoingMessages.TryReceiveAll(out var msgs);
                            var outMsgs = msgs.Aggregate(string.Empty, (current, msg) => current + msg);
                            if (_ws != null && _ws.IsConnected)
                            {
                                await _ws.WriteStringAsync(outMsgs, _linkedCancelSource.Token).ConfigureAwait(false);
                            }

                            writeTask = _outgoingMessages.OutputAvailableAsync(_linkedCancelSource.Token);
                        }
                        catch (WebSocketException e)
                        {
                            // Probably means we're replying to a message we received just before shutdown.
                            _logger.Error(e.Message, true);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                _logger.Error(e.Message, true);
            }
            finally
            {
                await ShutdownSession().ConfigureAwait(false);
            }
        }