1 // System.Runtime.Remoting.Channels.Simple.SimpleWireFormat.cs
4 // DietmarMaurer (dietmar@ximian.com)
6 // (C) 2002 Ximian, Inc. http://www.ximian.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Runtime.Serialization.Formatters;
30 using System.Runtime.Serialization;
31 using System.Reflection;
32 using System.Collections;
34 using System.Runtime.Remoting.Messaging;
36 namespace System.Runtime.Remoting.Channels.Simple {
38 public sealed class SimpleWireFormat
58 public SimpleWireFormat ()
62 void SerializeObject (BinaryWriter writer, object obj)
65 writer.Write ((byte)TypeId.NULL);
69 Type type = obj.GetType ();
71 if (type == typeof (String))
\r
73 writer.Write ((byte)TypeId.String);
74 writer.Write ((String)obj);
78 if (type == typeof (int)) {
79 writer.Write ((byte)TypeId.Int32);
80 writer.Write ((int)obj);
84 if (type == typeof (long)) {
85 writer.Write ((byte)TypeId.Int64);
86 writer.Write ((long)obj);
90 if (type == typeof (uint)) {
91 writer.Write ((byte)TypeId.UInt32);
92 writer.Write ((uint)obj);
96 if (type == typeof (ulong)) {
97 writer.Write ((byte)TypeId.UInt64);
98 writer.Write ((ulong)obj);
102 if (type == typeof (bool)) {
103 writer.Write ((byte)TypeId.Boolean);
104 writer.Write ((bool)obj);
108 if (type == typeof (byte)) {
109 writer.Write ((byte)TypeId.Byte);
110 writer.Write ((byte)obj);
114 if (type == typeof (sbyte)) {
115 writer.Write ((byte)TypeId.SByte);
116 writer.Write ((sbyte)obj);
120 if (type == typeof (char)) {
121 writer.Write ((byte)TypeId.Char);
122 writer.Write ((char)obj);
126 if (type == typeof (double)) {
127 writer.Write ((byte)TypeId.Double);
128 writer.Write ((double)obj);
132 if (type == typeof (Single)) {
133 writer.Write ((byte)TypeId.Single);
134 writer.Write ((Single)obj);
138 if (type == typeof (Int16)) {
139 writer.Write ((byte)TypeId.Int16);
140 writer.Write ((Int16)obj);
144 if (type == typeof (UInt16)) {
145 writer.Write ((byte)TypeId.UInt16);
146 writer.Write ((UInt16)obj);
150 if (type == typeof (Decimal)) {
151 writer.Write ((byte)TypeId.Decimal);
152 writer.Write ((Decimal)obj);
156 throw new NotSupportedException ();
159 object DeserializeObject (BinaryReader reader)
161 TypeId tid = (TypeId)reader.ReadByte ();
163 if (tid == TypeId.NULL)
166 if (tid == TypeId.String) {
167 return reader.ReadString ();
170 if (tid == TypeId.Int32) {
171 return reader.ReadInt32 ();
174 if (tid == TypeId.Int64) {
175 return reader.ReadInt64 ();
178 if (tid == TypeId.UInt32) {
179 return reader.ReadUInt32 ();
182 if (tid == TypeId.UInt64) {
183 return reader.ReadUInt64 ();
186 if (tid == TypeId.Boolean) {
187 return reader.ReadBoolean ();
190 if (tid == TypeId.Byte) {
191 return reader.ReadByte ();
194 if (tid == TypeId.SByte) {
195 return reader.ReadSByte ();
198 if (tid == TypeId.Char) {
199 return reader.ReadChar ();
202 if (tid == TypeId.Double) {
203 return reader.ReadDouble ();
206 if (tid == TypeId.Single) {
207 return reader.ReadSingle ();
210 if (tid == TypeId.Byte) {
211 return reader.ReadByte ();
214 if (tid == TypeId.Int16) {
215 return reader.ReadInt16 ();
218 if (tid == TypeId.UInt16) {
219 return reader.ReadUInt16 ();
222 if (tid == TypeId.Decimal) {
223 return reader.ReadDecimal ();
226 throw new NotSupportedException ();
229 public IMethodCallMessage DeserializeRequest (Stream serializationStream, string uri)
231 if (serializationStream == null) {
232 throw new ArgumentNullException ("serializationStream is null");
235 Type svr_type = RemotingServices.GetServerTypeForUri (uri);
236 if (svr_type == null)
237 throw new RemotingException ("no registered server for uri " + uri);
239 BinaryReader reader = new BinaryReader (serializationStream);
241 string method_name = reader.ReadString ();
242 int arg_count = reader.ReadInt32 ();
244 object [] args = new object [arg_count];
245 for (int i = 0; i < arg_count; i++) {
246 args [i] = DeserializeObject (reader);
249 MonoMethodMessage msg = new MonoMethodMessage (svr_type, method_name, args);
255 public IMethodReturnMessage DeserializeResponse (Stream serializationStream,
256 IMethodCallMessage request)
259 BinaryReader reader = new BinaryReader (serializationStream);
261 object return_value = DeserializeObject (reader);
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);
268 return new ReturnMessage (return_value, out_args, arg_count, null, request);
271 public void SerializeRequest (Stream serializationStream, object graph)
273 if (serializationStream == null) {
274 throw new ArgumentNullException ("serializationStream is null");
277 BinaryWriter writer = new BinaryWriter (serializationStream);
279 IMethodCallMessage msg = graph as IMethodCallMessage;
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));
288 throw new NotSupportedException ();
291 public void SerializeResponse (Stream serializationStream, object graph)
293 if (serializationStream == null) {
294 throw new ArgumentNullException ("serializationStream is null");
297 BinaryWriter writer = new BinaryWriter (serializationStream);
299 IMethodReturnMessage res = graph as IMethodReturnMessage;
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)
307 SerializeObject (writer, res.ReturnValue);
308 writer.Write (res.OutArgCount);
310 for (int i = 0; i < res.OutArgCount; i++)
311 SerializeObject (writer, res.GetOutArg (i));
316 throw new NotSupportedException ();