Example #1
0
        private void OnSubscriberMessage(object sender, NetMQSocketEventArgs e)
        {
            NetMQMessage message = null;

            while (_subscriber.TryReceiveMultipartMessage(ref message))
            {
                // Debug.WriteLine(message);

                var topic = message[0].ConvertToString();

                if (topic == WelcomeMessage)
                {
                    SubscriberAddress = e.Socket.Options.LastEndpoint;
                    Debug.WriteLine($"Subsciber Address: {SubscriberAddress}");
                    _welcomeMessageHandler?.Invoke();
                }
                else if (topic == HeartbeatMessage)
                {
                    // we got a heartbeat, lets postponed the timer
                    _timeoutTimer.EnableAndReset();
                }
                else
                {
                    _shim.SendMultipartMessage(message);
                }
            }
        }
Example #2
0
        public ZeroMqNotificationReader Init()
        {
            if (Initialized)
            {
                throw new InvalidOperationException($"{nameof(ZeroMqNotificationReader)} already initialized.");
            }

            var completionSource = new TaskCompletionSource <object>();
            var token            = _source.Token;

            _receiveTask = Task.Factory.StartNew(() =>
            {
                var timeout = TimeSpan.FromSeconds(1);

                using (var subSocket = new SubscriberSocket(EndPoint))
                {
                    try
                    {
                        subSocket.Options.ReceiveHighWatermark = 1000;
                        subSocket.SubscribeToAnyTopic();

                        while (!token.IsCancellationRequested)
                        {
                            var zMessage        = new NetMQMessage(2);
                            var messageReceived = subSocket.TryReceiveMultipartMessage(timeout, ref zMessage, 2);
                            completionSource.TrySetResult(null);

                            if (!messageReceived)
                            {
                                continue;
                            }

                            var topic = zMessage.Pop().ConvertToString(Encoding.UTF8);
                            var json  = zMessage.Pop().ConvertToString(Encoding.UTF8);

                            OnNotificationReceived?.Invoke(topic, json);
                        }
                    }
                    catch (Exception e)
                    {
                        OnError?.Invoke(e);
                    }
                }
            }, TaskCreationOptions.LongRunning);

            _receiveTask.ContinueWith(t =>
            {
                // Propagate exception from initialization if occured
                if (t.Exception != null)
                {
                    completionSource.TrySetException(t.Exception);
                }
            });

            completionSource.Task.GetAwaiter().GetResult();

            Initialized = true;
            return(this);
        }
Example #3
0
        public void ReceiveMessageWithTimeout()
        {
            {
                var       pubSync  = new AutoResetEvent(false);
                var       payload  = new byte[300];
                const int waitTime = 500;

                var t1 = new Task(() =>
                {
                    using (var pubSocket = new PublisherSocket())
                    {
                        pubSocket.Bind("tcp://127.0.0.1:12345");
                        pubSync.WaitOne();
                        Thread.Sleep(waitTime);
                        pubSocket.SendFrame(payload);
                        pubSync.WaitOne();
                    }
                }, TaskCreationOptions.LongRunning);

                var t2 = new Task(() =>
                {
                    using (var subSocket = new SubscriberSocket())
                    {
                        subSocket.Connect("tcp://127.0.0.1:12345");
                        subSocket.Subscribe("");
                        Thread.Sleep(100);
                        pubSync.Set();

                        NetMQMessage msg = null;
                        Assert.IsFalse(subSocket.TryReceiveMultipartMessage(TimeSpan.FromMilliseconds(100), ref msg));

                        Assert.IsTrue(subSocket.TryReceiveMultipartMessage(TimeSpan.FromMilliseconds(waitTime), ref msg));
                        Assert.NotNull(msg);
                        Assert.AreEqual(1, msg.FrameCount);
                        Assert.AreEqual(300, msg.First.MessageSize);
                        pubSync.Set();
                    }
                }, TaskCreationOptions.LongRunning);

                t1.Start();
                t2.Start();

                Task.WaitAll(t1, t2);
            }
        }
