public static void True(bool condition, string format, params object[] args) { if (!condition) { 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); } }
public static void NotEmpty(string text, string format, params object[] args) { if (string.IsNullOrWhiteSpace(text)) { throw ExceptionTools.Make(format, args); } }
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); }
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); }