예제 #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 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);
        }
예제 #3
0
 public abstract MCFunction Generate(PacketDirection direction, ushort subCommandCode, MCFunction request);