Example #4
0
    protected void startReceiving()
    {
        elasticServerTimeout = serverTimeout;
        done = false;
        timer.Start();
        lastMessageTS = timer.getElapsedMS();

        UnityEngine.Debug.Log("Client: start processing incoming messages");
        while (!done)
        {
            NetMQMessage msg = new NetMQMessage();
            //If client receives any message, it means that the client is connected
            if (subscriber.TryReceiveMultipartMessage(TimeSpan.FromMilliseconds(1), ref msg, 2))
            {
                isConnected          = true;
                lastMessageTS        = timer.getElapsedMS();
                elasticServerTimeout = serverTimeout;

                System.Diagnostics.Debug.Assert(msg.First.Buffer[0] == (byte)'A');
                UInt64 signature = messageSignature(msg.First.Buffer[1], msg.First.Buffer[2]);

                Action <NetMQMessage> f = null;
                try
                {
                    f = messageHandlers[signature];
                }
                catch (KeyNotFoundException)
                {
                    //UnityEngine.Debug.Log(
                    //    String.Format("Client: key {0} was not present in messageHandlers, module {1}, action {2}",
                    //    signature, msg.First.Buffer[1], msg.First.Buffer[2]));
                }
                if (f != null)
                {
                    f(msg);
                }
            }
            else
            {
                float elapsed = (timer.getElapsedMS() - lastMessageTS) * 0.001f;
                if (elapsed > elasticServerTimeout)
                {
                    isConnected = false;

                    UnityEngine.Debug.Log(String.Format("Connection to the server lost for {0} seconds", elapsed));
                    elasticServerTimeout *= 2;
                }

                Thread.Sleep(TimeSpan.FromMilliseconds(1));
            }
        }

        UnityEngine.Debug.Log("Client: main-loop ends");
    }
Example #5
0
        public static bool TryReceive(this SubscriberSocket socket, TimeSpan timeOut,
                                      out string topic, out string typeName, out byte[] rawMessage)
        {
            typeName   = null;
            rawMessage = null;
            topic      = null;

            var message = new NetMQMessage();

            if (socket.TryReceiveMultipartMessage(timeOut, ref message, ExpectedFrameCount))
            {
                message.ParseFrames(out topic, out typeName, out rawMessage);
                return(true);
            }

            return(false);
        }
Example #6
0
        public bool TryReceive()
        {
            if (!IsConnected)
            {
                return(false);
            }

            //try to receive something from the network... if that succeeds get the distance from the given Erg
            var message = new NetMQMessage();

            if (subSocket.TryReceiveMultipartMessage(System.TimeSpan.Zero, ref message))
            {
                foreach (var frame in message.Skip(1)) //the first frame is always just the envelope/topic... let's ignore it by using Linq
                {
                    HandleFrame(frame);
                }
                return(true);
            }
            return(false);
        }
Example #7
0
        private void OnMessageReceived(object sender, NetMQSocketEventArgs e)
        {
            NetMQMessage message = null;

            if (!socket.TryReceiveMultipartMessage(ref message, 2))
            {
                return;
            }

            // Move handling request off NetMQPoller thread and onto TaskPool as soon as possible
            Task.Run(() =>
            {
                if (!messageFactory.IsValidTopicMessage(message))
                {
                    return;
                }

                var package = messageFactory.ExtractTopic(message);
                topicDispatcher.Handle(this, package);
            });
        }
Example #8
0
    private IList <Erg> ReceiveBoats()
    {
        //try to receive something from the network... return all the ergs we get
        var message = new NetMQMessage();
        IList <EasyErgsocket.Erg> receivedBoats = new List <EasyErgsocket.Erg>();

        while (subSocket.TryReceiveMultipartMessage(System.TimeSpan.Zero, ref message))
        {
            foreach (var frame in message.Skip(1)) //the first frame is always just the envelope/topic... let's ignore it by using Linq
            {
                byte[] rawMessage = frame.Buffer;
                using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(rawMessage))
                {
                    var givenErg = Serializer.Deserialize <EasyErgsocket.Erg>(memoryStream);
                    receivedBoats.Add(givenErg);
                }
            }
        }

        return(receivedBoats);
    }
        private void HandleWork()
        {
            using (_cacheUpdateSocket = new SubscriberSocket())
            {
                _cacheUpdateSocket.Options.ReceiveHighWatermark = _configuration.ZmqHighWatermark;

                _cacheUpdateSocket.Subscribe(_configuration.Subject);
                _cacheUpdateSocket.Connect(_configuration.SubscriptionEndpoint);

                while (!_cancel.IsCancellationRequested)
                {
                    NetMQMessage message = null;

                    var hasMessage = _cacheUpdateSocket.TryReceiveMultipartMessage(_configuration.IsStaleTimeout, ref message);

                    if (_cancel.IsCancellationRequested)
                    {
                        return;
                    }

                    if (hasMessage)
                    {
                        var eventIdBytes      = message[1].Buffer;
                        var eventMessageBytes = message[2].Buffer;

                        var eventId         = _eventSerializer.Serializer.Deserialize <IEventId>(eventIdBytes);
                        var producerMessage = _eventSerializer.Serializer.Deserialize <IProducerMessage>(eventMessageBytes);

                        var @event = _eventSerializer.ToEvent <TKey, TAggregate>(eventId, producerMessage);

                        OnEventReceived(@event);
                    }
                    else
                    {
                        _state.OnNext(DynamicCacheState.Staled);
                    }
                }
            }
        }
