Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Context.cs « System.Runtime.Remoting.Contexts « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 40dd572d2540281e44de44baaa4e9d8ae46a9dc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//
// System.Runtime.Remoting.Contexts.Context..cs
//
// Author:
//   Miguel de Icaza (miguel@ximian.com)
//   Lluis Sanchez Gual (lluis@ideary.com)
//   Patrik Torstensson
//
// (C) Ximian, Inc.  http://www.ximian.com
//

//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System.Collections;
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Lifetime;

namespace System.Runtime.Remoting.Contexts {

	public class Context 
	{
		#region Sync with domain-internals.h
		int domain_id;
		int context_id;
		IntPtr static_data;
		#endregion

		// Default server context sink chain
		static IMessageSink default_server_context_sink;

		// The sink chain that has to be used by all calls entering the context
		IMessageSink server_context_sink_chain = null;

		// The sink chain that has to be used by all calls exiting the context
		IMessageSink client_context_sink_chain = null;

		Hashtable datastore;
		ArrayList context_properties;
		bool frozen;
		
		static int global_count;
		static Hashtable namedSlots = new Hashtable ();

		static DynamicPropertyCollection global_dynamic_properties;
		DynamicPropertyCollection context_dynamic_properties;
		ContextCallbackObject callback_object = null;
		
		public Context ()
		{
			domain_id = Thread.GetDomainID();
			context_id = 1 + global_count++;
		}

		~Context ()
		{
		}

		public static Context DefaultContext {
			get {
				return AppDomain.InternalGetDefaultContext ();
			}
		}

		public virtual int ContextID {
			get {
				return context_id;
			}
		}

		public virtual IContextProperty[] ContextProperties
		{
			get 
			{
				if (context_properties == null) return new IContextProperty[0];
				else return (IContextProperty[]) context_properties.ToArray (typeof(IContextProperty[]));
			}
		}
		
		internal bool IsDefaultContext
		{
			get { return context_id == 0; }
		}

		internal bool NeedsContextSink
		{
			get {
				return context_id != 0 || 
					(global_dynamic_properties != null && global_dynamic_properties.HasProperties) || 
					(context_dynamic_properties != null && context_dynamic_properties.HasProperties);
			}
		}

		public static bool RegisterDynamicProperty(IDynamicProperty prop, ContextBoundObject obj, Context ctx)
		{
			DynamicPropertyCollection col = GetDynamicPropertyCollection (obj, ctx);
			return col.RegisterDynamicProperty (prop);
		}

		public static bool UnregisterDynamicProperty(string name, ContextBoundObject obj, Context ctx)
		{
			DynamicPropertyCollection col = GetDynamicPropertyCollection (obj, ctx);
			return col.UnregisterDynamicProperty (name);
		}
		
		static DynamicPropertyCollection GetDynamicPropertyCollection(ContextBoundObject obj, Context ctx)
		{
			if (ctx == null && obj != null)
			{
				if (RemotingServices.IsTransparentProxy(obj))
				{
					RealProxy rp = RemotingServices.GetRealProxy (obj);
					return rp.ObjectIdentity.ClientDynamicProperties;
				}
				else
					return obj.ObjectIdentity.ServerDynamicProperties;
			}
			else if (ctx != null && obj == null)
			{
				if (ctx.context_dynamic_properties == null) ctx.context_dynamic_properties = new DynamicPropertyCollection ();
				return ctx.context_dynamic_properties;
			}
			else if (ctx == null && obj == null)
			{
				if (global_dynamic_properties == null) global_dynamic_properties = new DynamicPropertyCollection ();
				return global_dynamic_properties;
			}
			else
				throw new ArgumentException ("Either obj or ctx must be null");
		}
		
		internal static void NotifyGlobalDynamicSinks  (bool start, IMessage req_msg, bool client_site, bool async)
		{
			if (global_dynamic_properties != null && global_dynamic_properties.HasProperties) 
				global_dynamic_properties.NotifyMessage (start, req_msg, client_site, async);
		}

		internal static bool HasGlobalDynamicSinks
		{
			get { return (global_dynamic_properties != null && global_dynamic_properties.HasProperties); }
		}

		internal void NotifyDynamicSinks  (bool start, IMessage req_msg, bool client_site, bool async)
		{
			if (context_dynamic_properties != null && context_dynamic_properties.HasProperties) 
				context_dynamic_properties.NotifyMessage (start, req_msg, client_site, async);
		}

