2002-04-29 Dietmar Maurer <dietmar@ximian.com>
[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 struct MyStruct {
62         public int a;
63         public int b;
64         public int c;
65 }
66         
67 class R1 : MarshalByRefObject {
68
69         public int test_field = 5;
70         
71         public virtual MyStruct Add (int a, out int c, int b) {
72                 Console.WriteLine ("ADD");
73                 c = a + b;
74
75                 MyStruct res = new MyStruct ();
76
77                 res.a = a;
78                 res.b = b;
79                 res.c = c;
80                 
81                 return res;
82         }
83
84         public long nonvirtual_Add (int a, int b) {
85                 Console.WriteLine ("nonvirtual_Add");
86                 return a + b;
87         }
88 }
89
90 class Test {
91
92         delegate MyStruct RemoteDelegate1 (int a, out int c, int b);
93         delegate long RemoteDelegate2 (int a, int b);
94
95         static long test_call (R1 o)
96         {
97                 return o.nonvirtual_Add (2, 3);
98         }
99         
100         static int Main () {
101                 R1 myobj = new R1 ();
102                 int res = 0;
103                 long lres;
104                 
105                 MyProxy real_proxy = new MyProxy (myobj);
106
107                 R1 o = (R1)real_proxy.GetTransparentProxy ();
108
109                 if (RemotingServices.IsTransparentProxy (null))
110                         return 1;
111                 
112                 if (!RemotingServices.IsTransparentProxy (o))
113                         return 1;
114
115                 if (o.GetType () != myobj.GetType ())
116                         return 1;
117                 
118                 MyStruct myres = o.Add (2, out res, 3);
119
120                 Console.WriteLine ("Result: " + myres.a + " " +
121                                    myres.b + " " + myres.c +  " " + res);
122
123                 if (myres.a != 2)
124                         return 1;
125                 
126                 if (myres.b != 3)
127                         return 1;
128                 
129                 if (myres.c != 5)
130                         return 1;
131
132                 if (res != 5)
133                         return 1;
134
135                 R1 o2 = new R1 ();
136                 
137                 lres = test_call (o2);
138                 
139                 lres = test_call (o);
140
141                 Console.WriteLine ("Result: " + lres);
142                 if (lres != 5)
143                         return 1;
144                 
145                 lres = test_call (o);
146
147                 o.test_field = 2;
148                 
149                 Console.WriteLine ("test_field: " + o.test_field);
150                 if (o.test_field != 2)
151                         return 1;
152
153                 RemoteDelegate1 d1 = new RemoteDelegate1 (o.Add);
154                 MyStruct myres2 = d1 (2, out res, 3);
155
156                 Console.WriteLine ("Result: " + myres2.a + " " +
157                                    myres2.b + " " + myres2.c +  " " + res);
158
159                 if (myres2.a != 2)
160                         return 1;
161                 
162                 if (myres2.b != 3)
163                         return 1;
164                 
165                 if (myres2.c != 5)
166                         return 1;
167
168                 if (res != 5)
169                         return 1;
170
171                 RemoteDelegate2 d2 = new RemoteDelegate2 (o.nonvirtual_Add);
172                 d2 (6, 7);
173
174                 return 0;
175         }
176 }