Ejemplo n.º 1
0
 public void Encode(GossipMessage message, Stream stream)
 {
     using (var writer = new BinaryWriter(stream, Encoding.UTF8, true))
     {
         writer.Write((byte)message.Type);
         switch (message.Type)
         {
             case GossipMessageType.Compound:
                 EncodeCompound((CompoundMessage)message, writer);
                 break;
             case GossipMessageType.Compressed:
                 EncodeCompressed((CompressedMessage)message, writer);
                 break;
             case GossipMessageType.Ack:
                 EncodeAck((AckMessage)message, writer);
                 break;
             case GossipMessageType.Alive:
                 EncodeAlive((AliveMessage)message, writer);
                 break;
             case GossipMessageType.Dead:
                 EncodeDead((DeadMessage)message, writer);
                 break;
             case GossipMessageType.Ping:
                 EncodePing((PingMessage)message, writer);
                 break;
             default:
                 throw new NotSupportedException();
         }
     }
 }
Ejemplo n.º 2
0
        public void Encode(GossipMessage message, Stream stream)
        {
            using (var writer = new BinaryWriter(stream, Encoding.UTF8, true))
            {
                writer.Write((byte)message.Type);
                switch (message.Type)
                {
                case GossipMessageType.Compound:
                    EncodeCompound((CompoundMessage)message, writer);
                    break;

                case GossipMessageType.Compressed:
                    EncodeCompressed((CompressedMessage)message, writer);
                    break;

                case GossipMessageType.Ack:
                    EncodeAck((AckMessage)message, writer);
                    break;

                case GossipMessageType.Alive:
                    EncodeAlive((AliveMessage)message, writer);
                    break;

                case GossipMessageType.Dead:
                    EncodeDead((DeadMessage)message, writer);
                    break;

                case GossipMessageType.Ping:
                    EncodePing((PingMessage)message, writer);
                    break;

                default:
                    throw new NotSupportedException();
                }
            }
        }
Ejemplo n.º 3
0
 private void OnMessageReceived(IPEndPoint remoteEndPoint, GossipMessage message)
 {
     _configuration.Logger.Verbose("Received {@Message} from {RemoteEndPoint}", message, remoteEndPoint);
     switch (message.Type)
     {
         case GossipMessageType.Ping:
             HandlePing(remoteEndPoint, (PingMessage)message);
             break;
         case GossipMessageType.Ack:
             HandleAck(remoteEndPoint, (AckMessage)message);
             break;
         case GossipMessageType.Alive:
             HandleAlive(remoteEndPoint, (AliveMessage)message);
             break;
         case GossipMessageType.Dead:
             HandleDead(remoteEndPoint, (DeadMessage)message);
             break;
         default:
             throw new NotSupportedException();
     }
 }
Ejemplo n.º 4
0
 public CompressedMessage(CompressionType compressionType, GossipMessage message)
 {
     Message         = message;
     CompressionType = compressionType;
 }
Ejemplo n.º 5
0
 public CompressedMessage(CompressionType compressionType, GossipMessage message)
 {
     Message = message;
     CompressionType = compressionType;
 }
Ejemplo n.º 6
0
        public void Send(IPEndPoint remoteEndPoint, GossipMessage message)
        {
            Debug.Assert(remoteEndPoint != null);
            Debug.Assert(message != null);

            List<EventWaitHandle> eventsToTrigger = null;
            using (var ms = new MemoryStream())
            {
                try
                {
                    _configuration.Logger.Verbose("Sending {@Message} to {RemoteEndPoint}", message, remoteEndPoint);
                    _messageEncoder.Encode(message, ms);

                    List<byte[]> messageBytes = null;
                    foreach (var broadcast in _broadcasts.GetBroadcasts(0, 4096))
                    {
                        _configuration.Logger.Verbose("Sending {@Message} to {RemoteEndPoint}", broadcast.Message, remoteEndPoint);
                        if (messageBytes == null)
                        {
                            messageBytes = new List<byte[]>();
                            messageBytes.Add(ms.ToArray());
                            ms.SetLength(0);
                        }

                        messageBytes.Add(broadcast.MessageBytes);

                        if (broadcast.Event != null)
                        {
                            if (eventsToTrigger == null)
                            {
                                eventsToTrigger = new List<EventWaitHandle>();
                            }

                            eventsToTrigger.Add(broadcast.Event);
                        }
                    }

                    if (messageBytes != null)
                    {
                        message = new CompoundMessage(messageBytes);
                        ms.SetLength(0);
                        _messageEncoder.Encode(message, ms);
                    }

                    if (_configuration.CompressionType.HasValue)
                    {
                        _configuration.Logger.Verbose("Compressing {@Message} to {RemoteEndPoint} with {CompressionType}", message, remoteEndPoint, _configuration.CompressionType.Value);
                        message = new CompressedMessage(_configuration.CompressionType.Value, message);
                        ms.SetLength(0);
                        _messageEncoder.Encode(message, ms);
                    }
                }
                catch (Exception ex)
                {
                    _configuration.Logger.Error(ex, "Unable to send message(s) to {RemoteEndPoint}.", remoteEndPoint);
                    return;
                }

                _client.Send(remoteEndPoint, ms.ToArray());

                if (eventsToTrigger != null)
                {
                    foreach (var @event in eventsToTrigger)
                    {
                        @event.Set();
                    }
                }
            }
        }