public static HresultResponse FromResponse(WebcamResponse response)
        {
            var start = new HresultResponse();

            start.type = response.type;
            start.hr   = BitConverter.ToInt32(response.packet, 0);

            return(start);
        }
        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> WaitForEnumResponses(Port <List <CameraInstance> > resultPort)
        {
            List <CameraInstance> cameras = new List <CameraInstance>();

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

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

                if (done != null)
                {
                    if (done.type == WebcamResponse.EnumerateDevices)
                    {
                        break;
                    }
                }
                else
                {
                    var camera = new CameraInstance();

                    camera.DevicePath   = response.device;
                    camera.FriendlyName = response.name;

                    cameras.Add(camera);
                }
            }

            foreach (var camera in cameras)
            {
                var formatPort = EnumFormats(camera);

                yield return(Arbiter.Receive(false, formatPort, success => camera.SupportedFormats = success));
            }

            resultPort.Post(cameras);
        }