Example #1
0
        public override void Listen()
        {
            byte[]     sa    = isAbstract ? UnixNativeTransport.GetSockAddrAbstract(unixPath) : UnixNativeTransport.GetSockAddr(unixPath);
            UnixSocket usock = new UnixSocket();

            usock.Bind(sa);
            usock.Listen(50);

            while (true)
            {
                Console.WriteLine("Waiting for client on " + (isAbstract ? "abstract " : String.Empty) + "path " + unixPath);
                UnixSocket csock = usock.Accept();
                Console.WriteLine("Client connected");

                ServerConnection conn;
                if (!AcceptClient(csock, out conn))
                {
                    Console.WriteLine("Client rejected");
                    csock.Close();
                    continue;
                }

                //GLib.Idle.Add (delegate {

                if (NewConnection != null)
                {
                    NewConnection(conn);
                }

                //BusG.Init (conn);

                /*
                 * conn.Iterate ();
                 * Console.WriteLine ("done iter");
                 * BusG.Init (conn);
                 * Console.WriteLine ("done init");
                 */

                //GLib.Idle.Add (delegate { BusG.Init (conn); return false; });
        #if USE_GLIB
                BusG.Init(conn);
        #else
                new Thread(new ThreadStart(delegate { while (conn.IsConnected)
                                                      {
                                                          conn.Iterate();
                                                      }
                                           })).Start();
        #endif
                Console.WriteLine("done init");


                //return false;
                //});
            }
        }
Example #2
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();
            }
        }
    }
Example #3
0
        public override void Listen()
        {
            byte[] sa = isAbstract ? UnixNativeTransport.GetSockAddrAbstract(unixPath) : UnixNativeTransport.GetSockAddr(unixPath);
            UnixSocket usock = new UnixSocket();
            usock.Bind(sa);
            usock.Listen(50);

            while (true)
            {
                Console.WriteLine("Waiting for client on " + (isAbstract ? "abstract " : String.Empty) + "path " + unixPath);
                UnixSocket csock = usock.Accept();
                Console.WriteLine("Client connected");

                ServerConnection conn;
                if (!AcceptClient(csock, out conn))
                {
                    Console.WriteLine("Client rejected");
                    csock.Close();
                    continue;
                }

                //GLib.Idle.Add (delegate {

                if (NewConnection != null)
                    NewConnection(conn);

                //BusG.Init (conn);
                /*
                conn.Iterate ();
                Console.WriteLine ("done iter");
                BusG.Init (conn);
                Console.WriteLine ("done init");
                */

                //GLib.Idle.Add (delegate { BusG.Init (conn); return false; });
            #if USE_GLIB
                BusG.Init (conn);
            #else
                new Thread(new ThreadStart(delegate { while (conn.IsConnected) conn.Iterate(); })).Start();
            #endif
                Console.WriteLine("done init");

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