Example #1
0
    public bool sendAndReceive(ref NetMQ.Msg req_msg, ref NetMQ.Msg resp_msg)
    {
        if (socket == null)
        {
            reconnect();
        }

        StdMessage m = new StdMessage(req_msg.Data[1], req_msg.Data[2]);

        NetMQ.Msg copy = new NetMQ.Msg();
        bool      ok   = false;

        for (uint i = 0; i < retries; i++)
        {
            copy.Copy(ref req_msg);

            // send
            if (!socket.TrySend(ref copy, TimeSpan.FromMilliseconds(min_trysend_ms), false))
            {
                ok = false;
                UnityEngine.Debug.Log("ReliableExternalClient: could not send");
                break;

                //TODO: clear enqueued messages when server is offline
            }
            //UnityEngine.Debug.Log("ReliableExternalClient: request sent " + m.to_string());

            // receive
            if (socket.TryReceive(ref resp_msg, timeout))
            {
                ok = true;
                // UnityEngine.Debug.Log("ReliableExternalClient: response received "
                //+ new StdMessage(resp_msg.Data[1], resp_msg.Data[2]).to_string());
                break;
            }

            //UnityEngine.Debug.Log(String.Format("ReliableExternalClient: no response from server {0}. Retrying", srvAddress));
            reconnect();
        }

        if (!ok)
        {
            UnityEngine.Debug.Log(String.Format("ReliableExternalClient: server {0} seems to be offline. Abandoning", srvAddress));
        }

        copy.Close();
        socket.Dispose();  //call Dispose on all sockets before cleanup the netmq context
        socket.Close();

        socket = null;

        if (socket == null)
        {
            UnityEngine.Debug.Log("ReliableExternalClient: socket closed and null");
        }

        return(ok);
    }
Example #2
0
        // on message pushed to me.
        private void OnPull()
        {
            while (!this.token_pp.IsCancellationRequested)
            {
                puller.Poll(TimeSpan.FromMilliseconds(100));
                //try
                //{
                //    while (hasMore)
                //    {
                //        sb.Append(Encoding.UTF8.GetString(this.puller.Receive(SendReceiveOptions.DontWait, out hasMore)));
                //    }

                //    string msg = sb.ToString();
                //    sb.Clear();
                //    if (msg.Length > 0)
                //    {
                //        pullMsgHandler(msg);
                //    }
                //}
                //catch (Exception ce)
                //{
                //    Console.WriteLine("OnPull Error: {0}", ce.Message);
                //    log.Error("Pulling Message error: ", ce);
                //}
            }
            log.Debug("Puller Closed");
            puller.Close();
        }
Example #3
0
 public void Stop()
 {
     poller.Stop();
     poller.Dispose();
     router.ReceiveReady -= RouterOnReceiveReady;
     router.Close();
 }
Example #4
0
        /// <summary>
        ///     Stop the server.
        /// </summary>
        public void StopServer()
        {
            if (!ServerRunning)
            {
                return;
            }

            _poller?.Stop();
            _poller?.Dispose();

            lock (_socketLock)
            {
                if (_socket != null)
                {
                    try
                    {
                        _socket.Disconnect(_connectionString);
                    }
                    finally
                    {
                        _socket.ReceiveReady -= SocketReceiveReady;
                        _socket.Close();
                        _socket = null;
                    }
                }
            }

            _poller = null;
        }
Example #5
0
        /// <summary>
        /// Stops the server
        /// </summary>
        public void Stop()
        {
            isDone = true;
            listenerThread.Join();

            netMqSocket.Unbind(serverConnectionString);
            netMqSocket.Close();
        }
Example #6
0
        /// <summary>
        /// Stops the Client
        /// </summary>
        public void Stop()
        {
            isDone = true;
            listenerThread.Join();

            clientSocket.Disconnect(clientConnectionString);
            clientSocket.Close();
        }
Example #7
0
 public void Stop()
 {
     _running = false;
     if (_socket != null && _context != null)
     {
         _socket.Close();
         _context.Dispose();
     }
 }
Example #8
0
        public void Stop(bool waitForCloseToComplete = true)
        {
            if (m_pollerIsOwned && m_poller.IsStarted)
            {
                m_poller.Stop(waitForCloseToComplete);
            }

            FrontendSocket.Close();
            BackendSocket.Close();
            IsRunning = false;
        }
