示例#1
0
        public bool CallMethod <TReturn, TReturnBase>([NotNull] XmlRpcMethodCall <TReturn, TReturnBase> methodCall, int timeout) where TReturn : XmlRpcType <TReturnBase>, new()
        {
            var methodHandle = xmlRpcClient.SendRequest(methodCall.GenerateCallXml().ToString());

            if (timeout <= 0)
            {
                return(true);
            }

            if (!methodResponses.TryAdd(methodHandle, null))
            {
                return(false);
            }

            var response = awaitResponse(methodHandle, timeout);

            if (string.IsNullOrWhiteSpace(response))
            {
                return(false);
            }

            try
            {
                if (!methodCall.ParseResponseXml(XDocument.Parse(response, LoadOptions.None).Root))
                {
                    return(false);
                }
            }
            catch
            {
                return(false);
            }

            return(true);
        }
        public void Ctor_WithMultiCallTrue_MultiCallPropertyIsTrue()
        {
            var method = new XmlRpcMethodCall("test");

            var request = new XmlRpcRequest(new[] { method }, true);

            Assert.True(request.IsMultiCall);
        }
        public void Ctor_WithMultiCallFalse_MultiCallPropertyIsFalse()
        {
            var method = new XmlRpcMethodCall("test");

            var request = new XmlRpcRequest(new[] { method }, false);

            Assert.False(request.IsMultiCall);
        }
示例#4
0
        public XmlRpcRequest Build(string methodName, params object[] parameters)
        {
            var methodCall = new XmlRpcMethodCall(methodName, CreateXmlRpcValues(parameters).ToArray());

            var xmlRpcRequest = new XmlRpcRequest(new [] { methodCall }, false);

            return(xmlRpcRequest);
        }
        public void Ctor_WithMethod_HasMethodSetToParameter()
        {
            var method = new XmlRpcMethodCall("test");

            var request = new XmlRpcRequest(new [] { method }, false);

            Assert.Single(request.Methods);
            Assert.Same(method, request.Methods.First());
        }
        public void Ctor_WithMethods_HasMethodsSetToParameter()
        {
            var method  = new XmlRpcMethodCall("test");
            var method1 = new XmlRpcMethodCall("test1");

            var request = new XmlRpcRequest(new[] { method, method1 }, false);

            Assert.Equal(2, request.Methods.Count());
            Assert.Equal(request.Methods, new [] { method, method1 });
        }
示例#7
0
        private XElement CreateMethodCall(XmlRpcMethodCall method)
        {
            if (!method.Parameters.Any())
            {
                return(new XElement(XmlRpcTags.MethodCall, new XElement(XmlRpcTags.MethodName, method.Name)));
            }

            var parameters = CreateParameters(method.Parameters);

            return(new XElement(XmlRpcTags.MethodCall,
                                new XElement(XmlRpcTags.MethodName, method.Name),
                                new XElement(XmlRpcTags.Params, parameters)));
        }
        private static XmlRpcRequest UnwrapMultiCall(XmlRpcMethodCall methodCall)
        {
            var callsParameter = methodCall.Parameters.FirstOrDefault();

            if (callsParameter is not ArrayValue arrayValue)
            {
                throw new InvalidOperationException();
            }

            var callStructs = arrayValue.Value.OfType <StructValue>();
            var methodCalls = callStructs.Select(UnwrapCall);

            return(new XmlRpcRequest(methodCalls, true));
        }
        public void Ctor_ParametersAreGiven_ParametersAreSet()
        {
            var stringValue = new StringValue("SomeValue");
            var intValue    = new IntegerValue(1234);

            var methodCall = new XmlRpcMethodCall("Test", stringValue, intValue);

            Assert.Equal(2, methodCall.Parameters.Count());

            Assert.Same(stringValue, methodCall.Parameters.First());
            Assert.Same(intValue, methodCall.Parameters.Last());

            Assert.Equal("SomeValue", methodCall.Parameters.First().GetValue <string>());
            Assert.Equal(1234, methodCall.Parameters.Last().GetValue <int>());
        }
        public void Ctor_AddParameter_ParameterIsAdd()
        {
            var stringValue = new StringValue("SomeValue");
            var intValue    = new IntegerValue(1234);
            var addedValue  = new StringValue("This is added");

            var methodCall = new XmlRpcMethodCall("Test", stringValue, intValue);

            methodCall.AddParameter(addedValue);

            Assert.Equal(3, methodCall.Parameters.Count());

            Assert.Same(stringValue, methodCall.Parameters.First());
            Assert.Same(intValue, methodCall.Parameters.Skip(1).First());
            Assert.Same(addedValue, methodCall.Parameters.Last());

            Assert.Equal("SomeValue", methodCall.Parameters.First().GetValue <string>());
            Assert.Equal(1234, methodCall.Parameters.Skip(1).First().GetValue <int>());
            Assert.Equal("This is added", methodCall.Parameters.Last().GetValue <string>());
        }
示例#11
0
        public async Task <object> Invoke(XmlRpcMethodCall methodCall)
        {
            Ensure.IsNotNull(methodCall, nameof(methodCall));

            var methodRegistration = _xmlRpcServerMethods.GetMethod(methodCall.Name);

            if (methodRegistration == null)
            {
                throw new MissingMethodException();
            }

            var parameters = GetParameters(methodRegistration.Method.GetParameters(), methodCall.Parameters.ToArray());

            if (!methodRegistration.Method.ReturnType.IsGenericType ||
                methodRegistration.Method.ReturnType.GetGenericTypeDefinition() != typeof(Task <>))
            {
                return(methodRegistration.Method.Invoke(methodRegistration.Target, parameters));
            }

            var task = (Task)methodRegistration.Method.Invoke(methodRegistration.Target, parameters);

            return(await task.ToTask <object>());
        }
        public void Ctor_NameIsCorrect_NameIsSet()
        {
            var methodCall = new XmlRpcMethodCall("Test");

            Assert.Equal("Test", methodCall.Name);
        }
示例#13
0
        private async Task <object> InvokeMethod(XmlRpcMethodCall methodCall)
        {
            Log.Debug($"Invoke method '{methodCall.Name}'");

            return(await _executor.Invoke(methodCall));
        }