public ProtoBuf.IExtensible Deserialize(Pmd.ForwardNullUserPmd_CS package)
        {
            if (package == null)
            {
                return(null);
            }
            netType.Cmd   = package.byCmd;
            netType.Param = package.byParam;
            //var type = this[new NetMessageType() { Cmd = package.byCmd, Param = package.byParam }];

            var oper = this[netType];

            if (oper == null)
            {
                return(null);
            }
            // 消息内容反序列化

            if (package.data == null || package.data.Length == 0)
            {
                return(oper.New() as ProtoBuf.IExtensible);
            }



            return(oper.Deserialize(package.data) as ProtoBuf.IExtensible);
        }
Exemplo n.º 2
0
        public bool Write(Pmd.ForwardNullUserPmd_CS package, Engine.INetLink netLink, bool isFirst)
        {
            if (netLink == null)
            {
                Engine.Utility.Log.Error("严重错误:NetLink为空。");
                return(false);
            }

            count++;
            if (isFirst)
            {
                Engine.Utility.Log.Error("isfirst bitmask Bitmask_Reconnect。");
                package.bitmask |= (int)Pmd.FrameHeader.Bitmask_Reconnect;
            }
            // 消息压缩
            if (package.data.Length > 32 && count != 1)
            {
                //var zip = Common.ZlibCodec.Encode(package.data);
                //if (zip.Length < package.data.Length)
                //{
                //    package.bitmask |= (int)Pmd.FrameHeader.Bitmask_Compress;
                //    package.data = zip;
                //}
            }

            using (var mem = new MemoryStream())
            {
                package.SerializeLengthDelimited(mem);

                //Console.WriteLine("SEND: {0}\t{1}", mem.Length, BitConverter.ToString(mem.ToArray()));
                // Engine.Utility.Log.Error("SEND: {0}\t{1}", mem.Length, BitConverter.ToString(mem.ToArray()));

                Engine.PackageOut pack = new Engine.PackageOut(mem, (int)package.byCmd);
                netLink.SendMsg(pack);
            }

            return(true);
        }
        public Pmd.ForwardNullUserPmd_CS Serialize(ProtoBuf.IExtensible message)
        {
            if (message == null)
            {
                return(null);
            }
            var type = this[message.GetType()];

            if (type == NetMessageType.Empty)
            {
                return(null);
            }
            var package = new Pmd.ForwardNullUserPmd_CS()
            {
                byCmd   = type.Cmd,
                byParam = type.Param,
                time    = DateTime.Now.ToUnixTime(),
            };
            var msg = message as ProtoBuf.IMessage;

            package.data = msg.SerializeToBytes();
            return(package);
        }