public S22.Xmpp.Core.Iq IqRequest(S22.Xmpp.Core.Iq request, int millisecondsTimeout = -1, string seqID = "") { S22.Xmpp.Core.Iq iq; this.AssertValid(); request.ThrowIfNull <S22.Xmpp.Core.Iq>("request"); if ((request.Type != IqType.Set) && (request.Type != IqType.Get)) { throw new ArgumentException("The IQ type must be either 'set' or 'get'."); } request.Id = !string.IsNullOrEmpty(seqID) ? seqID : this.GetId(); AutoResetEvent event2 = new AutoResetEvent(false); this.Send(request); this.waitHandles[request.Id] = event2; switch (WaitHandle.WaitAny(new WaitHandle[] { event2, this.cancelIq.Token.WaitHandle }, millisecondsTimeout)) { case 0x102: throw new TimeoutException(); case 1: CommonConfig.Logger.WriteInfo("The incoming XML stream could not read."); throw new IOException("The incoming XML stream could not read."); } if (!this.iqResponses.TryRemove(request.Id, out iq)) { throw new InvalidOperationException(); } return(iq); }
private void HandleIqResponse(S22.Xmpp.Core.Iq iq) { AutoResetEvent event2; ThreadStart start = null; Action <string, S22.Xmpp.Core.Iq> cb; string id = iq.Id; this.iqResponses[id] = iq; if (this.waitHandles.TryRemove(id, out event2)) { CommonConfig.Logger.WriteInfo(string.Concat(new object[] { id, ",队列数", this.waitHandles.Count, ",waitHandles清除队列成功,结果已返回,执行ev.Set()" })); event2.Set(); } else if (this.iqCallbacks.TryRemove(id, out cb)) { CommonConfig.Logger.WriteInfo(string.Concat(new object[] { id, ",队列数", this.iqCallbacks.Count, ",iqCallbacks清除队列成功,结果已返回,准备执行Task.Factory.StartNew(() => { cb(id, iq); });" })); if (start == null) { start = delegate { CommonConfig.Logger.WriteInfo("执行cb(id, iq);"); cb(id, iq); CommonConfig.Logger.WriteInfo("执行完cb(id, iq);"); }; } new Thread(start).Start(); } }
public void IqResponse(S22.Xmpp.Core.Iq response) { this.AssertValid(); response.ThrowIfNull <S22.Xmpp.Core.Iq>("response"); if ((response.Type != IqType.Result) && (response.Type != IqType.Error)) { throw new ArgumentException("The IQ type must be either 'result' or 'error'."); } this.Send(response); }
public string IqRequestAsync(S22.Xmpp.Core.Iq request, Action <string, S22.Xmpp.Core.Iq> callback = null) { this.AssertValid(); request.ThrowIfNull <S22.Xmpp.Core.Iq>("request"); if ((request.Type != IqType.Set) && (request.Type != IqType.Get)) { throw new ArgumentException("The IQ type must be either 'set' or 'get'."); } request.Id = this.GetId(); if (callback != null) { this.iqCallbacks[request.Id] = callback; } this.Send(request); return(request.Id); }
private void DispatchEvents() { while (true) { try { Stanza stanza = this.stanzaQueue.Take(this.cancelDispatch.Token); if (stanza is S22.Xmpp.Core.Iq) { CommonConfig.Logger.WriteInfo("IQ消息出列"); S22.Xmpp.Core.Iq iq = stanza as S22.Xmpp.Core.Iq; this.Iq.Raise <IqEventArgs>(this, new IqEventArgs(stanza as S22.Xmpp.Core.Iq)); } else if (stanza is S22.Xmpp.Core.Message) { this.Message.Raise <MessageEventArgs>(this, new MessageEventArgs(stanza as S22.Xmpp.Core.Message)); } else if (stanza is S22.Xmpp.Core.Presence) { CommonConfig.Logger.WriteInfo("Presence消息出列"); this.Presence.Raise <PresenceEventArgs>(this, new PresenceEventArgs(stanza as S22.Xmpp.Core.Presence)); } } catch (OperationCanceledException exception) { CommonConfig.Logger.WriteError("消息出列过程出错", exception); //CommonConfig.Logger.WriteError(exception); return; } catch (Exception exception2) { CommonConfig.Logger.WriteError("消息出列过程出错", exception2); //CommonConfig.Logger.WriteError(exception2); } } }
private void ReadXmlStream() { try { Stopwatch stopwatch; bool flag; goto Label_01A0; Label_0007: stopwatch = new Stopwatch(); stopwatch.Start(); //CommonConfig.Logger.WriteInfo("读取流--开始。。。"); XmlElement element = this.parser.NextElement(new string[] { "iq", "message", "presence" }); //CommonConfig.Logger.WriteInfo("读取流--解析Xml,耗时:" + stopwatch.ElapsedMilliseconds); if (element["data"] == null) { CommonConfig.Logger.WriteInfo("接收到数据:" + element.OuterXml); } else { XmlNode node = element.Clone(); node["data"].InnerText = ""; CommonConfig.Logger.WriteInfo("接收到文件:" + node.OuterXml); } string name = element.Name; if (name != null) { if (!(name == "iq")) { if (name == "message") { goto Label_0150; } if (name == "presence") { goto Label_0164; } } else { S22.Xmpp.Core.Iq item = new S22.Xmpp.Core.Iq(element); if (item.IsRequest) { this.stanzaQueue.Add(item); } else { this.HandleIqResponse(item); } } } goto Label_0178; Label_0150: this.stanzaQueue.Add(new S22.Xmpp.Core.Message(element)); goto Label_0178; Label_0164: this.stanzaQueue.Add(new S22.Xmpp.Core.Presence(element)); Label_0178: CommonConfig.Logger.WriteInfo(string.Format("读取流--结束,耗时:【{0}】", stopwatch.ElapsedMilliseconds)); stopwatch.Stop(); Label_01A0: flag = true; goto Label_0007; } catch (Exception exception) { CommonConfig.Logger.WriteError("接收异常,此处认为是已经断开了。", exception); this.cancelDispatch.Cancel(); this.cancelDispatch = new CancellationTokenSource(); this.cancelIq.Cancel(); this.cancelIq = new CancellationTokenSource(); this.Connected = false; if (!this.disposed) { this.Error.Raise <S22.Xmpp.Core.ErrorEventArgs>(this, new S22.Xmpp.Core.ErrorEventArgs(exception)); } } }