private static async Task <bool> GetParamAsync(string key, XmlRpcValue resultValue, bool useCache) { string mappepKey = Names.Resolve(key); if (useCache) { lock (gate) { if (cachedValues.TryGetValue(mappepKey, out var cachedValue) && !cachedValue.IsEmpty) { resultValue.Copy(cachedValue); return(true); } } } var parm = new XmlRpcValue(); var result = new XmlRpcValue(); parm.Set(0, ThisNode.Name); parm.Set(1, mappepKey); resultValue.SetArray(0); bool ret = await Master.ExecuteAsync("getParam", parm, result, resultValue, false).ConfigureAwait(false); if (ret && useCache) { lock (gate) { cachedValues[mappepKey] = resultValue.Clone(); } } return(ret); }
public bool ValidateXmlRpcResponse(string method, XmlRpcValue response, XmlRpcValue payload) { if (response.Type != XmlRpcType.Array) { return(ValidateFailed(method, "didn't return an array -- {0}", response)); } if (response.Count != 3) { return(ValidateFailed(method, "didn't return a 3-element array -- {0}", response)); } if (response[0].Type != XmlRpcType.Int) { return(ValidateFailed(method, "didn't return an int as the 1st element -- {0}", response)); } int status_code = response[0].GetInt(); if (response[1].Type != XmlRpcType.String) { return(ValidateFailed(method, "didn't return a string as the 2nd element -- {0}", response)); } string status_string = response[1].GetString(); if (status_code != 1) { return(ValidateFailed(method, "returned an error ({0}): [{1}] -- {2}", status_code, status_string, response)); } switch (response[2].Type) { case XmlRpcType.Array: { payload.SetArray(0); for (int i = 0; i < response[2].Count; i++) { payload.Set(i, response[2][i]); } } break; case XmlRpcType.Int: case XmlRpcType.Double: case XmlRpcType.String: case XmlRpcType.Boolean: payload.Copy(response[2]); break; case XmlRpcType.Empty: break; default: throw new ArgumentException("Unhandled valid XML-RPC payload type: " + response[2].Type, nameof(response)); } return(true); }
public void CheckStructRoundTrip() { var today = DateTime.Today; var v = new XmlRpcValue(); v.Set("memberInt", 789); v.Set("memberBool", true); v.Set("memberDouble", 765.678); v.Set("memberBinary", new byte[] { 0, 2, 4, 6, 8, 10, 12 }); v.Set("memberString", "qwerty"); v.Set("memberDate", today); var innerArray = new XmlRpcValue(1, 2.0, "three", today); v.Set("memberArray", innerArray); var innerStruct = new XmlRpcValue(); innerStruct.Copy(v); v.Set("memberStruct", innerStruct); var xml = v.ToXml(); var w = new XmlRpcValue(); w.FromXml(xml); Assert.Equal(8, w.Count); Assert.Equal(XmlRpcType.Struct, w.Type); Assert.True(w.HasMember("memberInt")); Assert.True(w.HasMember("memberBool")); Assert.True(w.HasMember("memberDouble")); Assert.True(w.HasMember("memberBinary")); Assert.True(w.HasMember("memberString")); Assert.True(w.HasMember("memberDate")); Assert.True(w.HasMember("memberArray")); Assert.True(w.HasMember("memberStruct")); Assert.Equal(XmlRpcType.Int, w["memberInt"].Type); Assert.Equal(XmlRpcType.Boolean, w["memberBool"].Type); Assert.Equal(XmlRpcType.Double, w["memberDouble"].Type); Assert.Equal(XmlRpcType.Base64, w["memberBinary"].Type); Assert.Equal(XmlRpcType.String, w["memberString"].Type); Assert.Equal(XmlRpcType.DateTime, w["memberDate"].Type); Assert.Equal(XmlRpcType.Array, w["memberArray"].Type); Assert.Equal(XmlRpcType.Struct, w["memberStruct"].Type); Assert.Equal(4, w["memberArray"].Count); Assert.Equal(7, w["memberStruct"].Count); Action <XmlRpcValue> checkValueOneLevel = (XmlRpcValue value) => { Assert.Equal(789, value["memberInt"].GetInt()); Assert.True(value["memberBool"].GetBool()); Assert.Equal(765.678, value["memberDouble"].GetDouble(), 3); Assert.Equal(new byte[] { 0, 2, 4, 6, 8, 10, 12 }, value["memberBinary"].GetBinary()); Assert.Equal("qwerty", value["memberString"].GetString()); Assert.True(Math.Abs((today - value["memberDate"].GetDateTime()).TotalSeconds) < 1); var a = value["memberArray"]; Assert.Equal(4, a.Count); Assert.Equal(XmlRpcType.Array, a.Type); Assert.Equal(1, a[0].GetInt()); Assert.Equal(2.0, a[1].GetDouble()); Assert.Equal("three", a[2].GetString()); Assert.True(Math.Abs((today - a[3].GetDateTime()).TotalSeconds) < 1); }; checkValueOneLevel(w); checkValueOneLevel(w["memberStruct"]); }