Пример #1
0
        private void OnSubscriberMessage(object sender, NetMQSocketEventArgs e)
        {
            // we just forward the message to the actor
            var message = _subscriber.ReceiveMultipartMessage();

            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);
            }
        }
Пример #2
0
        static void ReadBlocks()
        {
            var zmq = new SubscriberSocket(Config.BitcoinRawBlocks);

            zmq.Subscribe("rawblock");
            //zmq.Subscribe("rawtx");
            //zmq.SubscribeToAnyTopic();

            Console.WriteLine("Waiting for blocks..");
            while (IsRunning)
            {
                var msg      = zmq.ReceiveMultipartMessage();
                var tag      = msg[0].ConvertToString();
                var msg_data = msg[1].Buffer;

                if (tag == "rawtx")
                {
                    var tx = Transaction.Load(msg_data, Network.Main);
                    Console.WriteLine($"Got new tx! {tx}");
                }
                else if (tag == "rawblock")
                {
                    var bp = Block.Load(msg_data, Consensus.Main);
                    BlockStream.Post(bp);
                }
            }
        }
Пример #3
0
            public async Task HandleAsync()
            {
                if (_token.IsCancellationRequested)
                {
                    Dispose();
                    _configuration.Logger.Log(new InfoLogMsg("SubscriberHandler was cancelled by cancellationRequest"));
                    return;
                }

                try
                {
                    NetMQMessage received = _socket.ReceiveMultipartMessage();
                    _configuration.Logger.Log(new DebugLogMsg($"handling message for [Subscriber:{typeof(TMessage)}]"));
                    var actualMessage = received.ParsePubSubMessage <TMessage>(_configuration);

                    var msg = actualMessage.IsSuccess ? actualMessage.GetResult() : new TMessage();
                    if (this._asyncCallback is null)
                    {
                        _syncCallback(msg);
                    }
                    else
                    {
                        await _asyncCallback(msg);
                    }
                }
                catch (NetMQ.TerminatingException trmnt)
                {
                    _configuration.Logger.Log(new ErrorLogMsg($"Subscriber handle for [Message:{typeof(TMessage)}] did fail: " + trmnt.Message));
                }
                catch (System.Exception ex)
                {
                    _configuration.Logger.Log(new ErrorLogMsg($"Subscriber handle for [Message:{typeof(TMessage)}] did fail: " + ex.Message));
                }
            }
Пример #4
0
        public static void Main()
        {
            // using (var responder = new ResponseSocket())
            // {
            //     responder.Bind("tcp://*:5555");

            //     while (true)
            //     {
            //         string str = responder.ReceiveFrameString();
            //         Console.WriteLine(str);
            //         Thread.Sleep(50);  //  Do some 'work'
            //         responder.SendFrame("World");
            //     }
            // }
            Console.WriteLine("Connecting to hello world server…");
            using (var requester = new SubscriberSocket())
            {
                requester.Connect("tcp://localhost:5555");
                requester.Subscribe("PUB");
                int requestNumber = 0;
                while (true)
                {
                    NetMQMessage arr = requester.ReceiveMultipartMessage();
                    string       str = arr[1].ConvertToString();
                    Console.WriteLine(str);
                    Console.WriteLine(requestNumber++);
                }
            }
        }
Пример #5
0
        private void _Subscire_ReceiveReady(object sender, NetMQSocketEventArgs e)
        {
            var msg = subscriber.ReceiveMultipartMessage();

            if (msg.FrameCount == 0)
            {
                return;
            }
            MarketData data = MarketData.Parser.ParseFrom(msg.Pop().ToByteArray());

            var type = data.Type;

            if (type == MarketData.Types.Type.Stock)
            {
                OnStockQuoteUpdated?.Invoke(Transfer(data.StockData));
            }
            else if (type == MarketData.Types.Type.Transaction)
            {
                OnTransactionUpdated?.Invoke(Transfer(data.TransactionData));
            }
            else if (type == MarketData.Types.Type.Order)
            {
                OnOrderUpdated?.Invoke(Transfer(data.OrderData));
            }
            else if (type == MarketData.Types.Type.Queue)
            {
                OnOrderQueueUpdated?.Invoke(Transfer(data.QueueData));
            }
        }
Пример #6
0
        static void Main(string[] args)
        {
            string topic = "Test"; // one of "TopicA" or "TopicB"

            using (var subSocket = new SubscriberSocket(">tcp://127.0.0.1:1234"))
            {
                subSocket.Options.ReceiveHighWatermark = 1000;
                subSocket.Subscribe(topic);
                Console.WriteLine("Subscriber socket connecting...");
                while (true)
                {
                    NetMQMessage message = subSocket.ReceiveMultipartMessage();

                    if (message.FrameCount == 2)
                    {
                        string messageTopicReceived = message[0].ConvertToString();
                        string messageReceived      = message[1].ConvertToString();
                        Console.WriteLine(messageReceived);
                    }
                    else
                    {
                        Console.WriteLine($"Receieved Message with {message.FrameCount} frames but expected only 2 frames.");
                    }
                }
            }
        }