		internal bool HasDynamicSinks
		{
			get { return (context_dynamic_properties != null && context_dynamic_properties.HasProperties); }
		}

		internal bool HasExitSinks
		{
			get
			{
				// Needs to go through the client context sink if there are custom
				// client context or dynamic sinks.

				return ( !(GetClientContextSinkChain() is ClientContextTerminatorSink) || HasDynamicSinks || HasGlobalDynamicSinks);
			}
		}

		public virtual IContextProperty GetProperty (string name)
		{
			if (context_properties == null)
				return null;

			foreach (IContextProperty p in context_properties)
				if (p.Name == name)
					return p;
			
			return null;
		}

		public virtual void SetProperty (IContextProperty prop)
		{
			if (prop == null)
				throw new ArgumentNullException ("IContextProperty");
			if (this == DefaultContext)
				throw new InvalidOperationException ("Can not add properties to " +
								     "default context");
			if (frozen)
				throw new InvalidOperationException ("Context is Frozen");
			
			if (context_properties == null)
				context_properties = new ArrayList ();

			context_properties.Add (prop);
		}

		public virtual void Freeze ()
		{
			if (context_properties != null)
			{
				foreach (IContextProperty prop in context_properties)
					prop.Freeze (this);
			}
		}

		public override string ToString()
		{
			return "ContextID: " + context_id;
		}

		internal IMessageSink GetServerContextSinkChain()
		{
			if (server_context_sink_chain == null)
			{
				if (default_server_context_sink == null)
					default_server_context_sink = new ServerContextTerminatorSink();

				server_context_sink_chain = default_server_context_sink;

				if (context_properties != null) {
					// Enumerate in reverse order
					for (int n = context_properties.Count-1; n>=0; n--) {
						IContributeServerContextSink contributor = context_properties[n] as IContributeServerContextSink;
						if (contributor != null)
							server_context_sink_chain = contributor.GetServerContextSink (server_context_sink_chain);
					}
				}
			}
			return server_context_sink_chain;
		}

		internal IMessageSink GetClientContextSinkChain()
		{
			if (client_context_sink_chain == null)
			{
				client_context_sink_chain = new ClientContextTerminatorSink (this);

				if (context_properties != null) {
					foreach (IContextProperty prop in context_properties) {
						IContributeClientContextSink contributor = prop as IContributeClientContextSink;
						if (contributor != null)
							client_context_sink_chain = contributor.GetClientContextSink (client_context_sink_chain);
					}
				}
			}
			return client_context_sink_chain;
		}

		internal IMessageSink CreateServerObjectSinkChain (MarshalByRefObject obj, bool forceInternalExecute)
		{
			IMessageSink objectSink = new StackBuilderSink (obj, forceInternalExecute);
			objectSink = new ServerObjectTerminatorSink (objectSink);
			objectSink = new Lifetime.LeaseSink (objectSink);

			if (context_properties != null)
			{
				// Contribute object sinks in reverse order
				for (int n = context_properties.Count-1; n >= 0; n--)
				{
					IContextProperty prop = (IContextProperty) context_properties[n];
					IContributeObjectSink contributor = prop as IContributeObjectSink;
					if (contributor != null)
						objectSink = contributor.GetObjectSink (obj, objectSink);
				}
			}
			return objectSink;
		}

		internal IMessageSink CreateEnvoySink (MarshalByRefObject serverObject)
		{
			IMessageSink sink = EnvoyTerminatorSink.Instance;
			if (context_properties != null)
			{
				foreach (IContextProperty prop in context_properties)
				{
					IContributeEnvoySink contributor = prop as IContributeEnvoySink;
					if (contributor != null)
						sink = contributor.GetEnvoySink (serverObject, sink);
				}
			}
			return sink;
		}

		internal static Context SwitchToContext (Context newContext)
		{
			return AppDomain.InternalSetContext (newContext);
		}

		internal static Context CreateNewContext (IConstructionCallMessage msg)
		{
			// Create the new context

			Context newContext = new Context();

			foreach (IContextProperty prop in msg.ContextProperties)
			{
				if (newContext.GetProperty (prop.Name) == null)
					newContext.SetProperty (prop);
			}
			newContext.Freeze();


			// Ask each context property whether the new context is OK

			foreach (IContextProperty prop in msg.ContextProperties)
				if (!prop.IsNewContextOK (newContext)) 
					throw new RemotingException("A context property did not approve the candidate context for activating the object");

			return newContext;
		}
		
