🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / C# / Protobuf

1-) C# - Protobuf

 

test link : https://protobuf-decoder.netlify.app/

 

 PROTOBUF GOOGLE İLE indirme link yeni : https://drive.google.com/file/d/13Ei9TQTNWTL0tdnUUKehJPtNxdqViclo/view?usp=sharing

 

 

AÇIKLAMA: MODEL OLUŞTURMADAN DİNAMİK OLARAK DECODER ENCODER YAPILMASI İÇİN KULLANDIM

 

Install-Package Google.Protobuf -Version 3.19.2

Install-Package Google.Protobuf.Tools -Version 3.19.2

Install-Package Google.ProtocolBuffers -Version 2.4.1.555

 

// Install-Package protobuf-net

// Install-Package Newtonsoft.Json

 

 

using System;

using System.Data;

using System.Linq;

using System.Windows.Forms;

 

namespace ProtoBufGoogle

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            var fdp = getFdp("CMC4Ag==");

 

            fdp = getFdp("GgQBAwQFIhYIgMLXLxCA3vNoIICI3r4BMICI3r4BKMig5o4GMAQ4gJycOUDA0o9aSAJSBhD/4Ijzf1IJCAEQ/+P84bUCUg0IAhDYmqzWcxoDAQMEUg4IAxDVr4DznQIaAwEDBFIOCAQQ3evapHsaBAEGAgRggJycOQ==");

 

            fdp = getFdp("CgEBEh0IATjnz/vQkAJqEgjWlZXJXxCJERizCyi/puaOBg==");

 

 

            //Console.WriteLine("*********1********");

            //foreach (var item in fdp.MessageTypeList)

            //{

            //    Console.WriteLine(item.ToString());

            //}

 

            //Console.WriteLine(fdp.GetMessageType(0));

 

        }

 

        public global::Google.ProtocolBuffers.DescriptorProtos.FileDescriptorProto getFdp(string base64hexProtobuf)

        {

            byte[] bytes = Convert.FromBase64String(base64hexProtobuf);

 

            string hex = BitConverter.ToString(bytes).ToLower().Replace("-", "");

            byte[] bytee = Enumerable.Range(0, hex.Length)

                                .Where(x => x % 2 == 0)

                                .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))

                                .ToArray();

 

            global::Google.ProtocolBuffers.DescriptorProtos.FileDescriptorProto fdp = global::Google.ProtocolBuffers.DescriptorProtos.FileDescriptorProto.ParseFrom(bytee);

 

            return fdp;

        }

 

 

    }

}

 

 

kaynak : https://stackoverflow.com/questions/27380217/dynamic-protobuf-messages-in-protobuf-net/27505474#27505474

indirme link : https://drive.google.com/open?id=10c3QA7FEHccXQ-GDq3uobcMUnu-jenES&authuser=proje.ramazan.haber%40gmail.com&usp=drive_fs

 

 

2-) 2. YOL DİĞERİ YÜKLENECEK PAKETLER

 

// Install-Package protobuf-net

// Install-Package Newtonsoft.Json

 

Person.cs

 

using ProtoBuf;

using System.IO;

 

namespace WindowsFormsApp4

{

    [ProtoContract(SkipConstructor = true)]

    public class Person

    {

        [ProtoMember(1)]

        public int a { get; set; }

        public static byte[] ProtoSerialize<T>(T record) where T : class

        {

            if (null == record) return null;

            try

            {

                using (var stream = new MemoryStream())

                {

                    Serializer.Serialize(stream, record);

                    return stream.ToArray();

                }

            }

            catch

            {

                // Log error

                throw;

            }

        }

        public static T ProtoDeserialize<T>(byte[] data) where T : class

        {

            if (null == data) return null;

 

            try

            {

                using (var stream = new MemoryStream(data))

                {

                    return Serializer.Deserialize<T>(stream);

                }

            }

            catch

            {

                // Log error

                throw;

            }

        }

    }

}

 

KULLANIMI FORM1.CS

 

using Newtonsoft.Json;

using System;

using System.Data;

using System.Linq;

using System.Windows.Forms;

 

namespace WindowsFormsApp4

{

    public partial class Form1 : Form

    {

 

        // Install-Package protobuf-net

        // Install-Package Newtonsoft.Json

        public Form1()

        {

            InitializeComponent();

            calistir();

        }

 

        /*

         CMC4Ag== -> 08 c0 b8 02 -> {"a": 40000}

         */

        public void calistir()

        {

            Person person = getDecodePerson("CMC4Ag==");

            string hex = getEncodePerson(person);

 

            person.a = 70000;

            hex = getEncodePerson(person);

 

            person = getDecodePerson(hex);

 

            String newJson= JsonConvert.SerializeObject(person);

 

            Person newPerson = JsonConvert.DeserializeObject<Person>(newJson);

 

 

            Person person3 = getDecodePersonHex("08 c0 b8 02");

            string hexPerson3= getEncodePersonHex(person3);

        }

        public Person getDecodePerson(string base64Hex) // base64 edilmiş hex'den objeye çevirir

        {

            byte[] bytes = Convert.FromBase64String(base64Hex);

            string hex = BitConverter.ToString(bytes).ToLower().Replace("-", "");

            byte[] bytee = Enumerable.Range(0, hex.Length)

                             .Where(x => x % 2 == 0)

                             .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))

                             .ToArray();

            Person pObjBack = Person.ProtoDeserialize<Person>(bytee);

            return pObjBack;

        }

        public string getEncodePerson(Person project)

        {

            byte[] tempByte = Person.ProtoSerialize<Person>(project);

            //string hex = BitConverter.ToString(tempByte).ToLower().Replace("-"," ");

            string base64 = System.Convert.ToBase64String(tempByte);

            return base64;

        }

        public Person getDecodePersonHex(string hex) // hex'den objeye çevirir

        {

            hex = hex.Replace(" ", "");

            byte[] bytee = Enumerable.Range(0, hex.Length)

                             .Where(x => x % 2 == 0)

                             .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))

                             .ToArray();

 

            Person person = Person.ProtoDeserialize<Person>(bytee);

            return person;

        }

 

        public string getEncodePersonHex(Person project)

        {

            byte[] tempByte = Person.ProtoSerialize<Person>(project);

            string hex = BitConverter.ToString(tempByte).ToLower().Replace("-"," ");

            return hex;

        }

    }

}

 

 

ayrıca : https://www.borakasmer.com/protobuf-nedir/

 

 

 

\020\203\215\356\314}\032\004\001\006\002\004 hataları için bakınız

https://www.programcreek.com/cpp/?CodeExample=add+adddescriptorsimpl+descriptors+impl

 

 

http://jamesdbrock.github.io/protobuf-decoder-explainer/

 

 2022 Şubat 20 Pazar
 442