Beispiel #1
0
    void TestCapnProto()
    {
        if (isOnce)
        {
            return;
        }
        isOnce = true;
        var msg      = ProtoMessageMgr.CreateCapnProtoMsg();
        var loginMsg = CapnProto_Msg.LoginMsg.Create(msg.Root);

        loginMsg.userId = 200;

        msg.SaveToFile("D:/test.bin");

        //   Debug.LogError(loginMsg.ToString());

        /*
         * FileStream stream = new FileStream("D:/test.bin", FileMode.Create);
         */


        //  NetManager.Instance.SendCapnProto(msg, 1);


        // msg.TestInfoToFile("d:/test.txt");

        msg.Dispose();
    }
Beispiel #2
0
        public void SendProtoBuf <T>(T data, int packetHandle) where T : class, Google.Protobuf.IMessage <T>
        {
            if (data == null)
            {
                return;
            }

            // ProtoBuf 2.0接口

            /*
             *          System.IO.MemoryStream stream = new System.IO.MemoryStream ();
             *          ProtoBuf.Serializer.Serialize<T> (stream, data);
             *          byte[] buf = stream.ToArray ();
             *          stream.Close ();
             *          stream.Dispose ();
             *          Send (buf, packetHandle);
             */

            // protobuf 3.0接口
            //  byte[] buf = ProtoMessageMgr.GetInstance().ToBuffer<T>(data);
            // Send(buf, packetHandle);

            // 优化后版本使用byte[]池
            int outSize;
            var stream = ProtoMessageMgr.ToBufferNode <T>(data, out outSize);

            if (stream == null)
            {
                return;
            }
            try
            {
                if (outSize <= 0)
                {
                    return;
                }
                var buf = stream.GetBuffer();
                Send(buf, packetHandle, outSize);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                    stream = null;
                }
            }
        }
Beispiel #3
0
    void TestProtoMessage()
    {
        var req = new C_S_Login_Req();

        req.UserName = "******";
        req.Password = "******";
        int dataSize;

        UnityEngine.Profiling.Profiler.BeginSample("ProtoToBuf");
        var stream = ProtoMessageMgr.ToBufferNode <C_S_Login_Req>(req, out dataSize);

        UnityEngine.Profiling.Profiler.EndSample();

        UnityEngine.Profiling.Profiler.BeginSample("BufToProto");
        // var rep = ProtoMessageMgr.Instance.Parser<C_S_Login_Req>(stream.GetBuffer(), stream.DataSize);
        var rep = C_S_Login_Req.Parser.ParseFrom(stream.GetBuffer(), 0, stream.DataSize);

        UnityEngine.Profiling.Profiler.EndSample();

        stream.Dispose();
    }
Beispiel #4
0
 void Start()
 {
     ProtoMessageMgr.GetInstance().Register <C_S_Login_Req>(C_S_Login_Req.Parser);
     NetManager.Instance.AddPacketListener(100, OnTestProtoCallBack);
 }