Beispiel #1
0
        static Application()
        {
            // patch the awaiter..
            //Console.SetOut(new xConsole());

            HopToIFrame.VirtualOnCompleted =
                continuation =>
            {
                Console.WriteLine("enter VirtualOnCompleted..");

                Action <ShadowIAsyncStateMachine> MoveNext = null;

                var s = ShadowIAsyncStateMachine.FromContinuation(continuation, ref MoveNext);

                // we want to run in it!
                new IHTMLIFrame
                {
                    allowTransparency = true,
                    frameBorder       = "0",

                    src = Native.document.location.href
                }.With(
                    async f =>
                {
                    f.style.width  = "100%";
                    f.style.height = "30em";

                    var sw = Stopwatch.StartNew();

                    //var mm = f.contentWindow.async.onmessage;
                    await f.async.onload;

                    Console.WriteLine("loaded in " + new { sw.ElapsedMilliseconds });

                    // loaded in {{ ElapsedMilliseconds = 2417 }}

                    //var m = await f.contentWindow.async.onmessage;
                    //var m = await Native.window.async.onmessage;
                    //var m = await f.ownerDocument.defaultView.async.onmessage;
                    //var m = await f.ownerDocument.defaultView.async.onmessage;
                    var m = await f.async.onmessage;

                    //var m = await mm;

                    Console.WriteLine("onmessage in " + new { sw.ElapsedMilliseconds });

                    // wait for response?
                    m.postMessage(s);
                }
                    ).AttachToDocument();
            };
        }
 static Application()
 {
     // patch the awaiter..
     //Console.SetOut(new xConsole());
     HopToService.VirtualOnCompleted =
         continuation =>
     {
         Console.WriteLine("enter VirtualOnCompleted..");
         Action <ShadowIAsyncStateMachine> MoveNext = null;
         var s         = ShadowIAsyncStateMachine.FromContinuation(continuation, ref MoveNext);
         var MoveNext1 = MoveNext;
         new ApplicationWebService {
         }.Invoke(s).ContinueWith(
             x =>
         {
             var sNext = x.Result;
             Console.WriteLine("exit VirtualOnCompleted.. " + new { sNext.state });
             MoveNext1(sNext);
         }
             );
     };
 }
		public Task<ShadowIAsyncStateMachine> Invoke(ShadowIAsyncStateMachine that) =>
			CLRWebServiceInvoke.Invoke(
				typeof(ApplicationWebService),
				that
			);
		public async Task<ShadowIAsyncStateMachine> Invoke(ShadowIAsyncStateMachine that)
		{
			var xAsyncStateMachineTypeName = that.TypeName;


			// do we trust the caller?
			// can we jump?

			// is the typename RSA signed?
			Console.WriteLine(new { that.state, that.TypeName });

			Console.WriteLine("looking for the type...");

			var xAsyncStateMachineType = typeof(SharedProgram).Assembly.GetTypes().FirstOrDefault(
				x =>
				{
					// safety check 1
					if (!(typeof(IAsyncStateMachine).IsAssignableFrom(x)))
						return false;

					// we need a rsa lookup table?
					// or a special one time pad?

					Console.WriteLine(new { x.FullName, x.Name });

					// js wont know declaringtype name
					return ("<Namespace>." + x.Name).Replace("-", "_").Replace("+", "_").Replace("<", "_").Replace(">", "_")
					== xAsyncStateMachineTypeName.Replace("-", "_").Replace("+", "_").Replace("<", "_").Replace(">", "_");
				}
			);

			//{ state = 0, TypeName = <Namespace>._Invoke_d__3 }
			//looking for the type...
			//{ FullName = <>f__AnonymousType6`2 }
			//{ FullName = <>f__AnonymousType4`1 }
			//{ FullName = <>f__AnonymousType7`2 }
			//{ FullName = <>f__AnonymousType2`2 }
			//{ FullName = <>f__AnonymousType3`1 }
			//{ FullName = <>f__AnonymousType5`1 }
			//{ FullName = <>f__AnonymousType0`2 }
			//{ FullName = <>f__AnonymousType11`1 }
			//{ FullName = <>f__AnonymousType8 }
			//{ FullName = <>f__AnonymousType9`1 }
			//{ FullName = <>f__AnonymousType10`1 }
			//{ FullName = <>f__AnonymousType1`1 }
			//{ FullName = TestSwitchToServiceContextAsync.ApplicationWebService }
			//{ FullName = TestSwitchToServiceContextAsync.HopFromService }
			//{ FullName = TestSwitchToServiceContextAsync.HopToService }
			//{ FullName = TestSwitchToServiceContextAsync.Application }
			//{ FullName = TestSwitchToServiceContextAsync.ShadowIAsyncStateMachine }
			//{ FullName = TestSwitchToServiceContextAsync.SharedProgram }
			//{ FullName = TestSwitchToServiceContextAsync.xConsole }
			//{ FullName = TestSwitchToServiceContextAsync.Program }
			//{ FullName = TestSwitchToServiceContextAsync.ApplicationWebService+<>c__DisplayClass1_0 }
			//{ FullName = TestSwitchToServiceContextAsync.ApplicationWebService+<Invoke>d__1 }
			//{ FullName = TestSwitchToServiceContextAsync.Application+<>c__DisplayClass0_0 }
			//{ FullName = TestSwitchToServiceContextAsync.Application+<>c__DisplayClass0_1 }
			//{ FullName = TestSwitchToServiceContextAsync.Application+<>c }
			//{ FullName = TestSwitchToServiceContextAsync.Application+<<-ctor>b__1_0>d }
			//{ FullName = TestSwitchToServiceContextAsync.SharedProgram+<Invoke>d__3 }
			//{ FullName = TestSwitchToServiceContextAsync.Application+<>c__DisplayClass0_0+<<-cctor>b__3>d }

			// xAsyncStateMachineType = {Name = "<Invoke>d__3" FullName = "TestSwitchToServiceContextAsync.SharedProgram+<Invoke>d__3"}

			var NewStateMachine = Activator.CreateInstance(xAsyncStateMachineType);
			var NewStateMachineI = NewStateMachine as IAsyncStateMachine;

			#region 1__state
			xAsyncStateMachineType.GetFields(
					  System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance
				  ).WithEach(
				   AsyncStateMachineSourceField =>
				   {
					   if (AsyncStateMachineSourceField.Name.EndsWith("1__state"))
					   {
						   AsyncStateMachineSourceField.SetValue(
							   NewStateMachineI,
							   that.state
							);

					   }
				   }
			  );
			#endregion

			var s = default(ShadowIAsyncStateMachine);

			var reset = new AutoResetEvent(false);

			// we only care about the new thread we will be creating... skip other threads if any?
			// or should we care about sync context?
			HopFromService.VirtualOnCompleted =
				(Action continuation) =>
				{
					Action<ShadowIAsyncStateMachine> MoveNext = null;
					s = ShadowIAsyncStateMachine.FromContinuation(continuation, ref MoveNext);

					// should be the same state machine!
					Console.WriteLine("time to jump back? yes " + new { s.state, s.TypeName });

					reset.Set();
				};



			// are we on a chrome server?
			new Thread(
				delegate ()
				{
					// this will take step into next await.
					NewStateMachineI.MoveNext();
				}
			).Start();


			reset.WaitOne();

			Console.WriteLine("time to jump back? " + new { xAsyncStateMachineType, that.state });

			return s;
		}
