コード例 #1
0
        private string FormatCACLog(AmpMessage req, AmpMessage rsp, long elapsedMS, Google.Protobuf.MessageParser reqParser, Google.Protobuf.MessageParser resParser)
        {
            string mothedName = req.FriendlyServiceName ?? req.MethodIdentifier;

            IMessage reqMsg = null;
            IMessage resMsg = null;

            if (req.Data != null)
            {
                reqMsg = reqParser.ParseFrom(req.Data);
            }
            if (rsp.Data != null)
            {
                resMsg = resParser.ParseFrom(rsp.Data);
            }

            var jsonReq = reqMsg == null ? "" : JsonFormatter.Format(reqMsg);
            var jsonRsp = resMsg == null ? "" : JsonFormatter.Format(resMsg);

            var clientIP  = FindFieldValue(reqMsg, "client_ip");
            var requestId = FindFieldValue(reqMsg, "x_request_id");

            if (string.IsNullOrEmpty(clientIP))
            {
                clientIP = "UNKNOWN";
            }
            if (string.IsNullOrEmpty(requestId))
            {
                requestId = "UNKNOWN";
            }
            //clientIp,requestId,serviceName, elapsedMS,status_code
            return(string.Format("{0},  {1},  {2},  req={3},  res={4},  {5}", clientIP, requestId, mothedName, jsonReq, jsonRsp, elapsedMS, rsp.Code));
        }
コード例 #2
0
        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);
        }