Example #9
0
            protected void socketThread()
            {
                bool is_crashed = false;

                reportLog("Socket thread started", LogType.INFO, section);
                try
                {
                    while (thread_exit == false)
                    {
                        if (socket != null)
                        {
                            _CheckState();
                            _Receive();
                            _Send();
                            Thread.Sleep(1);
                        }
                        else
                        {
                            _SocketBuilder();
                            Thread.Sleep(100);
                        }
                    }
                }
                catch (Exception e)
                {
                    is_crashed = true;
                    reportLog(e.Message, LogType.ERROR, section);
                }
                finally
                {
                    reportLog("Thread exited clearly for " + Name, LogType.INFO, section);
                    reportLog("Exit params " + thread_exit + " " + (socket == null) + " " + is_crashed, LogType.INFO, section);

                    StopMonitor();
                    reportLog("Monitor stopped.", LogType.INFO, section);

                    threadExitProcess();

                    if (socket != null)
                    {
                        socket.Close();
                    }

                    if (is_crashed)
                    {
                        Dispose(false);
                        reportLog("Reporting crash", LogType.INFO, section);
                        delegateCrashed?.Invoke(Id);
                        reportLog("Crash reported", LogType.INFO, section);
                    }
                }
            }
 public void Dispose()
 {
     if (frontend != null)
     {
         frontend.Close();
         frontend.Dispose();
     }
     if (backend != null)
     {
         backend.Close();
         backend.Dispose();
     }
 }
Example #11
0
        public static void Blow(string song, string notes)
        {
            new Thread(() => {
                using (NetMQSocket request_socket = context.CreateRequestSocket()) {
                    request_socket.Connect(string.Format("tcp://{0}:{1}", Address, RequestPort));
                    request_socket.SendMore("blow").SendMore(song).Send(notes);

                    string response = request_socket.ReceiveString();
                    Console.WriteLine("[request_socket] Response: {0}", response);

                    request_socket.Close();
                    request_socket.Dispose();
                }
            }).Start();
        }
Example #12
0
        internal static void Stop(this NetMQSocket socket, string address, SocketType socketType)
        {
            switch (socketType)
            {
            case SocketType.Client:
                socket.Disconnect(address);
                break;

            case SocketType.Server:
                socket.Unbind(address);
                break;

            default:
                throw new ArgumentException(string.Format("Unknown SocketType {0}", socketType), "socketType");
            }
            socket.Close();
        }
Example #13
0
        /// <summary>
        ///     Stops the server.
        /// </summary>
        public void StopServer()
        {
            if (!ServerRunning)
            {
                return;
            }

            poller?.Stop();
            poller?.Dispose();

            lock (publisherSocketLock)
            {
                if (publisherSocket != null)
                {
                    try
                    {
                        publisherSocket.Disconnect(publisherConnectionString);
                    }
                    finally
                    {
                        publisherSocket.Close();
                        publisherSocket = null;
                    }
                }
            }

            lock (requestSocketLock)
            {
                if (requestSocket != null)
                {
                    try
                    {
                        requestSocket.Disconnect(requestConnectionString);
                    }
                    finally
                    {
                        requestSocket.ReceiveReady -= RequestSocketReceiveReady;
                        requestSocket.Close();
                        requestSocket = null;
                    }
                }
            }

            poller = null;
        }
Example #14
0
        /// <summary>
        /// Stop the device and safely close the underlying sockets.
        /// </summary>
        /// <param name="waitForCloseToComplete">If true, this method will block until the
        /// underlying poller is fully stopped. Defaults to true.</param>
        public void Stop(bool waitForCloseToComplete = true)
        {
            if (m_pollerIsOwned && m_poller.IsRunning)
            {
                if (waitForCloseToComplete)
                {
                    m_poller.Stop();
                }
                else
                {
                    m_poller.StopAsync();
                }
            }

            FrontendSocket.Close();
            BackendSocket.Close();
            IsRunning = false;
        }
        private void SendEvents(String submissionUri, CancellationToken cancellationToken)
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                using (NetMQSocket pushSocket = context.CreatePushSocket())
                {
                    pushSocket.IgnoreErrors = true;
                    pushSocket.Connect(submissionUri);

                    Logger.Info("ZeroMQCache: Connected to submissionUri \"{0}\".", submissionUri);

                    while (!cancellationToken.IsCancellationRequested)
                    {
                        try
                        {
                            ZeroMQMessage message;

                            if (mQueue.TryTake(out message, TimeSpan.FromSeconds(1)))
                            {
                                Logger.Debug("ZeroMQCache: Sending -> {0}", message.ToString());

                                pushSocket.SendMore(message.Topic);
                                pushSocket.SendMore(message.Client);
                                pushSocket.Send(message.Content);
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            // We have been asked to cancel operation
                            break;
                        }
                        catch (Exception ex)
                        {
                            Logger.Error("ZeroMQCache: Error sending message.", ex);
                        }
                    }

                    // Close socket
                    pushSocket.Close();
                }

                context.Terminate();
            }
        }