Example #10
0
    void NetMQClient()
    {
        //thanks for Yuta Itoh sample code to connect via NetMQ with Pupil Service
        string IPHeader = ">tcp://" + ServerIP + ":";
        var    timeout  = new System.TimeSpan(0, 0, 1);     //1sec

        // Necessary to handle this NetMQ issue on Unity editor
        // https://github.com/zeromq/netmq/issues/526
        AsyncIO.ForceDotNet.Force();
        NetMQConfig.ManualTerminationTakeOver();
        NetMQConfig.ContextCreate(true);

        string subport = "";

        print("Connect to the server: " + IPHeader + ServicePort + ".");
        Thread.Sleep(ServiceStartupDelay);

        _requestSocket = new RequestSocket(IPHeader + ServicePort);

        _requestSocket.SendFrame("SUB_PORT");
        _isconnected = _requestSocket.TryReceiveFrameString(timeout, out subport);
        print(_isconnected + " isconnected");
        _gazeFps.Reset();
        _eyeFps [0].Reset();
        _eyeFps [1].Reset();

        if (_isconnected)
        {
            //_serviceStarted = true;
            StartProcess();
            var subscriberSocket = new SubscriberSocket(IPHeader + subport);

            subscriberSocket.Subscribe("gaze");             //subscribe for gaze data
            subscriberSocket.Subscribe("notify.");          //subscribe for all notifications
            _setStatus(EStatus.ProcessingGaze);
            var msg = new NetMQMessage();
            while (_isDone == false)
            {
                _isconnected = subscriberSocket.TryReceiveMultipartMessage(timeout, ref (msg));
                if (_isconnected)
                {
                    try
                    {
                        string msgType = msg[0].ConvertToString();
                        UnityEngine.Debug.Log(msgType);
                        if (msgType == "gaze")
                        {
                            var message = MsgPack.Unpacking.UnpackObject(msg[1].ToByteArray());

                            MsgPack.MessagePackObject mmap = message.Value;
                            lock (_dataLock)
                            {
                                _pupilData = JsonUtility.FromJson <Pupil.PupilData3D>(mmap.ToString());
                                if (_pupilData.confidence > 0.5f)
                                {
                                    //UnityEngine.Debug.Log(_pupilData.base_data[0].id);
                                    OnPacket(_pupilData);
                                }
                            }
                        }
                        else if (msgType == "notify.eye_process.started")
                        {
                            var message = MsgPack.Unpacking.UnpackObject(msg[1].ToByteArray());
                            MsgPack.MessagePackObject mmap = message.Value;
                            var id = JsonUtility.FromJson <Pupil.EyeStatus>(mmap.ToString());
                            UnityEngine.Debug.Log(id.eye_id);
                        }
                        //Debug.Log(message);
                    }
                    catch
                    {
                        //	Debug.Log("Failed to unpack.");
                    }
                }
                else
                {
                    print("Failed to receive a message.");
                    Thread.Sleep(500);
                }
            }

            StopProcess();
            subscriberSocket.Close();
        }
        else
        {
            print("Failed to connect the server.");
            //If needed here could come a retry connection.
        }

        //Can only send request via IPC if the connection has been established, otherwise we are facing, errors and potential freezing.
        if (_serviceStarted && _isconnected)
        {
            StopService();
        }

        //Kill process
        if (serviceProcess != null)
        {
            UnityEngine.Debug.Log("Killing Pupil service");
            serviceProcess.Kill();
            serviceProcess.Close();
        }

        _requestSocket.Close();
        // Necessary to handle this NetMQ issue on Unity editor
        // https://github.com/zeromq/netmq/issues/526
        print("ContextTerminate.");
        NetMQConfig.ContextTerminate();
    }
        private void StartListeners()
        {
            var stratumsByUrl = clusterConfig.Pools.Where(x => x.Enabled && x.ExternalStratums?.Any() == true)
                                .SelectMany(x => x.ExternalStratums)
                                .Where(x => x.Url != null && x.Topic != null)
                                .GroupBy(x =>
            {
                var tmp = x.Url.Trim();
                return(!tmp.EndsWith("/") ? tmp : tmp.Substring(0, tmp.Length - 1));
            }, x => x.Topic.Trim())
                                .ToArray();

            var serializer = new JsonSerializer
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

            foreach (var item in stratumsByUrl)
            {
                var thread = new Thread(arg =>
                {
                    var urlAndTopic  = (IGrouping <string, string>)arg;
                    var url          = urlAndTopic.Key;
                    var topics       = new HashSet <string>(urlAndTopic.Distinct());
                    var receivedOnce = false;

                    while (true)
                    {
                        try
                        {
                            using (var subSocket = new SubscriberSocket())
                            {
                                subSocket.Connect(url);

                                // subscribe to all topics
                                foreach (var topic in topics)
                                {
                                    subSocket.Subscribe(topic);
                                }

                                logger.Info($"Monitoring external stratum {url}/[{string.Join(", ", topics)}]");

                                while (true)
                                {
                                    // receive
                                    var msg = (NetMQMessage)null;

                                    if (!subSocket.TryReceiveMultipartMessage(relayReceiveTimeout, ref msg, 3))
                                    {
                                        if (receivedOnce)
                                        {
                                            logger.Warn(() => $"Timeout receiving message from {url}. Reconnecting ...");
                                            break;
                                        }

                                        // retry
                                        continue;
                                    }

                                    // extract frames
                                    var topic    = msg.Pop().ConvertToString(Encoding.UTF8);
                                    var flags    = msg.Pop().ConvertToInt32();
                                    var data     = msg.Pop().ToByteArray();
                                    receivedOnce = true;

                                    // validate
                                    if (!topics.Contains(topic))
                                    {
                                        logger.Warn(() => $"Received non-matching topic {topic} on ZeroMQ subscriber socket");
                                        continue;
                                    }

                                    if (data?.Length == 0)
                                    {
                                        logger.Warn(() => $"Received empty data from {url}/{topic}");
                                        continue;
                                    }

                                    // deserialize
                                    var wireFormat = (ShareRelay.WireFormat)(flags & ShareRelay.WireFormatMask);
                                    Share share    = null;

                                    switch (wireFormat)
                                    {
                                    case ShareRelay.WireFormat.Json:
                                        using (var stream = new MemoryStream(data))
                                        {
                                            using (var reader = new StreamReader(stream, Encoding.UTF8))
                                            {
                                                using (var jreader = new JsonTextReader(reader))
                                                {
                                                    share = serializer.Deserialize <Share>(jreader);
                                                }
                                            }
                                        }

                                        break;

                                    case ShareRelay.WireFormat.ProtocolBuffers:
                                        using (var stream = new MemoryStream(data))
                                        {
                                            share             = Serializer.Deserialize <Share>(stream);
                                            share.BlockReward = (decimal)share.BlockRewardDouble;
                                        }

                                        break;

                                    default:
                                        logger.Error(() => $"Unsupported wire format {wireFormat} of share received from {url}/{topic} ");
                                        break;
                                    }

                                    if (share == null)
                                    {
                                        logger.Error(() => $"Unable to deserialize share received from {url}/{topic}");
                                        continue;
                                    }

                                    // store
                                    share.PoolId  = topic;
                                    share.Created = clock.Now;
                                    messageBus.SendMessage(new ClientShare(null, share));

                                    // update poolstats from shares
                                    if (pools.TryGetValue(topic, out var poolContext))
                                    {
                                        var pool = poolContext.Pool;
                                        poolContext.Logger.Info(() => $"External {(!string.IsNullOrEmpty(share.Source) ? $"[{share.Source.ToUpper()}] " : string.Empty)}share accepted: D={Math.Round(share.Difficulty, 3)}");

                                        if (pool.NetworkStats != null)
                                        {
                                            pool.NetworkStats.BlockHeight       = share.BlockHeight;
                                            pool.NetworkStats.NetworkDifficulty = share.NetworkDifficulty;

                                            if (poolContext.BlockHeight != share.BlockHeight)
                                            {
                                                pool.NetworkStats.LastNetworkBlockTime = clock.Now;
                                                poolContext.BlockHeight = share.BlockHeight;
                                                poolContext.LastBlock   = clock.Now;
                                            }

                                            else
                                            {
                                                pool.NetworkStats.LastNetworkBlockTime = poolContext.LastBlock;
                                            }
                                        }
                                    }

                                    else
                                    {
                                        logger.Info(() => $"External {(!string.IsNullOrEmpty(share.Source) ? $"[{share.Source.ToUpper()}] " : string.Empty)}share accepted: D={Math.Round(share.Difficulty, 3)}");
                                    }
                                }
                            }
                        }

                        catch (ObjectDisposedException)
                        {
                            logger.Info($"Exiting monitoring thread for external stratum {url}/[{string.Join(", ", topics)}]");
                            break;
                        }

                        catch (Exception ex)
                        {
                            logger.Error(ex);
                        }
                    }
                });

                thread.Start(item);
            }

            if (stratumsByUrl.Any())
            {
                logger.Info(() => "Online");
            }
        }
