Esempio n. 1
0
        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 { }
                }
            }
        }
Esempio n. 2
0
        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);
            }
        }