Ejemplo n.º 1
0
            public static InternalPacket ProtoDeserialize(byte[] data, int pos, int size)
            {
                InternalPacket ret         = new InternalPacket();
                PacketProto    protoPacket = new PacketProto();

                MemoryStream ms = new MemoryStream(data, pos, size);

                protoPacket.MergeFrom(ms);

                ret.MsgId          = protoPacket.MsgId;
                ret.FullMethodName = protoPacket.FullMethodName;
                ret.Options        = protoPacket.Options;

                if (protoPacket.DataTypeCase == PacketProto.DataTypeOneofCase.Payload)
                {
                    ret.Payload = protoPacket.Payload.Data.ToByteArray();
                    int payloadSize = protoPacket.Payload.Size;

                    if (payloadSize < 0 || payloadSize > ms.Length || ret.Payload.Length != payloadSize)
                    {
                        throw new InvalidDataException("Pacote inválido: tamanho de payload inválido");
                    }
                }

                if (protoPacket.DataTypeCase == PacketProto.DataTypeOneofCase.Exception)
                {
                    ret.Exception = RpcExceptionFactory.CreateFrom(protoPacket.Exception);
                }

                return(ret);
            }
Ejemplo n.º 2
0
            public static byte [] ProtoSerialize(InternalPacket data)
            {
                PacketProto packet = new PacketProto();

                packet.FullMethodName = data.FullMethodName ?? "";
                packet.MsgId          = data.MsgId;
                packet.Options        = data.Options;

                if (data.Payload != null)
                {
                    packet.Payload      = new PayloadProto();
                    packet.Payload.Size = data.Payload.Length;
                    packet.Payload.Data = ByteString.CopyFrom(data.Payload);
                }

                if (data.Exception != null)
                {
                    packet.Exception            = new RpcExceptionProto();
                    packet.Exception.StatusCode = (int)data.Exception.Status.StatusCode;
                    packet.Exception.Detail     = data.Exception.Status.Detail;

                    if (data.Exception.Message == data.Exception.Status.ToString())
                    {
                        packet.Exception.Message = "";
                    }
                    else
                    {
                        packet.Exception.Message = data.Exception.Message;
                    }
                }

                return(packet.ToByteArray());
            }