public void DynamicRpcGenericEvent() { 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); string aReceivedEvent = ""; EventHandler <OpenArgs> anEventHandler = (x, y) => { aReceivedEvent = y.Name; anEventReceived.Set(); }; // Subscribe. anRpcClient.SubscribeRemoteEvent("Open", anEventHandler); // Raise the event in the service. OpenArgs anOpenArgs = new OpenArgs() { Name = "Hello" }; aService.RaiseOpen(anOpenArgs); Assert.IsTrue(anEventReceived.WaitOne()); Assert.AreEqual("Hello", aReceivedEvent); // Unsubscribe. anRpcClient.UnsubscribeRemoteEvent("Open", anEventHandler); // Try to raise again. aService.RaiseOpen(anOpenArgs); Assert.IsFalse(anEventReceived.WaitOne(1000)); } finally { if (anRpcClient.IsDuplexOutputChannelAttached) { anRpcClient.DetachDuplexOutputChannel(); } if (anRpcService.IsDuplexInputChannelAttached) { anRpcService.DetachDuplexInputChannel(); } } }
public void RpcGenericEvent() { 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; AutoResetEvent anEventReceived = new AutoResetEvent(false); Action <object, OpenArgs> anEventHandler = (x, y) => { anEventReceived.Set(); }; // Subscribe. aServiceProxy.Open += anEventHandler.Invoke; // Raise the event in the service. OpenArgs anOpenArgs = new OpenArgs() { Name = "Hello" }; aService.RaiseOpen(anOpenArgs); Assert.IsTrue(anEventReceived.WaitOne()); // Unsubscribe. aServiceProxy.Open -= anEventHandler.Invoke; // Try to raise again. aService.RaiseOpen(anOpenArgs); Assert.IsFalse(anEventReceived.WaitOne(1000)); } 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(); } } }
public void RpcGenericEvent_SerializerPerClient() { string aFirstClientId = null; RpcFactory anRpcClientFactory1 = new RpcFactory(new XmlStringSerializer()); RpcFactory anRpcClientFactory2 = new RpcFactory(new BinarySerializer()); RpcFactory anRpcServiceFactory = new RpcFactory() { SerializerProvider = x => (x == aFirstClientId) ? (ISerializer) new XmlStringSerializer() : (ISerializer) new BinarySerializer() }; HelloService aService = new HelloService(); IRpcService <IHello> anRpcService = anRpcServiceFactory.CreateSingleInstanceService <IHello>(aService); anRpcService.ResponseReceiverConnected += (x, y) => aFirstClientId = aFirstClientId ?? y.ResponseReceiverId; IRpcClient <IHello> anRpcClient1 = anRpcClientFactory1.CreateClient <IHello>(); IRpcClient <IHello> anRpcClient2 = anRpcClientFactory2.CreateClient <IHello>(); try { anRpcService.AttachDuplexInputChannel(myMessaging.CreateDuplexInputChannel(myChannelId)); anRpcClient1.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId, "Client1")); anRpcClient2.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId, "Client2")); AutoResetEvent anEvent1Received = new AutoResetEvent(false); Action <object, OpenArgs> anEventHandler1 = (x, y) => { anEvent1Received.Set(); }; AutoResetEvent anEvent2Received = new AutoResetEvent(false); Action <object, OpenArgs> anEventHandler2 = (x, y) => { anEvent2Received.Set(); }; // Subscribe. anRpcClient1.Proxy.Open += anEventHandler1.Invoke; anRpcClient2.Proxy.Open += anEventHandler2.Invoke; // Raise the event in the service. OpenArgs anOpenArgs = new OpenArgs() { Name = "Hello" }; aService.RaiseOpen(anOpenArgs); anEvent1Received.WaitIfNotDebugging(1000); anEvent2Received.WaitIfNotDebugging(1000); // Unsubscribe. anRpcClient1.Proxy.Open -= anEventHandler1.Invoke; anRpcClient2.Proxy.Open -= anEventHandler2.Invoke; // Try to raise again. aService.RaiseOpen(anOpenArgs); Assert.IsFalse(anEvent1Received.WaitOne(1000)); Assert.IsFalse(anEvent2Received.WaitOne(1000)); } finally { if (anRpcClient1.IsDuplexOutputChannelAttached) { anRpcClient1.DetachDuplexOutputChannel(); } if (anRpcClient2.IsDuplexOutputChannelAttached) { anRpcClient2.DetachDuplexOutputChannel(); } if (anRpcService.IsDuplexInputChannelAttached) { anRpcService.DetachDuplexInputChannel(); } } }