Пример #1
0
        public override void Run()
        {
            try
            {
                ArrayList msgList = new ArrayList();
                byte[]    tmpBuffer;
                int       totalMsgSize = 0;
                int       offset       = 0;
                ArrayList msgsTobeSent = new ArrayList();
                while (!_msgQueue.Closed)
                {
                    try
                    {
                        msgsTobeSent.Clear();
                        lock (_syncLock)
                        {
                            tmpBuffer    = _sendBuffer;
                            totalMsgSize = 0;
                            offset       = 0;
                            while (true)
                            {
                                byte[] msg = (byte[])_msgQueue.remove();

                                if (msg != null)
                                {
                                    msgsTobeSent.Add(msg);
                                    totalMsgSize += msg.Length;

                                    if (totalMsgSize > _sendBuffer.Length)
                                    {
                                        tmpBuffer = new byte[totalMsgSize];
                                    }

                                    Buffer.BlockCopy(msg, 0, tmpBuffer, offset, msg.Length);
                                    offset += msg.Length;
                                }
                                msg = null;
                                try
                                {
                                    bool success = false;
                                    msg = _msgQueue.peek(_waitTimeout, out success) as byte[];
                                }
                                catch (Alachisoft.NCache.Common.Exceptions.TimeoutException)
                                {
                                }
                                if ((msg == null || ((msg.Length + totalMsgSize) > _nagglingSize)))
                                {
                                    break;
                                }
                            }
                        }
                        _parent.AssureSend(_workingSocket, tmpBuffer, totalMsgSize, true);
                    }
                    catch (ThreadInterruptedException e)
                    {
                        break;
                    }
                    catch (System.Exception e)
                    {
                    }
                }
            }
            catch (ThreadInterruptedException e)
            {
            }
            catch (Exception e)
            {
            }
        }
        /// <summary>Removes events from mq and calls handler.down(evt) </summary>
        override public void Run()
        {
            try
            {
                ArrayList msgList       = new ArrayList();
                byte[]    tmpBuffer     = null;
                int       totalMsgSize  = 4;
                int       noOfMsgs      = 0;
                int       offset        = 8;
                bool      resendMessage = false;
                ArrayList msgsTobeSent  = new ArrayList();
                while (!mq.Closed)
                {
                    try
                    {
                        if (resendMessage)
                        {
                            if (tmpBuffer != null && tmpBuffer.Length > 0)
                            {
                                peerConnection.send(tmpBuffer, null, totalMsgSize + 4);

                                if (perfStatsCollector != null)
                                {
                                    perfStatsCollector.IncrementNagglingMessageStats(noOfMsgs);
                                }
                            }
                            resendMessage = false;
                            continue;
                        }
                        msgsTobeSent.Clear();
                        lock (sync_lock)
                        {
                            tmpBuffer    = sendBuffer;
                            totalMsgSize = 4;
                            noOfMsgs     = 0;
                            offset       = 8;
                            while (true)
                            {
                                BinaryMessage bMsg = (BinaryMessage)mq.remove();

                                if (bMsg != null)
                                {
                                    if (!peerConnection.IsPrimary)
                                    {
                                        msgsTobeSent.Add(bMsg);
                                    }

                                    noOfMsgs++;

                                    totalMsgSize += bMsg.Size;

                                    if (totalMsgSize + 8 > sendBuffer.Length)
                                    {
                                        byte[] bigbuffer = new byte[totalMsgSize + 8];
                                        Buffer.BlockCopy(tmpBuffer, 0, bigbuffer, 0, totalMsgSize - bMsg.Size);
                                        tmpBuffer = bigbuffer;
                                    }

                                    Buffer.BlockCopy(bMsg.Buffer, 0, tmpBuffer, offset, bMsg.Buffer.Length);
                                    offset += bMsg.Buffer.Length;
                                    if (bMsg.UserPayLoad != null)
                                    {
                                        byte[] buf = null;
                                        for (int i = 0; i < bMsg.UserPayLoad.Length; i++)
                                        {
                                            buf = bMsg.UserPayLoad.GetValue(i) as byte[];
                                            Buffer.BlockCopy(buf, 0, tmpBuffer, offset, buf.Length);
                                            offset += buf.Length;
                                        }
                                    }
                                }
                                bMsg = null;
                                bool success;
                                bMsg = mq.peek(waitTimeout, out success) as BinaryMessage;
                                if ((!ServiceConfiguration.EnableNagling || bMsg == null || ((bMsg.Size + totalMsgSize + 8) > ServiceConfiguration.NaglingSize)))
                                {
                                    break;
                                }
                            }
                        }


                        byte[] bTotalLength = Util.Util.WriteInt32(totalMsgSize);
                        Buffer.BlockCopy(bTotalLength, 0, tmpBuffer, 0, bTotalLength.Length);
                        byte[] bNoOfMsgs = Util.Util.WriteInt32(noOfMsgs);
                        Buffer.BlockCopy(bNoOfMsgs, 0, tmpBuffer, 4, bNoOfMsgs.Length);
                        peerConnection.send(tmpBuffer, null, totalMsgSize + 4);

                        if (perfStatsCollector != null)
                        {
                            perfStatsCollector.IncrementNagglingMessageStats(noOfMsgs);
                        }
                    }
                    catch (ExtSocketException e)
                    {
                        NCacheLog.Error(Name, e.ToString());

                        if (peerConnection.IsPrimary)
                        {
                            if (peerConnection.LeavingGracefully)
                            {
                                NCacheLog.Error("DmSender.Run", peerConnection.peer_addr + " left gracefully");
                                break;
                            }


                            NCacheLog.Error("DMSender.Run", "Connection broken with " + peerConnection.peer_addr + ". node left abruptly");
                            ConnectionTable.Connection connection = peerConnection.Enclosing_Instance.Reconnect(peerConnection.peer_addr);
                            if (connection != null)
                            {
                                Thread.Sleep(3000);
                                resendMessage = true;
                                continue;
                            }
                            else
                            {
                                NCacheLog.Error("DMSender.Run", Name + ". Failed to re-establish connection with " + peerConnection.peer_addr);
                                break;
                            }
                        }
                        else
                        {
                            NCacheLog.Error("DmSender.Run", "secondary connection broken; peer_addr : " + peerConnection.peer_addr);
                            try
                            {
                                foreach (BinaryMessage bMsg in msgsTobeSent)
                                {
                                    try
                                    {
                                        if (bMsg != null && mq != null && !mq.Closed)
                                        {
                                            mq.add(bMsg);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        NCacheLog.Error("DmSender.Run", "an error occurred while requing the messages. " + ex.ToString());
                                    }
                                }
                            }
                            catch (Exception) { }
                        }

                        break;
                    }
                    catch (QueueClosedException e)
                    {
                        break;
                    }
                    catch (ThreadInterruptedException e)
                    {
                        break;
                    }
                    catch (ThreadAbortException) { break; }
                    catch (System.Exception e)
                    {
                        NCacheLog.Error(Name + "", Name + " exception is " + e.ToString());
                    }
                }
            }
            catch (ThreadInterruptedException)
            {
            }
            catch (ThreadAbortException) { }

            catch (Exception ex)
            {
                NCacheLog.Error(Name + "", "exception=" + ex.ToString());
            }
            try
            {
                peerConnection.Enclosing_Instance.notifyConnectionClosed(peerConnection.peer_addr);

                peerConnection.Enclosing_Instance.remove(peerConnection.peer_addr, peerConnection.IsPrimary);
            }
            catch (Exception e)
            {
            }
        }
Пример #3
0
        /// <summary>Removes events from mq and calls handler.down(evt) </summary>
        override public void Run()
        {
            bool connectionCloseNotified = false;

            try
            {
                ArrayList             msgList       = new ArrayList();
                byte[]                tmpBuffer     = null;
                int                   totalMsgSize  = 4;
                int                   noOfMsgs      = 0;
                int                   offset        = 8;
                bool                  resendMessage = false;
                ArrayList             msgsTobeSent  = new ArrayList();
                ClusteredMemoryStream stream        = null;
                while (!mq.Closed)
                {
                    try
                    {
                        if (resendMessage)
                        {
                            if (stream != null && stream.Length > 0)
                            {
                                peerConnection.send(stream.GetInternalBuffer(), null, totalMsgSize + 4);
                            }
                            resendMessage = false;
                            continue;
                        }
                        msgsTobeSent.Clear();
                        stream = new ClusteredMemoryStream();

                        lock (sync_lock)
                        {
                            tmpBuffer    = sendBuffer;
                            totalMsgSize = 4;
                            noOfMsgs     = 0;
                            offset       = 8;

                            stream.Seek(8, System.IO.SeekOrigin.Begin);
                            while (true)
                            {
                                BinaryMessage bMsg = (BinaryMessage)mq.remove();
                                if (bMsg != null)
                                {
                                    if (!peerConnection.IsPrimary)
                                    {
                                        msgsTobeSent.Add(bMsg);
                                    }

                                    noOfMsgs++;
                                    totalMsgSize += bMsg.Size;

                                    foreach (byte[] buffer in bMsg.Buffer)
                                    {
                                        stream.Write(buffer, 0, buffer.Length);
                                    }
                                    if (bMsg.UserPayLoad != null)
                                    {
                                        byte[] buf = null;
                                        for (int i = 0; i < bMsg.UserPayLoad.Length; i++)
                                        {
                                            buf = bMsg.UserPayLoad.GetValue(i) as byte[];
                                            stream.Write(buf, 0, buf.Length);
                                            offset += buf.Length;
                                        }
                                    }
                                }
                                bMsg = null;
                                bool success;
                                bMsg = mq.peek(waitTimeout, out success) as BinaryMessage;
                                if ((!ServiceConfiguration.EnableNagling || bMsg == null || ((bMsg.Size + totalMsgSize + 8) > ServiceConfiguration.NaglingSize)))
                                {
                                    break;
                                }
                            }
                        }

                        byte[] bTotalLength = Util.Util.WriteInt32(totalMsgSize);
                        stream.Seek(0, System.IO.SeekOrigin.Begin);
                        stream.Write(bTotalLength, 0, bTotalLength.Length);
                        byte[] bNoOfMsgs = Util.Util.WriteInt32(noOfMsgs);
                        stream.Write(bNoOfMsgs, 0, bNoOfMsgs.Length);
                        peerConnection.send(stream.GetInternalBuffer(), null, totalMsgSize + 4);
                        stream = null;
                    }
                    catch (ExtSocketException e)
                    {
                        connectionCloseNotified = false;
                        NCacheLog.Error(Name, e.ToString());

                        if (peerConnection.IsPrimary)
                        {
                            if (peerConnection.LeavingGracefully)
                            {
                                NCacheLog.Error("DmSender.Run", peerConnection.peer_addr + " left gracefully");
                                break;
                            }
                            NCacheLog.Error("DMSender.Run", "Connection broken with " + peerConnection.peer_addr + ". node left abruptly");
                            ConnectionTable.Connection connection = peerConnection.Enclosing_Instance.Reconnect(peerConnection.peer_addr, out connectionCloseNotified);

                            if (connection != null)
                            {
                                Thread.Sleep(3000);
                                resendMessage = true;
                                continue;
                            }
                            else
                            {
                                NCacheLog.Error("DMSender.Run", Name + ". Failed to re-establish connection with " + peerConnection.peer_addr);
                                break;
                            }
                        }
                        else
                        {
                            NCacheLog.Error("DmSender.Run", "secondary connection broken; peer_addr : " + peerConnection.peer_addr);
                            try
                            {
                                foreach (BinaryMessage bMsg in msgsTobeSent)
                                {
                                    try
                                    {
                                        if (bMsg != null && mq != null && !mq.Closed)
                                        {
                                            mq.add(bMsg);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        NCacheLog.Error("DmSender.Run", "an error occurred while requing the messages. " + ex.ToString());
                                    }
                                }
                            }
                            catch (Exception) { }
                        }

                        break;
                    }
                    catch (QueueClosedException e)
                    {
                        connectionCloseNotified = false;
                        break;
                    }
                    catch (ThreadInterruptedException e)
                    {
                        connectionCloseNotified = false;
                        break;
                    }
                    catch (ThreadAbortException)
                    {
                        break;
                    }
                    catch (System.Exception e)
                    {
                        connectionCloseNotified = false;
                        NCacheLog.Error(Name + "", Name + " exception is " + e.ToString());
                    }
                }
            }
            catch (ThreadInterruptedException) { }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                connectionCloseNotified = false;
                NCacheLog.Error(Name + "", "exception=" + ex.ToString());
            }
            try
            {
                if (!connectionCloseNotified)
                {
                    peerConnection.Enclosing_Instance.notifyConnectionClosed(peerConnection.peer_addr);
                }
                else
                {
                    NCacheLog.CriticalInfo("DmSender.Run", "no need to notify about connection close");
                }

                peerConnection.Enclosing_Instance.remove(peerConnection.peer_addr, peerConnection.IsPrimary);
            }
            catch (Exception e)
            {
                //who cares...
            }
        }