Exemplo n.º 1
0
        private static async System.Threading.Tasks.Task TestUnixgramSendAsync()
        {
            Console.WriteLine("TestUnixgramSendAsync start:");
            string   filepath   = "/tmp/udp.sock";
            EndPoint ipEndPoint = new UnixDomainSocketEndPoint(filepath);
            string   address    = $"unixgram://{filepath}";

            try { File.Delete(filepath); } catch (Exception) { };
            using (Socket socket = new Socket(AddressFamily.Unix, SocketType.Dgram, ProtocolType.Unspecified))
            {
                socket.Bind(ipEndPoint);
                byte[]        bytes        = new byte[1024];
                var           bytesRecTask = socket.ReceiveAsync(bytes, SocketFlags.None);
                MetricMessage msg          = new MetricMessage("mymeasure", "ping", 0.03d);
                string        sample       = msg.ToString();
                var           transport    = TgfClientProvider.GetTransport(address);
                TgfClient     client       = new TgfClient(transport);
                client.SendMetric("mymeasure", "ping", 0.03d);
                transport.Disconnect();
                int    bytesRec = await bytesRecTask;
                string msgRcv   = Encoding.UTF8.GetString(bytes, 0, bytesRec);
                if (!sample.Equals(msgRcv.Trim()))
                {
                    throw new Exception($"TestUnixgramSendAsync: {msgRcv} not equal {sample}");
                }
            }
            try { File.Delete(filepath); } catch (Exception) { };
            Console.WriteLine("TestUnixgramSendAsync end");
        }
Exemplo n.º 2
0
        private static async System.Threading.Tasks.Task TestUdpSendAsync()
        {
            Console.WriteLine("TestUdpSendAsync start:");
            int         port       = 14231;
            IPHostEntry ipHost     = Dns.GetHostEntry("localhost");
            IPAddress   ipAddr     = ipHost.AddressList[1];
            IPEndPoint  ipEndPoint = new IPEndPoint(ipAddr, port);
            string      address    = $"udp://{ipAddr}:{port}";

            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
            {
                socket.Bind(ipEndPoint);
                byte[]        bytes        = new byte[1024];
                var           bytesRecTask = socket.ReceiveAsync(bytes, SocketFlags.None);
                MetricMessage msg          = new MetricMessage("mymeasure", "ping", 0.03d);
                string        sample       = msg.ToString();
                var           transport    = TgfClientProvider.GetTransport(address);
                TgfClient     client       = new TgfClient(transport);
                client.SendMetric("mymeasure", "ping", 0.03d);
                transport.Disconnect();
                int    bytesRec = await bytesRecTask;
                string msgRcv   = Encoding.UTF8.GetString(bytes, 0, bytesRec);
                if (!sample.Equals(msgRcv.Trim()))
                {
                    throw new Exception($"TestUdpSendAsync: {msgRcv} not equal {sample}");
                }
            }
            Console.WriteLine("TestUdpSendAsync end");
        }
Exemplo n.º 3
0
        private static void Send(string measurement, string address, List <string> inds, List <string> tags)
        {
            string[] tagKeys   = null, tagValues = null, indKeys = null;
            double[] indValues = null;
            if (tags.Count > 0)
            {
                tagKeys   = new string[tags.Count];
                tagValues = new string[tags.Count];
                int i = 0;
                foreach (var s in tags)
                {
                    var tg = s.Split(':');
                    if (tg.Length > 1)
                    {
                        tagKeys[i]   = tg[0];
                        tagValues[i] = tg[1];
                        i++;
                    }
                }
            }
            if (inds.Count > 0)
            {
                indKeys   = new string[inds.Count];
                indValues = new double[inds.Count];
                foreach (var s in inds)
                {
                    int i   = 0;
                    var ind = s.Split(':');
                    if (ind.Length > 1 && double.TryParse(ind[1], out double v))
                    {
                        indKeys[i]   = ind[0];
                        indValues[i] = v;
                        i++;
                    }
                }
            }
            var       transport = TgfClientProvider.GetTransport(address);
            TgfClient client    = new TgfClient(transport);

            {
                client.SendMetric(measurement, indKeys, indValues, tagKeys, tagValues);
            }
            transport.Disconnect();
        }