Example #12
0
        protected IObservable <string> BtStreamSubscribe(ZmqPubSubEndpointConfig config)
        {
            return(Observable.Defer(() => Observable.Create <string>(obs =>
            {
                var tcs = new CancellationTokenSource();

                Task.Factory.StartNew(() =>
                {
                    using (tcs)
                    {
                        while (!tcs.IsCancellationRequested)
                        {
                            try
                            {
                                using (var subSocket = new SubscriberSocket())
                                {
                                    //subSocket.Options.ReceiveHighWatermark = 1000;
                                    subSocket.Connect(config.Url);
                                    subSocket.Subscribe(config.Topic);

                                    logger.Debug($"Subscribed to {config.Url}/{config.Topic}");

                                    while (!tcs.IsCancellationRequested)
                                    {
                                        var msg = (NetMQMessage)null;

                                        if (!subSocket.TryReceiveMultipartMessage(btStreamReceiveTimeout, ref msg, 4))
                                        {
                                            logger.Warn(() => $"Timeout receiving message from {config.Url}. Reconnecting ...");
                                            break;
                                        }

                                        // extract frames
                                        var topic = msg.Pop().ConvertToString(Encoding.UTF8);
                                        var flags = msg.Pop().ConvertToInt32();
                                        var data = msg.Pop().ToByteArray();
                                        var timestamp = msg.Pop().ConvertToInt64();

                                        // compressed
                                        if ((flags & 1) == 1)
                                        {
                                            using (var stm = new MemoryStream(data))
                                            {
                                                using (var stmOut = new MemoryStream())
                                                {
                                                    using (var ds = new DeflateStream(stm, CompressionMode.Decompress))
                                                    {
                                                        ds.CopyTo(stmOut);
                                                    }

                                                    data = stmOut.ToArray();
                                                }
                                            }
                                        }

                                        // convert
                                        var json = Encoding.UTF8.GetString(data);

                                        // publish
                                        obs.OnNext(json);

                                        // telemetry
                                        messageBus.SendMessage(new TelemetryEvent(clusterConfig.ClusterName ?? poolConfig.PoolName, poolConfig.Id,
                                                                                  TelemetryCategory.BtStream, DateTime.UtcNow - DateTimeOffset.FromUnixTimeSeconds(timestamp)));
                                    }
                                }
                            }

                            catch (Exception ex)
                            {
                                logger.Error(ex);
                            }

                            // do not consume all CPU cycles in case of a long lasting error condition
                            Thread.Sleep(1000);
                        }
                    }
                }, tcs.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);

                return Disposable.Create(() => { tcs.Cancel(); });
            }))
                   .Publish()
                   .RefCount());
        }
