Exemplo n.º 1
0
        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");
            }
        }
Exemplo n.º 2
0
 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");
     }
 }
Exemplo n.º 3
0
        public void GetData()
        {
            if (msgRdr == null)
            {
                msgRdr = ReadMessageReal2();
            }

            SocketTransport st = this as SocketTransport;

            int avail = st.socket.Available;

            if (mmneeded == 0)
            {
                throw new Exception();
            }

            if (avail == 0)
            {
                return;
            }

            avail = Math.Min(avail, mmneeded);
            int nread = st.socket.Receive(mmbuf, mmpos, avail, System.Net.Sockets.SocketFlags.None);

            mmpos    += nread;
            mmneeded -= nread;
            if (!msgRdr.MoveNext())
            {
                throw new Exception();
            }

            MsgState state = msgRdr.Current;

            if (state != MsgState.Done)
            {
                return;
            }

            mmpos    = 0;
            mmneeded = 16;

            msgRdr = null;
        }
Exemplo n.º 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");
        }
Exemplo n.º 5
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 = 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");
        }
Exemplo n.º 6
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 = 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");
        }
Exemplo n.º 7
0
	public static void Main (string[] args)
	{
		bool isServer;

		int port;
		string hostname = "127.0.0.1";
		//IPAddress ipaddr = IPAddress.Parse ("127.0.0.1");

		if (args.Length == 2 && args[0] == "server") {
			isServer = true;
			port = Int32.Parse (args[1]);
		} else if (args.Length == 3 && args[0] == "client") {
			isServer = false;
			hostname = args[1];
			port = Int32.Parse (args[2]);
		} else {
			Console.Error.WriteLine ("Usage: test-server-tcp [server PORT|client HOSTNAME PORT]");
			return;
		}

		Connection conn;

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

		if (!isServer) {
			SocketTransport transport = new SocketTransport ();
			transport.Open (hostname, port);
			conn = new Connection (transport);

			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 {
			TcpListener server = new TcpListener (IPAddress.Any, port);

			server.Start ();

			while (true) {
				Console.WriteLine ("Waiting for client on " + port);
				TcpClient client = server.AcceptTcpClient ();
				Console.WriteLine ("Client accepted");

				//TODO: use the right abstraction here, probably using the Server class
				SocketTransport transport = new SocketTransport ();
				transport.Stream = client.GetStream ();
				conn = new Connection (transport);

				//conn.SocketHandle = (long)clientSocket.Handle;

				//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 ();
			}
		}
	}