Пример #1
0
        /// <summary>
        /// Creates a device option
        /// </summary>
        /// <param name="wire">The wire to read on</param>
        /// <param name="number">The option number</param>
        /// <param name="handle">The device handle</param>
        /// <param name="userName">The username</param>
        /// <param name="password">The password</param>
        /// <param name="reloadFunction">The function to call if
        /// the device requires reloading options</param>
        /// <returns>The device option</returns>
        private IDeviceOption CreateDeviceOption(NetworkMethods wire,
                                                 int number,
                                                 int handle,
                                                 string userName,
                                                 string password,
                                                 Action reloadFunction)
        {
            var name        = wire.ReadString();
            var title       = wire.ReadString();
            var description = wire.ReadString();

            int type = wire.ReadWord();
            int unit = wire.ReadWord();
            int size = wire.ReadWord();
            int cap  = wire.ReadWord();
            int ct   = wire.ReadWord();

            var opt = new NetworkDeviceOption(name,
                                              title,
                                              description,
                                              size,
                                              number,
                                              (SaneType)type,
                                              (SaneUnit)unit,
                                              (SaneCapabilities)cap,
                                              handle,
                                              this,
                                              userName,
                                              password,
                                              reloadFunction);

            switch (ct)
            {
            case (int)SaneConstraint.None:
                opt.Constraint = new NoneConstraint();
                break;

            case (int)SaneConstraint.IntegerList:
                opt.Constraint = CreateIntegerListConstraint(wire);
                break;

            case (int)SaneConstraint.StringList:
                opt.Constraint = CreateStringListConstraint(wire);
                break;

            case (int)SaneConstraint.Range:
                opt.Constraint = CreateRangeConstraint(wire);
                break;

            default:
                throw new NotSupportedException(
                          string.Format
                              (CultureInfo.CurrentCulture,
                              "The constraint type '{0}' is not supported",
                              ct));
            }

            return(opt);
        }
Пример #2
0
        private IOpenableDevice CreateDevice(NetworkMethods wire,
                                             string userName,
                                             string password)
        {
            var name   = wire.ReadString();
            var vendor = wire.ReadString();
            var model  = wire.ReadString();
            var type   = wire.ReadString();

            return(new NetworkDevice(name,
                                     vendor,
                                     model,
                                     type,
                                     this,
                                     userName,
                                     password));
        }
Пример #3
0
        /// <summary>
        /// Actually performs the Start remote procedure call
        /// </summary>
        /// <param name="handle">The handle to the device</param>
        /// <param name="userName">The username</param>
        /// <param name="password">The password</param>
        /// <param name="firstTime"><c>true</c> if this is the first attempt
        /// (in which case it will retry with the credentials)</param>
        /// <param name="stream">The stream to add onto (in the case of
        /// multiple calls for one image)</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <param name="bytesPerLine">The returned bytes per line</param>
        /// <param name="pixelsPerLine">The returned pixels per line</param>
        /// <param name="lines">The returned lines</param>
        /// <param name="depth">The returned color depth</param>
        /// <param name="littleEndian"><c>true</c> if the data is little
        /// endian</param>
        /// <param name="color"><c>true</c> if it is color</param>
        /// <returns>The final result of the scan</returns>
        private MemoryStream DoScan(int handle,
                                    string userName,
                                    string password,
                                    bool firstTime,
                                    MemoryStream stream,
                                    CancellationToken cancellationToken,
                                    out int bytesPerLine,
                                    out int pixelsPerLine,
                                    out int lines,
                                    out int depth,
                                    out bool littleEndian,
                                    out bool color)
        {
            FrameFormat format;
            bool        lastFrame;

            GetParameters(handle,
                          out format,
                          out lastFrame,
                          out bytesPerLine,
                          out pixelsPerLine,
                          out lines,
                          out depth);

            color = format != FrameFormat.Gray;
            cancellationToken.ThrowIfCancellationRequested();

            _wire.SendCommand(NetworkCommand.Start);
            _wire.SendWord(handle);

            int    status    = _wire.ReadWord();
            int    port      = _wire.ReadWord();
            int    byteOrder = _wire.ReadWord();
            string resource  = _wire.ReadString();

            littleEndian = byteOrder == 0x1234;

            cancellationToken.ThrowIfCancellationRequested();

            if (!string.IsNullOrEmpty(resource) && firstTime)
            {
                Authorize(userName, password, resource);
                return(DoScan(handle,
                              userName,
                              password,
                              false,
                              stream,
                              cancellationToken,
                              out bytesPerLine,
                              out pixelsPerLine,
                              out lines,
                              out depth,
                              out littleEndian,
                              out color));
            }

            if (status != (int)SaneStatus.Success)
            {
                throw NSaneException.CreateFromStatus(status);
            }

            MemoryStream ret;

            if (!lastFrame)
            {
                ret = AddImageData(stream, format, port);
                return(DoScan(handle,
                              userName,
                              password,
                              false,
                              ret,
                              cancellationToken,
                              out bytesPerLine,
                              out pixelsPerLine,
                              out lines,
                              out depth,
                              out littleEndian,
                              out color));
            }

            ret = AddImageData(stream, format, port);
            return(ret);
        }