Example #1
0
 private void EncodeCompound(CompoundMessage message, BinaryWriter writer)
 {
     writer.Write(message.EncodedMessages.Count);
     foreach (var msg in message.EncodedMessages)
     {
         // TODO: write each message length?
         writer.Write(msg);
     }
 }
Example #2
0
 private void EncodeCompound(CompoundMessage message, BinaryWriter writer)
 {
     writer.Write(message.EncodedMessages.Count);
     foreach(var msg in message.EncodedMessages)
     {
         // TODO: write each message length?
         writer.Write(msg);
     }
 }
Example #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();
                    }
                }
            }
        }