Пример #1
0
        public static IEnumerable <byte[]> Execute(Twain32 twain32, IEnumerable <byte[]> commands)
        {
            var _proxy = new TwainProxy {
                Twain32 = twain32
            };

            foreach (var _data in commands)
            {
                for (var _command = TwainCommand.FromArray(_data); _command != null;)
                {
                    _proxy._ExecuteCore(_command);
                    yield return(_command.ToArray());

                    break;
                }
            }
            yield break;
        }
Пример #2
0
        public static void Handler(Twain32 twain32)
        {
            try {
                #region Method Handler

                Action _method = () => {
                    var _result = TwainProxy.Execute(twain32, Convert.FromBase64String(Console.In.ReadLine()));

                    Console.Out.WriteLine(Separators.BeginResponse);
                    Console.Out.WriteLine(Convert.ToBase64String(_result));
                    Console.Out.WriteLine(Separators.EndResponse);
                };

                #endregion

                #region Event Handler

                Action <EventHandlerTwainCommand> _event = command => {
                    Console.Out.WriteLine(Separators.BeginEvent);
                    try {
                        Console.Out.WriteLine(Convert.ToBase64String(command.ToArray()));
                        for (string _responce = null; !string.IsNullOrEmpty(_responce = Console.In.ReadLine());)
                        {
                            if (_responce == Separators.BeginRequest)
                            {
                                _method();
                                continue;
                            }

                            var _res = ((EventHandlerTwainCommand)TwainCommand.FromArray(Convert.FromBase64String(_responce))).Args;
                            foreach (var _property in _res.GetType().GetProperties())
                            {
                                var _setter = _property.GetSetMethod();
                                if (_setter != null)
                                {
                                    _setter.Invoke(command.Args, new object[] { _property.GetValue(_res, null) });
                                }
                            }
                            break;
                        }
                    } finally {
                        Console.Out.WriteLine(Separators.EndEvent);
                    }
                };

                #endregion

                #region AcquireError

                twain32.AcquireError += (sender, e) => {
                    _event(new EventHandlerTwainCommand {
                        Member = typeof(Twain32).GetEvent(Twain32Events.AcquireError),
                        Args   = e
                    });
                };

                #endregion

                #region XferDone

                twain32.XferDone += (sender, e) => {
                    var _args = new Twain32.SerializableCancelEventArgs {
                        Cancel = e.Cancel
                    };
                    _event(new EventHandlerTwainCommand {
                        Member = typeof(Twain32).GetEvent(Twain32Events.XferDone),
                        Args   = _args
                    });
                    e.Cancel = _args.Cancel;
                };

                #endregion

                #region EndXfer

                twain32.EndXfer += (sender, e) => {
                    _event(new EventHandlerTwainCommand {
                        Member = typeof(Twain32).GetEvent(Twain32Events.EndXfer),
                        Args   = e
                    });
                };

                #endregion

                #region SetupMemXferEvent

                twain32.SetupMemXferEvent += (sender, e) => {
                    _event(new EventHandlerTwainCommand {
                        Member = typeof(Twain32).GetEvent(Twain32Events.SetupMemXferEvent),
                        Args   = e
                    });
                };

                #endregion

                #region MemXferEvent

                twain32.MemXferEvent += (sender, e) => {
                    _event(new EventHandlerTwainCommand {
                        Member = typeof(Twain32).GetEvent(Twain32Events.MemXferEvent),
                        Args   = e
                    });
                };

                #endregion

                #region SetupFileXferEvent

                twain32.SetupFileXferEvent += (sender, e) => {
                    _event(new EventHandlerTwainCommand {
                        Member = typeof(Twain32).GetEvent(Twain32Events.SetupFileXferEvent),
                        Args   = e
                    });
                };

                #endregion

                #region FileXferEvent

                twain32.FileXferEvent += (sender, e) => {
                    _event(new EventHandlerTwainCommand {
                        Member = typeof(Twain32).GetEvent(Twain32Events.FileXferEvent),
                        Args   = e
                    });
                };

                #endregion

                #region TwainStateChanged

                twain32.TwainStateChanged += (sender, e) => {
                    _event(new EventHandlerTwainCommand {
                        Member = typeof(Twain32).GetEvent(Twain32Events.TwainStateChanged),
                        Args   = e
                    });
                };

                #endregion

                #region AcquireCompleted

                twain32.AcquireCompleted += (sender, e) => {
                    _event(new EventHandlerTwainCommand {
                        Member = typeof(Twain32).GetEvent(Twain32Events.AcquireCompleted),
                        Args   = e
                    });
                };

                #endregion

                Console.Out.WriteLine(Separators.Ready);
                for (string _query = null; !string.IsNullOrEmpty(_query = Console.In.ReadLine()) && _query != Separators.End;)
                {
                    if (_query == Separators.BeginRequest)
                    {
                        _method();
                    }
                }
            } catch (Exception ex) {
                try {
                    Console.Error.WriteLine(Separators.BeginException);
                    for (var _ex = ex; _ex != null; _ex = _ex.InnerException)
                    {
                        Console.Error.WriteLine("{0}: {1}", _ex.GetType().Name, _ex.Message);
                        Console.Error.WriteLine(_ex.StackTrace);
                        Console.Error.WriteLine();
                    }
                    Console.Error.WriteLine(Separators.EndException);
                } catch {
                }
            }
        }