Add licensing info
[mono.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels.CORBA / CDRFormatter.cs
1 // System.Runtime.Remoting.Channels.CORBA.CDRFormatter.cs
2 //
3 // Author:
4 //      DietmarMaurer (dietmar@ximian.com)
5 //
6 // (C) 2002 Ximian, Inc.  http://www.ximian.com
7
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29
30 // this is only a dummy, not CDR compatible
31
32 using System.Runtime.Serialization.Formatters;
33 using System.Runtime.Serialization;
34 using System.Reflection;
35 using System.Collections;
36 using System.IO;
37 using System.Runtime.Remoting.Messaging;
38
39 namespace System.Runtime.Remoting.Channels.CORBA {
40
41         public sealed class CDRFormatter 
42         {
43                 enum TypeId : byte {
44                         Boolean,
45                         Byte,
46                         Char,
47                         Decimal,
48                         Double,
49                         Int16,
50                         Int32,
51                         Int64,
52                         SByte,
53                         String,
54                         Single,
55                         UInt16,
56                         UInt32,
57                         UInt64,
58                         NULL
59                 }
60
61                 public CDRFormatter ()
62                 {
63                 }               
64
65                 void SerializeObject (BinaryWriter writer, object obj)
66                 {
67                         Type type = obj.GetType ();
68
69                         if (obj == null) {
70                                 writer.Write ((byte)TypeId.NULL);
71                                 return;
72                         }
73                         
74                         if (type == typeof (String)) {
75                                 writer.Write ((byte)TypeId.String);
76                                 writer.Write ((String)obj);
77                                 return;
78                         }
79                         
80                         if (type == typeof (int)) {
81                                 writer.Write ((byte)TypeId.Int32);
82                                 writer.Write ((int)obj);
83                                 return;
84                         }               
85
86                         if (type == typeof (long)) {
87                                 writer.Write ((byte)TypeId.Int64);
88                                 writer.Write ((long)obj);
89                                 return;
90                         }               
91
92                         if (type == typeof (uint)) {
93                                 writer.Write ((byte)TypeId.UInt32);
94                                 writer.Write ((uint)obj);
95                                 return;
96                         }               
97
98                         if (type == typeof (ulong)) {
99                                 writer.Write ((byte)TypeId.UInt64);
100                                 writer.Write ((ulong)obj);
101                                 return;
102                         }               
103
104                         if (type == typeof (bool)) {
105                                 writer.Write ((byte)TypeId.Boolean);
106                                 writer.Write ((bool)obj);
107                                 return;
108                         }               
109
110                         if (type == typeof (byte)) {
111                                 writer.Write ((byte)TypeId.Byte);
112                                 writer.Write ((byte)obj);
113                                 return;
114                         }               
115
116                         if (type == typeof (sbyte)) {
117                                 writer.Write ((byte)TypeId.SByte);
118                                 writer.Write ((sbyte)obj);
119                                 return;
120                         }               
121
122                         if (type == typeof (char)) {
123                                 writer.Write ((byte)TypeId.Char);
124                                 writer.Write ((char)obj);
125                                 return;
126                         }               
127
128                         if (type == typeof (double)) {
129                                 writer.Write ((byte)TypeId.Double);
130                                 writer.Write ((double)obj);
131                                 return;
132                         }               
133
134                         if (type == typeof (Single)) {
135                                 writer.Write ((byte)TypeId.Single);
136                                 writer.Write ((Single)obj);
137                                 return;
138                         }
139                         
140                         if (type == typeof (Int16)) {
141                                 writer.Write ((byte)TypeId.Int16);
142                                 writer.Write ((Int16)obj);
143                                 return;
144                         }               
145
146                         if (type == typeof (UInt16)) {
147                                 writer.Write ((byte)TypeId.UInt16);
148                                 writer.Write ((UInt16)obj);
149                                 return;
150                         }               
151
152                         if (type == typeof (Decimal)) {
153                                 writer.Write ((byte)TypeId.Decimal);
154                                 writer.Write ((Decimal)obj);
155                                 return;
156                         }
157
158                         throw new NotSupportedException (); 
159                 }
160
161                 object DeserializeObject (BinaryReader reader)
162                 {
163                         TypeId tid = (TypeId)reader.ReadByte ();
164
165                         if (tid == TypeId.NULL)
166                                 return null;
167
168                         if (tid == TypeId.String) {
169                                 return reader.ReadString ();
170                         }
171                         
172                         if (tid == TypeId.Int32) {
173                                 return reader.ReadInt32 ();
174                         }
175                         
176                         if (tid == TypeId.Int64) {
177                                 return reader.ReadInt64 ();
178                         }
179                         
180                         if (tid == TypeId.UInt32) {
181                                 return reader.ReadUInt32 ();
182                         }
183                         
184                         if (tid == TypeId.UInt64) {
185                                 return reader.ReadUInt64 ();
186                         }
187                         
188                         if (tid == TypeId.Boolean) {
189                                 return reader.ReadBoolean ();
190                         }
191                         
192                         if (tid == TypeId.Byte) {
193                                 return reader.ReadByte ();
194                         }
195                         
196                         if (tid == TypeId.SByte) {
197                                 return reader.ReadSByte ();
198                         }
199                         
200                         if (tid == TypeId.Char) {
201                                 return reader.ReadChar ();
202                         }
203                         
204                         if (tid == TypeId.Double) {
205                                 return reader.ReadDouble ();
206                         }
207                         
208                         if (tid == TypeId.Single) {
209                                 return reader.ReadSingle ();
210                         }
211                         
212                         if (tid == TypeId.Byte) {
213                                 return reader.ReadByte ();
214                         }
215                         
216                         if (tid == TypeId.Int16) {
217                                 return reader.ReadInt16 ();
218                         }
219                         
220                         if (tid == TypeId.UInt16) {
221                                 return reader.ReadUInt16 ();
222                         }
223                         
224                         if (tid == TypeId.Decimal) {
225                                 return reader.ReadDecimal ();
226                         }
227                         
228                         throw new NotSupportedException (); 
229                 }
230                 
231                 public IMethodCallMessage DeserializeRequest (Stream serializationStream, string uri)
232                 {
233                         if (serializationStream == null) {
234                                 throw new ArgumentNullException ("serializationStream is null");
235                         }
236
237                         Type svr_type = RemotingServices.GetServerTypeForUri (uri);
238                         if (svr_type == null)
239                                 throw new RemotingException ("no registered server for uri " + uri); 
240
241                         BinaryReader reader = new BinaryReader (serializationStream);
242                         
243                         string method_name = reader.ReadString ();
244                         int arg_count = reader.ReadInt32 ();
245
246                         object [] args = new object [arg_count];
247                         for (int i = 0; i < arg_count; i++) {
248                                 args [i] = DeserializeObject (reader);
249                         }
250                         
251                         MonoMethodMessage msg = new MonoMethodMessage (svr_type, method_name, args);
252                         
253                         return msg;
254                 }
255
256                 public IMethodReturnMessage DeserializeResponse (Stream serializationStream,
257                                                                  IMethodCallMessage request) 
258                 {
259
260                         BinaryReader reader = new BinaryReader (serializationStream);
261
262                         object return_value = DeserializeObject (reader);
263                         
264                         int arg_count = reader.ReadInt32 ();
265                         object [] out_args = new object [arg_count];
266                         for (int i = 0; i < arg_count; i++)
267                                 out_args [i] = DeserializeObject (reader);
268                         
269                         return new ReturnMessage (return_value, out_args, arg_count, null, request);
270                 }
271                 
272                 public void SerializeRequest (Stream serializationStream, object graph)
273                 {
274                         if (serializationStream == null) {
275                                 throw new ArgumentNullException ("serializationStream is null");
276                         }
277
278                         BinaryWriter writer = new BinaryWriter (serializationStream);
279
280                         IMethodCallMessage msg = graph as IMethodCallMessage;
281                         if (msg != null) {                      
282                                 writer.Write (msg.MethodName);
283                                 writer.Write ((int)msg.InArgCount);
284                                 for (int i = 0; i < msg.InArgCount; i++)
285                                         SerializeObject (writer, msg.GetInArg (i));
286                                 return;
287                         }
288
289                         throw new NotSupportedException ();
290                 }
291
292                 public void SerializeResponse (Stream serializationStream, object graph)
293                 {
294                         if (serializationStream == null) {
295                                 throw new ArgumentNullException ("serializationStream is null");
296                         }
297
298                         BinaryWriter writer = new BinaryWriter (serializationStream);
299
300                         IMethodReturnMessage res = graph as IMethodReturnMessage;
301                         if (res != null) {
302
303                                 // this channel does not support serialization of exception,
304                                 // so we simply let the transport decide what to do
305                                 if (res.Exception != null)
306                                         return;
307                                 
308                                 SerializeObject (writer, res.ReturnValue);
309                                 writer.Write (res.OutArgCount);
310                         
311                                 for (int i = 0; i < res.OutArgCount; i++)
312                                         SerializeObject (writer, res.GetOutArg (i));
313
314                                 return;
315                         }
316
317                         throw new NotSupportedException ();
318                 }
319         }
320 }