public SetFormatRequest(FormatResponse format)
 {
     Width        = format.Width;
     Height       = format.Height;
     MinFrameRate = 0;
     MaxFrameRate = 0;
     Compression  = format.Compression;
 }
        public static FormatResponse FromResponse(WebcamResponse response)
        {
            var format = new FormatResponse();

            format.Index       = BitConverter.ToInt32(response.packet, 0);
            format.Width       = BitConverter.ToInt32(response.packet, 8);
            format.Height      = BitConverter.ToInt32(response.packet, 12);
            format.Compression = BitConverter.ToUInt32(response.packet, 20);

            return(format);
        }
        IEnumerator <ITask> DoEnumFormats(CameraInstance camera, Port <List <Format> > resultPort)
        {
            List <Format> formats = new List <Format>();

            new OpenRequest(camera.DevicePath).Send(_client);

            HresultResponse hr = null;

            do
            {
                yield return(Arbiter.Receive <HresultResponse>(false, _pipeDataPort, success => hr = success));
            } while (hr.type != WebcamResponse.OpenDevices);

            new FormatRequest().Send(_client);

            for (; ;)
            {
                FormatResponse  response = null;
                HresultResponse done     = null;

                yield return(Arbiter.Choice(
                                 Arbiter.Receive <FormatResponse>(false, _pipeDataPort, success => response = success),
                                 Arbiter.Receive <HresultResponse>(false, _pipeDataPort, hresult => done = hresult)
                                 ));

                if (done != null)
                {
                    if (done.type == WebcamResponse.EnumeratFormats)
                    {
                        break;
                    }
                }
                else
                {
                    var format = new Format(
                        response.Width,
                        response.Height,
                        0,
                        0,
                        response.Compression
                        );

                    formats.Add(format);
                }
            }


            resultPort.Post(formats);
        }
        IEnumerator <ITask> ProcessPipe(NamedPipeClientStream client)
        {
            Port <IAsyncResult> continuation = new Port <IAsyncResult>();

            for (; ;)
            {
                IAsyncResult ar       = null;
                var          response = new WebcamResponse();

                client.BeginRead(response.header,
                                 0,
                                 WebcamResponse.HeaderSize,
                                 continuation.Post,
                                 null
                                 );

                yield return(Arbiter.Receive(false, continuation, result => ar = result));

                var length = client.EndRead(ar);
                if (length != 8)
                {
                    if (0 < Interlocked.CompareExchange(ref _shutdown, 1, 1))
                    {
                        LogInfo("Pipe has been closed");
                        client = null;
                    }
                    else
                    {
                        LogError("Bad header length read from pipe");
                    }
                    yield break;
                }

                response.packet = new byte[response.packetSize];

                client.BeginRead(response.packet,
                                 0,
                                 response.packetSize,
                                 continuation.Post,
                                 null
                                 );

                yield return(Arbiter.Receive(false, continuation, result => ar = result));

                length = client.EndRead(ar);
                if (length != response.packetSize)
                {
                    if (0 < Interlocked.CompareExchange(ref _shutdown, 1, 1))
                    {
                        LogInfo("Pipe has been closed");
                        client = null;
                    }
                    else
                    {
                        LogError("Bad packet length read from pipe");
                    }
                    yield break;
                }

                if (response.IsKnownType)
                {
                    var item = response.KnownType;

                    if (item is EnumResponse)
                    {
                        _pipeDataPort.Post((EnumResponse)item);
                    }
                    else if (item is FormatResponse)
                    {
                        var format = (FormatResponse)item;

                        if (format.Index == -1)
                        {
                            _format = format;
                            if (_state.Selected != null)
                            {
                                if (_state.Selected.Format == null)
                                {
                                    _state.Selected.Format = new Format();
                                }
                                _state.Selected.Format.Width  = _format.Width;
                                _state.Selected.Format.Height = _format.Height;
                            }
                        }
                        else
                        {
                            _pipeDataPort.Post(format);
                        }
                    }
                    else if (item is HresultResponse)
                    {
                        _pipeDataPort.Post((HresultResponse)item);
                    }
                }
                else if (response.IsFrame)
                {
                    ProcessFrame(response);
                }
            }
        }