示例#1
0
        internal Message(Frame frame, NsqTcpConnection connection)
        {
            _connection = connection;

            if (frame.Type != FrameType.Message)
            {
                throw new ArgumentException("Frame must have FrameType 'Message'", "frame");
            }

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(frame.Data, TIMESTAMP_START, TIMESTAMP_COUNT);
                Array.Reverse(frame.Data, ATTEMPTS_START, ATTEMPTS_COUNT);
            }

            Timestamp = BitConverter.ToInt64(frame.Data, TIMESTAMP_START);
            Attempts  = BitConverter.ToInt16(frame.Data, ATTEMPTS_START);
            Id        = Encoding.ASCII.GetString(frame.Data, ID_START, ID_COUNT);

            // Data
            var dataLength = frame.Data.Length - DATA_START;

            Body = new byte[dataLength];
            Array.ConstrainedCopy(frame.Data, DATA_START, Body, 0, dataLength);
        }
示例#2
0
        void ConnectButton_Click(object sender, EventArgs e)
        {
            var host = Host.Text;
            var port = int.Parse(Port.Text);

            var options = ConsumerOptions.Parse(string.Format("nsqd={0}:{1}", host, port));
            options.Topic = TopicTextBox.Text;
            options.Channel = ChannelTextBox.Text;
            _nsq = new NsqTcpConnection(new DnsEndPoint(host, port), options);
            _nsq.InternalMessages += _nsq_InternalMessages;
            _nsq.Connect(async msg =>
            {
                await c_MessageReceived(msg);
                await msg.FinishAsync();
            });

        }
