示例#1
0
 protected override void UpdateFrameOutcome(Queue <int> rolls)
 {
     if (_pinsRemaining == 0)
     {
         if (Turns.Count < _maxTurns)
         {
             FrameOutcome = new Strike();
         }
         else
         {
             FrameOutcome = new Spare();
         }
     }
     else
     {
         if (Turns.Count < _maxTurns)
         {
             PlayFrame(rolls);
         }
         else
         {
             FrameOutcome = new OpenFrame();
         }
     }
 }
示例#2
0
        internal static Frame ReadFromStream(BinaryReader br, int size, byte dataOffset, FrameType frameType)
        {
            var channelNumber = br.ReadUInt16();

            var ignoredExtendedHeader = br.ReadBytes((dataOffset * 4) - 8);

            var performative = (PerformativeType)(int)br.ReadByte();

            AMQPFrame frame = null;

            switch (performative)
            {
            case PerformativeType.Open:
                frame = new OpenFrame(br, dataOffset, size, channelNumber);
                break;
            }

            return(frame);
        }
示例#3
0
        /// <summary>
        /// 从远端上下载指定的文件到给定流中。
        /// </summary>
        /// <exception cref="Exception"></exception>
        /// <returns></returns>
        public async Task StartAsync(string file, Stream stream)
        {
            if (!Started)
            {
                Started = true;

                //发送请求
                OpenFrame frame = new OpenFrame()
                {
                    FileName = file,
                    FileSize = stream.Length,
                    ReadOnly = true,
                };
                base.GuidFilter = frame.Guid;
                await Send(frame).ConfigureAwait(false);

                //等待结果
                switch (await base.Answers.WaitAsync(Common.Configs.FrameTimeout).ConfigureAwait(false))
                {
                case OpenAnswerFrame answerFrame: break;

                case ErrorFrame errorFrame: throw new RemoteErrorException(errorFrame);

                default: throw new InvalidDataException();
                }

                //发送请求
                await Send(new RequestFrame(frame.Guid)).ConfigureAwait(false);

                //接收数据
                bool terminated = false;
                try
                {
                    while (Size == -1 || TransferedSize < Size)
                    {
                        switch (await base.Answers.WaitAsync(Common.Configs.FrameTimeout).ConfigureAwait(false))
                        {
                        case DataFrame dataFrame:
                        {
                            if (Size != -1 && Size != dataFrame.FileSize)
                            {
                                throw new InvalidDataException("传输大小在中途更变");
                            }
                            else
                            {
                                Size = dataFrame.FileSize;
                            }

                            await stream.WriteAsync(dataFrame.Data, 0, dataFrame.Data.Length).ConfigureAwait(false);

                            TransferedSize = Math.Min(Size, TransferedSize + dataFrame.Data.Length);
                            Recorder.Record(TransferedSize);
                        }
                        break;

                        case ErrorFrame errorFrame: throw new RemoteErrorException(errorFrame);

                        default: throw new InvalidDataException();
                        }
                    }
                }
                catch
                {
                    terminated = true;
                }


                //结束请求
                await Send(new CloseFrame(frame.Guid)).ConfigureAwait(false);

                ////等待结果
                //switch (await base.Answers.WaitAsync().ConfigureAwait(false))
                //{
                //    case CloseAnswerFrame answerFrame: break;
                //    case ErrorFrame errorFrame: throw new RemoteErrorException(errorFrame);
                //    default: throw new InvalidDataException();
                //}

                if (terminated)
                {
                    throw new IOException();
                }
                Finished = true;
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
 public OpenFrameTests()
 {
     _frameUnderTest = new OpenFrame(new Roll(1), new Roll(1));
     _other          = new OpenFrame(new Roll(1), new Roll(1));
 }
示例#5
0
        /// <summary>
        /// 从流中读取数据并保存到远端文件中。
        /// </summary>
        /// <exception cref="Exception"></exception>
        /// <returns></returns>
        public async Task ToStreamAsync(Stream stream, string file)
        {
            if (!Started)
            {
                Started = true;
                Size    = stream.Length;

                //发送请求
                OpenFrame frame = new OpenFrame()
                {
                    FileName = file,
                    FileSize = stream.Length,
                    ReadOnly = false,
                };
                base.GuidFilter = frame.Guid;
                await Send(frame).ConfigureAwait(false);

                //等待结果
                switch (await base.Answers.WaitAsync(Common.Configs.FrameTimeout).ConfigureAwait(false))
                {
                case OpenAnswerFrame answerFrame: break;

                case ErrorFrame errorFrame: throw new RemoteErrorException(errorFrame);

                default: throw new InvalidDataException();
                }

                //发送数据
                bool terminated = false;
                try
                {
                    const int sizeMax = 1024 * 128 < Configs.MaxFrameSize ? 1024 * 128 : Configs.MaxFrameSize;
                    const int sizeMin = 1024 < Configs.MaxFrameSize ? 1024 : Configs.MaxFrameSize;
                    int       size    = sizeMin;

                    byte[] buffer = new byte[sizeMax];
                    while (stream.Position != stream.Length)
                    {
                        long position = stream.Position;
                        int  count    = await stream.ReadAsync(buffer, 0, size).ConfigureAwait(false);

                        DataFrame dataFrame = new DataFrame(frame.Guid)
                        {
                            FileSize   = stream.Length,
                            DataOffset = position,
                            Data       = buffer.Take(count).ToArray(),
                        };

                        await Send(dataFrame).ConfigureAwait(false);

                        TransferedSize += count;

                        Recorder.Record(TransferedSize);
                        size = Math.Max(sizeMin, Math.Min(sizeMax, Common.Tools.SpeedRecorder.UpdateFrameSize((int)Recorder.SpeedPerSecond, size)));
                    }
                }
                catch (Exception ex)
                {
                    terminated = true;
                }

                //结束请求
                CloseFrame writeCloseFrame = new CloseFrame(frame.Guid);
                await Send(writeCloseFrame).ConfigureAwait(false);

                //等待结果
                switch (await base.Answers.WaitAsync().ConfigureAwait(false))
                {
                case CloseAnswerFrame answerFrame: break;

                case ErrorFrame errorFrame: throw new RemoteErrorException(errorFrame);

                default: throw new InvalidDataException();
                }

                if (terminated)
                {
                    throw new IOException();
                }
                Finished = true;
            }
        }
示例#6
0
 public StrikeFrameTests()
 {
     _frameUnderTest = new StrikeFrame();
     _other1         = new OpenFrame(new Roll(1), new Roll(1));
     _other2         = new OpenFrame(new Roll(1), new Roll(1));
 }