Example #16
0
 public void Dispose()
 {
     _running = false;
     _socket.Close();
 }
Example #17
0
        private void ProcessDataFromWorkers(BPlusTree <string, int> map)
        {
            using (var context = NetMQContext.Create())
            {
                using (NetMQSocket receiver = context.CreatePullSocket())
                {
                    Task.Run(() =>
                    {
                        // The Sink listens to the Ventilator for news about jobs
                        using (NetMQSocket sink = context.CreatePullSocket())
                        {
                            sink.Connect(_ventilatorReceiveAddress);

                            while (_writingData)
                            {
                                string message = "";
                                try
                                {
                                    message = sink.ReceiveString();
                                }
                                catch (ObjectDisposedException e)
                                {
                                    // We're done. Exit the loop.
                                    break;
                                }

                                // A new job was pushed
                                if (message != "done")
                                {
                                    _knownJobs.Add(message);
                                }
                                // We're done!
                                else
                                {
                                    Task.Run(() =>
                                    {
                                        // While the count isn't the same, wait.
                                        // Todo: Bake better status monitoring to prevent deadlocking
                                        //       if things are ever dropped.
                                        while (_processedJobs.Count != _knownJobs.Count)
                                        {
                                            Thread.Sleep(10);
                                        }
                                        receiver.Close();
                                        sink.Close();
                                        _writingData = false;
                                    });
                                }
                            }
                        }
                    });

                    receiver.Bind(_receiverAddress);

                    var dict = new Dictionary <string, int>();

                    while (_writingData)
                    {
                        string message = "";
                        try
                        {
                            message = receiver.ReceiveString();
                        }
                        catch (ObjectDisposedException e)
                        {
                            // We know there isn't more work, so we break this loop.
                            break;
                        }

                        var jobResult = JsonConvert.DeserializeObject <JobResult>(message);

                        foreach (var word in jobResult.Data)
                        {
                            // Update existing values
                            if (dict.ContainsKey(word.Key))
                            {
                                dict[word.Key] = word.Value + dict[word.Key];
                            }
                            else // Add new value
                            {
                                dict[word.Key] = word.Value;
                            }

                            // This is around the max that we can hold in 4gb safely.
                            // Assuming our strings aren't suuuper long.
                            if (dict.Count > 3020000)
                            {
                                Console.WriteLine("Flushing " + jobResult.JobId + "... ");

                                var stopwatch = new Stopwatch();
                                stopwatch.Start();

                                FlushToDisk(map, dict);

                                dict = new Dictionary <string, int>();

                                stopwatch.Stop();

                                Console.WriteLine("Done flushing " + jobResult.JobId + ". Time: " + stopwatch.ElapsedMilliseconds + "ms");
                            }
                        }

                        _processedJobs.Add(jobResult.JobId);
                    }

                    Console.WriteLine("Flattening data to TSV file.");

                    // Save our changes to the ondisk db
                    if (map.Any())
                    {
                        Console.WriteLine("Pushing data to BPlusTree.");
                        FlushToDisk(map, dict);

                        Console.WriteLine(map.Count);

                        WriteToTSVFile(map, "Output.tsv");
                    }
                    // Everything is in memory so just use that.
                    else
                    {
                        WriteToTSVFile(dict, "Output.tsv");
                    }

                    // We're done!
                    WaitingOnWork = false;
                }
            }
        }
Example #18
0
 public Task Disconnect(CancellationToken cancellation = new CancellationToken())
 {
     Socket.Close();
     return(TaskEx.Completed);
 }
Example #19
0
 public virtual void Dispose()
 {
     Socket.Close();
 }
