Ejemplo n.º 1
0
        public async Task SendButtonAsync(ButtonTypes type)
        {
            if (!_mouseSocket.IsAlive)
            {
                await ConnectAsync(_hostName);
            }

            _logger.LogTrace($"Sending Button: {type}");

            _mouseSocket.Send($"type:button\nname:{type.ToString().ToUpperInvariant()}\n\n");
        }
Ejemplo n.º 2
0
        public virtual async Task <TResponse> SendCommandAsync <TResponse>(CommandBase command) where TResponse : ResponseBase
        {
            if (!_socket.IsAlive)
            {
                await ConnectAsync(_hostName);
            }

            var request = new Message
            {
                Uri     = command.Uri,
                Type    = "request",
                Payload = command.ToJObject()
            };

            if (!string.IsNullOrEmpty(command.CustomId))
            {
                request.Id = command.CustomId;
            }

            if (!string.IsNullOrEmpty(command.CustomType))
            {
                request.Type = command.CustomType;
            }

            var taskSource = new TaskCompletionSource <Message>();

            _completionSources.TryAdd(request.Id, taskSource);

            var json = JsonConvert.SerializeObject(request, SerializationSettings.Default);

            _logger.LogTrace($"Sending: {json}");

            _socket.Send(json);

            var timeoutTask  = Task.Delay(request.Type == "register" ? 60000 : CommandTimeout);
            var responseTask = taskSource.Task;

            if (await Task.WhenAny(timeoutTask, responseTask) == responseTask)
            {
                var response = responseTask.Result;

                return(response.Payload.ToObject <TResponse>(
                           JsonSerializer.CreateDefault(SerializationSettings.Default)));
            }

            _completionSources.TryRemove(request.Id, out _);
            throw new TimeoutException();
        }