private Google.Protobuf.MessageParser GetMessageParser(ushort serviceId, ushort messageId, int type) { string cacheKey = string.Format("{0}_{1}_{2}", serviceId, messageId, type); Google.Protobuf.MessageParser tmp = null; if (tempCache.ContainsKey(cacheKey)) { tempCache.TryGetValue(cacheKey, out tmp); } if (tmp == null) { IMessage tMsg = null; if (type == 1) { tMsg = _factory.GetRequestTemplate(serviceId, messageId); } else { tMsg = _factory.GetResponseTemplate(serviceId, messageId); } if (tMsg != null) { tmp = tMsg.Descriptor.Parser; tempCache.TryAdd(cacheKey, tmp); } } return(tmp); }
public virtual AmpMessage ToMessage(RequestData reqData) { ushort serviceId = (ushort)reqData.ServiceId; ushort messageId = (ushort)reqData.MessageId; AmpMessage message = AmpMessage.CreateRequestMessage(serviceId, messageId); IMessage reqTemp = _factory.GetRequestTemplate(serviceId, messageId); if (reqTemp == null) { this._logger.LogError("serviceId={0},messageId={1}的消息模板不存在", serviceId, messageId); return(null); } try { var descriptor = reqTemp.Descriptor; if (!string.IsNullOrEmpty(reqData.RawBody)) { reqTemp = descriptor.Parser.ParseJson(reqData.RawBody); } if (reqData.Data != null && reqData.Data.Count > 0) { foreach (var field in descriptor.Fields.InDeclarationOrder()) { if (reqData.Data.ContainsKey(field.Name)) { ProtobufHelper.SetValue(field.Accessor, reqTemp, reqData.Data[field.Name]); } else if (reqData.Data.ContainsKey(field.JsonName)) { ProtobufHelper.SetValue(field.Accessor, reqTemp, reqData.Data[field.JsonName]); } } } message.Data = reqTemp.ToByteArray(); } catch (Exception ex) { this._logger.LogError(ex, "从请求中解析数据错误"); message = null; } return(message); }