コード例 #1
0
        public MCPacket ReadPacket(Stream stream, MCFunction requestFunction)
        {
            var packet = new MCPacket();

            var source = this.CreateDataProcessor(stream);

            source.ForceBigEndian = true;
            var frameId = source.ReadUShort();

            source.ForceBigEndian = false;
            var frame     = MCFrame.Find(frameId);
            var direction = frame.GetDirection(frameId);

            var subHeader = MCSubHeader.Find(frame).CreateInstance();

            subHeader.Read(source);
            packet.SubHeader = subHeader;

            var qHeader = this.CreateQHeader(direction);

            qHeader.ReadPre(source);
            packet.QHeader = qHeader;

            ushort dataLength = (ushort)(source.ReadUShort() / source.LengthPerByte);
            var    data       = source.ReadBytes(dataLength);

            using (var ms = new MemoryStream(data))
            {
                var dataSource = this.CreateDataProcessor(ms);
                dataSource.IgnoreConvert = true;
                qHeader.ReadPost(dataSource);

                var function = this.CreateFucntion(requestFunction, direction, qHeader, dataSource);

                if (function != null)
                {
                    function.Read(dataSource);
                }

                packet.Function = function;
            }

            return(packet);
        }
コード例 #2
0
        public void WritePacket(Stream stream, MCPacket packet)
        {
            var direction = packet.Function.GetDirection();
            var target    = this.CreateDataProcessor(stream);

            var frame   = MCSubHeader.Find(packet.SubHeader.GetType()).Frame;
            var frameId = frame.GetId(direction);

            target.ForceBigEndian = true;
            target.WriteUShort(frameId);
            target.ForceBigEndian = false;

            var subHeader = packet.SubHeader;

            subHeader.Write(target);

            var qHeader = packet.QHeader;

            qHeader.WritePre(target);

            using (var ms = new MemoryStream())
            {
                var dataTarget = this.CreateDataProcessor(ms);
                qHeader.WritePost(dataTarget);
                var function = packet.Function;

                if (direction == PacketDirection.Request)
                {
                    dataTarget.WriteUShort(MCFunctionRegistry.Find(function.GetType()).Id);
                    dataTarget.WriteUShort(function.GetSubCommandCode());
                }

                function.Write(dataTarget);

                var data = ms.ToArray();
                target.WriteUShort((ushort)data.Length);
                target.IgnoreConvert = true;
                target.WriteBytes(data);
            }
        }