Ejemplo n.º 1
0
        public static JsonRpcUrl Parse(string packedUrlValue)
        {
            if (packedUrlValue == null)
            {
                throw new ArgumentNullException(nameof(packedUrlValue));
            }

            string[] parts = packedUrlValue.Split('|');
            if (parts.Length != 3 && parts.Length != 4)
            {
                throw new FormatException("Packed url value must contain 3 or 4 parts delimited by '|'");
            }

            string url = parts[0];

            if (!Uri.TryCreate(url, UriKind.Absolute, out Uri? uri) ||
                (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps) ||
                uri.Segments.Count() > 1 ||
                uri.Port == 0)
            {
                throw new FormatException("First part must be a valid url with the format: scheme://host:port");
            }

            string[] endpointValues = parts[1].Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
            if (endpointValues.Length == 0)
            {
                throw new FormatException("Second part must contain at least one valid endpoint value delimited by ';'");
            }

            RpcEndpoint endpoint = RpcEndpoint.None;

            foreach (string endpointValue in endpointValues)
            {
                if (FastEnum.TryParse(endpointValue, ignoreCase: true, out RpcEndpoint parsedEndpoint) &&
                    (parsedEndpoint == RpcEndpoint.Http || parsedEndpoint == RpcEndpoint.Ws))
                {
                    endpoint |= parsedEndpoint;
                }
            }

            if (endpoint == RpcEndpoint.None)
            {
                throw new FormatException($"Second part must contain at least one valid endpoint value (http, https, ws, wss)");
            }

            string[] enabledModules = parts[2].Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
            if (enabledModules.Length == 0)
            {
                throw new FormatException("Third part must contain at least one module delimited by ';'");
            }

            bool isAuthenticated = enabledModules.Any(m => m.ToLower() == "engine");

            // Check if authentication disabled for this url
            if (parts.Length == 4)
            {
                if (parts[3] != "no-auth")
                {
                    throw new FormatException("Fourth part should be \"no-auth\"");
                }

                isAuthenticated = false;
            }

            JsonRpcUrl result = new (uri.Scheme, uri.Host, uri.Port, endpoint, isAuthenticated, enabledModules);

            return(result);
        }
Ejemplo n.º 2
0
 public void ParseIgnoreCase()
 => FluentActions.Invoking(() => FastEnum.Parse <TEnum>("ABCDE", true)).Should().Throw <ArgumentException>();
Ejemplo n.º 3
0
 public void TryParseIgnoreCase()
 {
     FastEnum.TryParse <TEnum>("ABCDE", out var _).Should().BeFalse();
     FastEnum.TryParse <TEnum>("", out var _).Should().BeFalse();
 }
Ejemplo n.º 4
0
 public void ContinuousContainsSameValue()
 => FastEnum.IsContinuous <ContinuousValueContainsSameValueEnum>().Should().Be(true);
Ejemplo n.º 5
0
 public void IsEmpty()
 => FastEnum.IsEmpty <TEnum>().Should().BeTrue();
Ejemplo n.º 6
0
 public void GetUnderlyingType()
 => FastEnum.GetUnderlyingType <TEnum>().Should().Be <TUnderlying>();
Ejemplo n.º 7
0
 public void IsContinuous()
 => FastEnum.IsContinuous <TEnum>().Should().Be(false);
Ejemplo n.º 8
0
 public void Setup()
 {
     _ = UniEnum.GetValues <IntEnum1>();
     _ = FastEnum.GetValues <IntEnum1>();
     _ = Enum.GetValues(typeof(IntEnum1));
 }
Ejemplo n.º 9
0
 public IReadOnlyList <IntEnum1> FastEnum_Values()
 {
     return(FastEnum.GetValues <IntEnum1>());
 }