Example #20
0
 public void Dispose()
 {
     _replySocket.Close();
 }
        private void ReceiveEvents(String subscriptionUri, String topic, CancellationToken cancellationToken)
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                using (NetMQSocket subscriberSocket = context.CreateSubscriberSocket())
                {
                    subscriberSocket.IgnoreErrors = true;
                    subscriberSocket.Connect(subscriptionUri);
                    subscriberSocket.Subscribe(topic);

                    Logger.Info("ZeroMQCache: Connected to subscriptionUri \"{0}\".", subscriptionUri);

                    // Eventhandler delegate to handle receiving messages
                    subscriberSocket.ReceiveReady += (sender, args) =>
                    {
                        try
                        {
                            if (args.ReceiveReady)
                            {
                                // Recieve and relay
                                NetMQMessage netMQmessage = args.Socket.ReceiveMessage();

                                // Recieve the message
                                ZeroMQMessage message = new ZeroMQMessage()
                                {
                                    Topic   = netMQmessage.Pop().ConvertToString(),
                                    Client  = netMQmessage.Pop().ConvertToString(),
                                    Content = netMQmessage.Pop().ConvertToString()
                                };

                                Logger.Debug("ZeroMQCache: Received -> {0}", message.ToString());

                                XmlCacheEvent cacheEvent = XmlCacheEvent.FromXml(message.Content);

                                if (cacheEvent != null)
                                {
                                    OnCacheEvent(cacheEvent.CacheRegion, cacheEvent.Key, cacheEvent.EventType);
                                }
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            // We have been asked to cancel operation
                            return;
                        }
                        catch (Exception ex)
                        {
                            Logger.Error("ZeroMQCache: Error receiving message.", ex);
                        }
                    };

                    while (!cancellationToken.IsCancellationRequested)
                    {
                        try
                        {
                            subscriberSocket.Poll(TimeSpan.FromSeconds(1));
                        }
                        catch (OperationCanceledException)
                        {
                            // We have been asked to cancel operation
                            break;
                        }
                        catch (Exception ex)
                        {
                            Logger.Error("ZeroMQCache: Error polling for messages.", ex);
                        }
                    }

                    // Close socket
                    subscriberSocket.Close();
                }

                context.Terminate();
            }
        }
 public void Dispose()
 {
     _requestSocket.Close();
 }
Example #23
0
 private void TerminateClient(NetMQSocket client)
 {
     client.Disconnect(Address.OriginalString);
     client.Close();
 }
Example #24
0
 private static void TerminateClient(NetMQSocket client)
 {
     client.Disconnect(ServerEndpoint);
     client.Close();
 }
        private void ReceiveEvents(String submissionUri, String topic, CancellationToken cancellationToken)
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                using (NetMQSocket pullSocket = context.CreatePullSocket())
                {
                    pullSocket.IgnoreErrors = true;
                    pullSocket.Bind(submissionUri);

                    Logger.Info("ZeroMQBroker: Bound to submissionUri \"{0}\".", submissionUri);

                    // Eventhandler delegate to handle receiving messages
                    pullSocket.ReceiveReady += (sender, args) =>
                    {
                        try
                        {
                            if (args.ReceiveReady)
                            {
                                // Recieve and relay
                                NetMQMessage netMQmessage = args.Socket.ReceiveMessage();

                                // Recieve the message
                                ZeroMQMessage message = new ZeroMQMessage()
                                {
                                    Topic   = netMQmessage.Pop().ConvertToString(),
                                    Client  = netMQmessage.Pop().ConvertToString(),
                                    Content = netMQmessage.Pop().ConvertToString()
                                };

                                Logger.Debug("ZeroMQBroker: Received -> {0}", message.ToString());

                                mQueue.Add(message);

                                // ZeroMQBroker relays multiple topics, verify if the message was meant for the current client
                                if (String.Equals(topic, message.Topic, StringComparison.OrdinalIgnoreCase))
                                {
                                    XmlCacheEvent cacheEvent = XmlCacheEvent.FromXml(message.Content);

                                    if (cacheEvent != null)
                                    {
                                        OnCacheEvent(cacheEvent.CacheRegion, cacheEvent.Key, cacheEvent.EventType);
                                    }
                                }
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            // We have been asked to cancel operation
                            return;
                        }
                        catch (Exception ex)
                        {
                            Logger.Error("ZeroMQBroker: Error receiving message.", ex);
                        }
                    };

                    while (!cancellationToken.IsCancellationRequested)
                    {
                        try
                        {
                            pullSocket.Poll(TimeSpan.FromSeconds(1));
                        }
                        catch (OperationCanceledException)
                        {
                            // We have been asked to cancel operation
                            break;
                        }
                        catch (Exception ex)
                        {
                            Logger.Error("ZeroMQBroker: Error polling for messages.", ex);
                        }
                    }

                    // Close socket
                    pullSocket.Close();
                }

                context.Terminate();
            }
        }
Example #26
0
 private static void TerminateClient(NetMQSocket client)
 {
     client.Disconnect(ServerEndpoint);
     client.Close();
 }