		public void DoCallBack (CrossContextDelegate deleg)
		{
			lock (this)
			{
				if (callback_object == null) {
					Context oldContext = Context.SwitchToContext (this);
					callback_object = new ContextCallbackObject ();
					Context.SwitchToContext (oldContext);
				}
			}
			
			callback_object.DoCallBack (deleg);
		}
		
		public static LocalDataStoreSlot AllocateDataSlot ()
		{
			return new LocalDataStoreSlot ();
		}
		
		public static LocalDataStoreSlot AllocateNamedDataSlot (string name)
		{
			lock (namedSlots.SyncRoot)
			{
				LocalDataStoreSlot slot = new LocalDataStoreSlot ();
				namedSlots.Add (name, slot);
				return slot;
			}
		}
		
		public static void FreeNamedDataSlot (string name)
		{
			lock (namedSlots.SyncRoot)
			{
				namedSlots.Remove (name);
			}
		}
		
		public static object GetData (LocalDataStoreSlot slot)
		{
			Context ctx = Thread.CurrentContext;
			if (ctx.datastore == null) return null;
			
			lock (ctx.datastore.SyncRoot)
			{
				return ctx.datastore [slot];
			}
		}
		
		public static LocalDataStoreSlot GetNamedDataSlot (string name)
		{
			lock (namedSlots.SyncRoot)
			{
				LocalDataStoreSlot slot = namedSlots [name] as LocalDataStoreSlot;
				if (slot == null) return AllocateNamedDataSlot (name);
				else return slot;
			}
		}
		
		public static void SetData (LocalDataStoreSlot slot, object data)
		{
			Context ctx = Thread.CurrentContext;
			if (ctx.datastore == null)
			{
				lock (ctx)
				{
					if (ctx.datastore == null)
						ctx.datastore = new Hashtable ();
				}
			}
			
			lock (ctx.datastore.SyncRoot)
			{
				ctx.datastore [slot] = data;
			}
		}
	}

	class DynamicPropertyCollection
	{
		ArrayList _properties = new ArrayList();

		class DynamicPropertyReg
		{
			public IDynamicProperty Property;
			public IDynamicMessageSink Sink;
		}

		public bool HasProperties
		{
			get { return _properties.Count > 0; }
		}

		public bool RegisterDynamicProperty(IDynamicProperty prop)
		{
			lock (this)
			{
				if (FindProperty (prop.Name) != -1) 
					throw new InvalidOperationException ("Another property by this name already exists");

				// Make a copy, do not interfere with threads running dynamic sinks
				ArrayList newProps = new ArrayList (_properties);

				DynamicPropertyReg reg = new DynamicPropertyReg();
				reg.Property = prop;
				IContributeDynamicSink contributor = prop as IContributeDynamicSink;
				if (contributor != null) reg.Sink = contributor.GetDynamicSink ();
				newProps.Add (reg);

				_properties = newProps;

				return true;	// When should be false?
			}
		}

		public bool UnregisterDynamicProperty(string name)
		{
			lock (this)
			{
				int i = FindProperty (name);
				if (i == -1) throw new RemotingException ("A property with the name " + name + " was not found");

				_properties.RemoveAt (i);
				return true;	// When should be false?
			}
		}

		public void NotifyMessage (bool start, IMessage msg, bool client_site, bool async)
		{
			ArrayList props = _properties;
			if (start)
			{
				foreach (DynamicPropertyReg reg in props)
					if (reg.Sink != null) reg.Sink.ProcessMessageStart (msg, client_site, async);
			}
			else
			{
				foreach (DynamicPropertyReg reg in props)
					if (reg.Sink != null) reg.Sink.ProcessMessageFinish (msg, client_site, async);
			}
		}

		int FindProperty (string name)
		{
			for (int n=0; n<_properties.Count; n++)
				if (((DynamicPropertyReg)_properties[n]).Property.Name == name)
					return n;
			return -1;
		}
	}
	
	class ContextCallbackObject: ContextBoundObject
	{
		public void DoCallBack (CrossContextDelegate deleg)
		{
		}
	}
}