private void SaveHandler(LNReq_Save msg, PBChannel channel, int handle, uint seq) { if (!Enable) { LogSys.Log(LOG_TYPE.ERROR, "Save a message while DataOperator is Disable"); return; } var reply = NLRep_Save.CreateBuilder(); reply.SetDsMsgId(msg.DsMsgId); reply.SetKey(msg.Key); reply.Result = NLRep_Save.Types.SaveResult.Success; try { byte[] data_bytes = ByteString.Unsafe.GetBuffer(msg.DsBytes); int calc_checksum = Crc32.Compute(data_bytes); if (msg.Checksum != calc_checksum) { throw new DataChecksumError(msg.Checksum, calc_checksum); } string dataTypeName = string.Empty; if (m_DSDMessages.TryGetValue(msg.DsMsgId, out dataTypeName)) { DataCacheSystem.Instance.QueueAction(DataCacheSystem.Instance.DirectSave, msg.DsMsgId, msg.Key, data_bytes); } else { dataTypeName = MessageMapping.Query(msg.DsMsgId).Name; if (dataTypeName.StartsWith("DSD_")) { //直接写入数据库 m_DSDMessages.AddOrUpdate(msg.DsMsgId, dataTypeName, (key, oldValue) => dataTypeName); DataCacheSystem.Instance.QueueAction(DataCacheSystem.Instance.DirectSave, msg.DsMsgId, msg.Key, data_bytes); } else { //写入数据缓存 DataCacheSystem.Instance.SaveActionQueue.QueueAction(DataCacheSystem.Instance.Save, msg.DsMsgId, msg.Key, data_bytes); } } } catch (Exception e) { reply.Result = NLRep_Save.Types.SaveResult.Error; reply.SetError(e.Message); LogSys.Log(LOG_TYPE.ERROR, "Save data ERROR: msgId:({0}) seq:({1}) error:({2}) detail:{3}", msg.DsMsgId, seq, e.Message, e.StackTrace); } channel.Send(reply.Build()); }
public void Save(string key, IMessage data, SaveCallback cb, bool isForce) { Constraints.MaxSize(data); string error = null; LNReq_Save saveData = null; uint dsMsgId = MessageMapping.Query(data.GetType()); if (dsMsgId == uint.MaxValue) { error = string.Format("unknown data message: " + data.GetType().Name); } string timeoutKey = string.Format("{0}:{1}", dsMsgId, key); if (!isForce && saveOpt_timeout_.Exists(timeoutKey)) { error = "Save operation are too frequent"; cb(DSSaveResult.PrepError, error); return; } try { byte[] bytes = data.ToByteArray(); LNReq_Save.Builder saveBuilder = LNReq_Save.CreateBuilder(); saveBuilder.SetDsMsgId(dsMsgId); saveBuilder.SetKey(key); saveBuilder.SetDsBytes(ByteString.Unsafe.FromBytes(bytes)); saveBuilder.SetChecksum(Crc32.Compute(bytes)); saveData = saveBuilder.Build(); } catch (Exception e) { error = e.Message; } if (null != error) { cb(DSSaveResult.PrepError, error); return; } if (!channel_.Send(saveData)) { cb(DSSaveResult.PrepError, "unknown"); } else { //添加到超时验证 string timeoutTip = string.Format("DataStore save request timeout. MsgId:{0}, Key:{1}", dsMsgId, key); saveOpt_timeout_.Set(timeoutKey, cb, () => cb(DSSaveResult.TimeoutError, timeoutTip)); } }
private Tuple <string, LNReq_Save> BuildSaveRequestMsg(IMessage data) { uint id = MessageMapping.Query(data.GetType()); if (id == uint.MaxValue) { return(Tuple.Create("unknown data message: " + data.GetType().Name, default(LNReq_Save))); } try { byte[] bytes = data.ToByteArray(); return(Tuple.Create( default(string), LNReq_Save.CreateBuilder() .SetDsMsgId(id) .SetDsBytes(ByteString.Unsafe.FromBytes(bytes)) .SetChecksum(Crc32.Compute(bytes)) .Build())); } catch (Exception e) { return(Tuple.Create(e.Message, default(LNReq_Save))); } }