示例#3
0
        internal Message(Frame frame, NsqTcpConnection connection)
        {
            _connection = connection;

            if (frame.Type != FrameType.Message)
                throw new ArgumentException("Frame must have FrameType 'Message'", "frame");

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(frame.Data, TIMESTAMP_START, TIMESTAMP_COUNT);
                Array.Reverse(frame.Data, ATTEMPTS_START, ATTEMPTS_COUNT);
            }

            Timestamp = BitConverter.ToInt64(frame.Data, TIMESTAMP_START);
            Attempts = BitConverter.ToInt16(frame.Data, ATTEMPTS_START);
            Id = Encoding.ASCII.GetString(frame.Data, ID_START, ID_COUNT);

            // Data
            var dataLength = frame.Data.Length - DATA_START;
            Body = new byte[dataLength];
            Array.ConstrainedCopy(frame.Data, DATA_START, Body, 0, dataLength);
        }
        void LookupTask(object messageHandler)
        {
            MessageHandler handler = (MessageHandler)messageHandler;

            OnInternalMessage("Begin lookup cycle");
            int beginningCount, endingCount,
                added = 0, removed = 0;

            List<DnsEndPoint> currentEndPoints;
            List<NsqAddress> nsqAddresses;
            lock (_connections)
            {
                var tasks = _lookupServers.Select(server => server.LookupAsync(_options.Topic)).ToList();
                var delay = Task.Delay(5000);
                Task.WhenAny(Task.WhenAll(tasks), delay).Wait();

                nsqAddresses =
                    tasks.Where(t => t.Status == TaskStatus.RanToCompletion)
                    .SelectMany(t => t.Result)
                    .Distinct()
                    .ToList();

                var servers =
                    nsqAddresses
                    .Select(add => new DnsEndPoint(add.BroadcastAddress, add.TcpPort))
                    .ToList();

                currentEndPoints = _connections.Keys.ToList();
                var newEndPoints = servers.Except(currentEndPoints).ToList();
                var removedEndPoints = currentEndPoints.Except(servers).ToList();

                foreach (var endPoint in removedEndPoints)
                {
                    var connection = _connections[endPoint];
                    _connections.Remove(endPoint);
                    connection.Dispose();
                    removed++;
                }

                foreach (var endPoint in newEndPoints)
                {
                    if (!_connections.ContainsKey(endPoint))
                    {
                        var connection = new NsqTcpConnection(endPoint, _options, _noRetryBackoff);
                        connection.InternalMessages +=
                            ((EventHandler<InternalMessageEventArgs>)((sender, e) => OnInternalMessage("{0}: {1}", endPoint, e.Message)));
                        try
                        {
                            connection.Connect(handler);
                            _connections[endPoint] = connection;
                            added++;
                        }
                        catch (Exception ex)
                        {
                            // If Connect() fails, move on with life
                            // We'll try again next round
                            OnInternalMessage("Connection to endpoint {0} failed: {1}", endPoint, ex.Message);
                        }
                    }
                }

                beginningCount = currentEndPoints.Count;
                endingCount = _connections.Count;

                SetMaxInFlightWithoutWaitingForInitialConnectionAsync(_maxInFlight).Wait();
            }

            if (_firstDiscoveryCycle)
            {
                _firstConnectionTaskCompletionSource.TrySetResult(true);
                _firstDiscoveryCycle = false;
            }

            OnDiscoveryCompleted(nsqAddresses);
            OnInternalMessage("End lookup cycle. BeginningCount = {0}, EndingCount = {1}, Added = {2}, Removed = {3}", beginningCount, endingCount, added, removed);
        }
        void LookupTask(object messageHandler)
        {
            MessageHandler handler = (MessageHandler)messageHandler;

            OnInternalMessage("Begin lookup cycle");
            int beginningCount, endingCount,
                added = 0, removed = 0;

            List <DnsEndPoint> currentEndPoints;
            List <NsqAddress>  nsqAddresses;

            lock (_connections)
            {
                var tasks = _lookupServers.Select(server => server.LookupAsync(_options.Topic)).ToList();
                var delay = Task.Delay(5000);
                Task.WhenAny(Task.WhenAll(tasks), delay).Wait();

                nsqAddresses =
                    tasks.Where(t => t.Status == TaskStatus.RanToCompletion)
                    .SelectMany(t => t.Result)
                    .Distinct()
                    .ToList();

                var servers =
                    nsqAddresses
                    .Select(add => new DnsEndPoint(add.BroadcastAddress, add.TcpPort))
                    .ToList();

                currentEndPoints = _connections.Keys.ToList();
                var newEndPoints     = servers.Except(currentEndPoints).ToList();
                var removedEndPoints = currentEndPoints.Except(servers).ToList();

                foreach (var endPoint in removedEndPoints)
                {
                    var connection = _connections[endPoint];
                    _connections.Remove(endPoint);
                    connection.Dispose();
                    removed++;
                }

                foreach (var endPoint in newEndPoints)
                {
                    if (!_connections.ContainsKey(endPoint))
                    {
                        var connection = new NsqTcpConnection(endPoint, _options, _noRetryBackoff);
                        connection.InternalMessages +=
                            ((EventHandler <InternalMessageEventArgs>)((sender, e) => OnInternalMessage("{0}: {1}", endPoint, e.Message)));
                        try
                        {
                            connection.Connect(handler);
                            _connections[endPoint] = connection;
                            added++;
                        }
                        catch (Exception ex)
                        {
                            // If Connect() fails, move on with life
                            // We'll try again next round
                            OnInternalMessage("Connection to endpoint {0} failed: {1}", endPoint, ex.Message);
                        }
                    }
                }

                beginningCount = currentEndPoints.Count;
                endingCount    = _connections.Count;

                SetMaxInFlightWithoutWaitingForInitialConnectionAsync(_maxInFlight).Wait();
            }

            if (_firstDiscoveryCycle)
            {
                _firstConnectionTaskCompletionSource.TrySetResult(true);
                _firstDiscoveryCycle = false;
            }

            OnDiscoveryCompleted(nsqAddresses);
            OnInternalMessage("End lookup cycle. BeginningCount = {0}, EndingCount = {1}, Added = {2}, Removed = {3}", beginningCount, endingCount, added, removed);
        }