Пример #1
0
 public static void True(bool condition, string format, params object[] args)
 {
     if (!condition)
     {
         throw ExceptionTools.Make(format, args);
     }
 }
Пример #2
0
 public void Dispose()
 {
     Log("Dispose before lock");
     lock (locker)
     {
         Log("Dispose in lock");
         var t = new ThreadInfo();
         if (t.Id != thread.Id)
         {
             Log("Thread mismatch d:{0} != c:{1}", t, thread);
         }
         //AssertTools.True(t.Id == thread.Id, "Thread mismatch d:{0} != c:{1}", t, thread);
         if (!disposed)
         {
             Log("Disposing...");
             ExceptionTools.Try(() => Dispose(disposed), HandleException);
             Log("Dispose OK");
             Counter.Minus(this);
             disposed = true;
             var found = undisposed.Remove(this);
             AssertTools.True(found, "Disposable not found in undisposed");
             Log("Disposed");
         }
     }
 }
Пример #3
0
 public void Dispose()
 {
     while (actions.Count > 0)
     {
         ExceptionTools.Try(actions.Pop(), handler);
     }
 }
Пример #4
0
 public static void NotEmpty(string text, string format, params object[] args)
 {
     if (string.IsNullOrWhiteSpace(text))
     {
         throw ExceptionTools.Make(format, args);
     }
 }
Пример #5
0
 public static void Ip(string text, string format, params object[] args)
 {
     if (!IPAddress.TryParse(text, out var ip))
     {
         throw ExceptionTools.Make(format, args);
     }
 }
Пример #6
0
 private void Loop()
 {
     //provide a non throwing handler to avoid using try here
     while (!quit)
     {
         ExceptionTools.Try(Process);
     }
 }
Пример #7
0
        private static string[] Tokens(string line, char quote = '`')
        {
            var list = new List <string>();
            var sb   = new StringBuilder();

            var e = line.GetEnumerator();

            while (e.MoveNext())
            {
                if (char.IsWhiteSpace(e.Current))
                {
                    continue;
                }
                else if (e.Current == quote)
                {
                    sb.Clear();
                    sb.Append(e.Current);
                    while (e.MoveNext())
                    {
                        sb.Append(e.Current);
                        if (e.Current == quote)
                        {
                            break;
                        }
                    }
                    var part = sb.ToString();
                    if (part.Length >= 2 && part[0] == quote && part[part.Length - 1] == quote)
                    {
                        list.Add(part.Substring(1, part.Length - 2));
                    }
                    else
                    {
                        throw ExceptionTools.Make("Unclosed quote {0}", quote);
                    }
                }
                else
                {
                    sb.Clear();
                    sb.Append(e.Current);
                    while (e.MoveNext())
                    {
                        if (char.IsWhiteSpace(e.Current))
                        {
                            break;
                        }
                        sb.Append(e.Current);
                    }
                    list.Add(sb.ToString());
                }
            }

            return(list.ToArray());
        }
Пример #8
0
        public static TcpClient ConnectWithTimeout(string ip, int port, int timeout)
        {
            var client = new TcpClient();
            var result = client.BeginConnect(ip, port, null, null);

            if (!result.AsyncWaitHandle.WaitOne(timeout, true))
            {
                ExceptionTools.Try(client.Close);
                throw ExceptionTools.Make("Timeout connecting to {0}:{1}", ip, port);
            }
            client.EndConnect(result);
            return(client);
        }
Пример #9
0
        public static SslStream SslWithTimeout(TcpClient client, int timeout)
        {
            var endpoint = client.Client.RemoteEndPoint;
            var stream   = new SslStream(client.GetStream(), false, AcceptAnyCertificate);
            var result   = stream.BeginAuthenticateAsClient(string.Empty, null, null);

            if (!result.AsyncWaitHandle.WaitOne(timeout, true))
            {
                ExceptionTools.Try(stream.Dispose);
                ExceptionTools.Try(client.Close);
                throw ExceptionTools.Make("Timeout authenticating to {0}", endpoint);
            }
            return(stream);
        }
Пример #10
0
 protected override void Dispose(bool disposed)
 {
     ExceptionTools.Try(() =>
     {
         process.StandardInput.Close();
         var to = wait > 0 ? wait : 5000;
         Logger.Trace("WaitForExit {0} {1}ms...", id, to);
         var start   = DateTime.Now;
         var exited  = process.WaitForExit(to);
         var elapsed = DateTime.Now - start;
         Logger.Trace("WaitForExit {0} {1:0}ms {2}", id, elapsed.TotalMilliseconds, exited);
     });
     ExceptionTools.Try(process.Kill);
     ExceptionTools.Try(process.Dispose);
 }
Пример #11
0
        public static void Endpoint(string text, string format, params object[] args)
        {
            var parts = text.Split(':');

            if (parts.Length != 2)
            {
                throw ExceptionTools.Make(format, args);
            }
            if (!IPAddress.TryParse(parts[0], out var ip))
            {
                throw ExceptionTools.Make(format, args);
            }
            if (!int.TryParse(parts[1], out var p))
            {
                throw ExceptionTools.Make(format, args);
            }
        }
Пример #12
0
        public static void SetProperty(object target, string line)
        {
            var parts = line.Split(new char[] { '=' });

            if (parts.Length != 2)
            {
                throw ExceptionTools.Make("Expected 2 parts in {0}", TextTools.Readable(line));
            }
            var propertyName  = parts[0];
            var propertyValue = parts[1];
            var property      = target.GetType().GetProperty(propertyName);

            if (property == null)
            {
                throw ExceptionTools.Make("Property not found {0}", TextTools.Readable(propertyName));
            }
            var value = Convert.ChangeType(propertyValue, property.PropertyType);

            property.SetValue(target, value, null);
        }