/// <summary>
        /// Is called when a command is received
        /// </summary>
        /// <param name="command">The command.</param>
        private async void DataReceived(Command command)
        {
            try
            {
                if (!await AsyncCommandProcessor.Notify(command))
                {
                    if (command.Type == CommandType.Action)
                    {
                        this.OnCommandReceived?.Invoke(command, this.Client);
                    }
                }
            }
            catch (Exception ex)
            {
                await Task.Delay(1);

                this.Log(this.SrcDeviceId, "Command process exception", ex);
            }
        }
Пример #2
0
        /// <summary>
        /// Sends and receive data from/to other clients
        /// </summary>
        /// <param name="command">The request message</param>
        /// <param name="streamId">The streamId<see cref="string"/></param>
        /// <param name="token">The token<see cref="string"/></param>
        /// <param name="header">The header<see cref="Metadata"/></param>
        /// <returns>The <see cref="Task"/></returns>
        private async Task <GrpcResponse> Action(Command command, string streamId, string token, Metadata header)
        {
            try
            {
                if (string.IsNullOrEmpty(token) &&
                    command.Topic != StaticCommandKeys.Connect)
                {
                    throw new Exception("no token provided.");
                }

                if (!await AsyncCommandProcessor.Notify(command))
                {
                    if (command.Type == CommandType.Action)
                    {
                        this.Log(this, this.SrcDeviceId, "Command received", LogLevel.Trace);
                        return(await this.OnAction?.Invoke(command, streamId, token, header));
                    }

                    return(new GrpcResponse()
                    {
                        Result = ResultCode.Error, Message = "Unknown type"
                    });
                }
                else
                {
                    return(new GrpcResponse()
                    {
                        Result = ResultCode.Ok
                    });
                }
            }
            catch (Exception ex)
            {
                this.Log("", "Receiving error", ex);
                return(new GrpcResponse()
                {
                    Result = ResultCode.Error, Message = ex.ToText("Unknown action error")
                });
            }
        }