private void WorkWithPriorityQuery(int maxSize) { ShowName("PriorityQuery"); PriorityQuery <long> prioQ = new PriorityQuery <long>(maxSize); Random rnd = new Random(); for (int i = 0; i < maxSize; i++) { prioQ.Insert(rnd.Next(0, 2000)); } try { prioQ.Insert(6); } catch (Exception e) { Console.WriteLine(e.Message); } Remove(prioQ); try { prioQ.Remove(); } catch (Exception e) { Console.WriteLine(e.Message); } ShowEndMessage(); }
/// <summary> /// Note: Since this is a struct and this function can modify the fields of the struct, /// make sure you are calling this on a "ref" version of the struct, and if it's a member /// of another object, make sure it IS NOT readonly. /// </summary> /// <param name="dnsPriorityQuery">Callback that determines priority to select an address from Dns record. /// Use DnsPriority.(QueryName) for standard queries.</param> public void ForceIPResolution(PriorityQuery <IPAddress> dnsPriorityQuery) { if (ipEndPoint == null) { ipEndPoint = new IPEndPoint(EndPoints.DnsResolve(ipOrHost, dnsPriorityQuery), port); } }
public static void Connect(this Socket socket, ref StringEndPoint endpoint, PriorityQuery <IPAddress> dnsPriorityQuery) { if (endpoint.ipEndPoint == null) { endpoint.ipEndPoint = new IPEndPoint(EndPoints.DnsResolve(endpoint.ipOrHost, dnsPriorityQuery), endpoint.port); } socket.Connect(endpoint.ipEndPoint); }
// // millisecondsRefreshTime: negative Always refresh // millisecondsRefreshTime: 0 Never refresh // millisecondsRefreshTime: positive Refresh after this many milliseconds // public DnsEndPoint(String domainName, Int32 port, PriorityQuery <IPAddress> dnsPriorityQuery, Int64 millisecondRefreshTime) { this.domainName = domainName; this.port = port; this.dnsPriorityQuery = dnsPriorityQuery; this.millisecondRefreshTime = millisecondRefreshTime; this.lastRefreshStopwatchTicks = 0; this.lastRefreshEndPoint = null; }
/// <summary> /// Tries to parse the string as an IPAddress. If that fails, it will resolve /// it as a host name and select the best address using the given priority query. /// </summary> /// <param name="ipOrHost">An ip address or host name</param> /// <param name="dnsPriorityQuery">Callback that determines priority to select an address from Dns record. /// Use DnsPriority.(QueryName) for standard queries.</param> /// <returns>The parsed or resolved ip address.</returns> public static IPAddress ParseIPOrResolveHost(String ipOrHost, PriorityQuery <IPAddress> dnsPriorityQuery) { IPAddress ip; if (IPParser.TryParse(ipOrHost, out ip)) { return(ip); } return(DnsResolve(ipOrHost, dnsPriorityQuery)); }
static void Main(string[] args) { RegisterMappings.Register(); var categories = new CategoryQuery().GetAll(); Console.WriteLine("Categories"); foreach (var category in categories) { Console.WriteLine($"\t{category.Id} | {category.Description}"); } var people = new PersonQuery().GetAll(); Console.WriteLine("People and categories"); foreach (var person in people) { Console.WriteLine($"\t{person.Id} | {person.Name} | {person.Age} | {person.CategoryId} | {person.Category.Description}"); } var peopleProjects = new PersonQuery().GetAllWithProjects(); Console.WriteLine("People and projects"); foreach (var person in peopleProjects) { Console.WriteLine($"\t{person.Id} | {person.Name}"); if (person.Projects != null) { foreach (var project in person.Projects) { Console.WriteLine($"\t\t{project.Id} | {project.Name}"); } } else { Console.WriteLine($"\t\tNo projects"); } } var priorities = new PriorityQuery().GetAll(); Console.WriteLine("Priorities"); foreach (var priority in priorities) { Console.WriteLine($"\t{priority.Id} | {priority.Description}"); } var tickets = new TicketQuery().GetAll(); Console.WriteLine("Tickets"); foreach (var ticket in tickets) { Console.WriteLine($"\t{ticket.Description} | {ticket.PersonId} | {(ticket.Person?.Name ?? "No requester")} | {(ticket.Person?.CategoryId.ToString() ?? "No category id")} | {(ticket.Person?.Category?.Description ?? "No category")} | {(ticket.PriorityId.ToString() ?? "No priority Id")} | {(ticket.Priority?.Description ?? "No Priority")}"); } }
/// <summary> /// Resolves the DNS host name, and uses the given <paramref name="dnsPriorityQuery"/> to determine which address to use. /// Note: in order to distinguish between dns resolution errors and having no suitable addresses, /// it is recommended to not ignore any addresses in your priority query, but instead, /// give them low priority and check if the address family is suitable in the return value. /// </summary> /// <param name="host">host to resolve</param> /// <param name="dnsPriorityQuery">Callback that determines priority to select an address from Dns record. /// Use DnsPriority.(QueryName) for standard queries.</param> /// <returns>The resolved ip address with the highest priority</returns> public static PriorityValue <IPAddress> TryDnsResolve(String host, PriorityQuery <IPAddress> dnsPriorityQuery) { IPHostEntry hostEntry = Dns.GetHostEntry(host); IPAddress[] addresses = hostEntry.AddressList; if (hostEntry == null || addresses == null || addresses.Length <= 0) { return(new PriorityValue <IPAddress>(Priority.Ignore, null)); } return(addresses.PrioritySelect(dnsPriorityQuery)); }
public void Connect(Socket socket, PriorityQuery <IPAddress> dnsPriorityQuery, ProxyConnectOptions proxyOptions, ref BufStruct buf) { if (proxy == null) { targetEndPoint.ForceIPResolution(dnsPriorityQuery); socket.Connect(targetEndPoint.ipEndPoint); } else { proxy.ProxyConnectTcp(socket, ref targetEndPoint, proxyOptions, ref buf); } }
public static void Connect(this Socket socket, More.Net.InternetHost host, PriorityQuery <IPAddress> dnsPriorityQuery, More.Net.ProxyConnectOptions proxyOptions, ref BufStruct buf) { if (host.proxy == null) { host.targetEndPoint.ForceIPResolution(dnsPriorityQuery); socket.Connect(host.targetEndPoint.ipEndPoint); } else { host.proxy.ProxyConnectTcp(socket, ref host.targetEndPoint, proxyOptions, ref buf); } }
public static Proxy ParseProxy(String proxySpecifier, PriorityQuery <IPAddress> dnsPriorityQuery, Proxy proxyForProxy) { // format // http:<ip-or-host>:<port> // socks4:<ip-or-host>:<port> if (proxySpecifier == null || proxySpecifier.Length <= 0) { return(default(Proxy)); } String[] splitStrings = proxySpecifier.Split(':'); if (splitStrings.Length != 3) { throw new FormatException(String.Format("Invalid proxy '{0}', expected 'http:<host>:<port>', 'socks4:<host>:<port>' or 'socks5:<host>:<port>'", proxySpecifier)); } String proxyTypeString = splitStrings[0]; String ipOrHost = splitStrings[1]; String portString = splitStrings[2]; UInt16 port; if (!UInt16Parser.TryParse(portString, out port)) { throw new FormatException(String.Format("Invalid port '{0}'", portString)); } InternetHost proxyHost = new InternetHost(ipOrHost, port, dnsPriorityQuery, proxyForProxy); if (proxyTypeString.Equals("socks4", StringComparison.CurrentCultureIgnoreCase)) { return(new Socks4Proxy(proxyHost, null)); } else if (proxyTypeString.Equals("socks5", StringComparison.CurrentCultureIgnoreCase)) { return(new Socks5NoAuthenticationConnectSocket(proxyHost)); } else if (proxyTypeString.Equals("http", StringComparison.CurrentCultureIgnoreCase)) { return(new HttpProxy(proxyHost)); } else if (proxyTypeString.Equals("gateway", StringComparison.CurrentCultureIgnoreCase)) { return(new GatewayProxy(proxyHost)); } else if (proxyTypeString.Equals("httpconnect", StringComparison.CurrentCultureIgnoreCase)) { return(new HttpConnectProxyProxy(proxyHost)); } throw new FormatException(String.Format("Unexpected proxy type '{0}', expected 'gateway', 'http', 'httpconnect', 'socks4' or 'socks5'", proxyTypeString)); }
/// <summary> /// Strips proxies from connection specifier. /// Proxies are configured before the '%' sign, i.e. socks4:myproxy:1080%host. /// You can also configure multiple proxies, i.e. gateway:my-gateway-proxy:8080%socks4:myproxy:1080%host. /// </summary> /// <param name="connectorString">A connector string with potential proxies configured.</param> /// <param name="dnsPriorityQuery">Callback that determines priority to select an address from Dns record. /// Use DnsPriority.(QueryName) for standard queries.</param> /// <param name="proxy">The proxy (or proxies) stripped from the connector string.</param> /// <returns>Connector string with proxies stripped. Also returns the parsed proxies.</returns> public static String StripAndParseProxies(String connectorString, PriorityQuery <IPAddress> dnsPriorityQuery, out Proxy proxy) { proxy = null; while (true) { int percentIndex = connectorString.IndexOf('%'); if (percentIndex < 0) { return(connectorString); } proxy = ParseProxy(connectorString.Remove(percentIndex), dnsPriorityQuery, proxy); connectorString = connectorString.Substring(percentIndex + 1); } }
/// <summary> /// Resolves the DNS host name, and uses the given <paramref name="dnsPriorityQuery"/> to determine which address to use. /// </summary> /// <param name="host">host to resolve</param> /// <param name="dnsPriorityQuery">Callback that determines priority to select an address from Dns record. /// Use DnsPriority.(QueryName) for standard queries.</param> /// <returns>The resolved ip address and it's priority</returns> public static IPAddress DnsResolve(String host, PriorityQuery <IPAddress> dnsPriorityQuery) { IPHostEntry hostEntry = Dns.GetHostEntry(host); IPAddress[] addresses = hostEntry.AddressList; if (hostEntry == null || addresses == null || addresses.Length <= 0) { throw new DnsException("host"); } var priorityValue = addresses.PrioritySelect(dnsPriorityQuery); if (priorityValue.value == null) { throw new NoSuitableAddressesException(host, addresses); } return(priorityValue.value); }
public static InternetHost FromIPOrHostWithOptionalPort( String ipOrHostOptionalPort, UInt16 defaultPort, PriorityQuery <IPAddress> dnsPriorityQuery, Proxy proxy) { Int32 colonIndex = ipOrHostOptionalPort.IndexOf(':'); if (colonIndex >= 0) { // NOTE: I could parse this without creating another string String portString = ipOrHostOptionalPort.Substring(colonIndex + 1); if (!UInt16Parser.TryParse(portString, out defaultPort)) { throw new FormatException(String.Format("Port '{0}' could not be parsed as a 2 byte unsigned integer", portString)); } ipOrHostOptionalPort = ipOrHostOptionalPort.Remove(colonIndex); } return(new InternetHost(ipOrHostOptionalPort, defaultPort, dnsPriorityQuery, proxy)); }
public static InternetHost FromIPOrHostWithPort(String ipOrHostWithPort, PriorityQuery <IPAddress> dnsPriorityQuery, Proxy proxy) { Int32 colonIndex = ipOrHostWithPort.IndexOf(':'); if (colonIndex < 0) { throw new FormatException(String.Format("Missing colon to designate the port on '{0}'", ipOrHostWithPort)); } // NOTE: I could parse this without creating another string String portString = ipOrHostWithPort.Substring(colonIndex + 1); UInt16 port; if (!UInt16Parser.TryParse(portString, out port)) { throw new FormatException(String.Format("Port '{0}' could not be parsed as a 2 byte unsigned integer", portString)); } String ipOrHost = ipOrHostWithPort.Remove(colonIndex); return(new InternetHost(ipOrHost, port, dnsPriorityQuery, proxy)); }
public DnsEndPoint(String domainName, Int32 port, PriorityQuery <IPAddress> dnsPriorityQuery) : this(domainName, port, dnsPriorityQuery, 0) { }
public static PriorityValue <T> PrioritySelect <T>(this IEnumerable <T> values, PriorityQuery <T> priorityQuery) { PriorityValue <T> highestPriorityValue = new PriorityValue <T>(Priority.Ignore, default(T)); foreach (var value in values) { Priority priority = priorityQuery(value); if (priority.IsHighest) { return(new PriorityValue <T>(priority, value)); } if (priority.value > highestPriorityValue.priority.value) { highestPriorityValue = new PriorityValue <T>(priority, value); } } return(highestPriorityValue); }
public InternetHost(String ipOrHost, UInt16 port, PriorityQuery <IPAddress> dnsPriorityQuery, Proxy proxy) { this.targetEndPoint = new StringEndPoint(ipOrHost, port); this.dnsPriorityQuery = dnsPriorityQuery; this.proxy = proxy; }
// neverRefresh: true to never refresh, false to always refresh public DnsEndPoint(String domainName, Int32 port, PriorityQuery <IPAddress> dnsPriorityQuery, Boolean neverRefresh) : this(domainName, port, dnsPriorityQuery, neverRefresh ? 0 : -1) { }
public InternetHost(InternetHost other, UInt16 port) { this.targetEndPoint = new StringEndPoint(other.targetEndPoint, port); this.dnsPriorityQuery = other.dnsPriorityQuery; this.proxy = other.proxy; }
public InternetHost(InternetHost other, Proxy proxy) { this.targetEndPoint = other.targetEndPoint; this.dnsPriorityQuery = other.dnsPriorityQuery; this.proxy = proxy; }