Ejemplo n.º 1
0
        public async Task <bool> SendMessageAsync(GossipMessage message)
        {
            if (Connection == null)
            {
                return(false);
            }
            await message.WriteToStreamAsync(Connection.Stream);

            return(true);
        }
Ejemplo n.º 2
0
        public async Task BroadcastMessageAsync(GossipMessage message, Func <GossipClusterMember, bool> selector = null)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            if (selector == null)
            {
                selector = m => true;
            }

            var tasks = new List <Task>();

            foreach (var member in _members.Values.Where(selector))
            {
                tasks.Add(member.SendMessageAsync(message));
            }
            await Task.Run(() =>
                           Task.WaitAll(tasks.ToArray()));
        }
Ejemplo n.º 3
0
        protected async void StartReadingMessageData()
        {
            if (_startedReadingMessageData)
            {
                throw new InvalidOperationException("Already started reading message data");
            }

            _startedReadingMessageData = true;
            try
            {
                while (Client.Connected)
                {
                    var message = await GossipMessage.ReadFromStreamAsync(Stream);

                    if (message == null)
                    {
                        break;
                    }
                    RecordActivity();

                    OnMessageReceived(this, message);
                }
            }
            catch (IOException)
            {
            }
            catch (ObjectDisposedException)
            {
            }
            catch (Exception ex)
            {
                OnException(this, ex);
            }
            finally
            {
                OnDisconnected(this);
            }
        }
Ejemplo n.º 4
0
        public int BroadcastMessage(GossipMessage message, Func <GossipClusterMember, bool> selector = null)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            if (selector == null)
            {
                selector = m => true;
            }

            var waitTasks   = new List <Task>();
            var resultTasks = new List <Task <bool> >();

            foreach (var member in _members.Values.Where(selector))
            {
                var task = member.SendMessageAsync(message);
                waitTasks.Add(task);
                resultTasks.Add(task);
            }
            Task.WaitAll(waitTasks.ToArray());
            return(resultTasks.Count(x => x.Result));
        }
Ejemplo n.º 5
0
        public async Task <bool> SendMessageAsync(GossipMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            if (message.Expires >= GossipTimestampProvider.CurrentTimestamp)
            {
                OnMessageExpired(this, message);
                return(false);
            }

            using (var session = CreateSession())
            {
                if (message.Expires >= GossipTimestampProvider.CurrentTimestamp)
                {
                    OnMessageExpired(this, message);
                    return(false);
                }

                try
                {
                    return(await session.SendMessageAsync(message));
                }
                catch (IOException)
                {
                    session.DisposeAction = c => c.Close();
                    return(false);
                }
                catch (ObjectDisposedException)
                {
                    session.DisposeAction = c => c.Close();
                    return(false);
                }
            }
        }
Ejemplo n.º 6
0
        private static void ProcessSimpleTextMessage(GossipNode node, GossipConnection connection, GossipMessage message)
        {
            var rawMessage = message as RawGossipMessage;

            if (rawMessage == null)
            {
                return;
            }
            if (rawMessage.MessageType == 1)
            {
                Debug.WriteLine(String.Format("Node Info: {0}", node.NodeConfig.Tags.First()));
                Debug.WriteLine(String.Format("Node received: {0}", node.NodeConfig.NodeId.ToHexStringLower()));
                Debug.WriteLine("Message Size: {0} bytes", rawMessage.Size);
                Debug.WriteLine(String.Format("Message String: {0}", Encoding.UTF8.GetString(rawMessage.Buffer)));
            }
        }