public virtual void SetResponse(string responseTypeName, string responseContent) { if (isTimeout) { return; } this.response = new CommandResponseTypeNameAndContent() { TypeName = responseTypeName, Content = responseContent }; if (ResponseTask.Status == TaskStatus.Created) { ResponseTask.Start(); } }
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)); }