Exemple #1
0
        internal static EndPoint TryParseEndPoint(string endpoint)
        {
            if (string.IsNullOrWhiteSpace(endpoint))
            {
                return(null);
            }
            string host;
            int    port;
            int    i = endpoint.IndexOf(':');

            if (i < 0)
            {
                host = endpoint;
                port = 0;
            }
            else
            {
                host = endpoint.Substring(0, i);
                var portAsString = endpoint.Substring(i + 1);
                if (string.IsNullOrEmpty(portAsString))
                {
                    return(null);
                }
                if (!Format.TryParseInt32(portAsString, out port))
                {
                    return(null);
                }
            }
            if (string.IsNullOrWhiteSpace(host))
            {
                return(null);
            }

            return(Format.ParseEndPoint(host, port));
        }
        // true if ready to be completed (i.e. false if re-issued to another server)
        public virtual bool SetResult(PhysicalConnection connection, Message message, RawResult result)
        {
            if (result.IsError)
            {
                var  bridge  = connection.Bridge;
                var  server  = bridge.ServerEndPoint;
                bool log     = !message.IsInternalCall;
                bool isMoved = result.AssertStarts(MOVED);
                if (isMoved || result.AssertStarts(ASK))
                {
                    log = false;
                    string[] parts = result.GetString().Split(StringSplits.Space, 3);
                    int      hashSlot;
                    EndPoint endpoint;
                    if (Format.TryParseInt32(parts[1], out hashSlot) &&
                        (endpoint = Format.TryParseEndPoint(parts[2])) != null)
                    {
                        // no point sending back to same server, and no point sending to a dead server
                        if (!Equals(server.EndPoint, endpoint))
                        {
                            if (bridge.Multiplexer.TryResend(hashSlot, message, endpoint, isMoved))
                            {
                                connection.Multiplexer.Trace(message.Command + " re-issued to " + endpoint, isMoved ? "MOVED" : "ASK");
                                return(false);
                            }
                        }
                    }
                }

                string err = result.GetString();
                if (log)
                {
                    bridge.Multiplexer.OnErrorMessage(server.EndPoint, err);
                }
                connection.Multiplexer.Trace("Completed with error: " + err + " (" + GetType().Name + ")", ToString());
                ServerFail(message, err);
            }
            else
            {
                bool coreResult = SetResultCore(connection, message, result);
                if (coreResult)
                {
                    connection.Multiplexer.Trace("Completed with success: " + result.ToString() + " (" + GetType().Name + ")", ToString());
                }
                else
                {
                    UnexpectedResponse(message, result);
                }
            }
            return(true);
        }
Exemple #3
0
 public static int ParseInt32(string key, string value, int minValue = int.MinValue, int maxValue = int.MaxValue)
 {
     if (!Format.TryParseInt32(value, out int tmp))
     {
         throw new ArgumentOutOfRangeException("Keyword '" + key + "' requires an integer value");
     }
     if (tmp < minValue)
     {
         throw new ArgumentOutOfRangeException("Keyword '" + key + "' has a minimum value of " + minValue);
     }
     if (tmp > maxValue)
     {
         throw new ArgumentOutOfRangeException("Keyword '" + key + "' has a maximum value of " + maxValue);
     }
     return(tmp);
 }
 public static int ParseInt32(string key, string value, int minValue = int.MinValue, int maxValue = int.MaxValue)
 {
     if (!Format.TryParseInt32(value, out int tmp))
     {
         throw new ArgumentOutOfRangeException(key, $"Keyword '{key}' requires an integer value; the value '{value}' is not recognised.");
     }
     if (tmp < minValue)
     {
         throw new ArgumentOutOfRangeException(key, $"Keyword '{key}' has a minimum value of '{minValue}'; the value '{tmp}' is not permitted.");
     }
     if (tmp > maxValue)
     {
         throw new ArgumentOutOfRangeException(key, $"Keyword '{key}' has a maximum value of '{maxValue}'; the value '{tmp}' is not permitted.");
     }
     return(tmp);
 }
        internal static EndPoint TryParseEndPoint(string addressWithPort)
        {
            // Adapted from IPEndPointParser in Microsoft.AspNetCore
            // Link: https://github.com/aspnet/BasicMiddleware/blob/f320511b63da35571e890d53f3906c7761cd00a1/src/Microsoft.AspNetCore.HttpOverrides/Internal/IPEndPointParser.cs#L8
            // Copyright (c) .NET Foundation. All rights reserved.
            // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
            string addressPart = null;
            string portPart    = null;

            if (string.IsNullOrEmpty(addressWithPort))
            {
                return(null);
            }

            var lastColonIndex = addressWithPort.LastIndexOf(':');

            if (lastColonIndex > 0)
            {
                // IPv4 with port or IPv6
                var closingIndex = addressWithPort.LastIndexOf(']');
                if (closingIndex > 0)
                {
                    // IPv6 with brackets
                    addressPart = addressWithPort.Substring(1, closingIndex - 1);
                    if (closingIndex < lastColonIndex)
                    {
                        // IPv6 with port [::1]:80
                        portPart = addressWithPort.Substring(lastColonIndex + 1);
                    }
                }
                else
                {
                    // IPv6 without port or IPv4
                    var firstColonIndex = addressWithPort.IndexOf(':');
                    if (firstColonIndex != lastColonIndex)
                    {
                        // IPv6 ::1
                        addressPart = addressWithPort;
                    }
                    else
                    {
                        // IPv4 with port 127.0.0.1:123
                        addressPart = addressWithPort.Substring(0, firstColonIndex);
                        portPart    = addressWithPort.Substring(firstColonIndex + 1);
                    }
                }
            }
            else
            {
                // IPv4 without port
                addressPart = addressWithPort;
            }

            int?port = 0;

            if (portPart != null)
            {
                if (Format.TryParseInt32(portPart, out var portVal))
                {
                    port = portVal;
                }
                else
                {
                    // Invalid port, return
                    return(null);
                }
            }

            if (IPAddress.TryParse(addressPart, out IPAddress address))
            {
                return(new IPEndPoint(address, port ?? 0));
            }
            return(new DnsEndPoint(addressPart, port ?? 0));
        }