コード例 #1
0
ファイル: Protocol.cs プロジェクト: yongwuhou/NCache
        /// <summary> Internal method, should not be called by clients. Used by ProtocolStack. I would have
        /// used the 'friends' modifier, but this is available only in C++ ... If the up_handler thread
        /// is not available (down_thread == false), then directly call the up() method: we will run on the
        /// caller's thread (e.g. the protocol layer below us).
        /// </summary>
        public virtual void  receiveUpEvent(Event evt)
        {
            int type = evt.Type;

            if (_printMsgHdrs && type == Event.MSG)
            {
                printMsgHeaders(evt, "up()");
            }

            if (up_handler == null)
            {
                up(evt);
                return;
            }
            try
            {
                if (stack.NCacheLog.IsInfoEnabled)
                {
                    stack.NCacheLog.Info(Name + ".receiveUpEvent()", "RentId :" + evt.RentId + "up queue count : " + up_queue.Count);
                }
                up_queue.add(evt, evt.Priority);
            }
            catch (System.Exception e)
            {
                stack.NCacheLog.Warn("Protocol.receiveUpEvent()", e.ToString());
            }
        }
コード例 #2
0
 internal virtual void  addToQueue(long seqno, Message msg)
 {
     try
     {
         msg_queue.add(new AckSenderWindowEntry(this, seqno, msg));
     }
     catch (System.Exception ex)
     {
         NCacheLog.Error("AckSenderWindow.add()", ex.ToString());
     }
 }
コード例 #3
0
        public int QueueMessage(BinaryMessage bmsg, Priority prt)
        {
            int queueCount = 0;

            if (mq != null)
            {
                try
                {
                    mq.add(bmsg, prt);
                    queueCount = mq.Count;
                }
                catch (Exception e)
                {
                }
            }
            return(queueCount);
        }
コード例 #4
0
        /// <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)
            {
            }
        }
コード例 #5
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);

                            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;
                        }

                        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");
                            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...
            }
        }