Merge pull request #5714 from alexischr/update_bockbuild
[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         public object null_test_field;
88         
89         public virtual MyStruct Add (int a, out int c, int b) {
90                 Console.WriteLine ("ADD");
91                 c = a + b;
92
93                 MyStruct res = new MyStruct ();
94
95                 res.a = a;
96                 res.b = b;
97                 res.c = c;
98                 
99                 return res;
100         }
101
102         public long nonvirtual_Add (int a, int b) {
103                 Console.WriteLine ("nonvirtual_Add " + a + " + " + b);
104                 return a + b;
105         }
106 }
107
108 class R3 : MarshalByRefObject {
109         public object anObject;
110 }
111
112 class Test {
113
114         delegate MyStruct RemoteDelegate1 (int a, out int c, int b);
115         delegate long RemoteDelegate2 (int a, int b);
116
117         static long test_call (R1 o)
118         {
119                 return o.nonvirtual_Add (2, 3);
120         }
121         
122         static int Main () {
123                 R1 myobj = new R1 ();
124                 int res = 0;
125                 long lres;
126                 
127                 MyProxy real_proxy = new MyProxy (myobj);
128
129                 R1 o = (R1)real_proxy.GetTransparentProxy ();
130
131                 if (RemotingServices.IsTransparentProxy (null))
132                         return 1;
133                 
134                 if (!RemotingServices.IsTransparentProxy (o))
135                         return 2;
136
137                 Console.WriteLine ("XXXXXXXXXXXX: " + RemotingServices.GetRealProxy (o));
138
139                 if (o.GetType () != myobj.GetType ())
140                         return 3;
141
142                 MyStruct myres = o.Add (2, out res, 3);
143
144                 Console.WriteLine ("Result: " + myres.a + " " +
145                                    myres.b + " " + myres.c +  " " + res);
146
147                 if (myres.a != 2)
148                         return 4;
149                 
150                 if (myres.b != 3)
151                         return 5;
152                 
153                 if (myres.c != 5)
154                         return 6;
155
156                 if (res != 5)
157                         return 7;
158
159                 R1 o2 = new R1 ();
160                 
161                 lres = test_call (o2);
162                 
163                 lres = test_call (o);
164
165                 Console.WriteLine ("Result: " + lres);
166                 if (lres != 5)
167                         return 8;
168                 
169                 lres = test_call (o);
170
171                 o.test_field = 2;
172                 
173                 Console.WriteLine ("test_field: " + o.test_field);
174                 if (o.test_field != 2)
175                         return 9;
176
177                 RemoteDelegate1 d1 = new RemoteDelegate1 (o.Add);
178                 MyStruct myres2 = d1 (2, out res, 3);
179
180                 Console.WriteLine ("Result: " + myres2.a + " " +
181                                    myres2.b + " " + myres2.c +  " " + res);
182
183                 if (myres2.a != 2)
184                         return 10;
185                 
186                 if (myres2.b != 3)
187                         return 11;
188                 
189                 if (myres2.c != 5)
190                         return 12;
191
192                 if (res != 5)
193                         return 13;
194
195                 RemoteDelegate2 d2 = new RemoteDelegate2 (o.nonvirtual_Add);
196                 d2 (6, 7);
197
198                 if (!(real_proxy.GetTransparentProxy () is R2))
199                         return 14;
200
201                 /* Test what happens if the proxy doesn't return the required information */
202                 EmptyProxy handler = new EmptyProxy ( typeof (R3) );
203                 R3 o3 = (R3)handler.GetTransparentProxy();
204
205                 if (o3.anObject != null)
206                         return 15;
207
208                 if (o.null_test_field != null)
209                         return 16;
210
211                 return 0;
212         }
213 }