예제 #1
0
        private MCFunction CreateFucntion(MCFunction requestFunction, PacketDirection direction, MCQHeader qHeader, MCDataProcessor dataSource)
        {
            if (direction == PacketDirection.Request)
            {
                var cc = dataSource.ReadUShort();
                var sc = dataSource.ReadUShort();
                var functionRegistration = MCFunctionRegistry.Find(cc);

                if (functionRegistration != null)
                {
                    return(functionRegistration.Generate(direction, sc, requestFunction));
                }
            }
            else if (direction == PacketDirection.Response)
            {
                var qHeaderResponse = (MCQHeaderResponse)qHeader;

                if (qHeaderResponse.ResultCode == 0)
                {
                    var functionRegistration = MCFunctionRegistry.Find(requestFunction.GetType());

                    if (functionRegistration != null)
                    {
                        return(functionRegistration.Generate(direction, requestFunction.GetSubCommandCode(), requestFunction));
                    }
                }
                else
                {
                    return(new MCFunctionErrorResponse());
                }
            }

            return(null);
        }
예제 #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);
            }
        }