Пример #7
0
        private void OnSubscriberReady(object sender, NetMQSocketEventArgs e)
        {
            // we got a new message from the bus
            // let's forward everything to the shim
            NetMQMessage message = m_subscriber.ReceiveMultipartMessage();

            m_shim.SendMultipartMessage(message);
        }
Пример #8
0
        private IObservable <PooledArraySegment <byte>[]> ZmqSubscribeEndpoint(DaemonEndpointConfig endPoint, string url, string topic, int numMsgSegments = 2)
        {
            return(Observable.Defer(() => Observable.Create <PooledArraySegment <byte>[]>(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(url);
                                    subSocket.Subscribe(topic);

                                    logger.Debug($"Subscribed to {url}/{BitcoinConstants.ZmqPublisherTopicBlockHash}");

                                    while (!tcs.IsCancellationRequested)
                                    {
                                        var msg = subSocket.ReceiveMultipartMessage(numMsgSegments);

                                        // Export all frame data as array of PooledArraySegments
                                        var result = msg.Select(x =>
                                        {
                                            var buf = ArrayPool <byte> .Shared.Rent(x.BufferSize);
                                            Array.Copy(x.ToByteArray(), buf, x.BufferSize);
                                            return new PooledArraySegment <byte>(buf, 0, x.BufferSize);
                                        }).ToArray();

                                        obs.OnNext(result);
                                    }
                                }
                            }

                            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();
                });
            })));
        }
Пример #9
0
        static void Main(string[] args)
        {
            //TestSerilog();

            using (var subSocket = new SubscriberSocket())
            {
                var tcpLocalhost = "tcp://localhost:12345";
                subSocket.Connect(tcpLocalhost);
                subSocket.SubscribeToAnyTopic();
                Console.WriteLine("Waiting to Connect");

                var connectMessage = subSocket.ReceiveMultipartMessage();
                if (connectMessage[0].ConvertToString(Encoding.UTF8) == "CONNECT")
                {
                    Console.WriteLine("Connected");

                    var port = connectMessage[1].ConvertToString(Encoding.UTF8);
                    using (var reqSocket = new RequestSocket(port))
                    {
                        var syncMessage = new NetMQMessage();
                        syncMessage.Append(new NetMQFrame("SYNC", Encoding.UTF8));
                        syncMessage.Append(new NetMQFrame(tcpLocalhost, Encoding.UTF8));
                        reqSocket.SendMultipartMessage(syncMessage);
                        Console.WriteLine("Syncing");
                        var syncOk = reqSocket.ReceiveFrameString();
                        Console.WriteLine($"Synced: {syncOk}");
                    }

                    using (var poller = new NetMQPoller())
                    {
                        poller.Add(subSocket);

                        subSocket.ReceiveReady += (sender, eventArgs) =>
                        {
                            var subMessage = eventArgs.Socket.ReceiveMultipartMessage();
                            var topic      = subMessage[0].ConvertToString(Encoding.UTF8);
                            if (topic == "TopicA")
                            {
                                var decodedMessage = MessagePackSerializer.Deserialize <string>(subMessage[1].ToByteArray());
                                Console.WriteLine(
                                    $"{subMessage[0].ConvertToString(Encoding.UTF8)}: {decodedMessage}");
                            }
                        };

                        poller.RunAsync();

                        Console.WriteLine("Press Enter to Exit");
                        Console.ReadLine();

                        poller.StopAsync();
                    }
                }
            }
        }
Пример #10
0
        public static KeyData SubscriberReceive(this SubscriberSocket socket)
        {
            KeyData kd = new KeyData();

            NetMQMessage msg = socket.ReceiveMultipartMessage();

            kd.Key  = msg[0].ConvertToString();
            kd.Data = msg[1].Buffer;

            return(kd);
        }