Beispiel #5
0
 public Task <ShadowIAsyncStateMachine> Invoke(ShadowIAsyncStateMachine that) =>
 CLRWebServiceInvoke.Invoke(
     typeof(ApplicationWebService),
     that
     );
		public static async Task<ShadowIAsyncStateMachine> Invoke(Type service, ShadowIAsyncStateMachine that)
		{
			Console.WriteLine("looking for the type...");

			Func<string, string> DecoratedString =
				x => x.Replace("-", "_").Replace("+", "_").Replace("<", "_").Replace(">", "_");

			var xAsyncStateMachineType = service.Assembly.GetTypes().FirstOrDefault(
				x =>
				{
					// safety check 1
					if (!(typeof(IAsyncStateMachine).IsAssignableFrom(x)))
						return false;

					// we need a rsa lookup table?
					// or a special one time pad?

					Console.WriteLine(new { x.FullName, x.Name });

					// js wont know declaringtype name
					return DecoratedString("<Namespace>." + x.Name) == DecoratedString(that.TypeName);
				}
			);

			var NewStateMachine = FormatterServices.GetUninitializedObject(xAsyncStateMachineType);
			//var NewStateMachine = Activator.CreateInstance(xAsyncStateMachineType);
			var NewStateMachineI = NewStateMachine as IAsyncStateMachine;

			#region 1__state
			xAsyncStateMachineType.GetFields(
					  System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance
				  ).WithEach(
				   AsyncStateMachineSourceField =>
				   {
					   // we need to populate the data for the debugger?

					   //var SourceField_value = AsyncStateMachineSourceField.GetValue(NewStateMachine);

					   // it is a new type.
					   Console.WriteLine(new { AsyncStateMachineSourceField });

					   if (AsyncStateMachineSourceField.Name.EndsWith("1__state"))
					   {
						   AsyncStateMachineSourceField.SetValue(
							   NewStateMachineI,
							   that.state
							);
					   }

					   // X:\jsc.svn\examples\javascript\async\Test\TestSwitchToServiceContextAsync\TestSwitchToServiceContextAsync\CLRWebServiceInvoke.cs
					   // field names/ tokens need to be encrypted like typeinfo.

					   // some do manual restore
					   // X:\jsc.svn\examples\javascript\chrome\extensions\ChromeExtensionHopToTabThenIFrame\ChromeExtensionHopToTabThenIFrame\Application.cs

					   // or, are we supposed to initialize a string value here?
					   var xStringField = that.StringFields.AsEnumerable().FirstOrDefault(
						   f => DecoratedString(f.FieldName) == DecoratedString(AsyncStateMachineSourceField.Name)
					   );

					   if (xStringField != null)
					   {
						   // once we are to go back to client. we need to reverse it?

						   AsyncStateMachineSourceField.SetValue(
							   NewStateMachineI,
							   xStringField.value
							);
						   // next xml?
						   // before lets send our strings back with the new state!
						   // what about exceptions?
					   }
				   }
			  );
			#endregion

			var s = default(ShadowIAsyncStateMachine);

			var reset = new AutoResetEvent(false);

			// we only care about the new thread we will be creating... skip other threads if any?
			// or should we care about sync context?
			HopFromService.VirtualOnCompleted =
				(Action continuation) =>
				{
					Action<ShadowIAsyncStateMachine> MoveNext = null;
					s = ShadowIAsyncStateMachine.FromContinuation(continuation, ref MoveNext);

					// should be the same state machine!
					Console.WriteLine("time to jump back? yes " + new { s.state, s.TypeName });

					reset.Set();
				};



			// are we on a chrome server?
			new Thread(
				delegate ()
				{
					// this will take step into next await.
					NewStateMachineI.MoveNext();
				}
			).Start();


			reset.WaitOne();

			Console.WriteLine("time to jump back? " + new { xAsyncStateMachineType, that.state });

			return s;
		}