public Task <IIpcHandleRequestMessageResult> HandleRequestMessage(IIpcRequestContext requestContext) { // 我又不知道业务,不知道怎么玩…… var responseMessage = new IpcRequestMessage(nameof(EmptyIpcRequestHandler), new IpcBufferMessage(new byte[0])); return(Task.FromResult((IIpcHandleRequestMessageResult) new IpcHandleRequestMessageResult(responseMessage))); }
public void GetResponseAsync() { "发送消息之后,能等待收到对应的回复".Test(() => { var responseManager = new ResponseManager(); var requestByteList = new byte[] { 0xFF, 0xFE }; var request = new IpcRequestMessage("Tests", new IpcBufferMessage(requestByteList)); var ipcClientRequestMessage = responseManager.CreateRequestMessage(request); Assert.AreEqual(false, ipcClientRequestMessage.Task.IsCompleted); var requestStream = IpcBufferMessageContextToStream(ipcClientRequestMessage.IpcBufferMessageContext); IpcClientRequestArgs ipcClientRequestArgs = null; responseManager.OnIpcClientRequestReceived += (sender, args) => { ipcClientRequestArgs = args; }; Assert.IsNotNull(requestStream); responseManager.OnReceiveMessage(new PeerMessageArgs("Foo", requestStream, ack: 100, IpcMessageCommandType.RequestMessage)); Assert.IsNotNull(ipcClientRequestArgs); var responseByteList = new byte[] { 0xF1, 0xF2 }; var responseMessageContext = responseManager.CreateResponseMessage(ipcClientRequestArgs.MessageId, new IpcBufferMessage(responseByteList), "Tests"); var responseStream = IpcBufferMessageContextToStream(responseMessageContext); responseManager.OnReceiveMessage(new PeerMessageArgs("Foo", responseStream, ack: 100, IpcMessageCommandType.ResponseMessage)); Assert.AreEqual(true, ipcClientRequestMessage.Task.IsCompleted); }); }
/// <inheritdoc /> public async Task <IpcBufferMessage> GetResponseAsync(IpcRequestMessage request) { var ipcClientRequestMessage = IpcMessageRequestManager.CreateRequestMessage(request); await IpcClientService.WriteMessageAsync(ipcClientRequestMessage.IpcBufferMessageContext); return(await ipcClientRequestMessage.Task); }
public IpcClientRequestMessage CreateRequestMessage(IpcRequestMessage request) { ulong currentMessageId; var task = new TaskCompletionSource <IpcBufferMessage>(); lock (Locker) { currentMessageId = CurrentMessageId; // 在超过 ulong.MaxValue 之后,将会是 0 这个值 CurrentMessageId++; TaskList[currentMessageId] = task; } var requestMessage = CreateRequestMessageInner(request, currentMessageId); return(new IpcClientRequestMessage(requestMessage, task.Task, new IpcClientRequestMessageId(currentMessageId))); }
public IpcBufferMessageContext CreateResponseMessage(IpcClientRequestMessageId messageId, IpcRequestMessage response) => CreateResponseMessageInner(messageId, response);
public IpcHandleRequestMessageResult(IpcRequestMessage returnMessage) { ReturnMessage = returnMessage; }
// 为什么将请求和响应的消息封装都放在一个类里面?这是为了方便更改,和调试 // 如果放在两个类或两个文件里面,也许就不好调试对应关系 protected static IpcBufferMessageContext CreateResponseMessageInner(IpcClientRequestMessageId messageId, IpcRequestMessage response) { /* * MessageHeader * MessageId * Response Message Length * Response Message */ var currentMessageIdByteList = BitConverter.GetBytes(messageId.MessageIdValue); var responseMessageLengthByteList = BitConverter.GetBytes(response.RequestMessage.Count); return(new IpcBufferMessageContext ( response.Summary, IpcMessageCommandType.ResponseMessage, new IpcBufferMessage(ResponseMessageHeader), new IpcBufferMessage(currentMessageIdByteList), new IpcBufferMessage(responseMessageLengthByteList), response.RequestMessage )); }