コード例 #3
0
 static int Deserialize(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 2);
         Google.Protobuf.MessageParser arg0 = (Google.Protobuf.MessageParser)ToLua.CheckObject <Google.Protobuf.MessageParser>(L, 1);
         byte[] arg1 = ToLua.CheckByteBuffer(L, 2);
         Google.Protobuf.IMessage o = Client.Deserialize(arg0, arg1);
         ToLua.PushObject(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
コード例 #4
0
        private string FormatRequestLog(IRpcContext context, AmpMessage req, AmpMessage rsp, long elapsedMS, Google.Protobuf.MessageParser reqParser, Google.Protobuf.MessageParser resParser)
        {
            string remoteIP = "UNKNOWN";

            if (context != null && context.RemoteAddress != null)
            {
                remoteIP = DotBPE.Rpc.Utils.ParseUtils.ParseEndPointToIPString(context.RemoteAddress);
            }
            string   mothedName = req.FriendlyServiceName ?? req.MethodIdentifier;
            IMessage reqMsg     = null;
            IMessage resMsg     = null;

            if (req.Data != null)
            {
                reqMsg = reqParser.ParseFrom(req.Data);
            }
            if (rsp.Data != null)
            {
                resMsg = resParser.ParseFrom(rsp.Data);
            }

            var jsonReq = reqMsg == null ? "" : JsonFormatter.Format(reqMsg);
            var jsonRsp = resMsg == null ? "" : JsonFormatter.Format(resMsg);

            var clientIP  = FindFieldValue(reqMsg, "client_ip");
            var requestId = FindFieldValue(reqMsg, "x_request_id");

            if (string.IsNullOrEmpty(clientIP))
            {
                clientIP = "UNKNOWN";
            }
            if (string.IsNullOrEmpty(requestId))
            {
                requestId = "UNKNOWN";
            }
            //remoteIP,clientIp,requestId,serviceName,request_data,response_data , elapsedMS ,status_code
            return(string.Format("{0},  {1},  {2},  {3},  req={4},  res={5},  {6},  {7}", remoteIP, clientIP, requestId, mothedName, jsonReq, jsonRsp, elapsedMS, rsp.Code));
        }
コード例 #5
0
ファイル: FieldCodec.cs プロジェクト: lthcweb/Landlords
 /// <summary>
 /// Retrieves a codec suitable for a message field with the given tag.
 /// </summary>
 /// <param name="tag">The tag.</param>
 /// <param name="parser">A parser to use for the message type.</param>
 /// <returns>A codec for the given tag.</returns>
 public static FieldCodec <T> ForMessage <T>(uint tag, MessageParser <T> parser) where T : IMessage
 {
     return(new FieldCodec <T>(input => { T message = parser.CreateTemplate(); input.ReadMessage(message); return message; },
                               (output, value) => output.WriteMessage(value), message => CodedOutputStream.ComputeMessageSize(message), tag));
 }
コード例 #6
0
 /// <summary>
 /// Retrieves a codec suitable for a group field with the given tag.
 /// </summary>
 /// <param name="startTag">The start group tag.</param>
 /// <param name="endTag">The end group tag.</param>
 /// <param name="parser">A parser to use for the group message type.</param>
 /// <returns>A codec for given tag</returns>
 public static FieldCodec <T> ForGroup <T>(uint startTag, uint endTag, MessageParser <T> parser) where T : IMessage <T>
 {
     return(new FieldCodec <T>(input => { T message = parser.CreateTemplate(); input.ReadGroup(message); return message; },
                               (output, value) => output.WriteGroup(value), message => CodedOutputStream.ComputeGroupSize(message), startTag, endTag));
 }
コード例 #7
0
    public IEnumerator Load <R>(DataLoader.IResultReceiver loader, string path)
        where R : Google.Protobuf.IMessage <R>
    {
        //get message parser by reflection
        PropertyInfo propertyInfo;

        propertyInfo = typeof(R).GetProperty("Parser", BindingFlags.Public | BindingFlags.Static);
        // Use the PropertyInfo to retrieve the value from the type by not passing in an instance
        Google.Protobuf.MessageParser <R> parser =
            (Google.Protobuf.MessageParser <R>)propertyInfo.GetValue(null, null);

        //get record name by reflection
        var    eth      = Game.Main.RPCMgr.Eth;
        string contract = path;
        string name     = typeof(R).Name;

        //load local objects
        int current_gen   = 0;
        var storage       = Game.Main.StorageMgr;
        var local_records = storage.LoadAllRecords(name, parser, out current_gen);

        if (current_gen < 0)
        {
            loader.Error = new System.Exception("fail to load record");
            yield break;
        }

        //get size of updated records
        if (eth[contract] == null)
        {
            loader.Error = new System.Exception("contract not exists: name = " + contract);
            yield break;
        }
        var call = eth[contract].Call("recordIdDiff");

        Debug.Log("curent_gen:" + current_gen);
        yield return(call.Exec(name, current_gen));

        if (call.Error != null)
        {
            loader.Error = call.Error;
            yield break;
        }
        int next_gen;
        List <List <byte[]> > ids;

        try {
            var dto = call.AsDTO <RecordIdDiffOutput>();
            if (int.TryParse(dto.NextGen.ToString(), out next_gen) && next_gen > 0)
            {
                ids = dto.IdChunks;
            }
            else
            {
                throw new System.Exception(name + ":gen number is too big or out of range:" +
                                           dto.NextGen.ToString());
            }
        } catch (System.Exception e) {
            Debug.Log("recordIdDiff error:" + e.Message + " at " + e.StackTrace);
            loader.Error = e;
            yield break;
        }

        //if updated records found, retrieve it from contract
        if (ids.Count > 0)
        {
            HashSet <byte[]> hs = new HashSet <byte[]>();
            //de-dupe duplicate record (update multiple time since last updated time)
            for (int i = 0; i < ids.Count; i++)
            {
                for (int j = 0; j < ids[i].Count; j++)
                {
                    hs.Add(ids[i][j]);
                }
            }
            byte[][] updated_ids_distinct = hs.ToArray();
            R[]      updated_records      = new R[updated_ids_distinct.Length];

            //then query updated records
            for (int n_query = 0; n_query < updated_ids_distinct.Length; n_query += BATCH_QUERY_SIZE)
            {
                var batch_ids = updated_ids_distinct.Skip(n_query).Take(BATCH_QUERY_SIZE).ToArray();
                Debug.Log(name + ": n_query = " + n_query + " and " + batch_ids.Length);
                call = eth[contract].Call("getRecords");
                yield return(call.Exec(name, batch_ids));

                if (call.Error != null)
                {
                    loader.Error = call.Error;
                    yield break;
                }
                try {
                    var rs = call.AsMsgs <R>(parser, 0);
                    for (int i = n_query; i < (n_query + batch_ids.Length); i++)
                    {
                        local_records[batch_ids[i - n_query]] = rs[i];
                        updated_records[i] = rs[i];
                    }
                } catch (System.Exception e) {
                    loader.Error = e;
                    yield break;
                }
            }

            //save updated records into local storage
            var err = storage.SaveRecords(next_gen, name, updated_ids_distinct, updated_records);
            if (err != null)
            {
                loader.Error = err;
                yield break;
            }
        }
        else
        {
            Debug.Log(name + ": no update since last access:" + local_records.Count);
        }

        //set source
        var s = new ConstractSource <R>(local_records.Count);

        Source = s;
        foreach (var kv in local_records)
        {
            s.Add(kv.Value);
        }
        s.Rewind();
        yield break;
    }