Merge branch 'master' of github.com:tgiphil/mono
[mono.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels.Simple / SimpleWireFormat.cs
1 // System.Runtime.Remoting.Channels.Simple.SimpleWireFormat.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 using System.Runtime.Serialization.Formatters;
30 using System.Runtime.Serialization;
31 using System.Reflection;
32 using System.Collections;
33 using System.IO;
34 using System.Runtime.Remoting.Messaging;
35
36 namespace System.Runtime.Remoting.Channels.Simple {
37
38         public sealed class SimpleWireFormat 
39         {
40                 enum TypeId : byte {
41                         Boolean,
42                         Byte,
43                         Char,
44                         Decimal,
45                         Double,
46                         Int16,
47                         Int32,
48                         Int64,
49                         SByte,
50                         String,
51                         Single,
52                         UInt16,
53                         UInt32,
54                         UInt64,
55                         NULL
56                 }
57
58                 public SimpleWireFormat ()
59                 {
60                 }               
61
62                 void SerializeObject (BinaryWriter writer, object obj)
63                 {
64                         if (obj == null) {
65                                 writer.Write ((byte)TypeId.NULL);
66                                 return;
67                         }
68                         
69                         Type type = obj.GetType ();
70
71                         if (type == typeof (String)) \r
72                         {
73                                 writer.Write ((byte)TypeId.String);
74                                 writer.Write ((String)obj);
75                                 return;
76                         }
77                         
78                         if (type == typeof (int)) {
79                                 writer.Write ((byte)TypeId.Int32);
80                                 writer.Write ((int)obj);
81                                 return;
82                         }               
83
84                         if (type == typeof (long)) {
85                                 writer.Write ((byte)TypeId.Int64);
86                                 writer.Write ((long)obj);
87                                 return;
88                         }               
89
90                         if (type == typeof (uint)) {
91                                 writer.Write ((byte)TypeId.UInt32);
92                                 writer.Write ((uint)obj);
93                                 return;
94                         }               
95
96                         if (type == typeof (ulong)) {
97                                 writer.Write ((byte)TypeId.UInt64);
98                                 writer.Write ((ulong)obj);
99                                 return;
100                         }               
101
102                         if (type == typeof (bool)) {
103                                 writer.Write ((byte)TypeId.Boolean);
104                                 writer.Write ((bool)obj);
105                                 return;
106                         }               
107
108                         if (type == typeof (byte)) {
109                                 writer.Write ((byte)TypeId.Byte);
110                                 writer.Write ((byte)obj);
111                                 return;
112                         }               
113
114                         if (type == typeof (sbyte)) {
115                                 writer.Write ((byte)TypeId.SByte);
116                                 writer.Write ((sbyte)obj);
117                                 return;
118                         }               
119
120                         if (type == typeof (char)) {
121                                 writer.Write ((byte)TypeId.Char);
122                                 writer.Write ((char)obj);
123                                 return;
124                         }               
125
126                         if (type == typeof (double)) {
127                                 writer.Write ((byte)TypeId.Double);
128                                 writer.Write ((double)obj);
129                                 return;
130                         }               
131
132                         if (type == typeof (Single)) {
133                                 writer.Write ((byte)TypeId.Single);
134                                 writer.Write ((Single)obj);
135                                 return;
136                         }
137                         
138                         if (type == typeof (Int16)) {
139                                 writer.Write ((byte)TypeId.Int16);
140                                 writer.Write ((Int16)obj);
141                                 return;
142                         }               
143
144                         if (type == typeof (UInt16)) {
145                                 writer.Write ((byte)TypeId.UInt16);
146                                 writer.Write ((UInt16)obj);
147                                 return;
148                         }               
149
150                         if (type == typeof (Decimal)) {
151                                 writer.Write ((byte)TypeId.Decimal);
152                                 writer.Write ((Decimal)obj);
153                                 return;
154                         }
155
156                         throw new NotSupportedException (); 
157                 }
158
159                 object DeserializeObject (BinaryReader reader)
160                 {
161                         TypeId tid = (TypeId)reader.ReadByte ();
162
163                         if (tid == TypeId.NULL)
164                                 return null;
165
166                         if (tid == TypeId.String) {
167                                 return reader.ReadString ();
168                         }
169                         
170                         if (tid == TypeId.Int32) {
171                                 return reader.ReadInt32 ();
172                         }
173                         
174                         if (tid == TypeId.Int64) {
175                                 return reader.ReadInt64 ();
176                         }
177                         
178                         if (tid == TypeId.UInt32) {
179                                 return reader.ReadUInt32 ();
180                         }
181                         
182                         if (tid == TypeId.UInt64) {
183                                 return reader.ReadUInt64 ();
184                         }
185                         
186                         if (tid == TypeId.Boolean) {
187                                 return reader.ReadBoolean ();
188                         }
189                         
190                         if (tid == TypeId.Byte) {
191                                 return reader.ReadByte ();
192                         }
193                         
194                         if (tid == TypeId.SByte) {
195                                 return reader.ReadSByte ();
196                         }
197                         
198                         if (tid == TypeId.Char) {
199                                 return reader.ReadChar ();
200                         }
201                         
202                         if (tid == TypeId.Double) {
203                                 return reader.ReadDouble ();
204                         }
205                         
206                         if (tid == TypeId.Single) {
207                                 return reader.ReadSingle ();
208                         }
209                         
210                         if (tid == TypeId.Byte) {
211                                 return reader.ReadByte ();
212                         }
213                         
214                         if (tid == TypeId.Int16) {
215                                 return reader.ReadInt16 ();
216                         }
217                         
218                         if (tid == TypeId.UInt16) {
219                                 return reader.ReadUInt16 ();
220                         }
221                         
222                         if (tid == TypeId.Decimal) {
223                                 return reader.ReadDecimal ();
224                         }
225                         
226                         throw new NotSupportedException (); 
227                 }
228                 
229                 public IMethodCallMessage DeserializeRequest (Stream serializationStream, string uri)
230                 {
231                         if (serializationStream == null) {
232                                 throw new ArgumentNullException ("serializationStream is null");
233                         }
234
235                         Type svr_type = RemotingServices.GetServerTypeForUri (uri);
236                         if (svr_type == null)
237                                 throw new RemotingException ("no registered server for uri " + uri); 
238
239                         BinaryReader reader = new BinaryReader (serializationStream);
240                         
241                         string method_name = reader.ReadString ();
242                         int arg_count = reader.ReadInt32 ();
243
244                         object [] args = new object [arg_count];
245                         for (int i = 0; i < arg_count; i++) {
246                                 args [i] = DeserializeObject (reader);
247                         }
248                         
249                         MonoMethodMessage msg = new MonoMethodMessage (svr_type, method_name, args);
250                         msg.Uri = uri;
251                         
252                         return msg;
253                 }
254
255                 public IMethodReturnMessage DeserializeResponse (Stream serializationStream,
256                                                                  IMethodCallMessage request) 
257                 {
258
259                         BinaryReader reader = new BinaryReader (serializationStream);
260
261                         object return_value = DeserializeObject (reader);
262                         
263                         int arg_count = reader.ReadInt32 ();
264                         object [] out_args = new object [arg_count];
265                         for (int i = 0; i < arg_count; i++)
266                                 out_args [i] = DeserializeObject (reader);
267                         
268                         return new ReturnMessage (return_value, out_args, arg_count, null, request);
269                 }
270                 
271                 public void SerializeRequest (Stream serializationStream, object graph)
272                 {
273                         if (serializationStream == null) {
274                                 throw new ArgumentNullException ("serializationStream is null");
275                         }
276
277                         BinaryWriter writer = new BinaryWriter (serializationStream);
278
279                         IMethodCallMessage msg = graph as IMethodCallMessage;
280                         if (msg != null) {                      
281                                 writer.Write (msg.MethodName);
282                                 writer.Write ((int)msg.InArgCount);
283                                 for (int i = 0; i < msg.InArgCount; i++)
284                                         SerializeObject (writer, msg.GetInArg (i));
285                                 return;
286                         }
287
288                         throw new NotSupportedException ();
289                 }
290
291                 public void SerializeResponse (Stream serializationStream, object graph)
292                 {
293                         if (serializationStream == null) {
294                                 throw new ArgumentNullException ("serializationStream is null");
295                         }
296
297                         BinaryWriter writer = new BinaryWriter (serializationStream);
298
299                         IMethodReturnMessage res = graph as IMethodReturnMessage;
300                         if (res != null) {
301
302                                 // this channel does not support serialization of exception,
303                                 // so we simply let the transport decide what to do
304                                 if (res.Exception != null)
305                                         return;
306                                 
307                                 SerializeObject (writer, res.ReturnValue);
308                                 writer.Write (res.OutArgCount);
309                         
310                                 for (int i = 0; i < res.OutArgCount; i++)
311                                         SerializeObject (writer, res.GetOutArg (i));
312
313                                 return;
314                         }
315
316                         throw new NotSupportedException ();
317                 }
318         }
319 }