Exemplo n.º 1
0
        private void ListenerWork()
        {
            AsyncIO.ForceDotNet.Force();
            var spin = new SpinWait();

            using (var server = new ResponseSocket())
            {
                server.Bind(sockname);

                while (!_listenerCancelled)
                {
                    RepReady = false;
                    if (!server.TryReceiveFrameBytes(out ReqProto))
                    {
                        spin.SpinOnce();
                        continue;
                    }
                    ReqReady = true;
                    while (!(RepReady || _listenerCancelled))
                    {
                        spin.SpinOnce();
                    }
                    server.TrySendFrame(RepProto);
                }
            }
            NetMQConfig.Cleanup();
        }
Exemplo n.º 2
0
    /// <summary>
    ///   Start listening on udp port
    /// </summary>
    public override void Start()
    {
      if (_listen) return;

      try
      {
        Log.Info("PortZMq starting");

        Log.Debug(string.Format("Publisher binding on {0}", _endpoint));


        _res = new ResponseSocket();
        _res.Bind(_localpoint);

        _listen = true;

        new Thread(() =>
        {
          try
          {

            while (_listen)
            {
              byte[] data;

              while ((_res != null) && _res.HasIn)
                if (_res.TryReceiveFrameBytes(TimeSpan.FromSeconds(30), out data))
                {
                  Log.Debug(string.Format("Received {0} bytes", data.Length));

                  //Process received data
                  if (!ProcessData(data, data.Length))
                  {
                    _res.SignalError();

                    Log.Error(string.Format("Processing {0} bytes failed", data.Length));
                  }
                  else
                  {
                    _res.SignalOK();

                    Log.Debug(string.Format("Processed {0} bytes", data.Length));
                  }
                }
            }
          }
          catch (Exception e)
          {
            Log.Error("PortZMq loop error", e);
          }
        }).Start();

        base.Start();
      }
      catch (Exception e)
      {
        Log.Error("PortZMq start error", e);
      }
    }
Exemplo n.º 3
0
        private void Listen()
        {
            var timeout           = TimeSpan.FromSeconds(1);
            var cancellationToken = _cancellationTokenSource.Token;

            while (!cancellationToken.IsCancellationRequested)
            {
                if (_stream != null)
                {
                    continue;
                }

                if (_responseSocket.TryReceiveFrameBytes(timeout, out var bytes))
                {
                    _stream = new MemoryStream(bytes);
                }
            }
        }
Exemplo n.º 4
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);
            }
        }