public void SubscribeBeforeAttachOutputChannel() { RpcFactory anRpcFactory = new RpcFactory(mySerializer); HelloService aService = new HelloService(); IRpcService <IHello> anRpcService = anRpcFactory.CreateSingleInstanceService <IHello>(aService); IRpcClient <IHello> anRpcClient = anRpcFactory.CreateClient <IHello>(); try { IHello aServiceProxy = anRpcClient.Proxy; AutoResetEvent aClientConnected = new AutoResetEvent(false); anRpcClient.ConnectionOpened += (x, y) => { aClientConnected.Set(); }; AutoResetEvent anEventReceived = new AutoResetEvent(false); Action <object, EventArgs> anEventHandler = (x, y) => { anEventReceived.Set(); }; // Subscribe before the connection is open. aServiceProxy.Close += anEventHandler.Invoke; // Open the connection. anRpcService.AttachDuplexInputChannel(myMessaging.CreateDuplexInputChannel(myChannelId)); anRpcClient.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId)); // Wait until the client is connected. aClientConnected.WaitOne(); // Raise the event in the service. aService.RaiseClose(); Assert.IsTrue(anEventReceived.WaitOne()); // Unsubscribe. aServiceProxy.Close -= anEventHandler.Invoke; // Try to raise again. aService.RaiseClose(); Assert.IsFalse(anEventReceived.WaitOne(1000)); } finally { if (anRpcClient.IsDuplexOutputChannelAttached) { anRpcClient.DetachDuplexOutputChannel(); } if (anRpcService.IsDuplexInputChannelAttached) { anRpcService.DetachDuplexInputChannel(); } } }
public void DynamicRpcNonGenericEvent() { RpcFactory anRpcFactory = new RpcFactory(mySerializer); HelloService aService = new HelloService(); IRpcService <IHello> anRpcService = anRpcFactory.CreateSingleInstanceService <IHello>(aService); IRpcClient <IHello> anRpcClient = anRpcFactory.CreateClient <IHello>(); try { anRpcService.AttachDuplexInputChannel(myMessaging.CreateDuplexInputChannel(myChannelId)); anRpcClient.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId)); AutoResetEvent anEventReceived = new AutoResetEvent(false); EventHandler <EventArgs> anEventHandler = (x, y) => { anEventReceived.Set(); }; // Subscribe. anRpcClient.SubscribeRemoteEvent <EventArgs>("Close", anEventHandler); // Raise the event in the service. aService.RaiseClose(); Assert.IsTrue(anEventReceived.WaitOne()); // Unsubscribe. anRpcClient.UnsubscribeRemoteEvent("Close", anEventHandler); // Try to raise again. aService.RaiseClose(); Assert.IsFalse(anEventReceived.WaitOne(1000)); } finally { if (anRpcClient.IsDuplexOutputChannelAttached) { anRpcClient.DetachDuplexOutputChannel(); } if (anRpcService.IsDuplexInputChannelAttached) { anRpcService.DetachDuplexInputChannel(); } } }
public void RpcNonGenericEvent_10000() { RpcFactory anRpcFactory = new RpcFactory(mySerializer); HelloService aService = new HelloService(); IRpcService <IHello> anRpcService = anRpcFactory.CreateSingleInstanceService <IHello>(aService); IRpcClient <IHello> anRpcClient = anRpcFactory.CreateClient <IHello>(); try { anRpcService.AttachDuplexInputChannel(myMessaging.CreateDuplexInputChannel(myChannelId)); anRpcClient.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId)); IHello aServiceProxy = anRpcClient.Proxy; int aCounter = 0; AutoResetEvent anEventReceived = new AutoResetEvent(false); Action <object, EventArgs> anEventHandler = (x, y) => { ++aCounter; if (aCounter == 10000) { anEventReceived.Set(); } }; // Subscribe. aServiceProxy.Close += anEventHandler.Invoke; Stopwatch aStopWatch = new Stopwatch(); aStopWatch.Start(); // Raise the event in the service. for (int i = 0; i < 10000; ++i) { aService.RaiseClose(); } Assert.IsTrue(anEventReceived.WaitOne()); aStopWatch.Stop(); Console.WriteLine("Remote event. Elapsed time = " + aStopWatch.Elapsed); // Unsubscribe. aServiceProxy.Close -= anEventHandler.Invoke; } finally { if (anRpcClient.IsDuplexOutputChannelAttached) { anRpcClient.DetachDuplexOutputChannel(); } if (anRpcService.IsDuplexInputChannelAttached) { anRpcService.DetachDuplexInputChannel(); } } }
public void MultipleClients_RemoteEvent_10() { //EneterTrace.DetailLevel = EneterTrace.EDetailLevel.Debug; //EneterTrace.StartProfiler(); HelloService aService = new HelloService(); RpcFactory anRpcFactory = new RpcFactory(mySerializer); IRpcService <IHello> anRpcService = anRpcFactory.CreateSingleInstanceService <IHello>(aService); IRpcClient <IHello>[] aClients = new IRpcClient <IHello> [10]; for (int i = 0; i < aClients.Length; ++i) { aClients[i] = anRpcFactory.CreateClient <IHello>(); } try { anRpcService.AttachDuplexInputChannel(myMessaging.CreateDuplexInputChannel(myChannelId)); // Clients open connection. foreach (IRpcClient <IHello> aClient in aClients) { aClient.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId)); } // Subscribe to remote event from the service. AutoResetEvent anOpenReceived = new AutoResetEvent(false); AutoResetEvent aCloseReceived = new AutoResetEvent(false); AutoResetEvent anAllCleintsSubscribed = new AutoResetEvent(false); int anOpenCounter = 0; int aCloseCounter = 0; int aSubscribedClientCounter = 0; object aCounterLock = new object(); foreach (IRpcClient <IHello> aClient in aClients) { IRpcClient <IHello> aClientTmp = aClient; ThreadPool.QueueUserWorkItem(xx => { aClientTmp.Proxy.Open += (x, y) => { lock (aCounterLock) { ++anOpenCounter; if (anOpenCounter == aClients.Length) { anOpenReceived.Set(); } } }; aClientTmp.Proxy.Close += (x, y) => { lock (aCounterLock) { ++aCloseCounter; if (aCloseCounter == aClients.Length) { aCloseReceived.Set(); } } }; lock (aCounterLock) { ++aSubscribedClientCounter; if (aSubscribedClientCounter == aClients.Length) { anAllCleintsSubscribed.Set(); } } Thread.Sleep(1); }); } // Wait until all clients are subscribed. anAllCleintsSubscribed.WaitOne(); // Servicde raises two different events. OpenArgs anOpenArgs = new OpenArgs() { Name = "Hello" }; aService.RaiseOpen(anOpenArgs); aService.RaiseClose(); anOpenReceived.WaitOne(); aCloseReceived.WaitOne(); } finally { foreach (IRpcClient <IHello> aClient in aClients) { aClient.DetachDuplexOutputChannel(); } if (anRpcService.IsDuplexInputChannelAttached) { anRpcService.DetachDuplexInputChannel(); } } }