/// <summary> /// Stop polling. /// </summary> public override void Close() { Body body = CreateOpenBodyTag(); body.Type = BodyType.terminate; byte[] buf = ENC.GetBytes(body.OuterXml); //m_listener.OnWrite(this, buf, 0, buf.Length); GetSocket().Execute(METHOD, m_uri, buf, 0, buf.Length, CONTENT_TYPE); m_sockA.EnqueueClose(); m_sockB.EnqueueClose(); lock (m_lock) { m_running = false; m_sockA = m_sockB = m_lastSock = null; } m_listener.OnClose(this); }
private void ProcessThread() { Body body = null; int children = 0; while (m_running) { lock (m_queue) { //if (NeitherPending) // m_queue.AddFirst((XmlElement)null); Debug.WriteLine("A: " + m_sockA.IsPending); Debug.WriteLine("b: " + m_sockB.IsPending); while ((m_queue.First == null) || BothPending) { Monitor.Wait(m_queue); if (!m_running) { return; } } // We'll enq nulls to get a poll. // We'll enq a body in order to terminate. Debug.Assert(m_queue.First != null); body = m_queue.First.Value as Body; children = 0; if (body != null) { // TODO: what to do with leftover stanzas!? m_queue.RemoveFirst(); } else { body = CreateOpenBodyTag(); while (m_queue.First != null) { XmlElement elem = m_queue.First.Value; // ignore nulls. we're going munge together all pending poll requests. if (elem != null) { // if we get to a body in the queue, stop inserting, and wait for the body // to come around again next time. if (elem is Body) { break; } body.AddChild(elem); children++; } m_queue.RemoveFirst(); } } } if (NeitherPending || (children > 0) || (body.Type == BodyType.terminate)) { if (body.RID == -1) { body.RID = Interlocked.Increment(ref m_rid); } byte[] buf = ENC.GetBytes(body.OuterXml); GetSocket().Execute(METHOD, m_uri, buf, 0, buf.Length, CONTENT_TYPE); } if (body.Type == BodyType.terminate) { // shutting down. m_sockA.EnqueueClose(); m_sockB.EnqueueClose(); return; } } }