Example #13
0
        async Task <bool> Receive()
        {
            try
            {
                NetMQMessage msg = null;
                // NOTE:
                // After switching to async IO, when we close the socket in Dispose() the ReceiveMultipartMessage() wasn't returning.
                // So, we TryReceive() with a timeout.  If it times out the receive loop will check if we retry or exit.
                // If we get an async-friendly version of NetMQ (or switch transports) we can probably go back to regular Receive().
                if (socket.TryReceiveMultipartMessage(ReceiveTimeout, ref msg))
                {
                    if (msg == null || ZeroMQ.PubSubUtil.IsStoppingMessage(msg))
                    {
                        PublisherLogger.Info("stopped by stpping message!");
                        return(false);
                    }

                    var topic   = msg[0].ConvertToString();
                    var msgType = msg[1].ConvertToString();
                    if (MsgHandlers.TryGetValue(msgType, out Delegate mh))
                    {
                        var respBytes = msg[2].Buffer;
                        using (var proto = ThriftUtil.CreateReadProtocol(respBytes))
                        {
                            Type t = GeneratedTypeCache.GetType(msgType);
                            if (t == null)
                            {
                                throw new TargetInvocationException(new Exception("can't get type for: " + msgType));
                            }
                            TBase ret = Activator.CreateInstance(t) as TBase;
                            await ret.ReadAsync(proto, cts.Token);

                            mh.DynamicInvoke(topic, ret);
                        }
                    }
                    return(true);
                }
                else
                {
                    // Receive timed out.  Return true so receive loop can check if we should exit or try again.
                    return(true);
                }
            }
            catch (Exception e)
            {
                if (e is TargetInvocationException ee)
                {
                    Log($"invoke exception: {(ee).InnerException.Message} \n {ee.InnerException.StackTrace}", Logging.LogLevel.Warn);
                    return(true);
                }
                if (e is TerminatingException)
                {
                    Log($"terminated: {e.Message}", Logging.LogLevel.Info);
                    return(false);
                }
                else
                {
                    if (disposing)
                    {
                        Log("disposing exception: " + e.Message, Logging.LogLevel.Info);
                    }
                    else
                    {
                        Log($"receive exception: {e.Message} \n {e.StackTrace}", Logging.LogLevel.Error);
                    }

                    return(false);
                }
            }
        }
