/// <summary> /// TODO: Documentation MethodCallSerializerThreadStart /// </summary> private void MethodCallSerializerThreadStart() { //IPAddress.Loopback, 0 this.methodCallSerializer.Prefixes.Add(this.MethodCallSerializerAddress); this.methodCallSerializer.Start(); while (this.methodCallSerializerIsRunning) { try { HttpListenerContext context = this.methodCallSerializer.GetContext(); MatchCollection matches = Regex.Matches(context.Request.Url.Query, @"(\&|\?)(?:(?<key>.[^\=\&]*)\=(?<val>.[^\=\&]*))"); String to = null; foreach (Match match in matches) { switch (match.Groups["key"].Value) { case "to": to = Uri.UnescapeDataString(match.Groups["val"].Value); break; } if (!String.IsNullOrEmpty(to)) { break; } } StreamReader inputReader = new StreamReader(context.Request.InputStream); String inputString = inputReader.ReadToEnd(); RpcIQ methodCall = new RpcIQ(new XmlDocument()); methodCall.From = this.Stream.JID; methodCall.To = to; methodCall.Type = IQType.set; methodCall.Instruction.SetXmlRpcPayload(inputString); this.xmlRpcContexts.Add(methodCall.ID, context); this.BeginIQ(methodCall, new IqCB(this.GotRpcCall), null); } catch (HttpListenerException) { this.Stop(); } } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="iq"></param> /// <param name="state"></param> private void GotRpcCall(object sender, IQ iq, object state) { if (!this.IsTrusted(iq.From)) { return; } String rpcXmlString = Regex.Replace(iq.Query.FirstChild.OuterXml, " xmlns(.+?)>", ">"); switch (iq.Type) { case IQType.set: if (this.CanReceiveRpcXml) { RpcIQ methodResponse = new RpcIQ(new XmlDocument()); methodResponse.From = this.Stream.JID; methodResponse.To = iq.From; methodResponse.Type = IQType.result; methodResponse.ID = iq.ID; // Make IQ ID behaving like a transaction ID methodResponse.Instruction.SetXmlRpcPayload(this.InvokeXmlRpc(rpcXmlString)); this.BeginIQ(methodResponse, new IqCB(this.GotRpcCall), null); iq.Handled = true; } break; case IQType.result: if (this.CanSendRpcXml && this.xmlRpcContexts.ContainsKey(iq.ID)) { byte[] outputByteArray = Encoding.UTF8.GetBytes(rpcXmlString); MemoryStream outputStream = new MemoryStream(outputByteArray); HttpListenerResponse response = this.xmlRpcContexts[iq.ID].Response; response.ContentLength64 = outputStream.Length; Util.CopyStream(outputStream, response.OutputStream); response.OutputStream.Flush(); this.xmlRpcContexts.Remove(iq.ID); iq.Handled = true; } break; } }
/// <summary> /// TODO: Documentation MethodCallSerializerThreadStart /// </summary> private void MethodCallSerializerThreadStart() { //IPAddress.Loopback, 0 this.methodCallSerializer.Prefixes.Add(this.MethodCallSerializerAddress); this.methodCallSerializer.Start(); while (this.methodCallSerializerIsRunning) { try { HttpListenerContext context = this.methodCallSerializer.GetContext(); MatchCollection matches = Regex.Matches(context.Request.Url.Query, @"(\&|\?)(?:(?<key>.[^\=\&]*)\=(?<val>.[^\=\&]*))"); String to = null; foreach (Match match in matches) { switch (match.Groups["key"].Value) { case "to": to = Uri.UnescapeDataString(match.Groups["val"].Value); break; } if (!String.IsNullOrEmpty(to)) break; } StreamReader inputReader = new StreamReader(context.Request.InputStream); String inputString = inputReader.ReadToEnd(); RpcIQ methodCall = new RpcIQ(new XmlDocument()); methodCall.From = this.Stream.JID; methodCall.To = to; methodCall.Type = IQType.set; methodCall.Instruction.SetXmlRpcPayload(inputString); this.xmlRpcContexts.Add(methodCall.ID, context); this.BeginIQ(methodCall, new IqCB(this.GotRpcCall), null); } catch (HttpListenerException) { this.Stop(); } } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="iq"></param> /// <param name="state"></param> private void GotRpcCall(object sender, IQ iq, object state) { if (!this.IsTrusted(iq.From)) return; String rpcXmlString = Regex.Replace(iq.Query.FirstChild.OuterXml, " xmlns(.+?)>", ">"); switch (iq.Type) { case IQType.set: if (this.CanReceiveRpcXml) { RpcIQ methodResponse = new RpcIQ(new XmlDocument()); methodResponse.From = this.Stream.JID; methodResponse.To = iq.From; methodResponse.Type = IQType.result; methodResponse.ID = iq.ID; // Make IQ ID behaving like a transaction ID methodResponse.Instruction.SetXmlRpcPayload(this.InvokeXmlRpc(rpcXmlString)); this.BeginIQ(methodResponse, new IqCB(this.GotRpcCall), null); iq.Handled = true; } break; case IQType.result: if (this.CanSendRpcXml && this.xmlRpcContexts.ContainsKey(iq.ID)) { byte[] outputByteArray = Encoding.UTF8.GetBytes(rpcXmlString); MemoryStream outputStream = new MemoryStream(outputByteArray); HttpListenerResponse response = this.xmlRpcContexts[iq.ID].Response; response.ContentLength64 = outputStream.Length; Util.CopyStream(outputStream, response.OutputStream); response.OutputStream.Flush(); this.xmlRpcContexts.Remove(iq.ID); iq.Handled = true; } break; } }