Пример #1
0
        private Task RunServer <T>(ICommonSerializer serializer, Action <int> loadPort, Action <Poller> loadPoller, T service)
            where T : class
        {
            var task = new Task(() =>
            {
                var serverContext   = NetMQContext.Create();
                var serverSocket    = serverContext.CreateResponseSocket();             // serverContext.CreateRouterSocket();
                var serverPoller    = new Poller(serverSocket);
                var serverScheduler = new NetMQScheduler(serverContext, serverPoller);
                var serverTransport = serverSocket.GenerateTransportSource(serverScheduler);
                var serverRouter    = new DefaultMessageRouter(serverTransport, serializer);
                serverRouter.AddService(service);
                var port = serverSocket.BindRandomPort("tcp://localhost");
                loadPoller.Invoke(serverPoller);
                loadPort.Invoke(port);

                serverPoller.PollTillCancelled();

                serverScheduler.Dispose();
                serverPoller.Dispose();
                serverRouter.Dispose();
                serverTransport.Dispose();
                serverSocket.Dispose();
                serverContext.Dispose();
            }, TaskCreationOptions.LongRunning);

            task.Start();
            return(task);
        }
Пример #2
0
        private Task RunClient <T>(ICommonSerializer serializer, int port, Action <Poller> loadPoller, Action <T> loadProxy)
            where T : class
        {
            var task = new Task(() =>
            {
                var clientContext           = NetMQContext.Create();
                var clientSocket            = clientContext.CreateRequestSocket();     // clientContext.CreateRouterSocket();
                clientSocket.Options.Linger = TimeSpan.Zero;
                var clientPoller            = new Poller(clientSocket);
                var clientScheduler         = new NetMQScheduler(clientContext, clientPoller);
                var clientTransport         = clientSocket.GenerateTransportSource(clientScheduler);
                var clientRouter            = new DefaultMessageRouter(clientTransport, serializer);
                var proxy = clientRouter.AddInterface <T>();
                clientSocket.Connect("tcp://localhost:" + port);
                loadPoller.Invoke(clientPoller);
                loadProxy.Invoke(proxy);

                clientPoller.PollTillCancelled();

                clientScheduler.Dispose();
                clientPoller.Dispose();
                clientRouter.Dispose();
                clientTransport.Dispose();
                clientSocket.Dispose();
                clientContext.Dispose();
            }, TaskCreationOptions.LongRunning);

            task.Start();
            return(task);
        }
Пример #3
0
 public void Dispose()
 {
     _scheduler.Dispose();
     _client.Dispose();
     _poller.CancelAndJoin();
     _poller.Dispose();
     _context.Dispose();
 }
Пример #4
0
 public void Dispose()
 {
     scheduler.Dispose();
     clientSocket.Dispose();
     poller.Stop();
 }