public ICollection <RpcTransportMessageResponse> BroadcastMessage( RpcTransportMessageRequest request, IEnumerable <IRpcMessageSenderContext> requestContext, out ICollection <RpcTransportErrorResponse> errorResponse) { this.validateMessage(request); /* * List<RpcTransportMessageResponse> resultResponse = new List<RpcTransportMessageResponse>(); * errorResponse = new List<RpcTransportErrorResponse>(); * foreach (var senderContext in requestContext) * { * ExpoMessageSenderContext context = null; * try * { * context = this.validateExpoSenderContext(senderContext); * RpcTransportMessageResponse response = this.sendMessageCore(request, context); * resultResponse.Add(response); * } * catch (Exception ex) * { * RpcTransportErrorResponse errorMsg = new RpcTransportErrorResponse(request.MessageId, context, ex); * errorResponse.Add(errorMsg); * } * } * return resultResponse; */ //并行版本 var requestContextList = requestContext.ToList(); List <RpcTransportMessageResponse> resultResponse = new List <RpcTransportMessageResponse>(); List <RpcTransportErrorResponse> errorResponseList = new List <RpcTransportErrorResponse>(); using (var countdownEvent = new CountdownEvent(1)) { for (int i = 0; i < requestContextList.Count; i++) { countdownEvent.AddCount(); ThreadPool.QueueUserWorkItem(state => { int idx = (int)state; ExpoMessageSenderContext context = null; try { var senderContext = requestContextList[idx]; context = this.validateExpoSenderContext(senderContext); RpcTransportMessageResponse response = this.sendMessageCore(request, context); resultResponse.Add(response); } catch (Exception ex) { RpcTransportErrorResponse errorMsg = new RpcTransportErrorResponse(request.MessageId, context, ex); errorResponseList.Add(errorMsg); } finally { countdownEvent.Signal(); } }, i); } countdownEvent.Signal(); countdownEvent.Wait(); } errorResponse = errorResponseList; return(resultResponse); }
private RpcTransportMessageResponse sendMessageCore(RpcTransportMessageRequest request, ExpoMessageSenderContext context) { /* * var proxy = RealProxy.InitInstanceByConnection(this.appServer); * CommandHeader header = new CommandHeader((ushort)context.TargetAppClassId, (uint)context.TargetCommandId); * var reqObj = (new ExpoCommandMessageParser.ExpoMessageInput(request)).BuidExpoMessageBody(); * object[] resObj = null; * proxy.DoCommandProxy(header, reqObj, out resObj); * var resultRpcMessage = (new ExpoCommandMessageParser.ExpoMessageOutput(resObj)).BuidRpcTransportMessage(request); * return resultRpcMessage; */ //构建消息体 var requestMsg = new Message(); requestMsg.SetCommand((ushort)context.TargetAppClassId, (uint)context.TargetCommandId); requestMsg.FillBody((new ExpoCommandMessageParser.ExpoMessageInput(request)).BuidExpoMessageBody(), true); //调用RPC var expoSyncMessage = new WindMessageBus.SyncUserMessage(requestMsg); int resultVal = this.appServer.sendMessage(expoSyncMessage, context.CommandTimeout); //处理(抛出)异常 if (expoSyncMessage.ErrInfo != null) { throw new WindServiceBusException(expoSyncMessage.ErrInfo); } var responseMsg = expoSyncMessage.Response; if (responseMsg.Header.CommandClass != context.TargetAppClassId || responseMsg.Header.CommandValue != context.TargetCommandId) { throw new WindServiceBusException("expo response not match request!"); } if (responseMsg.isErrMsg()) { string errorInfo = "unknow expo response exception"; responseMsg.GetErrInfo(out errorInfo); throw new WindServiceBusException(errorInfo); } //构建消息返回 var expoResponse = responseMsg.ToAnswerObj(); var resultRpcMessage = (new ExpoCommandMessageParser.ExpoMessageOutput(expoResponse)).BuidRpcTransportMessage(request); //resultRpcMessage.MessageHeader... //TODO: 其他响应消息头 return(resultRpcMessage); }