Example #14
0
    // Client thread which does not block Update()
    void NetMQClient()
    {
        string IPHeader = ">tcp://" + IP + ":";
        var    timeout  = new System.TimeSpan(0, 0, 1); //1sec

        // Necessary to handle this NetMQ issue on Unity editor
        // https://github.com/zeromq/netmq/issues/526
        AsyncIO.ForceDotNet.Force();
        NetMQConfig.ManualTerminationTakeOver();
        NetMQConfig.ContextCreate(true);

        string subport = "";

        Debug.Log("Connect to the server: " + IPHeader + PORT + ".");
        var       requestSocket = new RequestSocket(IPHeader + PORT);
        double    t             = 0;
        const int N             = 1000;
        bool      is_connected  = false;

        for (int k = 0; k < N; k++)
        {
            var sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            requestSocket.SendFrame("SUB_PORT");
            is_connected = requestSocket.TryReceiveFrameString(timeout, out subport);
            sw.Stop();
            t = t + sw.Elapsed.Milliseconds;
            //Debug.Log("Round trip time:" + sw.Elapsed + "[sec].");
            if (is_connected == false)
            {
                break;
            }
        }
        Debug.Log("Round trip average time:" + t / N + "[msec].");

        requestSocket.Close();

        if (is_connected)
        {
            //
            var subscriberSocket = new SubscriberSocket(IPHeader + subport);
            subscriberSocket.Subscribe(ID);

            var msg = new NetMQMessage();
            while (is_connected && stop_thread_ == false)
            {
                Debug.Log("Receive a multipart message.");
                is_connected = subscriberSocket.TryReceiveMultipartMessage(timeout, ref (msg));
                if (is_connected)
                {
                    Debug.Log("Unpack a received multipart message.");
                    try
                    {
                        //Debug.Log(msg[0].ConvertToString());
                        var message = MsgPack.Unpacking.UnpackObject(msg[1].ToByteArray());
                        MsgPack.MessagePackObject mmap = message.Value;
                        lock (thisLock_)
                        {
                            data_ = JsonUtility.FromJson <Pupil.PupilData3D>(mmap.ToString());
                        }
                        //Debug.Log(message);
                    }
                    catch
                    {
                        Debug.Log("Failed to unpack.");
                    }
                }
                else
                {
                    Debug.Log("Failed to receive a message.");
                    Thread.Sleep(1000);
                }
            }
            subscriberSocket.Close();
        }
        else
        {
            Debug.Log("Failed to connect the server.");
        }

        // Necessary to handle this NetMQ issue on Unity editor
        // https://github.com/zeromq/netmq/issues/526
        Debug.Log("ContextTerminate.");
        NetMQConfig.ContextTerminate();
    }
