This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
/
TestServer.cs
137 lines (109 loc) · 3.2 KB
/
TestServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2006 Alp Toker <alp@atoker.com>
// This software is made available under the MIT License
// See COPYING for details
using System;
using DBus;
using org.freedesktop.DBus;
using System.IO;
using System.Net;
using System.Net.Sockets;
using Mono.Unix;
using System.Threading;
public class TestServer
{
//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";
Connection conn;
ObjectPath myOpath = new ObjectPath ("/org/ndesk/test");
string myNameReq = "org.ndesk.test";
if (!isServer) {
conn = new Connection (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;
Address.Parse (addr, out path, out abstr);
AbstractUnixEndPoint ep = new AbstractUnixEndPoint (path);
Socket server = new Socket (AddressFamily.Unix, SocketType.Stream, 0);
server.Bind (ep);
//server.Listen (1);
server.Listen (5);
while (true) {
Console.WriteLine ("Waiting for client on " + addr);
Socket 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);
conn = new Connection (null);
conn.ns = new NetworkStream (client);
conn.SocketHandle = (long)client.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 ();
}
}
}
}
public class ConnectionHandler
{
protected Connection conn;
public ConnectionHandler (Connection conn)
{
this.conn = conn;
}
public void Handle ()
{
ConnectionHandler.Handle (conn);
}
public static void Handle (Connection conn)
{
string myNameReq = "org.ndesk.test";
ObjectPath myOpath = new ObjectPath ("/org/ndesk/test");
DemoObject demo = new DemoObject ();
conn.Register (myNameReq, myOpath, demo);
//TODO: handle lost connections etc. properly instead of stupido try/catch
try {
while (true)
conn.Iterate ();
} catch (Exception e) {
//Console.Error.WriteLine (e);
}
conn.Unregister (myNameReq, myOpath);
}
}
[Interface ("org.ndesk.test")]
public class DemoObject : MarshalByRefObject
{
public float Hello (string arg0, int arg1)
{
Console.WriteLine ("Got a Hello(" + arg0 + ", " + arg1 +")");
return (float)arg1/2;
}
public void GiveNoReply ()
{
Console.WriteLine ("Asked to give no reply");
}
}