/// <summary> /// Select the mode for this session, batch or interactive, /// based on the arguments. Don't be weirded out by this /// function calling TWAIN Local On Twain and then that function using /// Sword. This is a static function, it's just a convenience /// to use the Sword object to hold it, it could go anywhere /// and later on probably will (like as a function inside of /// the Program module). /// </summary> /// <returns>true for batch mode, false for interactive mode</returns> public static bool SelectMode() { int iPid = 0; string szIpc; string szTaskFile; string szImagesFolder; bool blTestPdfRaster; string szTestDnssd; // Check the arguments... string szWriteFolder = Config.Get("writeFolder", null); string szExecutableName = Config.Get("executableName", null); szTaskFile = Config.Get("task", null); blTestPdfRaster = (Config.Get("testpdfraster", null) != null); szTestDnssd = Config.Get("testdnssd", null); szIpc = Config.Get("ipc", null); szImagesFolder = Config.Get("images", null); if (string.IsNullOrEmpty(szImagesFolder)) { szImagesFolder = Path.Combine(szWriteFolder, "images"); } iPid = int.Parse(Config.Get("parentpid", "0")); // Run in IPC mode. The caller has set up a 'pipe' for us, so we'll use // that to send commands back and forth. This is the normal mode when // we're running with a scanner... if (szIpc != null) { // With Windows we need a window for the driver, but we can hide it... if (TWAIN.GetPlatform() == TWAIN.Platform.WINDOWS) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FormTwain(szWriteFolder, szImagesFolder, szIpc, iPid)); return(true); } // Linux and Mac OS X are okay without a window... else { TwainLocalOnTwain twainlocalontwain; // Create our object... twainlocalontwain = new TwainLocalOnTwain(szWriteFolder, szImagesFolder, szIpc, iPid, null, null, IntPtr.Zero); // Run our object... twainlocalontwain.Run(); // All done... return(true); } } // Handle the TWAIN list, we use this during registration to find out // what drivers we can use, and collect some info about them... string szTwainList = Config.Get("twainlist", null); if (szTwainList != null) { string szTwainListAction = Config.Get("twainlistaction", null); string szTwainListData = Config.Get("twainlistdata", null); if (szTwainList == "") { szTwainList = Path.Combine(Config.Get("writeFolder", ""), "twainlist.txt"); } System.IO.File.WriteAllText(szTwainList, ProcessSwordTask.TwainListDrivers(szTwainListAction, szTwainListData)); return(true); } /// Test DNS-SD... if (!string.IsNullOrEmpty(szTestDnssd)) { if (szTestDnssd == "monitor") { int ii; int jj; Dnssd dnssd; bool blServiceIsAvailable; Interpreter.CreateConsole(); List <Dnssd.DnssdDeviceInfo> ldnssddeviceinfo = new List <Dnssd.DnssdDeviceInfo>(); dnssd = new Dnssd(Dnssd.Reason.Monitor, out blServiceIsAvailable); if (blServiceIsAvailable) { dnssd.MonitorStart(null, IntPtr.Zero); for (ii = 0; ii < 60; ii++) { bool blUpdated = false; bool blNoMonitor = false; Thread.Sleep(1000); ldnssddeviceinfo = dnssd.GetSnapshot(ldnssddeviceinfo, out blUpdated, out blNoMonitor); if (blUpdated) { Console.Out.WriteLine(""); if ((ldnssddeviceinfo == null) || (ldnssddeviceinfo.Count == 0)) { Console.Out.WriteLine("***empty***"); } else { for (jj = 0; jj < ldnssddeviceinfo.Count; jj++) { Console.Out.WriteLine(ldnssddeviceinfo[jj].GetInterface() + " " + ldnssddeviceinfo[jj].GetServiceName()); } } } } dnssd.MonitorStop(); } dnssd.Dispose(); dnssd = null; } else if (szTestDnssd == "register") { Dnssd dnssd; bool blServiceIsAvailable; Interpreter.CreateConsole(); dnssd = new Dnssd(Dnssd.Reason.Register, out blServiceIsAvailable); if (blServiceIsAvailable) { dnssd.RegisterStart("Instance", 55556, "Ty", "", "", "", "Note"); Thread.Sleep(60000); dnssd.RegisterStop(); } dnssd.Dispose(); dnssd = null; } // All done... return(true); } // Otherwise let the user interact with us... TWAINWorkingGroup.Log.Info("Interactive mode..."); return(false); }
/// <summary> /// Launch the TWAIN Local Manager... /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void m_buttonManageTwainLocal_Click(object sender, EventArgs e) { bool blServiceIsAvailable; string szTwainLocalManager; Process process; bool blDevicesFound = m_buttonOpen.Enabled; // Get the path to the manager... szTwainLocalManager = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "TwainDirect.Scanner.TwainLocalManager.exe"); if (!File.Exists(szTwainLocalManager)) { MessageBox.Show("TWAIN Local Manager is not installed on this system.", "Error"); return; } // We're busy... Cursor.Current = Cursors.WaitCursor; SetButtons(ButtonState.Undefined); this.Refresh(); // We need to shutdown m_dnssd while we're in here... if (m_dnssd != null) { m_dnssd.Dispose(); m_dnssd = null; } // Launch it as admin... process = new Process(); process.StartInfo.FileName = szTwainLocalManager; process.StartInfo.UseShellExecute = true; if (System.Environment.OSVersion.Version.Major >= 6) { process.StartInfo.Verb = "runas"; } try { process.Start(); } catch { Log.Error("User chose not to run TwainLocalManager in elevated mode..."); SetButtons(blDevicesFound ? ButtonState.Devices : ButtonState.Nodevices); Cursor.Current = Cursors.Default; // Create the mdns monitor, and start it... m_dnssd = new Dnssd(Dnssd.Reason.Monitor, out blServiceIsAvailable); if (blServiceIsAvailable) { m_dnssd.MonitorStart(null, IntPtr.Zero); } else { m_dnssd.Dispose(); m_dnssd = null; } return; } // Wait for it to finish... Thread.Sleep(1000); try { process.WaitForInputIdle(); this.Refresh(); process.WaitForExit(); process.Dispose(); } catch (Exception exception) { Log.Error("Error waiting for TwainLocalManager - " + exception.Message); } SetButtons(blDevicesFound ? ButtonState.Devices : ButtonState.Nodevices); Cursor.Current = Cursors.Default; // Create the mdns monitor, and start it... m_dnssd = new Dnssd(Dnssd.Reason.Monitor, out blServiceIsAvailable); if (blServiceIsAvailable) { m_dnssd.MonitorStart(null, IntPtr.Zero); } else { m_dnssd.Dispose(); m_dnssd = null; } }
/// <summary> /// Start the HTTP server and register us in mDNS / DNS-SD... /// </summary> /// <param name="a_dispatchcommand">our callback</param> /// <param name="a_szInstanceName">the instance name: xxx._twaindirect._sub._privet._tcp.local</param> /// <param name="a_iPort">socket port number (can be 0 to auto select)</param> /// <param name="a_szTy">the friendly name for the device, not forced to be unique</param> /// <param name="a_szUrl">url of cloud server or empty string</param> /// <param name="a_szCloudScannerId">device id guid for our scanner</param> /// <param name="a_szTwainCloudState">the cloud state</param> /// <param name="a_szNote">a helpful note about the device (optional)</param> /// <returns>true on success</returns> public bool ServerStart ( DispatchCommand a_dispatchcommand, string a_szInstanceName, int a_iPort, string a_szTy, string a_szUrl, string a_szCloudScannerId, string a_szTwainCloudState, string a_szNote, out bool a_blServiceIsAvailable ) { string szUri; // Sanity check... m_dnssd = new Dnssd(Dnssd.Reason.Register, out a_blServiceIsAvailable); if (!a_blServiceIsAvailable) { m_dnssd.Dispose(); m_dnssd = null; return(false); } // Make a note of our callback... m_dispatchcommand = a_dispatchcommand; // Create the listener... m_httplistener = new HttpListener(); // HTTPS support for mono, still have to sort out Windows // http://stackoverflow.com/questions/13379963/httplistener-with-https-on-monotouch // Find a port we can use... m_iPort = a_iPort; if (m_iPort == 0) { TcpListener tcplistener = new TcpListener(IPAddress.Any, 0); tcplistener.Start(); m_iPort = ((IPEndPoint)tcplistener.LocalEndpoint).Port; tcplistener.Stop(); } // Add our prefixes, we'll accept input from any address on this port // which is how the advertisement should work. We won't register // until the service is up. Note that our default is to require the // use of HTTPS... if (Config.Get("useHttps", "yes") == "yes") { szUri = @"https://+:" + m_iPort + "/privet/info/"; Log.Info("Monitoring: " + szUri); m_httplistener.Prefixes.Add(szUri); szUri = @"https://+:" + m_iPort + "/privet/infoex/"; Log.Info("Monitoring: " + szUri); m_httplistener.Prefixes.Add(szUri); szUri = @"https://+:" + m_iPort + "/privet/twaindirect/session/"; Log.Info("Monitoring: " + szUri); m_httplistener.Prefixes.Add(szUri); } else { szUri = @"http://+:" + m_iPort + "/privet/info/"; Log.Info("Monitoring: " + szUri); m_httplistener.Prefixes.Add(szUri); szUri = @"http://+:" + m_iPort + "/privet/infoex/"; Log.Info("Monitoring: " + szUri); m_httplistener.Prefixes.Add(szUri); szUri = @"http://+:" + m_iPort + "/privet/twaindirect/session/"; Log.Info("Monitoring: " + szUri); m_httplistener.Prefixes.Add(szUri); } // Start the service... try { m_httplistener.Start(); } catch (Exception exception) { Log.Error("ServerStart: Start failed..." + exception.Message); return(false); } // Handle stuff async... m_iasyncresult = m_httplistener.BeginGetContext(new AsyncCallback(ListenerCallback), m_httplistener); // Register our new device... m_dnssd.RegisterStart(a_szInstanceName, m_iPort, a_szTy, a_szUrl, a_szCloudScannerId, a_szTwainCloudState, a_szNote); // All done... return(true); }
/// <summary> /// Select the mode for this session, batch or interactive, /// based on the arguments. Don't be weirded out by this /// function calling TWAIN Local On Twain and then that function using /// Sword. This is a static function, it's just a convenience /// to use the Sword object to hold it, it could go anywhere /// and later on probably will (like as a function inside of /// the Program module). /// </summary> /// <returns>true for batch mode, false for interactive mode</returns> public static bool SelectMode() { int iPid = 0; string szIpc; string szTask; string szTaskFile; string szImagesFolder; bool blTestPdfRaster; string szTestDnssd; bool blSuccess; // Check the arguments... string szWriteFolder = Config.Get("writeFolder", null); string szExecutableName = Config.Get("executableName", null); szTaskFile = Config.Get("task", null); blTestPdfRaster = (Config.Get("testpdfraster", null) != null); szTestDnssd = Config.Get("testdnssd", null); szIpc = Config.Get("ipc", null); szImagesFolder = Config.Get("images", null); if (string.IsNullOrEmpty(szImagesFolder)) { szImagesFolder = Path.Combine(szWriteFolder, "images"); } iPid = int.Parse(Config.Get("parentpid", "0")); // Test ProcessSwordTask... if (!string.IsNullOrEmpty(Config.Get("testtask", null))) { // Create our object... ProcessSwordTask processswordtask = new ProcessSwordTask(szImagesFolder, null, null); // Did we get a valid filename? if ((szTaskFile == null) || !File.Exists(szTaskFile)) { Console.Out.WriteLine(""); Console.Out.WriteLine("Please provide a valid task=file argument..."); return(false); } // Load the file... szTask = File.ReadAllText(szTaskFile); // Handle certification files... string[] aszTask = szTask.Split(new string[] { "***DATADATADATA***" }, StringSplitOptions.RemoveEmptyEntries); if ((aszTask == null) || (aszTask.Length == 0)) { Console.Out.WriteLine(""); Console.Out.WriteLine("Please provide a task file with data..."); return(false); } // Get our task data... if (aszTask.Length == 1) { szTask = aszTask[0]; } else { szTask = aszTask[1]; } // Run a test... bool blSetAppCapabilities = true; blSuccess = processswordtask.BatchMode(null, szTask, true, ref blSetAppCapabilities); //blSuccess = processswordtask.Deserialize(szTask, "211a1e90-11e1-11e5-9493-1697f925ec7b"); if (blSuccess) { //blSuccess = processswordtask.ProcessAndRun(); } // All done... return(true); } // Run in IPC mode. The caller has set up a 'pipe' for us, so we'll use // that to send commands back and forth. This is the normal mode when // we're running with a scanner... if (szIpc != null) { // With Windows we need a window for the driver, but we can hide it... if (TWAINCSToolkit.GetPlatform() == "WINDOWS") { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FormTwain(szWriteFolder, szImagesFolder, szIpc, iPid, RunInUiThread)); return(true); } // Linux and Mac OS X are okay without a window... else { TwainLocalOnTwain twainlocalontwain; // Create our object... twainlocalontwain = new TwainLocalOnTwain(szWriteFolder, szImagesFolder, szIpc, iPid, null, null, IntPtr.Zero); // Run our object... twainlocalontwain.Run(); // All done... return(true); } } // Handle the TWAIN list, we use this during registration to find out // what drivers we can use, and collect some info about them... string szTwainList = Config.Get("twainlist", null); if (szTwainList != null) { if (szTwainList == "") { szTwainList = Path.Combine(Config.Get("writeFolder", ""), "twainlist.txt"); } System.IO.File.WriteAllText(szTwainList, ProcessSwordTask.TwainListDrivers()); return(true); } /// Test DNS-SD... if (!string.IsNullOrEmpty(szTestDnssd)) { if (szTestDnssd == "monitor") { int ii; int jj; Dnssd dnssd; Interpreter.CreateConsole(); Dnssd.DnssdDeviceInfo[] adnssddeviceinfo = null; dnssd = new Dnssd(Dnssd.Reason.Monitor); dnssd.MonitorStart(null, IntPtr.Zero); for (ii = 0; ii < 60; ii++) { bool blUpdated = false; Thread.Sleep(1000); adnssddeviceinfo = dnssd.GetSnapshot(adnssddeviceinfo, out blUpdated); if (blUpdated) { Console.Out.WriteLine(""); if ((adnssddeviceinfo == null) || (adnssddeviceinfo.Length == 0)) { Console.Out.WriteLine("***empty***"); } else { for (jj = 0; jj < adnssddeviceinfo.Length; jj++) { Console.Out.WriteLine(adnssddeviceinfo[jj].GetInterface() + " " + adnssddeviceinfo[jj].GetServiceName()); } } } } dnssd.MonitorStop(); dnssd.Dispose(); } else if (szTestDnssd == "register") { Dnssd dnssd; Interpreter.CreateConsole(); dnssd = new Dnssd(Dnssd.Reason.Register); dnssd.RegisterStart("Instance", 55556, "Ty", "", "Note"); Thread.Sleep(60000); dnssd.RegisterStop(); dnssd.Dispose(); } // All done... return(true); } // Execute the specified task... if (File.Exists(szTaskFile)) { bool blSetAppCapabilities = false; ProcessSwordTask processswordtask; // Init stuff... processswordtask = new ProcessSwordTask(szImagesFolder, null, null); // Run our task... processswordtask.BatchMode(Config.Get("scanner", null), szTaskFile, false, ref blSetAppCapabilities); // All done... return(true); } // Otherwise let the user interact with us... TWAINWorkingGroup.Log.Info("Interactive mode..."); return(false); }