Example #15
0
        public async Task ShouldHandleIncomingEvent()
        {
            using (var publisher = new PublisherSocket())
            {
                publisher.Connect(ToPublishersEndpoint);

                using (var subscriber = new SubscriberSocket())
                {
                    subscriber.Connect(ToSubscribersEndpoint);
                    subscriber.SubscribeToAnyTopic();

                    var command = new ChangeCcyPairState("EUR/USD", "TEST", CcyPairState.Passive);
                    var message = _eventSerializer.ToProducerMessage(command);

                    await Task.Delay(200);

                    publisher.SendMoreFrame(message.Subject)
                    .SendFrame(_eventSerializer.Serializer.Serialize(message));

                    NetMQMessage msg = null;

                    var hasResponse = subscriber.TryReceiveMultipartMessage(TimeSpan.FromMilliseconds(1000), ref msg);

                    Assert.IsTrue(hasResponse);

                    var eventIdBytes      = msg[1].Buffer;
                    var eventMessageBytes = msg[2].Buffer;

                    var eventId = _eventSerializer.Serializer.Deserialize <EventId>(eventIdBytes);

                    Assert.AreEqual("EUR/USD", eventId.EventStream);
                    Assert.AreEqual("EUR/USD.0", eventId.Id);
                    Assert.AreEqual(0, eventId.Version);
                    Assert.AreEqual(message.Subject, eventId.Subject);

                    var producerMessage = _eventSerializer.Serializer.Deserialize <ProducerMessage>(eventMessageBytes);

                    var @event = _serializer.Deserialize <ChangeCcyPairState>(producerMessage.MessageBytes);

                    Assert.AreEqual(message.Subject, producerMessage.Subject);
                    Assert.AreEqual(typeof(ChangeCcyPairState), producerMessage.MessageType);

                    Assert.AreEqual(command.EventStreamId, @event.EventStreamId);
                    Assert.AreEqual(command.Market, @event.Market);
                    Assert.AreEqual(command.State, @event.State);

                    command = new ChangeCcyPairState("EUR/USD", "TEST", CcyPairState.Active);
                    message = _eventSerializer.ToProducerMessage(command);

                    await Task.Delay(200);

                    publisher.SendMoreFrame(message.Subject)
                    .SendFrame(_eventSerializer.Serializer.Serialize(message));

                    await Task.Delay(50);

                    hasResponse = subscriber.TryReceiveMultipartMessage(TimeSpan.FromMilliseconds(1000), ref msg);

                    Assert.IsTrue(hasResponse);

                    eventIdBytes      = msg[1].Buffer;
                    eventMessageBytes = msg[2].Buffer;

                    eventId = _eventSerializer.Serializer.Deserialize <EventId>(eventIdBytes);

                    Assert.AreEqual("EUR/USD", eventId.EventStream);
                    Assert.AreEqual("EUR/USD.1", eventId.Id);
                    Assert.AreEqual(1, eventId.Version);
                    Assert.AreEqual(message.Subject, eventId.Subject);
                }
            }
        }
Example #16
0
    void NetMQClient()
    {
        //thanks for Yuta Itoh sample code to connect via NetMQ with Pupil Service
        string IPHeader = ">tcp://" + ServerIP + ":";
        var    timeout  = new System.TimeSpan(0, 0, 1);     //1sec

        // Necessary to handle this NetMQ issue on Unity editor
        // https://github.com/zeromq/netmq/issues/526
        AsyncIO.ForceDotNet.Force();
        NetMQConfig.ManualTerminationTakeOver();
        NetMQConfig.ContextCreate(true);

        string subport = "";

        Debug.Log("Connect to the server: " + IPHeader + ServicePort + ".");
        _requestSocket = new RequestSocket(IPHeader + ServicePort);

        _requestSocket.SendFrame("SUB_PORT");
        _isconnected = _requestSocket.TryReceiveFrameString(timeout, out subport);

        _lastT = DateTime.Now;

        if (_isconnected)
        {
            StartProcess();
            var subscriberSocket = new SubscriberSocket(IPHeader + subport);
            subscriberSocket.Subscribe("gaze");             //subscribe for gaze data
            subscriberSocket.Subscribe("notify.");          //subscribe for all notifications
            _setStatus(EStatus.ProcessingGaze);
            var msg = new NetMQMessage();
            while (_isDone == false)
            {
                _isconnected = subscriberSocket.TryReceiveMultipartMessage(timeout, ref (msg));
                if (_isconnected)
                {
                    try
                    {
                        string msgType = msg[0].ConvertToString();
                        //Debug.Log(msgType);
                        if (msgType == "gaze")
                        {
                            var message = MsgPack.Unpacking.UnpackObject(msg[1].ToByteArray());
                            MsgPack.MessagePackObject mmap = message.Value;
                            lock (_dataLock)
                            {
                                _pupilData = JsonUtility.FromJson <Pupil.PupilData3D>(mmap.ToString());
                                if (_pupilData.confidence > 0.5f)
                                {
                                    OnPacket(_pupilData);
                                }
                            }
                        }
                        //Debug.Log(message);
                    }
                    catch
                    {
                        //	Debug.Log("Failed to unpack.");
                    }
                }
                else
                {
                    //	Debug.Log("Failed to receive a message.");
                    Thread.Sleep(500);
                }
            }

            StopProcess();

            subscriberSocket.Close();
        }
        else
        {
            Debug.Log("Failed to connect the server.");
        }

        _requestSocket.Close();
        // Necessary to handle this NetMQ issue on Unity editor
        // https://github.com/zeromq/netmq/issues/526
        Debug.Log("ContextTerminate.");
        NetMQConfig.ContextTerminate();
    }
