Ejemplo n.º 1
0
        private static void SendToServer(string server, int port, IEnumerable <string> metrics, bool stream)
        {
            UdpMessenger messenger = null;
            SimpleObjectPool <UdpMessenger> serverPool = null;

            try
            {
                serverPool = _messengerPool.GetOrAdd(string.Format(CultureInfo.InvariantCulture, "{0}:{1}", server, port),
                                                     new SimpleObjectPool <UdpMessenger>(3, pool => new UdpMessenger(server, port)));
                messenger = serverPool.Pop();

                //all used up, sorry!
                if (null == messenger)
                {
                    return;
                }

                if (stream)
                {
                    messenger.StreamMetrics(metrics);
                }
                else
                {
                    messenger.SendMetrics(metrics);
                }
            }
            finally
            {
                if (null != serverPool && null != messenger)
                {
                    serverPool.Push(messenger);
                }
            }
        }
Ejemplo n.º 2
0
        public static string Sanitize(this string key)
        {
            if (IsValidKey(key))
            {
                return(key);
            }

            StringBuilder sanitized = null;

            try
            {
                sanitized = _builderPool.Pop();
                if (null != sanitized)
                {
                    sanitized.Clear();
                    sanitized.Append(key);
                }
                //autogrow pool as necessary
                else
                {
                    sanitized = new StringBuilder(key);
                }

                for (int i = 0; i < key.Length; ++i)
                {
                    if (_badChars.Contains(key[i]))
                    {
                        sanitized[i] = '_';
                    }
                }

                return(sanitized.ToString());
            }
            finally
            {
                if (null != sanitized)
                {
                    _builderPool.Push(sanitized);
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>	Represents a method that is called when an asynchronous operation completes. </summary>
 /// <remarks>	Adds the arguments back to the pool for future use. </remarks>
 /// <param name="e">	The event that is signaled. </param>
 protected override void OnCompleted(SocketAsyncEventArgs e)
 {
     base.OnCompleted(e);
     _parentPool.Push(this);
 }