**** Merged r40732-r40872 from MCS ****
[mono.git] / mono / tests / remoting1.cs
1 using System;
2 using System.Threading;
3 using System.Runtime.InteropServices;
4 using System.Runtime.Remoting;
5 using System.Runtime.Remoting.Messaging;
6 using System.Runtime.Remoting.Proxies;
7
8 class MyProxy : RealProxy {
9         readonly MarshalByRefObject target;
10
11         public MyProxy (MarshalByRefObject target) : base (target.GetType())
12         {
13                 this.target = target;
14         }
15
16         public override IMessage Invoke (IMessage request) {
17                 IMethodCallMessage call = (IMethodCallMessage)request;
18                 Console.WriteLine ("Invoke " + call.MethodName);
19
20                 Console.Write ("ARGS(");
21                 for (int i = 0; i < call.ArgCount; i++) {
22                         if (i != 0)
23                                 Console.Write (", ");
24                         Console.Write (call.GetArgName (i) +  " " +
25                                        call.GetArg (i));
26                 }
27                 Console.WriteLine (")");
28                 Console.Write ("INARGS(");
29                 for (int i = 0; i < call.InArgCount; i++) {
30                         if (i != 0)
31                                 Console.Write (", ");
32                         Console.Write (call.GetInArgName (i) +  " " +
33                                        call.GetInArg (i));
34                 }
35                 Console.WriteLine (")");
36
37                 IMethodReturnMessage res = RemotingServices.ExecuteMessage (target, call);
38
39                 Console.Write ("RESARGS(");
40                 for (int i = 0; i < res.ArgCount; i++) {
41                         if (i != 0)
42                                 Console.Write (", ");
43                         Console.Write (res.GetArgName (i) +  " " +
44                                        res.GetArg (i));
45                 }
46                 Console.WriteLine (")");                
47                 
48                 Console.Write ("RESOUTARGS(");
49                 for (int i = 0; i < res.OutArgCount; i++) {
50                         if (i != 0)
51                                 Console.Write (", ");
52                         Console.Write (res.GetOutArgName (i) +  " " +
53                                        res.GetOutArg (i));
54                 }
55                 Console.WriteLine (")");                
56                 
57                 return res;
58         }
59 }
60
61 public class EmptyProxy : RealProxy
62 {
63         public EmptyProxy ( Type type ) : base( type ) 
64         { 
65         }
66
67         public override IMessage Invoke( IMessage msg )
68         {
69                 IMethodCallMessage call = (IMethodCallMessage)msg;
70
71                 return new ReturnMessage( null, null, 0, null, call );
72         }
73 }
74
75 public struct MyStruct {
76         public int a;
77         public int b;
78         public int c;
79 }
80
81 interface R2 {
82 }
83         
84 class R1 : MarshalByRefObject, R2 {
85
86         public int test_field = 5;
87         
88         public virtual MyStruct Add (int a, out int c, int b) {
89                 Console.WriteLine ("ADD");
90                 c = a + b;
91
92                 MyStruct res = new MyStruct ();
93
94                 res.a = a;
95                 res.b = b;
96                 res.c = c;
97                 
98                 return res;
99         }
100
101         public long nonvirtual_Add (int a, int b) {
102                 Console.WriteLine ("nonvirtual_Add " + a + " + " + b);
103                 return a + b;
104         }
105 }
106
107 class R3 : MarshalByRefObject {
108         public object anObject;
109 }
110
111 class Test {
112
113         delegate MyStruct RemoteDelegate1 (int a, out int c, int b);
114         delegate long RemoteDelegate2 (int a, int b);
115
116         static long test_call (R1 o)
117         {
118                 return o.nonvirtual_Add (2, 3);
119         }
120         
121         static int Main () {
122                 R1 myobj = new R1 ();
123                 int res = 0;
124                 long lres;
125                 
126                 MyProxy real_proxy = new MyProxy (myobj);
127
128                 R1 o = (R1)real_proxy.GetTransparentProxy ();
129
130                 if (RemotingServices.IsTransparentProxy (null))
131                         return 1;
132                 
133                 if (!RemotingServices.IsTransparentProxy (o))
134                         return 2;
135
136                 Console.WriteLine ("XXXXXXXXXXXX: " + RemotingServices.GetRealProxy (o));
137
138                 if (o.GetType () != myobj.GetType ())
139                         return 3;
140
141                 MyStruct myres = o.Add (2, out res, 3);
142
143                 Console.WriteLine ("Result: " + myres.a + " " +
144                                    myres.b + " " + myres.c +  " " + res);
145
146                 if (myres.a != 2)
147                         return 4;
148                 
149                 if (myres.b != 3)
150                         return 5;
151                 
152                 if (myres.c != 5)
153                         return 6;
154
155                 if (res != 5)
156                         return 7;
157
158                 R1 o2 = new R1 ();
159                 
160                 lres = test_call (o2);
161                 
162                 lres = test_call (o);
163
164                 Console.WriteLine ("Result: " + lres);
165                 if (lres != 5)
166                         return 8;
167                 
168                 lres = test_call (o);
169
170                 o.test_field = 2;
171                 
172                 Console.WriteLine ("test_field: " + o.test_field);
173                 if (o.test_field != 2)
174                         return 9;
175
176                 RemoteDelegate1 d1 = new RemoteDelegate1 (o.Add);
177                 MyStruct myres2 = d1 (2, out res, 3);
178
179                 Console.WriteLine ("Result: " + myres2.a + " " +
180                                    myres2.b + " " + myres2.c +  " " + res);
181
182                 if (myres2.a != 2)
183                         return 10;
184                 
185                 if (myres2.b != 3)
186                         return 11;
187                 
188                 if (myres2.c != 5)
189                         return 12;
190
191                 if (res != 5)
192                         return 13;
193
194                 RemoteDelegate2 d2 = new RemoteDelegate2 (o.nonvirtual_Add);
195                 d2 (6, 7);
196
197                 if (!(real_proxy.GetTransparentProxy () is R2))
198                         return 14;
199
200                 /* Test what happens if the proxy doesn't return the required information */
201                 EmptyProxy handler = new EmptyProxy ( typeof (R3) );
202                 R3 o3 = (R3)handler.GetTransparentProxy();
203
204                 if (o3.anObject != null)
205                         return 15;
206
207                 return 0;
208         }
209 }