* RemotingServices.cs: Added support for WellKnownService objects.
[mono.git] / mcs / class / corlib / System.Runtime.Remoting / ObjRef.cs
1 //
2 // System.Runtime.Remoting.ObjRef.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Dietmar Maurer (dietmar@ximian.com)
7 //   Lluis Sanchez Gual (lluis@ideary.com)
8 //   Patrik Torstensson
9 //
10 // (C) Ximian, Inc.  http://www.ximian.com
11 //
12
13 using System;
14 using System.Runtime.Serialization;
15 using System.Runtime.Remoting.Channels;
16 using System.Runtime.Remoting.Messaging;
17 using System.Runtime.Remoting.Proxies;
18
19 namespace System.Runtime.Remoting {
20
21         [Serializable]
22         public class ObjRef : IObjectReference, ISerializable 
23         {
24                 IChannelInfo channel_info;
25                 string uri;
26                 IRemotingTypeInfo typeInfo;
27                 IEnvoyInfo envoyInfo;
28                 [NonSerialized] bool marshalledValue = true;
29                 
30                 public ObjRef ()
31                 {
32                         // no idea why this needs to be public
33                         channel_info = new ChannelInfoStore ();
34                 }
35                 
36                 public ObjRef (MarshalByRefObject mbr, Type type)
37                 {
38                         if (mbr == null)
39                                 throw new ArgumentNullException ("mbr");
40                         
41                         if (type == null)
42                                 throw new ArgumentNullException ("type");
43
44                         // The ObjRef can only be constructed if the given mbr
45                         // has already been marshalled using RemotingServices.Marshall
46
47                         uri = RemotingServices.GetObjectUri(mbr);
48                         typeInfo = new TypeInfo(type);
49
50                         if (!typeInfo.CanCastTo(mbr.GetType(), mbr))
51                                 throw new RemotingException ("The server object type cannot be cast to the requested type " + type.FullName + ".");
52
53                         channel_info = new ChannelInfoStore ();
54                 }
55
56                 protected ObjRef (SerializationInfo si, StreamingContext sc)
57                 {
58                         SerializationInfoEnumerator en = si.GetEnumerator();
59                         // Info to serialize: uri, objrefFlags, typeInfo, envoyInfo, channelInfo
60
61                         marshalledValue = true;
62
63                         while (en.MoveNext ()) {
64                                 switch (en.Name) {
65                                 case "uri":
66                                         uri = (string)en.Value;
67                                         break;
68                                 case "typeInfo":
69                                         typeInfo = (IRemotingTypeInfo)en.Value;
70                                         break;
71                                 case "channelInfo":
72                                         channel_info = (IChannelInfo)en.Value;
73                                         break;
74                                 case "envoyInfo":
75                                         envoyInfo = (IEnvoyInfo)en.Value;
76                                         break;
77                                 case "fIsMarshalled":
78                                         int status;
79                                         Object o = en.Value;
80                                         if (o.GetType().Equals(typeof(String)))\r
81                                                 status = ((IConvertible) o).ToInt32(null);\r
82                                         else\r
83                                                 status = (int) o;\r
84
85                                         if (status == 0)
86                                                 marshalledValue = false;
87                                         break;
88                                 case "objrefFlags":             // FIXME: do something with this
89                                         break;
90                                 default:
91                                         throw new NotSupportedException ();
92                                 }
93                         }
94                 }
95
96                 public virtual IChannelInfo ChannelInfo {
97
98                         get {
99                                 return channel_info;
100                         }
101                         
102                         set {
103                                 channel_info = value;
104                         }
105                 }
106                 
107                 [MonoTODO]
108                 public virtual IEnvoyInfo EnvoyInfo {
109                         get {
110                                 throw new NotImplementedException ();
111                         }
112                         set {
113                                 throw new NotImplementedException ();
114                         }
115                 }
116                 
117                 [MonoTODO]
118                 public virtual IRemotingTypeInfo TypeInfo {
119                         get {
120                                 return typeInfo;
121                         }
122                         set {
123                                 typeInfo = value;
124                         }
125                 }
126                 
127                 public virtual string URI {
128                         get {
129                                 return uri;
130                         }
131                         set {
132                                 uri = value;
133                         }
134                 }
135
136                 public virtual void GetObjectData (SerializationInfo si, StreamingContext sc)
137                 {
138                         si.SetType (GetType());
139                         si.AddValue ("uri", uri);
140                         si.AddValue ("typeInfo", typeInfo, typeof (IRemotingTypeInfo));
141                         si.AddValue ("envoyInfo", envoyInfo, typeof (IEnvoyInfo));
142                         si.AddValue ("channelInfo", channel_info, typeof(IChannelInfo));
143                         si.AddValue ("objrefFlags", 0);
144                 }
145
146                 public virtual object GetRealObject (StreamingContext sc)
147                 {
148                         if (marshalledValue)
149                                 return RemotingServices.GetRemoteObject(Type.GetType(typeInfo.TypeName), null, channel_info.ChannelData, uri);
150                         else
151                                 return this;
152                 }
153
154                 public bool IsFromThisAppDomain ()
155                 {
156                         Identity identity = RemotingServices.GetIdentityForUri (uri);
157                         if (identity == null) return false;             // URI not registered in this domain
158
159                         return identity.IsFromThisAppDomain;
160                 }
161
162                 [MonoTODO]
163                 public bool IsFromThisProcess ()
164                 {
165                         // as yet we do not consider this optimization
166                         return false;
167                 }
168         }
169 }