public static INodeEndpointProtocolServer WaitForServer <T>( this INodeEndpointProtocolServerListener serverListener, string address, IDuplexNodeEndpoint <T> endpoint, int timeout = DefaultTimeout ) where T : INodeEndpointClient { INodeEndpointProtocolServer server = WaitForServerBase(serverListener, address, endpoint, timeout); if (server != null) { if (server.EnableDuplex) { INodeEndpointClientProvider provider = new ProtocolEnabledClientProvider(); provider.Protocol = server; T endpointInterface = StrongTypedNodeEndpointClientBuilder.Create <T>(provider); endpoint.Callback = endpointInterface; } else { server.Disconnect(); throw new InvalidOperationException("The protocol does not support duplex communication."); } server.BeginListen(); } return(server); }
static void Main(string[] args) { INodeEndpointProtocolFactory factory = new NamedPipeProtocolFactory(); INodeEndpointProtocolServerListener listener = factory.CreateServerListener(); Thread serverThread = new Thread(() => { while (true) { CommunicationService service = new CommunicationService(); INodeEndpointProtocolServer server = listener.WaitForServer("VczhCommunication", service); lock (loggedInServices) { loggedInServices.Add(Tuple.Create(server, service)); } SendMessageToAll("Someone logged in"); } }); serverThread.Start(); Console.WriteLine("Press [ENTER] to exit."); Console.Read(); serverThread.Abort(); }
public Server(INodeEndpointProtocolServer outerProtocol, ITranslatorProtocolHandlerFactory handlerFactory, ServerListener serverListener) : base((TranslatorProtocolFactory)serverListener.Factory) { this.serverListener = serverListener; this.serverHandler = handlerFactory.CreateServerHandler(); SetOuterProtocol(outerProtocol); this.Handler = this.serverHandler; this.serverHandler.AttachServer(this); }
protected virtual void InstallServerTracer(INodeEndpointProtocolServer server) { foreach (var listener in server.GetListeners()) { var acceptableListener = listener as INodeEndpointProtocolRequestTracableListener; if (acceptableListener != null) { acceptableListener.AddTracer(this.tracer); } } }
public INodeEndpointProtocolServer Listen(int timeout) { INodeEndpointProtocolServer server = this.outerServerListener.Listen(timeout); if (server == null) { return(null); } else { return(new Server(server, this.handlerFactory, this)); } }
public void SetOuterProtocol(INodeEndpointProtocolServer protocol) { if (this.outerProtocol != null) { this.outerProtocol.OnInnerProtocolSet(null); } this.outerProtocol = protocol; this.ParentProtocol = protocol; if (this.outerProtocol != null) { this.outerProtocol.OnInnerProtocolSet(this); } }
public static INodeEndpointProtocolServer WaitForServer( this INodeEndpointProtocolServerListener serverListener, string address, INodeEndpoint endpoint, int timeout = DefaultTimeout ) { INodeEndpointProtocolServer server = WaitForServerBase(serverListener, address, endpoint, timeout); if (server != null) { server.BeginListen(); } return(server); }
private static void ClearServices(List <INodeEndpointProtocolServer> servers, bool disconnectedOnly) { lock (servers) { for (int i = servers.Count - 1; i >= 0; i--) { INodeEndpointProtocolServer server = servers[i]; if (!disconnectedOnly || !server.Connected) { server.Disconnect(); servers.RemoveAt(i); } } } }
public static void TestProtocolAsync(INodeEndpointProtocolFactory serverFactory, INodeEndpointProtocolFactory clientFactory, string serverAddress, string clientAddress) { INodeEndpointProtocolServer server = null; Thread serverThread = new Thread(() => { INodeEndpointProtocolServerListener serverListener = serverFactory.CreateServerListener(); server = serverListener.WaitForServer(serverAddress, new CalculationEndpoint(true)); }); serverThread.Start(); ICalculationEndpointAsync client = clientFactory.WaitForClient <ICalculationEndpointAsync>(clientAddress, "Calculation"); Assert.IsNotNull(client); Assert.AreEqual(3, client.Add(2, 1).Result); Assert.AreEqual(1, client.Sub(2, 1).Result); Assert.AreEqual(2, client.Mul(2, 1).Result); Assert.AreEqual(2, client.Div(2, 1).Result); Point point = client.Swap(new Point { X = 1, Y = 2 }).Result; Assert.AreEqual(2, point.X); Assert.AreEqual(1, point.Y); Cat cat = (Cat)client.CopyAnimal(new Cat { name = "cat", catName = "bigcat" }).Result; Assert.AreEqual("cat", cat.name); Assert.AreEqual("bigcat", cat.catName); Dog dog = (Dog)client.CopyAnimal(new Dog { name = "dog", dogName = "bigdog" }).Result; Assert.AreEqual("dog", dog.name); Assert.AreEqual("bigdog", dog.dogName); client.SendMessage("Vczh is a genius!").Wait(); Assert.AreEqual("Vczh is a genius!", client.ReceiveMessage().Result); }
public static void TestProtocolDuplex(INodeEndpointProtocolFactory serverFactory, INodeEndpointProtocolFactory clientFactory, string serverAddress, string clientAddress) { INodeEndpointProtocolServer server = null; Thread serverThread = new Thread(() => { INodeEndpointProtocolServerListener listener = serverFactory.CreateServerListener(); server = listener.WaitForServer(serverAddress, new DuplexServer()); }); serverThread.Start(); IDuplexServer client = clientFactory.WaitForClient <IDuplexServer, DuplexCallback>(clientAddress, "DuplexServer", new DuplexCallback()); Assert.IsNotNull(client); Assert.AreEqual(15, client.Add(5)); }
private void ClearServices(bool disconnectedOnly) { lock (this.runningServices) { for (int i = this.runningServices.Count - 1; i >= 0; i--) { var pair = this.runningServices[i]; T endpoint = pair.Item1; INodeEndpointProtocolServer server = pair.Item2; if (!disconnectedOnly || !server.Connected) { this.callback.OnEndpointStopped(endpoint, server); server.Disconnect(); endpoint.Dispose(); this.runningServices.RemoveAt(i); } } } }
private static INodeEndpointProtocolServer WaitForServerBase( this INodeEndpointProtocolServerListener serverListener, string address, INodeEndpoint endpoint, int timeout ) { if (!serverListener.Connected) { serverListener.Connect(address, endpoint.EndpointName); } INodeEndpointProtocolServer server = serverListener.Listen(timeout); if (server != null) { INodeEndpointProtocolRequestListener endpointListener = new ProtocolEnabledRequestListener(endpoint); server.AddListener(endpointListener); } return(server); }
private static void PortListeningThreadProc(int port, INodeEndpointProtocolServerListener serverListener) { List <INodeEndpointProtocolServer> servers = new List <INodeEndpointProtocolServer>(); try { while (true) { if (!serverListener.Connected) { serverListener.Connect(port.ToString(), TcpShareProviderServiceConfiguration.EndpointName); } if (!serverListener.Connected) { lock (portListeningThreads) { serverListener.Disconnect(); portListeningThreads.Remove(port); break; } } INodeEndpointProtocolServer server = serverListener.Listen(NodeEndpointProtocolFactoryExtension.DefaultTimeout); if (server != null) { TcpShareProviderServiceRedirector redirector = new TcpShareProviderServiceRedirector(port, outFactory, server); server.AddListener(redirector); server.BeginListen(); servers.Add(server); } ClearServices(servers, true); } } catch (ThreadAbortException) { } ClearServices(servers, false); }
private void ServerListenerThreadProc() { while (!this.needToStop) { T endpoint = this.callback.CreateEndpoint(); INodeEndpointProtocolServer server = CreateProtocolServer(endpoint, NodeEndpointProtocolFactoryExtension.DefaultTimeout); if (server == null) { endpoint.Dispose(); } else { InstallServerTracer(server); lock (this.runningServices) { this.runningServices.Add(Tuple.Create(endpoint, server)); this.callback.OnEndpointStart(endpoint, server); } } ClearServices(true); } }
public virtual void SetOuterProtocol(INodeEndpointProtocolServer protocol) { throw new InvalidOperationException("The protocol server cannot have a outer protocol."); }
public void OnEndpointStopped(Endpoint endpoint, INodeEndpointProtocolServer protocolServer) { Interlocked.Increment(ref this.endpointStoppedCounter); }
public void OnEndpointStopped(ExpressionService endpoint, INodeEndpointProtocolServer protocolServer) { }
public TcpShareProviderServiceRedirector(int port, NamedPipeProtocolFactory protocolFactory, INodeEndpointProtocolServer server) { this.protocolFactory = protocolFactory; this.server = server; this.port = port; }
public ResponseListener(INodeEndpointProtocolServer server) { this.server = server; }
public static void TestProtocol(INodeEndpointProtocolFactory serverFactory, INodeEndpointProtocolFactory clientFactory, string serverAddress, string clientAddress) { INodeEndpointProtocolServer server = null; Thread serverThread = new Thread(() => { INodeEndpointProtocolServerListener serverListener = serverFactory.CreateServerListener(); server = serverListener.WaitForServer(serverAddress, new CalculationEndpoint(true)); }); serverThread.Start(); ICalculationEndpoint client = clientFactory.WaitForClient <ICalculationEndpoint>(clientAddress, "Calculation"); Assert.IsNotNull(client); Assert.AreEqual(3, client.Add(2, 1)); Assert.AreEqual(1, client.Sub(2, 1)); Assert.AreEqual(2, client.Mul(2, 1)); Assert.AreEqual(2, client.Div(2, 1)); Point point = client.Swap(new Point { X = 1, Y = 2 }); Assert.AreEqual(2, point.X); Assert.AreEqual(1, point.Y); Cat cat = (Cat)client.CopyAnimal(new Cat { name = "cat", catName = "bigcat" }); Assert.AreEqual("cat", cat.name); Assert.AreEqual("bigcat", cat.catName); Dog dog = (Dog)client.CopyAnimal(new Dog { name = "dog", dogName = "bigdog" }); Assert.AreEqual("dog", dog.name); Assert.AreEqual("bigdog", dog.dogName); client.SendMessage("Vczh is a genius!"); Assert.AreEqual("Vczh is a genius!", client.ReceiveMessage()); AssertCollection(client.CopyArray(Enumerable.Range(0, 10).ToArray())); AssertCollection(client.CopyList(new List <int>(Enumerable.Range(0, 10)))); AssertCollection(client.CopyHashSet(new HashSet <int>(Enumerable.Range(0, 10)))); AssertCollection(client.CopyLinkedList(new LinkedList <int>(Enumerable.Range(0, 10)))); AssertCollection(client.CopyQueue(new Queue <int>(Enumerable.Range(0, 10)))); AssertCollection(client.CopySortedSet(new SortedSet <int>(Enumerable.Range(0, 10)))); AssertCollection(client.CopyStack(new Stack <int>(Enumerable.Range(0, 10).Reverse()))); Dictionary <int, int> dictionary = Enumerable.Range(0, 10).ToDictionary(i => i); AssertCollection(client.CopyDictionary(dictionary)); AssertCollection(client.CopySortedDictionary(new SortedDictionary <int, int>(dictionary))); AssertCollection(client.CopySortedList(new SortedList <int, int>(dictionary))); byte[] bytes = new byte[] { 1, 2, 3, 4, 5 }; using (Stream stream = client.CopyStream(bytes)) { byte[] copied = stream.ReadAllBytes(); Assert.AreEqual("[1][2][3][4][5]", bytes.Select(b => "[" + b.ToString() + "]").Aggregate("", (a, b) => a + b)); } }
public void OnEndpointStopped(GuardService endpoint, INodeEndpointProtocolServer protocolServer) { this.sharedData.Unregister(endpoint); }
public void OnEndpointStart(GuardService endpoint, INodeEndpointProtocolServer protocolServer) { }
public virtual void OnInnerProtocolSet(INodeEndpointProtocolServer protocol) { this.innerProtocol = protocol; }
public void AttachServer(INodeEndpointProtocolServer server) { }
public void AttachServer(INodeEndpointProtocolServer server) { this.server = server; }
public void OnEndpointStopped(CalculationService endpoint, INodeEndpointProtocolServer protocolServer) { }