-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathGraphiteTcpClient.cs
62 lines (50 loc) · 1.51 KB
/
GraphiteTcpClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Net.Sockets;
namespace Graphite
{
public class GraphiteTcpClient : IGraphiteClient, IDisposable
{
public string Hostname { get; private set; }
public int Port { get; private set; }
public string KeyPrefix { get; private set; }
private readonly TcpClient _tcpClient;
public GraphiteTcpClient(string hostname, int port = 2003, string keyPrefix = null)
{
Hostname = hostname;
Port = port;
KeyPrefix = keyPrefix;
_tcpClient = new TcpClient(Hostname, Port);
}
public void Send(string path, double value, DateTime timeStamp)
{
try
{
if (!string.IsNullOrWhiteSpace(KeyPrefix))
{
path = KeyPrefix+ "." + path;
}
var message = new PlaintextMessage(path, value, timeStamp).ToByteArray();
_tcpClient.GetStream().Write(message, 0, message.Length);
}
catch
{
// Supress all exceptions for now.
}
}
#region IDisposable
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposing) return;
if (_tcpClient != null)
{
_tcpClient.Close();
}
}
#endregion
}
}