예제 #1
0
        private void EncodeCompressed(CompressedMessage compressedMessage, BinaryWriter writer)
        {
            writer.Write((byte)compressedMessage.CompressionType);
            switch (compressedMessage.CompressionType)
            {
            case CompressionType.Gzip:
                using (var gzipStream = new GZipStream(writer.BaseStream, CompressionLevel.Optimal, true))
                {
                    Encode(compressedMessage.Message, gzipStream);
                }
                break;

            case CompressionType.Deflate:
                using (var deflateStream = new DeflateStream(writer.BaseStream, CompressionLevel.Optimal, true))
                {
                    Encode(compressedMessage.Message, deflateStream);
                }
                break;

            default:
                throw new NotSupportedException();
            }
        }
예제 #2
0
 private void EncodeCompressed(CompressedMessage compressedMessage, BinaryWriter writer)
 {
     writer.Write((byte)compressedMessage.CompressionType);
     switch(compressedMessage.CompressionType)
     {
         case CompressionType.Gzip:
             using(var gzipStream = new GZipStream(writer.BaseStream, CompressionLevel.Optimal, true))
             {
                 Encode(compressedMessage.Message, gzipStream);
             }
             break;
         case CompressionType.Deflate:
             using (var deflateStream = new DeflateStream(writer.BaseStream, CompressionLevel.Optimal, true))
             {
                 Encode(compressedMessage.Message, deflateStream);
             }
             break;
         default:
             throw new NotSupportedException();
     }
 }
예제 #3
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();
                    }
                }
            }
        }