Skip to content

Commit

Permalink
MemoryStream is disposable and holds resources, so wrap it in using()
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Steele authored and Anthony Steele committed Jul 15, 2013
1 parent 395fd9c commit 5f27f40
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading;
using ProtoBuf;

using ProtoBuf;
using Riemann.Proto;

namespace Riemann {
///
/// <summary>Client represents a connection to the Riemann service.</summary>
Expand Down Expand Up @@ -230,9 +231,7 @@ public void SendEvents(IEnumerable<Event> events) {
protoEvent.tags.AddRange(tags);
message.events.Add(protoEvent);
}
var memoryStream = new MemoryStream();
Serializer.Serialize(memoryStream, message);
var array = memoryStream.ToArray();
var array = MessageBytes(message);
try {
Datagram.Send(array);
} catch (SocketException se) {
Expand All @@ -252,6 +251,14 @@ public void SendEvents(IEnumerable<Event> events) {
}
}

private static byte[] MessageBytes(Msg message)
{
using (var memoryStream = new MemoryStream()) {
Serializer.Serialize(memoryStream, message);
return memoryStream.ToArray();
}
}

///
/// <summary>Send a single event to Riemann.</summary>
/// <param name='service'>Name of the service to push.</param>
Expand Down

0 comments on commit 5f27f40

Please sign in to comment.