private void ProcessOutgoingData() { bool noRest = false; if (m_State > 0) { IoDataStream msg = null; lock (m_OutgoingMessageQueue) { // before send out a new message, we must remove the old one first if (m_OutgoingMessageQueue.Count > 0) { m_OutgoingMessageQueue.Dequeue(); } // try to find some messages which are still waiting if (m_OutgoingMessageQueue.Count > 0) { msg = m_OutgoingMessageQueue.Peek(); } } if (msg != null) { DoSend(msg); // if found someone still waiting then send it } else // else check whether need to close the session { if (m_IsGoingToClose) // need to check unhandled messages only when closing { if (m_IncomingMessageQueue.Count <= 0) { noRest = true; } } } } if (noRest && m_IsGoingToClose) { Close(true); } }
private void DoSend(IoDataStream message) { if (m_Stream == null || m_State <= 0 || message == null) { return; } Object msg = message.IoData; MemoryStream stream = message.IoStream; if (stream.Length > 0) { stream.Position = 0; if (m_Stream != null) { SessionContext info = new SessionContext(this, msg); m_Stream.BeginWrite(stream.ToArray(), 0, Convert.ToInt32(stream.Length), new AsyncCallback(SendCallback), info); } } }
public void Send(Object message) { if (m_Stream == null || m_State <= 0 || m_IsGoingToClose || message == null) { return; } if (MaxWriteQueueSize > 0 && m_OutgoingMessageQueue.Count >= MaxWriteQueueSize) { if (FitWriteQueueAction == ACT_KEEP_DEFAULT && m_IoHandler != null) { try { m_IoHandler.OnError(this, Session.ERROR_SEND, new Exception("Outgoing queue is full")); } catch { } } if (FitWriteQueueAction == ACT_KEEP_DEFAULT || FitWriteQueueAction == ACT_KEEP_OLD) { return; } } MemoryStream stream = new MemoryStream(); stream.SetLength(0); stream.Position = 0; bool encodeOK = false; try { m_IoFilter.Encode(this, message, stream); encodeOK = true; IoDataStream msg = null; lock (m_OutgoingMessageQueue) { bool sending = m_OutgoingMessageQueue.Count > 0; if (MaxWriteQueueSize > 0 && m_OutgoingMessageQueue.Count >= MaxWriteQueueSize && FitWriteQueueAction == ACT_KEEP_NEW) { m_OutgoingMessageQueue.Dequeue(); // just remove one old packet, even it's being sent } m_OutgoingMessageQueue.Enqueue(new IoDataStream(message, stream)); if (!sending) { msg = m_OutgoingMessageQueue.Peek(); } } if (msg != null) { DoSend(msg); } } catch (Exception ex) { if (m_IoHandler != null) { try { m_IoHandler.OnError(this, encodeOK ? Session.ERROR_SEND : (Session.ERROR_CODEC | Session.ERROR_SEND), ex); } catch { } } } }