예제 #1
0
        private string _DoPrint(AnyProto proto, int tab)
        {
            StringBuilder sb     = new StringBuilder();
            string        strtab = "";

            for (var i = 0; i < tab; ++i)
            {
                strtab += "\t";
            }
            foreach (var fld in proto.Fields)
            {
                if (fld.Repeated)
                {
                    var repeated = (List <ProtoFiled>)fld.FieldValue;
                    sb.Append(string.Format("{2}{0} repeated {1}", fld.Index, GetTypeName(fld.FieldType), strtab));
                    foreach (var refld in repeated)
                    {
                        sb.Append(string.Format(" {0}", _DoPrintField(refld, tab + 1)));
                    }
                    sb.Append("\n");
                }
                else
                {
                    sb.Append(string.Format("{3}{0} {1} {2}\n", fld.Index, GetTypeName(fld.FieldType), _DoPrintField(fld, tab + 1), strtab));
                }
            }

            return(sb.ToString());
        }
        public override Task <AnyProto> SendInvocation(
            AnyProto request,
            ServerCallContext context)
        {
            var chaosInvocation = request.ConvertTo <ChaosInvocation>();
            var invocationReply = _chaosService.ProcessInvocation(chaosInvocation);
            var reply           = invocationReply.ToAnyProto();

            return(Task.FromResult(reply));
        }
예제 #3
0
        public T From <T>(AnyProto anyProto)
        {
            var byteArray = anyProto.Data.ToByteArray();

            return((T)_binarySerializer.Deserialize(typeof(T), byteArray));
        }
 public static T ConvertTo <T>(this AnyProto anyProto)
 {
     return(AnyProtoConvert.From <T>(anyProto));
 }
예제 #5
0
 public string Print(AnyProto proto)
 {
     return(_DoPrint(proto, 0));
 }
예제 #6
0
        private AnyProto _DoParse(byte[] data, uint start, uint len, bool log = true)
        {
            try
            {
                AnyProto anyProto = new AnyProto();

                uint index = start;
                while (len - index > 0)
                {
                    ProtoFiled field = new ProtoFiled();
                    uint       value = 0;
                    uint       used  = 0;

                    if (!ReadVarint32(out value, out used, data, index, len))
                    {
                        if (log)
                        {
                            LogError("ReadVarint32 Failed");
                        }
                        return(null);
                    }

                    index      += used;
                    field.Index = value >> 3;
                    uint nWireType = value & 0x7;
                    if (nWireType > (uint)WireType.WIRETYPE_FIXED32)
                    {
                        if (log)
                        {
                            LogError("WireType > WIRETYPE_FIXED32");
                        }
                        return(null);
                    }

                    WireType type = (WireType)nWireType;

                    switch (type)
                    {
                    case WireType.WIRETYPE_VARINT:
                    {
                        ulong vvalue = 0;
                        if (!ReadVarint64(out vvalue, out used, data, index, len))
                        {
                            if (log)
                            {
                                LogError("ReadVarint64 Failed");
                            }
                            return(null);
                        }
                        index += used;

                        long lvalue = (long)vvalue;
                        if (lvalue > int.MaxValue)
                        {
                            field.FieldType  = typeof(long);
                            field.FieldValue = lvalue;
                        }
                        else
                        {
                            field.FieldType  = typeof(int);
                            field.FieldValue = (int)vvalue;
                        }
                    }
                    break;

                    case WireType.WIRETYPE_FIXED32:
                    {
                        int rvalue = 0;
                        rvalue  = (int)data[index];
                        rvalue |= ((int)data[index + 1]) << 8;
                        rvalue |= ((int)data[index + 2]) << 16;
                        rvalue |= ((int)data[index + 3]) << 24;

                        field.FieldType  = typeof(int);
                        field.FieldValue = rvalue;

                        index += 4;
                    }
                    break;

                    case WireType.WIRETYPE_FIXED64:
                    {
                        long rvalue = 0;
                        rvalue  = (long)data[index];
                        rvalue |= ((long)data[index + 1]) << 8;
                        rvalue |= ((long)data[index + 2]) << 16;
                        rvalue |= ((long)data[index + 3]) << 24;
                        rvalue |= ((long)data[index + 4]) << 32;
                        rvalue |= ((long)data[index + 5]) << 40;
                        rvalue |= ((long)data[index + 6]) << 48;
                        rvalue |= ((long)data[index + 7]) << 56;

                        field.FieldType  = typeof(long);
                        field.FieldValue = rvalue;

                        index += 8;
                    }
                    break;

                    case WireType.WIRETYPE_LENGTH_DELIMITED:
                    {
                        if (!ReadVarint32(out value, out used, data, index, len))
                        {
                            if (log)
                            {
                                LogError("ReadVarint32 Failed");
                            }
                            return(null);
                        }
                        index += used;

                        if (index + value > len)
                        {
                            //Core.Debug.LogError("used data beyond data size");
                            value = (uint)data.Length - index;
                            return(null);
                        }

                        AnyProto tryProto = _DoParse(data, index, index + value, false);
                        if (tryProto != null)
                        {
                            index += value;

                            field.FieldType  = typeof(object);
                            field.IsObject   = true;
                            field.FieldValue = tryProto;
                        }
                        else
                        {
                            if (index + value > data.Length)
                            {
                                //Core.Debug.LogError("used data beyond data size");
                                value = (uint)data.Length - index;
                                return(null);
                            }
                            var getValue = Encoding.UTF8.GetString(data, (int)index, (int)value);
                            index += value;

                            field.FieldType  = typeof(string);
                            field.FieldValue = getValue;
                        }
                    }
                    break;

                    case WireType.WIRETYPE_START_GROUP:
                        break;

                    case WireType.WIRETYPE_END_GROUP:
                        break;

                    default:
                        if (log)
                        {
                            LogError("unexcept");
                        }
                        break;
                    }

                    bool added = false;
                    foreach (var fld in anyProto.Fields)
                    {
                        if (fld.Index == field.Index)
                        {
                            List <ProtoFiled> reptead = null;
                            if (!fld.Repeated)
                            {
                                fld.Repeated = true;
                                reptead      = new List <ProtoFiled>();
                                var nfld = new ProtoFiled();
                                nfld.FieldType  = fld.FieldType;
                                nfld.FieldValue = fld.FieldValue;
                                nfld.Index      = fld.Index;
                                nfld.Repeated   = false;
                                reptead.Add(nfld);
                                fld.FieldValue = reptead;
                            }
                            else
                            {
                                reptead = (List <ProtoFiled>)fld.FieldValue;
                            }

                            reptead.Add(field);
                            added = true;
                            break;
                        }
                    }
                    if (!added)
                    {
                        anyProto.Fields.Add(field);
                    }
                }

                return(anyProto);
            }
            catch (System.Exception e)
            {
                //Core.Debug.LogError(e);
                return(null);
            }
        }