コード例 #1
0
ファイル: Transport.cs プロジェクト: genesi/dbus-sharp
        public static Transport Create(AddressEntry entry)
        {
            switch (entry.Method)
            {
            case "tcp":
            {
                Transport transport = new SocketTransport();
                transport.Open(entry);
                return(transport);
            }

#if !PORTABLE
            case "unix":
            {
                Transport transport = new UnixNativeTransport();
                transport.Open(entry);
                return(transport);
            }
#endif
#if ENABLE_PIPES
            case "win": {
                Transport transport = new PipeTransport();
                transport.Open(entry);
                return(transport);
            }
#endif
            default:
                throw new NotSupportedException("Transport method \"" + entry.Method + "\" not supported");
            }
        }
コード例 #2
0
ファイル: Transport.cs プロジェクト: hyperair/dbus-sharp
 public static Transport Create(AddressEntry entry)
 {
     switch (entry.Method) {
         case "tcp":
         {
             Transport transport = new SocketTransport ();
             transport.Open (entry);
             return transport;
         }
     #if !PORTABLE
         case "unix":
         {
             Transport transport = new UnixNativeTransport ();
             transport.Open (entry);
             return transport;
         }
     #endif
     #if ENABLE_PIPES
         case "win": {
             Transport transport = new PipeTransport ();
             transport.Open (entry);
             return transport;
         }
     #endif
         default:
             throw new NotSupportedException ("Transport method \"" + entry.Method + "\" not supported");
     }
 }
コード例 #3
0
	//TODO: complete this test daemon/server example, and a client
	//TODO: maybe generalise it and integrate it into the core
	public static void Main (string[] args)
	{
		bool isServer;

		if (args.Length == 1 && args[0] == "server")
			isServer = true;
		else if (args.Length == 1 && args[0] == "client")
			isServer = false;
		else {
			Console.Error.WriteLine ("Usage: test-server [server|client]");
			return;
		}

		//string addr = "unix:abstract=/tmp/dbus-ABCDEFGHIJ";
		string addr = "unix:path=/tmp/dbus-ABCDEFGHIJ";

		Connection conn;

		ObjectPath myOpath = new ObjectPath ("/org/ndesk/test");
		string myNameReq = "org.ndesk.test";

		if (!isServer) {
			conn = new Connection (Transport.Create (AddressEntry.Parse (addr)));
			DemoObject demo = conn.GetObject<DemoObject> (myNameReq, myOpath);
			demo.GiveNoReply ();
			//float ret = demo.Hello ("hi from test client", 21);
			float ret = 200;
			while (ret > 5) {
				ret = demo.Hello ("hi from test client", (int)ret);
				Console.WriteLine ("Returned float: " + ret);
				System.Threading.Thread.Sleep (1000);
			}
		} else {
			string path;
			bool abstr;

			AddressEntry entry = AddressEntry.Parse (addr);
			path = entry.Properties["path"];

			UnixSocket server = new UnixSocket ();


			byte[] p = Encoding.Default.GetBytes (path);

			byte[] sa = new byte[2 + p.Length + 1];

			//we use BitConverter to stay endian-safe
			byte[] afData = BitConverter.GetBytes (UnixSocket.AF_UNIX);
			sa[0] = afData[0];
			sa[1] = afData[1];

			for (int i = 0 ; i != p.Length ; i++)
				sa[2 + i] = p[i];
			sa[2 + p.Length] = 0; //null suffix for domain socket addresses, see unix(7)


			server.Bind (sa);
			//server.Listen (1);
			server.Listen (5);

			while (true) {
				Console.WriteLine ("Waiting for client on " + addr);
				UnixSocket client = server.Accept ();
				Console.WriteLine ("Client accepted");
				//client.Blocking = true;

				//PeerCred pc = new PeerCred (client);
				//Console.WriteLine ("PeerCred: pid={0}, uid={1}, gid={2}", pc.ProcessID, pc.UserID, pc.GroupID);

				UnixNativeTransport transport = new UnixNativeTransport ();
				transport.Stream = new UnixStream (client.Handle);
				conn = new Connection (transport);

				//ConnectionHandler.Handle (conn);

				//in reality a thread per connection is of course too expensive
				ConnectionHandler hnd = new ConnectionHandler (conn);
				new Thread (new ThreadStart (hnd.Handle)).Start ();

				Console.WriteLine ();
			}
		}
	}
コード例 #4
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");
        }
コード例 #5
0
ファイル: Transport.cs プロジェクト: acklinr/dbus-sharp
        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 = 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");
        }
コード例 #6
0
ファイル: Transport.cs プロジェクト: brookpatten/dbus-sharp
        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 = 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");
        }