Пример #11
0
        bool Receive()
        {
            try
            {
                var msg = socket?.ReceiveMultipartMessage();
                if (msg == null || PubsubUtil.IsStoppingMessage(msg))
                {
                    PublisherLogger.Info("subscribe client 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;
                    var stream    = new MemoryStream(respBytes, 0, respBytes.Length);
                    using (TStreamTransport trans = new TStreamTransport(stream, stream))
                        using (TBinaryProtocol proto = new TBinaryProtocol(trans))
                        {
                            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;
                            ret.Read(proto);
                            mh.DynamicInvoke(topic, ret);
                        }
                }
                return(true);
            }
            catch (Exception e)
            {
                if (e is TargetInvocationException)
                {
                    var ee = e as TargetInvocationException;
                    Log($"SubscribeClient, invoke exception: {(ee).InnerException.Message} \n {ee.InnerException.StackTrace}", Logging.LogLevel.Warn);
                    return(true);
                }
                if (e is TerminatingException)
                {
                    Log($"SubscribeClient, terminated: {e.Message}", Logging.LogLevel.Info);
                    return(false);
                }
                else
                {
                    Log(disposing ? $"SubscribeClient, disposing exception: {e.Message}" : $"subscribe client receive exception: {e.Message} \n {e.StackTrace}",
                        disposing ? Logging.LogLevel.Info : Logging.LogLevel.Error
                        );
                    return(false);
                }
            }
        }
Пример #12
0
        private void ReceiveUpdates()
        {
            _resetEvent.Wait();

            while (!_cancel.IsCancellationRequested)
            {
                var enveloppe = _getMarketUpdates.ReceiveMultipartMessage()
                                .GetMessageFromPublisher <ISequenceItem <TDto> >();

                Updates.Add(enveloppe.Message.Position, enveloppe.Message);
            }
        }
Пример #13
0
 static private void WorkerTypeA()
 {
     using (var typeASubscriber = new SubscriberSocket())
     {
         typeASubscriber.Options.ReceiveHighWatermark = 1000;
         typeASubscriber.Connect("tcp://127.0.0.1:5556");
         typeASubscriber.Subscribe("TypeA");
         while (!cancellationTokenSource.Token.IsCancellationRequested)
         {
             var message = typeASubscriber.ReceiveMultipartMessage(2);
             Console.WriteLine("TypeA : Server Received '{0}'-{1}", message[0].ConvertToString(Encoding.UTF8), message[1].ConvertToString(Encoding.UTF8));
             Task.Factory.StartNew(WorkerGenerate3Offers, message);
         }
         // Cancel the task and exit
         cancellationTokenSource.Token.ThrowIfCancellationRequested();
     }
 }
Пример #14
0
        static void Main(string[] args)
        {
            var subscriberTask = Task.Run(() =>
            {
                using (var subSocket = new SubscriberSocket())
                {
                    var tcpLocalhost = "tcp://127.0.0.1:61044";
                    subSocket.Connect(tcpLocalhost);
                    subSocket.SubscribeToAnyTopic();

                    while (true)
                    {
                        var multipartMesssage = subSocket.ReceiveMultipartMessage();

                        if (multipartMesssage.Count() > 1)
                        {
                            Console.WriteLine($"Topic: {multipartMesssage[0].ConvertToString(Encoding.UTF8)}");
                            Console.WriteLine($"Message: {multipartMesssage[1].ConvertToString(Encoding.UTF8)}");
                        }
                        else
                        {
                            Console.WriteLine($"Bytes: {BitConverter.ToString(multipartMesssage[0].ToByteArray())}");
                        }
                    }
                }
            });

            var requesterTask = Task.Run(() =>
            {
                using (var reqSocket = new RequestSocket("tcp://127.0.0.1:8888"))
                {
                    for (var i = 0; i < 20; i++)
                    {
                        reqSocket.SendFrame("Test Message");
                        reqSocket.ReceiveFrameString(Encoding.UTF8);
                        Thread.Sleep(500);
                    }
                }
            });

            Task.WaitAll(subscriberTask, requesterTask);
            Console.ReadLine();
        }
Пример #15
0
        private void Work()
        {
            using (var subscriberSocket = new SubscriberSocket())
            {
                subscriberSocket.Connect(_brokerEndpoint);
                subscriberSocket.Subscribe("FX");

                while (!_cancel.IsCancellationRequested)
                {
                    var message = subscriberSocket.ReceiveMultipartMessage()
                                  .GetMessageFromProducer <Price>();
                    var price = message.Message;

                    _prices.AddOrUpdate(price.Asset, price, (key, value) =>
                    {
                        return(value);
                    });
                }
            }
        }
Пример #16
0
        private void Work()
        {
            using (var subscriberSocket = new SubscriberSocket())
            {
                subscriberSocket.Options.Identity = _id.ToByteArray();
                subscriberSocket.Connect(_brokerEndpoint);
                subscriberSocket.Subscribe(FxMarket.CCyPairWithUniquePrice);

                while (!_cancel.IsCancellationRequested)
                {
                    var message = subscriberSocket.ReceiveMultipartMessage()
                                  .GetMessageFromProducer <Price>();
                    var price = message.Message;

                    _prices.AddOrUpdate(price.Asset, price, (key, value) =>
                    {
                        return(value);
                    });
                }
            }
        }
Пример #17
0
        private void OnSubscriberMessage(object sender, NetMQSocketEventArgs e)
        {
            // we just forward the message to the actor
            var message = m_subscriber.ReceiveMultipartMessage();

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

            if (topic == WelcomeMessage)
            {
                // TODO: disconnection has happend, we might want to get snapshot from server
            }
            else if (topic == HeartbeatMessage)
            {
                // we got a heartbeat, lets postponed the timer
                m_timeoutTimer.Enable = false;
                m_timeoutTimer.Enable = true;
            }
            else
            {
                m_shim.SendMultipartMessage(message);
            }
        }
Пример #18
0
 private void OnSubscriberReady(object sender, NetMQSocketEventArgs e)
 {
     _shim.SendMultipartMessage(_subscriber.ReceiveMultipartMessage());
 }
Пример #19
0
        public void receiveMsg()
        {
            Task.Factory.StartNew(
                () =>
            {
                try
                {
                    using (var subscriber = new  SubscriberSocket())
                    {
                        subscriber.Connect(_DataPublisherServePubString);
                        subscriber.SubscribeToAnyTopic();
                        //subscriber.Subscribe("StartTrain");
                        //subscriber.Subscribe("StopTrain");
                        //while (true)
                        while (!cts.Token.IsCancellationRequested)
                        {
                            try
                            {
                                cts.Token.ThrowIfCancellationRequested();
                                var responseMessage = subscriber.ReceiveMultipartMessage();
                                if (responseMessage != null)
                                {
                                    var frame = responseMessage.Pop();
                                    var xx    = frame?.ConvertToString();
                                    switch (xx)
                                    {
                                    //历史遥测数据
                                    case "HistoryTm":
                                    //实时遥测数据
                                    case "RealtimeTm":
                                    case "Group1":
                                        frame         = responseMessage.Pop();
                                        var byteArray = frame?.ToByteArray();
                                        Model.TmParameterValues t1 = ProtobufHelper.BytesToObject <Model.TmParameterValues>(byteArray, 0, byteArray.Length);
                                        foreach (var item in t1.ValueList)
                                        {
                                            //RedisHelper.Default.SetStringKey<Model.TmParameterValue>("C513:" + item.Symbol + ":" + DateTools.DoubleToTime(item.Time).ToString("yyyyMMddHHmmssfff"), item);
                                            //SortedSet
                                            RedisHelper.Default.SortedSetObject <Model.TmParameterValue>("C513:" + item.Symbol + ":" + DateTools.DoubleToTime(item.Time).ToString("yyyyMMdd"), item, item.Time);
                                            //Console.WriteLine($"Topic:{xx} 内容:Symbol:{item.Symbol},Time:{item.Time},Value:{item.Value},Code:{item.Code},Text:{item.Text}");
                                        }
                                        hubContext.Clients.All.SendAsync("RecivMessage", new
                                        {
                                            MsgType  = "business",
                                            Symbol   = "TM",
                                            dataList = t1.ValueList
                                        });
                                        break;

                                    case "TrainProcess":    //训练学习进度
                                        frame                = responseMessage.Pop();
                                        var byteArray2       = frame?.ToByteArray();
                                        TrainProcessModel t2 = ProtobufHelper.BytesToObject <TrainProcessModel>(byteArray2, 0, byteArray2.Length);
                                        //Console.WriteLine($"Topic:{xx} 进度:{t2.Process.ToString()}");
                                        hubContext.Clients.All.SendAsync("RecivMessage", new
                                        {
                                            MsgType = "modeltrain",
                                            process = t2.Process
                                        });
                                        break;

                                    case "TMCEvent":    //测控事件检测结果
                                        frame            = responseMessage.Pop();
                                        var byteArray3   = frame?.ToByteArray();
                                        TMCEventModel t3 = ProtobufHelper.BytesToObject <TMCEventModel>(byteArray3, 0, byteArray3.Length);
                                        try
                                        {
                                            msgIntoDB.AddTMCEvent(t3);
                                        }
                                        catch (Exception er)
                                        {
                                            logSend(er.Message);
                                        }
                                        // Console.WriteLine($"Topic:{xx} Name:{t3.Name},BeginTime:{t3.BeginTime},EndTime:{t3.EndTime}");
                                        break;

                                    case "TmParameterAbnormity":    //异常检测结果
                                        frame                   = responseMessage.Pop();
                                        var byteArray4          = frame?.ToByteArray();
                                        TmParameterAbnormity t4 = ProtobufHelper.BytesToObject <TmParameterAbnormity>(byteArray4, 0, byteArray4.Length);
                                        //Console.WriteLine($"Topic:{xx} Symbol:{t4.Symbol},TMCEvent:{t4.TMCEvent},BeginTime:{t4.BeginTime},EndTime:{t4.EndTime}");
                                        try
                                        {
                                            msgIntoDB.AddBusinessAlarm(t4);
                                        }
                                        catch (Exception er)
                                        {
                                            logSend(er.Message);
                                        }
                                        break;

                                    case "TmParameterCycle":    //周期检测结果
                                        frame               = responseMessage.Pop();
                                        var byteArray5      = frame?.ToByteArray();
                                        TmParameterCycle t5 = ProtobufHelper.BytesToObject <TmParameterCycle>(byteArray5, 0, byteArray5.Length);
                                        //Console.WriteLine($"Topic:{xx} Symbol:{t5.Symbol},Cycle:{t5.Cycle}");
                                        try
                                        {
                                            msgIntoDB.AddCycleResult(t5);
                                        }
                                        catch (Exception er)
                                        {
                                            logSend(er.Message);
                                        }
                                        break;

                                    case "ServiceState":    //软件状态
                                        frame                = responseMessage.Pop();
                                        var byteArray6       = frame?.ToByteArray();
                                        ServiceStateModel t6 = ProtobufHelper.BytesToObject <ServiceStateModel>(byteArray6, 0, byteArray6.Length);
                                        Console.WriteLine($"Topic:{xx} Name: {t6.Name},State: {t6.State}");
                                        break;

                                    case "Log":    //软件日志
                                        frame          = responseMessage.Pop();
                                        var byteArray7 = frame?.ToByteArray();
                                        LogModel t7    = ProtobufHelper.BytesToObject <LogModel>(byteArray7, 0, byteArray7.Length);
                                        //Console.WriteLine($"Topic:{xx} Source:{t7.Source},SatelliteTime:{t7.SatelliteTime},SystemTime:{t7.SystemTime},Message:{t7.Message}");
                                        try
                                        {
                                            msgIntoDB.AddLog(t7);
                                        }
                                        catch (Exception er)
                                        {
                                            logSend(er.Message);
                                        }
                                        break;

                                    case "StartTrain":
                                    case "StopTrain":
                                        var tt = xx;
                                        break;

                                    default:
                                        if (xx.IndexOf("BUFFER") >= 0)
                                        {
                                            frame       = responseMessage.Pop();
                                            var rawData = RawData.Parser.ParseFrom(frame?.ToByteArray());
                                            if (rawData != null)
                                            {
                                                var mv = new { MsgType = "business", rawData = rawData, Symbol = rawData.Symbol, Data = ByteConverter.BytesFormatToHexString(rawData.Data.ToByteArray()).Split(' ') };
                                                //RedisHelper.Default.SetStringKey<string>("C513:" + rawData.Symbol + ":" + DateTools.DoubleToTime(rawData.Time).ToString("yyyyMMddHHmmssfff"), JsonConvert.SerializeObject(rawData));
                                                //SortedSet
                                                RedisHelper.Default.SortedSetObject <string>("C513:" + rawData.Symbol + ":" + DateTools.DoubleToTime(rawData.Time).ToString("yyyyMMdd"), JsonConvert.SerializeObject(rawData), rawData.Time);
                                                System.Threading.Thread.Sleep(1000);
                                                // 通过消息处理器上下文发送消息到客户端
                                                hubContext.Clients.All.SendAsync("RecivMessage", mv);
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                            catch (Exception er)
                            {
                                Log4netHelper.Error(this, er);
                                logSend(er.Message);
                            }
                        }
                    }
                }
                catch (Exception er)
                {
                    Log4netHelper.Error(this, er);
                    logSend(er.Message);
                }
            }, cts.Token);
        }
Пример #20
0
        /// <summary>
        /// Runs the agent
        /// </summary>
        public void Run()
        {
            try
            {
                // Check we have an address to connect to.
                if (string.IsNullOrEmpty(ApplicationSettings.Default.XmrNetworkAddress))
                {
                    throw new Exception("Empty XMR Network Address");
                }

                // Get the Private Key
                AsymmetricCipherKeyPair rsaKey = _hardwareKey.getXmrKey();

                // Connect to XMR
                using (SubscriberSocket socket = new SubscriberSocket())
                {
                    // Bind
                    socket.Connect(ApplicationSettings.Default.XmrNetworkAddress);
                    socket.Subscribe("H");
                    socket.Subscribe(_hardwareKey.Channel);

                    // Notify
                    _clientInfoForm.XmrSubscriberStatus = "Connected to " + ApplicationSettings.Default.XmrNetworkAddress;

                    while (!_forceStop)
                    {
                        lock (_locker)
                        {
                            try
                            {
                                NetMQMessage message = socket.ReceiveMultipartMessage();

                                // Update status
                                string statusMessage = "Connected (" + ApplicationSettings.Default.XmrNetworkAddress + "), last activity: " + DateTime.Now.ToString();

                                // Write this out to a log
                                _clientInfoForm.XmrSubscriberStatus = statusMessage;
                                Trace.WriteLine(new LogMessage("XmrSubscriber - Run", statusMessage), LogType.Audit.ToString());

                                // Deal with heart beat
                                if (message[0].ConvertToString() == "H")
                                {
                                    LastHeartBeat = DateTime.Now;
                                    continue;
                                }

                                // Decrypt the message
                                string opened = OpenSslInterop.decrypt(message[2].ConvertToString(), message[1].ConvertToString(), rsaKey.Private);

                                // Decode into a JSON string
                                PlayerAction action = JsonConvert.DeserializeObject <PlayerAction>(opened);

                                // Make sure the TTL hasn't expired
                                if (DateTime.Now > action.createdDt.AddSeconds(action.ttl))
                                {
                                    Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Expired Message: " + action.action), LogType.Info.ToString());
                                    continue;
                                }

                                // Decide what to do with the message, probably raise events according to the type of message we have
                                switch (action.action)
                                {
                                case "commandAction":

                                    // Create a schedule command out of the message
                                    Dictionary <string, string> obj     = JsonConvert.DeserializeObject <Dictionary <string, string> >(opened);
                                    ScheduleCommand             command = new ScheduleCommand();
                                    string code;
                                    obj.TryGetValue("commandCode", out code);
                                    command.Code = code;

                                    new Thread(new ThreadStart(command.Run)).Start();
                                    break;

                                case "collectNow":
                                case RevertToSchedulePlayerAction.Name:
                                    if (OnAction != null)
                                    {
                                        OnAction(action);
                                    }
                                    break;

                                case LayoutChangePlayerAction.Name:

                                    LayoutChangePlayerAction changeLayout = JsonConvert.DeserializeObject <LayoutChangePlayerAction>(opened);

                                    if (OnAction != null)
                                    {
                                        OnAction(changeLayout);
                                    }

                                    break;

                                case OverlayLayoutPlayerAction.Name:
                                    OverlayLayoutPlayerAction overlayLayout = JsonConvert.DeserializeObject <OverlayLayoutPlayerAction>(opened);

                                    if (OnAction != null)
                                    {
                                        OnAction(overlayLayout);
                                    }
                                    break;

                                case "screenShot":
                                    ScreenShot.TakeAndSend();
                                    _clientInfoForm.notifyStatusToXmds();
                                    break;

                                default:
                                    Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Unknown Message: " + action.action), LogType.Info.ToString());
                                    break;
                                }
                            }
                            catch (Exception ex)
                            {
                                // Log this message, but dont abort the thread
                                Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Exception in Run: " + ex.Message), LogType.Error.ToString());
                                _clientInfoForm.XmrSubscriberStatus = "Error. " + ex.Message;
                            }
                        }
                    }
                }

                // Update status
                _clientInfoForm.XmrSubscriberStatus = "Not Running, last activity: " + LastHeartBeat.ToString();

                Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Subscriber Stopped"), LogType.Info.ToString());
            }
            catch (Exception e)
            {
                Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Unable to Subscribe to XMR: " + e.Message), LogType.Info.ToString());
                _clientInfoForm.XmrSubscriberStatus = e.Message;
            }
        }
Пример #21
0
        public static async Task MainZmq(string[] args)
        {
            var txhashpipe        = new Pipe();
            var txhashreadingtask = ReadTxHashPipeAsync(txhashpipe.Reader);

            var txpipe        = new Pipe();
            var txreadingtask = ReadTxPipeAsync(txpipe.Reader);

            var blockhashpipe        = new Pipe();
            var blockhashreadingtask = ReadBlockHashPipeAsync(blockhashpipe.Reader);

            var blockpipe        = new Pipe();
            var blockreadingtask = ReadBlockPipeAsync(blockpipe.Reader);

            try {
                using (var sub = new SubscriberSocket()) {
                    sub.Options.ReceiveHighWatermark = 1000;
                    sub.Connect(KzH.S.BitcoinSvZmqAddress);
                    //sub.Subscribe("hashtx");
                    //sub.Subscribe("rawtx");
                    sub.Subscribe("hashblock");
                    //sub.Subscribe("rawblock");
                    while (_zmqRunning)
                    {
                        var mm = sub.ReceiveMultipartMessage();
                        var ch = mm[0].ConvertToString();
                        if (ch == "hashtx" || ch == "rawtx" || mm.FrameCount == 3)
                        {
                            var bytes = mm[1].Buffer;
                            var count = BitConverter.ToUInt32(mm[2].Buffer);
                            //KzH.WriteLine($"{ch} {count} {bytes.Length}");
                            switch (ch)
                            {
                            case "hashtx": {
                                var w = txhashpipe.Writer;
                                w.Write(bytes);         // 32 bytes transaction ID
                                // count is the transaction number in the mempool.
                                await w.FlushAsync();
                            } break;

                            case "rawtx": {
                                var w = txpipe.Writer;
                                w.Write(bytes);
                                await w.FlushAsync();
                            }
                            break;

                            case "hashblock": {
                                var w = blockhashpipe.Writer;
                                w.Write(bytes);
                                await w.FlushAsync();
                            }
                            break;

                            case "rawblock": {
                                var w = blockpipe.Writer;
                                w.Write(bytes);
                                await w.FlushAsync();
                            }
                            break;

                            default: break;
                            }
                        }
                        else
                        {
                            //KzH.WriteLine($"{ch}");
                        }
                    }
                }
            } catch (Exception e) {
                KzH.WriteLine(e.Message);
            }

            //await Task.WhenAll(txreadingtask, blockreadingtask);
        }
Пример #22
0
        protected virtual void SetupJobUpdates()
        {
	        if (poolConfig.ExternalStratum)
		        return;

            jobRebroadcastTimeout = TimeSpan.FromSeconds(Math.Max(1, poolConfig.JobRebroadcastTimeout));

            var sources = new List<IObservable<bool>>();
            var cancelTimeout = new List<IObservable<bool>>();

            // block updates via ZMQ pub/sub
            var zmqPublisherSocket = extraPoolConfig?.ZmqBlockNotifySocket?.Trim();

            if (!string.IsNullOrEmpty(zmqPublisherSocket))
            {
                var newJobsPubSub = Observable.Defer(()=> Observable.Create<bool>(obs =>
                {
                    var tcs = new CancellationTokenSource();

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

                                    logger.Info($"Subscribed to {zmqPublisherSocket}/{BitcoinConstants.ZmqPublisherTopicBlockHash} for ZMQ pub/sub block updates");

                                    while (true)
                                    {
                                        subSocket.ReceiveMultipartMessage(2);
                                        //var msg = subSocket.ReceiveMultipartMessage(2);
                                        //var topic = msg.First().ConvertToString(Encoding.UTF8);
                                        //var body = msg.Last().ConvertToString(Encoding.UTF8);

                                        obs.OnNext(true);
                                    }
                                }
                            }

                            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();
                    });
                }))
                .Select(_ => Observable.FromAsync(() => UpdateJob(false, "ZMQ pub/sub")))
                .Concat()
                .Publish()
                .RefCount();

                sources.Add(newJobsPubSub);
                cancelTimeout.Add(newJobsPubSub);
            }

            if (poolConfig.BlockRefreshInterval > 0)
            {
                // periodically update block-template from daemon
                var newJobsPolled = Observable.Interval(TimeSpan.FromMilliseconds(poolConfig.BlockRefreshInterval))
                    .Select(_ => Observable.FromAsync(() => UpdateJob(false, "RPC polling")))
                    .Concat()
                    .Where(isNew => isNew)
                    .Publish()
                    .RefCount();

                sources.Add(newJobsPolled);
                cancelTimeout.Add(newJobsPolled);
            }

            else
            {
                // poll for the first successful update after which polling is suspended forever
                var newJobsPolled = Observable.Interval(TimeSpan.FromMilliseconds(poolConfig.BlockRefreshInterval))
                    .Select(_ => Observable.FromAsync(() => UpdateJob(false, "RPC polling")))
                    .Concat()
                    .Where(isNew => isNew)
                    .Take(1)
                    .Publish()
                    .RefCount();

                sources.Add(newJobsPolled);
                cancelTimeout.Add(newJobsPolled);
            }

            // if there haven't been any new jobs for a while, force an update
            var cancelRebroadcast = cancelTimeout.Count > 0 ?
                cancelTimeout.Count > 1 ? Observable.Merge(cancelTimeout) : cancelTimeout.First() :
                Observable.Never<bool>();

            sources.Add(Observable.Timer(jobRebroadcastTimeout)
                .TakeUntil(cancelRebroadcast) // cancel timeout if an actual new job has been detected
                .Do(_ => logger.Debug(() => $"[{LogCat}] No new blocks for {jobRebroadcastTimeout.TotalSeconds} seconds - updating transactions & rebroadcasting work"))
                .Select(x => Observable.FromAsync(() => UpdateJob(true)))
                .Concat()
                .Repeat());

            Jobs = Observable.Merge(sources)
                .Select(GetJobParamsForStratum);
        }
Пример #23
0
        private void StartExternalStratumPublisherListeners()
        {
            foreach (var externalStratum in poolConfig.ExternalStratums)
            {
                var thread = new Thread(arg =>
                {
                    var serializer = new JsonSerializer
                    {
                        ContractResolver = new CamelCasePropertyNamesContractResolver()
                    };

                    var currentHeight = 0L;
                    var lastBlockTime = clock.Now;
                    var config        = (ZmqPubSubEndpointConfig)arg;

                    while (true)
                    {
                        try
                        {
                            using (var subSocket = new SubscriberSocket())
                            {
                                //subSocket.Options.ReceiveHighWatermark = 1000;
                                subSocket.Connect(config.Url);
                                subSocket.Subscribe(config.Topic);

                                logger.Info($"{LogCat}] Monitoring external stratum {config.Url}/{config.Topic}");

                                while (true)
                                {
                                    var msg   = subSocket.ReceiveMultipartMessage(2);
                                    var topic = msg.Pop().ConvertToString(Encoding.UTF8);
                                    var data  = msg.Pop().ConvertToString(Encoding.UTF8);

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

                                    if (string.IsNullOrEmpty(data))
                                    {
                                        logger.Warn(() => $"{LogCat}] Received empty data on ZeroMQ subscriber socket");
                                        continue;
                                    }

                                    // deserialize
                                    TShare share;

                                    using (var reader = new StringReader(data))
                                    {
                                        using (var jreader = new JsonTextReader(reader))
                                        {
                                            share = serializer.Deserialize <TShare>(jreader);
                                        }
                                    }

                                    if (share == null)
                                    {
                                        logger.Error(() => $"{LogCat}] Unable to deserialize share received from ZeroMQ subscriber socket");
                                        continue;
                                    }

                                    // update network stats
                                    blockchainStats.BlockHeight       = share.BlockHeight;
                                    blockchainStats.NetworkDifficulty = share.NetworkDifficulty;

                                    if (currentHeight != share.BlockHeight)
                                    {
                                        blockchainStats.LastNetworkBlockTime = clock.Now;
                                        currentHeight = share.BlockHeight;
                                        lastBlockTime = clock.Now;
                                    }

                                    else
                                    {
                                        blockchainStats.LastNetworkBlockTime = lastBlockTime;
                                    }

                                    // fill in the blacks
                                    share.PoolId  = poolConfig.Id;
                                    share.Created = clock.Now;

                                    // re-publish
                                    shareSubject.OnNext(new ClientShare(null, share));

                                    logger.Info(() => $"[{LogCat}] External share accepted: D={Math.Round(share.Difficulty, 3)}");
                                }
                            }
                        }

                        catch (Exception ex)
                        {
                            logger.Error(ex);
                        }
                    }
                })
                {
                    Name = $"{poolConfig.Id} external stratum listener"
                };

                thread.Start(externalStratum);
            }
        }
Пример #24
0
        private void _Subscire_ReceiveReady(object sender, NetMQSocketEventArgs e)
        {
            var msg = subscriber.ReceiveMultipartMessage();

            OnSubscribeRecieved?.Invoke(msg);
        }
Пример #25
0
        private void StartExternalStratumPublisherListener()
        {
            var thread = new Thread(() =>
            {
                var serializer = new JsonSerializer
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                };

                while (true)
                {
                    try
                    {
                        using (var subSocket = new SubscriberSocket())
                        {
                            //subSocket.Options.ReceiveHighWatermark = 1000;
                            subSocket.Connect(poolConfig.ExternalStratumZmqSocket);
                            subSocket.Subscribe(poolConfig.ExternalStratumZmqTopic);

                            logger.Info($"Monitoring external stratum {poolConfig.ExternalStratumZmqSocket}/{poolConfig.ExternalStratumZmqTopic}");

                            while (true)
                            {
                                var msg   = subSocket.ReceiveMultipartMessage(2);
                                var topic = msg.First().ConvertToString(Encoding.UTF8);
                                var data  = msg.Last().ConvertToString(Encoding.UTF8);

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

                                if (string.IsNullOrEmpty(data))
                                {
                                    logger.Warn(() => $"Received empty data on ZeroMQ subscriber socket");
                                    continue;
                                }

                                // deserialize
                                TShare share;

                                using (var reader = new StringReader(data))
                                {
                                    using (var jreader = new JsonTextReader(reader))
                                    {
                                        share = serializer.Deserialize <TShare>(jreader);
                                    }
                                }

                                if (share == null)
                                {
                                    logger.Error(() => "Unable to deserialize share received from ZeroMQ subscriber socket");
                                    continue;
                                }

                                // fill in the blacks
                                share.PoolId  = poolConfig.Id;
                                share.Created = clock.Now;

                                // re-publish
                                shareSubject.OnNext(new ClientShare(null, share));

                                logger.Info(() => $"[{LogCat}] External share accepted: D={Math.Round(share.Difficulty, 3)}");
                            }
                        }
                    }

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

            thread.Name = $"{poolConfig.Id} external stratum listener";
            thread.Start();
        }