/// <summary>
        /// Queries <see cref="AppProtocolVersion"/> of given <see cref="BoundPeer"/>.
        /// </summary>
        /// <param name="peer">The <see cref="BoundPeer"/> to query
        /// <see cref="AppProtocolVersion"/>.</param>
        /// <param name="timeout">Timeout value for request.</param>
        /// <returns><see cref="AppProtocolVersion"/> of given peer. </returns>
        public static AppProtocolVersion QueryAppProtocolVersion(
            this BoundPeer peer,
            TimeSpan?timeout = null
            )
        {
            using var dealerSocket = new DealerSocket(ToNetMQAddress(peer));
            var          key  = new PrivateKey();
            var          ping = new Ping();
            var          netMQMessageCodec = new NetMQMessageCodec();
            NetMQMessage request           = netMQMessageCodec.Encode(
                ping,
                key,
                new Peer(key.PublicKey),
                DateTimeOffset.UtcNow,
                default
                );

            TimeSpan timeoutNotNull = timeout ?? TimeSpan.FromSeconds(5);

            if (dealerSocket.TrySendMultipartMessage(timeoutNotNull, request))
            {
                var response = new NetMQMessage();
                if (dealerSocket.TryReceiveMultipartMessage(timeoutNotNull, ref response))
                {
                    return(AppProtocolVersion.FromToken(response.First.ConvertToString()));
                }
            }

            throw new TimeoutException(
                      $"Peer[{peer}] didn't respond within the specified time[{timeout}]."
                      );
        }
Esempio n. 2
0
        public Tuple <string, string> Recognize(byte[] phrase)
        {
            var request = new NetMQMessage();

            request.Append(phrase);
            client.SendMultipartMessage(request);
            var response    = new NetMQMessage();
            var msgReceived = client.TryReceiveMultipartMessage(TimeSpan.FromSeconds(5), ref response);

            return(msgReceived ? ParseResponse(response) : new Tuple <string, string>("NoText", "Не удалось подключиться к серверу :("));
        }
Esempio n. 3
0
        private void PendingMessage(object sender, NetMQSocketEventArgs e)
        {
            NetMQMessage responseMessage = null;

            if (!socket.TryReceiveMultipartMessage(ref responseMessage, 4))
            {
                return;
            }

            Task.Run(() =>
            {
                if (!messageFactory.IsValidResponseMessage(responseMessage))
                {
                    return;
                }

                var(requestId, response) = messageFactory.ExtractResponse(responseMessage);
                taskManager.CompleteTask(requestId, response);
            });
        }