public static void True(bool condition, string format, params object[] args) { if (!condition) { throw ExceptionTools.Make(format, args); } }
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"); } } }
public void Dispose() { while (actions.Count > 0) { ExceptionTools.Try(actions.Pop(), handler); } }
public static void NotEmpty(string text, string format, params object[] args) { if (string.IsNullOrWhiteSpace(text)) { throw ExceptionTools.Make(format, args); } }
public static void Ip(string text, string format, params object[] args) { if (!IPAddress.TryParse(text, out var ip)) { throw ExceptionTools.Make(format, args); } }
private void Loop() { //provide a non throwing handler to avoid using try here while (!quit) { ExceptionTools.Try(Process); } }
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()); }
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); }
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); }
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); }
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); } }
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); }