Example #17
0
        private void SetupFakeBroker(
            CancellationToken cancel,
            IEventCache eventCache,
            bool useHeartbeat       = true,
            bool useEventLoop       = true,
            bool useStateOfTheWorld = true)
        {
            if (useHeartbeat)
            {
                //heartbeat
                Task.Run(() =>
                {
                    using (var heartbeatSocket = new ResponseSocket(HeartbeatEndpoint))
                    {
                        while (!cancel.IsCancellationRequested)
                        {
                            var hasResponse = heartbeatSocket.TryReceiveFrameBytes(TimeSpan.FromSeconds(1), out var messageBytes);

                            if (hasResponse)
                            {
                                heartbeatSocket.SendFrame(_serializer.Serialize(Heartbeat.Response));
                            }
                        }
                    }
                }, cancel);
            }

            if (useEventLoop)
            {
                //event loop
                Task.Run(async() =>
                {
                    using (var stateUpdate = new SubscriberSocket())
                    {
                        stateUpdate.SubscribeToAnyTopic();
                        stateUpdate.Bind(ToPublishersEndpoint);

                        while (!cancel.IsCancellationRequested)
                        {
                            NetMQMessage message = null;

                            var hasResponse = stateUpdate.TryReceiveMultipartMessage(TimeSpan.FromSeconds(1), ref message);

                            if (hasResponse)
                            {
                                var subject = message[0];
                                var payload = message[1];

                                await eventCache.AppendToStream(subject.Buffer, payload.Buffer);
                            }
                        }
                    }
                }, cancel);
            }

            if (useStateOfTheWorld)
            {
                //stateOfTheWorld
                Task.Run(async() =>
                {
                    using (var stateRequestSocket = new RouterSocket())
                    {
                        stateRequestSocket.Bind(StateOfTheWorldEndpoint);

                        while (!cancel.IsCancellationRequested)
                        {
                            NetMQMessage message = null;

                            var hasResponse = stateRequestSocket.TryReceiveMultipartMessage(TimeSpan.FromSeconds(1), ref message);

                            if (hasResponse)
                            {
                                var sender  = message[0].Buffer;
                                var request = _serializer.Deserialize <StateRequest>(message[1].Buffer);

                                var stream = await eventCache.GetStreamBySubject(request.Subject);

                                var response = new StateReply()
                                {
                                    Subject = request.Subject,
                                    Events  = stream.ToList()
                                };

                                stateRequestSocket.SendMoreFrame(sender)
                                .SendFrame(_serializer.Serialize(response));
                            }
                        }
                    }
                }, cancel);
            }
        }
        private void receiveMessagesAsync(object sender, DoWorkEventArgs e)
        {
            Debug.Log("ZMQReceiver starting async worker");

            // need to create ZMQ socket in thread that is going to use it
            if (m_socket == null)
            {
                AsyncIO.ForceDotNet.Force();
                m_socket = new SubscriberSocket();
                m_socket.Connect(m_address);
                Debug.Log("ZMQReceiver socket init ok");
            }

            const int messagePartsCount = 2;

            while (!m_workerThread.CancellationPending)
            {
                while (m_pendingReceivePackets.Count > 0)
                {
                    ReceivePacket recv = null;
                    if (m_pendingReceivePackets.TryDequeue(out recv))
                    {
                        if (recv == null)
                        {
                            continue;
                        }

                        if (recv.messageCallback == null)
                        {
                            Debug.LogError("ZMQReceiver: Object '" + recv.objectName + "' registered null Action<> callback. Ignoring this subscription.");
                            continue;
                        }

                        //recv.objectName;
                        m_socket.Subscribe(recv.subscriptionName);
                        m_receivePackets.Add(recv);
                    }
                }

                NetMQMessage multipartMessage = new NetMQMessage(messagePartsCount);
                var          hasMessage       = m_socket.TryReceiveMultipartMessage(TimeSpan.FromMilliseconds(100), ref multipartMessage, messagePartsCount);

                if (multipartMessage.IsEmpty)
                {
                    log("ZMQReceiver: failed to receive message");
                    continue;
                }

                string address = multipartMessage[0].ConvertToString();
                string message = multipartMessage[1].ConvertToString();

                m_receivedValues.AddOrUpdate(address, message, (key, oldValue) => message);
                log("ZMQReceiver: " + address + " / " + message);
            }

            m_socket.Disconnect(m_address);
            m_socket.Dispose();
            m_socket = null;

            e.Cancel = true;
            Debug.Log("ZMQReceiver: async thread finished");
        }