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 m_OutgoingMessageQueue.Dequeue(); while (m_SkippableOutgoingMessageCount > 0 && m_OutgoingMessageQueue.Count > 0) { m_SkippableOutgoingMessageCount--; m_OutgoingMessageQueue.Dequeue(); // just process the new ones } // 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; try { if (m_Stream != null) { SessionContext info = new SessionContext(this, msg); m_Stream.BeginWrite(stream.ToArray(), 0, Convert.ToInt32(stream.Length), new AsyncCallback(SendCallback), info); } } catch (Exception ex) { if (m_IoHandler != null) { try { m_IoHandler.OnError(this, ERROR_SEND, ex.Message); } catch { } } } } }
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; try { if (m_Stream != null) { SessionContext info = new SessionContext(this, msg); m_Stream.BeginWrite(stream.ToArray(), 0, Convert.ToInt32(stream.Length), new AsyncCallback(SendCallback), info); } } catch (Exception ex) { if (m_IoHandler != null) { try { m_IoHandler.OnError(this, ERROR_SEND, ex.Message); } catch { } } } } }
public void Send(Object message) { if (m_Stream == null || m_State <= 0 || m_IsGoingToClose || message == null) { return; } int queueSize = 0; bool full = false; if (m_MaxWriteQueueSize > 0) // need to check queue size { queueSize = m_OutgoingMessageQueue.Count; } if (m_MaxWriteQueueSize > 0 && queueSize >= m_MaxWriteQueueSize) { full = true; if (m_FitWriteQueueAction == ACT_KEEP_DEFAULT && m_IoHandler != null) { try { m_IoHandler.OnError(this, Session.ERROR_SEND, "Outgoing queue is full"); } catch { } } if (m_FitWriteQueueAction == ACT_KEEP_DEFAULT || m_FitWriteQueueAction == ACT_KEEP_OLD) { return; } } MemoryStream stream = new MemoryStream(); stream.SetLength(0); stream.Position = 0; bool encodeOK = true; if (m_IoFilter != null) { try { m_IoFilter.Encode(this, message, stream); } catch (Exception ex) { encodeOK = false; if (m_IoHandler != null) { try { m_IoHandler.OnError(this, Session.ERROR_SEND, ex.Message); } catch { } } } } if (!encodeOK) { return; } IoDataStream msg = null; lock (m_OutgoingMessageQueue) { bool sending = m_OutgoingMessageQueue.Count > 0; m_OutgoingMessageQueue.Enqueue(new IoDataStream(message, stream)); if (full && m_FitWriteQueueAction == ACT_KEEP_NEW) { m_SkippableOutgoingMessageCount++; } if (!sending) { msg = m_OutgoingMessageQueue.Peek(); } } if (msg != null) { DoSend(msg); } }