예제 #1
0
        public static Transport Create(AddressEntry entry)
        {
            switch (entry.Method)
            {
            case "tcp":
            {
                Transport transport = new SocketTransport();
                transport.Open(entry);
                return(transport);
            }

            case "unix":
            {
                if (OSHelpers.PlatformIsUnixoid)
                {
                    Transport transport;
#if MONO_46
                    if (UnixSendmsgTransport.Available())
                    {
                        transport = new UnixSendmsgTransport();
                    }
                    else
                    {
                        if (ProtocolInformation.Verbose)
                        {
                            Console.Error.WriteLine("Warning: Syscall.sendmsg() not available, transfering unix FDs will not work");
                        }
#endif
                    transport = new UnixNativeTransport();
#if MONO_46
                }
#endif
                    transport.Open(entry);
                    return(transport);
                }
                break;
            }

#if ENABLE_PIPES
            case "win":
            {
                Transport transport = new PipeTransport();
                transport.Open(entry);
                return(transport);
            }
#endif

            case "launchd":
            {
                string env = entry.Properties["env"];
                var    psi = new ProcessStartInfo();
                psi.FileName               = "launchctl";
                psi.Arguments              = "getenv '" + env.Replace("'", "'\\''") + "'";
                psi.UseShellExecute        = false;
                psi.RedirectStandardOutput = true;
                var process = Process.Start(psi);
                var output  = process.StandardOutput.ReadToEnd();
                process.WaitForExit();
                if (process.ExitCode != 0)
                {
                    throw new Exception("Calling " + psi.FileName + " " + psi.Arguments + " failed with error code " + process.ExitCode);
                }
                var path   = output.Trim();
                var entry2 = new AddressEntry();
                entry2.Method             = "unix";
                entry2.Properties["path"] = path;
                return(Create(entry2));
            }

            // "autolaunch:" means: the first client user of the dbus library shall spawn the daemon on itself, see dbus 1.7.8 from http://dbus.freedesktop.org/releases/dbus/
            case "autolaunch":
            {
                if (OSHelpers.PlatformIsUnixoid)
                {
                    break;
                }

                string addr = Address.GetSessionBusAddressFromSharedMemory();

                if (string.IsNullOrEmpty(addr))                            // we have to launch the daemon ourselves
                {
                    string oldDir = Directory.GetCurrentDirectory();
                    // Without this, the "current" folder for the new process will be the one where the current
                    // executable resides, and as a consequence,that folder cannot be relocated/deleted unless the daemon is stopped
                    Directory.SetCurrentDirectory(Environment.GetFolderPath(Environment.SpecialFolder.System));

                    Process process = Process.Start(DBUS_DAEMON_LAUNCH_COMMAND);
                    if (process == null)
                    {
                        Directory.SetCurrentDirectory(oldDir);
                        throw new NotSupportedException("Transport method \"autolaunch:\" - cannot launch dbus daemon '" + DBUS_DAEMON_LAUNCH_COMMAND + "'");
                    }

                    // wait for daemon
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    do
                    {
                        addr = Address.GetSessionBusAddressFromSharedMemory();
                        if (String.IsNullOrEmpty(addr))
                        {
                            Thread.Sleep(100);
                        }
                    } while (String.IsNullOrEmpty(addr) && stopwatch.ElapsedMilliseconds <= 5000);

                    Directory.SetCurrentDirectory(oldDir);
                }

                if (string.IsNullOrEmpty(addr))
                {
                    throw new NotSupportedException("Transport method \"autolaunch:\" - timeout during access to freshly launched dbus daemon");
                }
                return(Create(AddressEntry.Parse(addr)));
            }
            }

            throw new NotSupportedException("Transport method \"" + entry.Method + "\" not supported");
        }
예제 #2
0
        public static Transport Create(AddressEntry entry)
        {
            switch (entry.Method)
            {
            case "tcp":
            {
                Transport transport = new SocketTransport();
                transport.Open(entry);
                return(transport);
            }

            case "unix":
            {
                if (OSHelpers.PlatformIsUnixoid)
                {
                    Transport transport;
                    if (UnixSendmsgTransport.Available())
                    {
                        transport = new UnixSendmsgTransport();
                    }
                    else
                    {
                        if (ProtocolInformation.Verbose)
                        {
                            Console.Error.WriteLine("Warning: Syscall.sendmsg() not available, transfering unix FDs will not work");
                        }
                        transport = new UnixNativeTransport();
                    }
                    transport.Open(entry);
                    return(transport);
                }
                break;
            }

#if ENABLE_PIPES
            case "win":
            {
                Transport transport = new PipeTransport();
                transport.Open(entry);
                return(transport);
            }
#endif

            // "autolaunch:" means: the first client user of the dbus library shall spawn the daemon on itself, see dbus 1.7.8 from http://dbus.freedesktop.org/releases/dbus/
            case "autolaunch":
            {
                if (OSHelpers.PlatformIsUnixoid)
                {
                    break;
                }

                string addr = Address.GetSessionBusAddressFromSharedMemory();

                if (string.IsNullOrEmpty(addr))                            // we have to launch the daemon ourselves
                {
                    string oldDir = Directory.GetCurrentDirectory();
                    // Without this, the "current" folder for the new process will be the one where the current
                    // executable resides, and as a consequence,that folder cannot be relocated/deleted unless the daemon is stopped
                    Directory.SetCurrentDirectory(Environment.GetFolderPath(Environment.SpecialFolder.System));

                    Process process = Process.Start(DBUS_DAEMON_LAUNCH_COMMAND);
                    if (process == null)
                    {
                        Directory.SetCurrentDirectory(oldDir);
                        throw new NotSupportedException("Transport method \"autolaunch:\" - cannot launch dbus daemon '" + DBUS_DAEMON_LAUNCH_COMMAND + "'");
                    }

                    // wait for daemon
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    do
                    {
                        addr = Address.GetSessionBusAddressFromSharedMemory();
                        if (String.IsNullOrEmpty(addr))
                        {
                            Thread.Sleep(100);
                        }
                    } while (String.IsNullOrEmpty(addr) && stopwatch.ElapsedMilliseconds <= 5000);

                    Directory.SetCurrentDirectory(oldDir);
                }

                if (string.IsNullOrEmpty(addr))
                {
                    throw new NotSupportedException("Transport method \"autolaunch:\" - timeout during access to freshly launched dbus daemon");
                }
                return(Create(AddressEntry.Parse(addr)));
            }
            }

            throw new NotSupportedException("Transport method \"" + entry.Method + "\" not supported");
        }