/// <summary> /// 创建命令信息实例 /// </summary> /// <typeparam name="TResponse"></typeparam> /// <returns></returns> public static QpCommandInfo Create <TResponse>(IQpCommandRequest <TResponse> request) where TResponse : class, new() { var requestType = request.GetType(); string name = null; if (name == null) { name = requestType.GetCustomAttribute <DisplayNameAttribute>()?.DisplayName; } if (name == null) { name = requestType.GetCustomAttribute <JsonSchemaAttribute>()?.Name; } if (name == null) { name = requestType.FullName; } return(new QpCommandInfo(name, requestType.GetCustomAttribute <DescriptionAttribute>()?.Description, requestType, typeof(TResponse))); }
public async Task <TCmdResponse> SendCommand <TCmdResponse>(IQpCommandRequest <TCmdResponse> request, int timeout = 30 * 1000, Action afterSendHandler = null) { var requestType = request.GetType(); var typeName = requestType.FullName; var requestContent = JsonConvert.SerializeObject(request); var commandContext = new CommandContext(typeName); commandDict.TryAdd(commandContext.Id, commandContext); CommandResponseTypeNameAndContent ret = null; if (timeout <= 0) { SendCommandRequestPackage(commandContext.Id, typeName, requestContent, afterSendHandler); ret = await commandContext.ResponseTask; } //如果设置了超时 else { try { await TaskUtils.TaskWait(Task.Run(() => SendCommandRequestPackage(commandContext.Id, typeName, requestContent, afterSendHandler)), timeout); } catch { if (LogUtils.LogCommand) { LogUtils.Log("{0}: [Send-CommandRequestPackage-Timeout]CommandId:{1},Type:{2},Content:{3}", DateTime.Now, commandContext.Id, typeName, LogUtils.LogContent ? requestContent : LogUtils.NOT_SHOW_CONTENT_MESSAGE); } if (commandContext.ResponseTask.Status == TaskStatus.Created) { commandContext.Timeout(); commandDict.TryRemove(commandContext.Id, out _); } } ret = await await TaskUtils.TaskWait(commandContext.ResponseTask, timeout); } return(JsonConvert.DeserializeObject <TCmdResponse>(ret.Content)); }