Ejemplo n.º 10
0
 public bool FastEnum_Short_IsDefined()
 {
     return(FastEnum.IsDefined <ShortEnum1>((short)1000));
 }
    protected override void ChannelRead0(IChannelHandlerContext ctx, DatagramPacket packet)
    {
        IByteBuffer content = packet.Content;
        EndPoint    address = packet.Sender;

        byte[] msgBytes = new byte[content.ReadableBytes];
        content.ReadBytes(msgBytes);

        Interlocked.Add(ref Metrics.DiscoveryBytesReceived, msgBytes.Length);

        if (msgBytes.Length < 98)
        {
            if (_logger.IsDebug)
            {
                _logger.Debug($"Incorrect discovery message, length: {msgBytes.Length}, sender: {address}");
            }
            return;
        }

        byte typeRaw = msgBytes[97];

        if (!FastEnum.IsDefined <MsgType>((int)typeRaw))
        {
            if (_logger.IsDebug)
            {
                _logger.Debug($"Unsupported message type: {typeRaw}, sender: {address}, message {msgBytes.ToHexString()}");
            }
            return;
        }

        MsgType type = (MsgType)typeRaw;

        if (_logger.IsTrace)
        {
            _logger.Trace($"Received message: {type}");
        }

        DiscoveryMsg msg;

        try
        {
            msg            = Deserialize(type, msgBytes);
            msg.FarAddress = (IPEndPoint)address;
        }
        catch (Exception e)
        {
            if (_logger.IsDebug)
            {
                _logger.Debug($"Error during deserialization of the message, type: {type}, sender: {address}, msg: {msgBytes.ToHexString()}, {e.Message}");
            }
            return;
        }

        try
        {
            ReportMsgByType(msg);

            if (!ValidateMsg(msg, type, address, ctx, packet))
            {
                return;
            }

            _discoveryManager.OnIncomingMsg(msg);
        }
        catch (Exception e)
        {
            if (_logger.IsDebug)
            {
                _logger.Error($"DEBUG/ERROR Error while processing message, type: {type}, sender: {address}, message: {msg}", e);
            }
        }
    }
Ejemplo n.º 12
0
 public void Setup()
 {
     UniEnum.IsDefined <ShortEnum1>((short)1000);
     FastEnum.IsDefined <ShortEnum1>((short)1000);
 }
        internal sealed override TEnum?AssertValueType(object value)
        {
            var integer = value as long?;

            return(integer.HasValue ? FastEnum.ToEnum <TEnum>(integer.Value) : base.AssertValueType(value));
        }
Ejemplo n.º 14
0
 public void NotContinuous()
 => FastEnum.IsContinuous <NotContinuousValueEnum>().Should().Be(false);
 internal sealed override TEnum?ReadValue(EmberReader reader, out ParameterType?parameterType)
 {
     parameterType = ParameterType.Enum;
     return(FastEnum.ToEnum <TEnum>(reader.AssertAndReadContentsAsInt64()));
 }
Ejemplo n.º 16
0
 public void GetNames()
 => FastEnum.GetNames <TEnum>().Should().BeEmpty();
 internal sealed override void WriteValue(EmberWriter writer, TEnum?value) =>
 writer.WriteValue(GlowParameterContents.Value.OuterId, FastEnum.ToInt64(value.GetValueOrDefault()));
Ejemplo n.º 18
0
 public void GetMembers()
 => FastEnum.GetMembers <TEnum>().Should().BeEmpty();
Ejemplo n.º 19
0
 public void IsEmpty()
 => FastEnum.IsEmpty <TEnum>().Should().Be(false);
Ejemplo n.º 20
0
 public void GetMaxValue()
 => FastEnum.GetMaxValue <TEnum>().Should().BeNull();
Ejemplo n.º 21
0
 public void IsFlags()
 => FastEnum.IsFlags <TEnum>().Should().Be(false);
Ejemplo n.º 22
0
 public void Continuous()
 => FastEnum.IsContinuous <ContinuousValueEnum>().Should().Be(true);