1 // System.Runtime.Remoting.Channels.CORBA.CDRFormatter.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.
30 // this is only a dummy, not CDR compatible
32 using System
.Runtime
.Serialization
.Formatters
;
33 using System
.Runtime
.Serialization
;
34 using System
.Reflection
;
35 using System
.Collections
;
37 using System
.Runtime
.Remoting
.Messaging
;
39 namespace System
.Runtime
.Remoting
.Channels
.CORBA
{
41 public sealed class CDRFormatter
61 public CDRFormatter ()
65 void SerializeObject (BinaryWriter writer
, object obj
)
67 Type type
= obj
.GetType ();
70 writer
.Write ((byte)TypeId
.NULL
);
74 if (type
== typeof (String
)) {
75 writer
.Write ((byte)TypeId
.String
);
76 writer
.Write ((String
)obj
);
80 if (type
== typeof (int)) {
81 writer
.Write ((byte)TypeId
.Int32
);
82 writer
.Write ((int)obj
);
86 if (type
== typeof (long)) {
87 writer
.Write ((byte)TypeId
.Int64
);
88 writer
.Write ((long)obj
);
92 if (type
== typeof (uint)) {
93 writer
.Write ((byte)TypeId
.UInt32
);
94 writer
.Write ((uint)obj
);
98 if (type
== typeof (ulong)) {
99 writer
.Write ((byte)TypeId
.UInt64
);
100 writer
.Write ((ulong)obj
);
104 if (type
== typeof (bool)) {
105 writer
.Write ((byte)TypeId
.Boolean
);
106 writer
.Write ((bool)obj
);
110 if (type
== typeof (byte)) {
111 writer
.Write ((byte)TypeId
.Byte
);
112 writer
.Write ((byte)obj
);
116 if (type
== typeof (sbyte)) {
117 writer
.Write ((byte)TypeId
.SByte
);
118 writer
.Write ((sbyte)obj
);
122 if (type
== typeof (char)) {
123 writer
.Write ((byte)TypeId
.Char
);
124 writer
.Write ((char)obj
);
128 if (type
== typeof (double)) {
129 writer
.Write ((byte)TypeId
.Double
);
130 writer
.Write ((double)obj
);
134 if (type
== typeof (Single
)) {
135 writer
.Write ((byte)TypeId
.Single
);
136 writer
.Write ((Single
)obj
);
140 if (type
== typeof (Int16
)) {
141 writer
.Write ((byte)TypeId
.Int16
);
142 writer
.Write ((Int16
)obj
);
146 if (type
== typeof (UInt16
)) {
147 writer
.Write ((byte)TypeId
.UInt16
);
148 writer
.Write ((UInt16
)obj
);
152 if (type
== typeof (Decimal
)) {
153 writer
.Write ((byte)TypeId
.Decimal
);
154 writer
.Write ((Decimal
)obj
);
158 throw new NotSupportedException ();
161 object DeserializeObject (BinaryReader reader
)
163 TypeId tid
= (TypeId
)reader
.ReadByte ();
165 if (tid
== TypeId
.NULL
)
168 if (tid
== TypeId
.String
) {
169 return reader
.ReadString ();
172 if (tid
== TypeId
.Int32
) {
173 return reader
.ReadInt32 ();
176 if (tid
== TypeId
.Int64
) {
177 return reader
.ReadInt64 ();
180 if (tid
== TypeId
.UInt32
) {
181 return reader
.ReadUInt32 ();
184 if (tid
== TypeId
.UInt64
) {
185 return reader
.ReadUInt64 ();
188 if (tid
== TypeId
.Boolean
) {
189 return reader
.ReadBoolean ();
192 if (tid
== TypeId
.Byte
) {
193 return reader
.ReadByte ();
196 if (tid
== TypeId
.SByte
) {
197 return reader
.ReadSByte ();
200 if (tid
== TypeId
.Char
) {
201 return reader
.ReadChar ();
204 if (tid
== TypeId
.Double
) {
205 return reader
.ReadDouble ();
208 if (tid
== TypeId
.Single
) {
209 return reader
.ReadSingle ();
212 if (tid
== TypeId
.Byte
) {
213 return reader
.ReadByte ();
216 if (tid
== TypeId
.Int16
) {
217 return reader
.ReadInt16 ();
220 if (tid
== TypeId
.UInt16
) {
221 return reader
.ReadUInt16 ();
224 if (tid
== TypeId
.Decimal
) {
225 return reader
.ReadDecimal ();
228 throw new NotSupportedException ();
231 public IMethodCallMessage
DeserializeRequest (Stream serializationStream
, string uri
)
233 if (serializationStream
== null) {
234 throw new ArgumentNullException ("serializationStream is null");
237 Type svr_type
= RemotingServices
.GetServerTypeForUri (uri
);
238 if (svr_type
== null)
239 throw new RemotingException ("no registered server for uri " + uri
);
241 BinaryReader reader
= new BinaryReader (serializationStream
);
243 string method_name
= reader
.ReadString ();
244 int arg_count
= reader
.ReadInt32 ();
246 object [] args
= new object [arg_count
];
247 for (int i
= 0; i
< arg_count
; i
++) {
248 args
[i
] = DeserializeObject (reader
);
251 MonoMethodMessage msg
= new MonoMethodMessage (svr_type
, method_name
, args
);
256 public IMethodReturnMessage
DeserializeResponse (Stream serializationStream
,
257 IMethodCallMessage request
)
260 BinaryReader reader
= new BinaryReader (serializationStream
);
262 object return_value
= DeserializeObject (reader
);
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
);
269 return new ReturnMessage (return_value
, out_args
, arg_count
, null, request
);
272 public void SerializeRequest (Stream serializationStream
, object graph
)
274 if (serializationStream
== null) {
275 throw new ArgumentNullException ("serializationStream is null");
278 BinaryWriter writer
= new BinaryWriter (serializationStream
);
280 IMethodCallMessage msg
= graph
as IMethodCallMessage
;
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
));
289 throw new NotSupportedException ();
292 public void SerializeResponse (Stream serializationStream
, object graph
)
294 if (serializationStream
== null) {
295 throw new ArgumentNullException ("serializationStream is null");
298 BinaryWriter writer
= new BinaryWriter (serializationStream
);
300 IMethodReturnMessage res
= graph
as IMethodReturnMessage
;
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)
308 SerializeObject (writer
, res
.ReturnValue
);
309 writer
.Write (res
.OutArgCount
);
311 for (int i
= 0; i
< res
.OutArgCount
; i
++)
312 SerializeObject (writer
, res
.GetOutArg (i
));
317 throw new NotSupportedException ();