Esempio n. 1
0
 public object Execute(TwainCommand command)
 {
     if (this.IsValid)
     {
         this._proc.StandardInput.WriteLine(Separators.BeginRequest);
         this._proc.StandardInput.WriteLine(Convert.ToBase64String(command.ToArray()));
         for (string _response = null; (this.IsValid = !string.IsNullOrEmpty(_response = this._proc.StandardOutput.ReadLine())) && _response != Separators.BeginResponse;)
         {
             if (_response == Separators.BeginEvent)
             {
                 this._OnEvent();
                 continue;
             }
         }
         for (string _data = null; this.IsValid && (this.IsValid = !string.IsNullOrEmpty(_data = this._proc.StandardOutput.ReadLine()));)
         {
             try {
                 return(TwainCommand.FromArray(Convert.FromBase64String(_data)).Result);
             } catch (Exception ex) {
                 return(ex);
             } finally {
                 for (string _response = null; this.IsValid && (this.IsValid = !string.IsNullOrEmpty(_response = this._proc.StandardOutput.ReadLine())) && _response != Separators.EndResponse;)
                 {
                 }
             }
         }
         return(this.GetException() ?? new InvalidOperationException());
     }
     throw this.GetException() ?? new InvalidOperationException();
 }
Esempio n. 2
0
        public static byte[] Execute(Twain32 twain32, byte[] command)
        {
            var _command = TwainCommand.FromArray(command);

            new TwainProxy {
                Twain32 = twain32
            }._ExecuteCore(_command);
            return(_command.ToArray());
        }
Esempio n. 3
0
 private void _ExecuteCore(TwainCommand command)
 {
     try {
         for (var _method = command as MethodTwainCommand; _method != null;)
         {
             command.Result = ((MethodInfo)_method.Member).Invoke(this[_method.Member.DeclaringType], _method.Parameters);
             return;
         }
     } catch (Exception ex) {
         command.Result = ex;
     }
 }
Esempio n. 4
0
 private void _OnEvent()
 {
     for (string _event = null; this.IsValid && (this.IsValid = !string.IsNullOrEmpty(_event = this._proc.StandardOutput.ReadLine()));)
     {
         var _res = TwainCommand.FromArray(Convert.FromBase64String(_event)) as EventHandlerTwainCommand;
         if (_res != null && this.FireEvent != null)
         {
             this.FireEvent(_res);
         }
         this._proc.StandardInput.WriteLine(Convert.ToBase64String(_res.ToArray()));
         break;
     }
     for (string _response = null; this.IsValid && (this.IsValid = !string.IsNullOrEmpty(_response = this._proc.StandardOutput.ReadLine())) && _response != Separators.EndEvent;)
     {
     }
 }
Esempio n. 5
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;
        }
Esempio n. 6
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 {
                }
            }
        }