Exemplo n.º 1
0
        static void AddChain2Save(Chains2Save chain2Save, EndPoint ipEndPoint)
        {
            Monitor.Enter(m_Chains2SaveMutex);
            try
            {
#if false
                m_Chains2Save.Add(chain2Save);
#else
                List <Chains2Save> list;
                if (m_ChainsPerIpEndpoint.TryGetValue(ipEndPoint, out list))
                {
                    list.Add(chain2Save);
                }
                else
                {
                    list = new List <Chains2Save>();
                    list.Add(chain2Save);
                    m_ChainsPerIpEndpoint.Add(ipEndPoint, list);
                }
#endif
            }
            catch (Exception exc)
            {
                LogUtility.LogUtility.LogFile("EXCEPTION " + exc.Message + " " + exc.StackTrace, LogUtility.LogLevels.LEVEL_LOG_HIGH);
            }
            Monitor.Exit(m_Chains2SaveMutex);
        }
Exemplo n.º 2
0
        uint ReceiverOnDataMsg(byte[] packet, int packet_offset, byte flag, out List <ChunkListAndChainId> chainChunkList, out uint chainOffset)
        {
            m_onDataReceived(packet, packet_offset, packet.Length - packet_offset);

            List <long> chunkList = new List <long>();

            Monitor.Enter(m_libMutex);
            /* process the stream (+reminder) to get chunks */
            int  processed_bytes = m_packChunking.getChunks(chunkList, packet, packet_offset, packet.Length, /*is_last*/ true, true);
            uint offset          = (uint)packet_offset;
            List <ChunkMetaData[]> chunkMetaDataList = new List <ChunkMetaData[]>(100);
            int idx = 0;
            int lastNonMatchingChunk        = chunkList.Count;
            int firstNonMatchingChunk       = chunkList.Count;
            int firstNonMatchingChunkOffset = 0;

            chainChunkList = new List <ChunkListAndChainId>(100);
            List <ChunkListAndChainId> dummyChainChunkList = new List <ChunkListAndChainId>();
            int  rc;
            bool foundMatch = false;

            LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + " processing " + Convert.ToString(chunkList.Count) + " chunks, CurrentOffset " + Convert.ToString(m_CurrentOffset), ModuleLogLevel);
#if true
            if (chunkList.Count > 0)
            {
                rc = m_chunkAndChainManager.ChainMatch(chunkList, chunkList.Count - 1, chainChunkList, m_SentChainList);
                if (rc > 0)
                {
                    m_SentChainList.Add(chainChunkList[0].chainId);
                    foundMatch = true;
                    LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + " found long chain (fast path) ", ModuleLogLevel);
                }
                else if (rc == 0)
                {
                    foundMatch = true;
                    LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + " found equal chain (fast path) ", ModuleLogLevel);
                }
            }
            if (foundMatch)
            {
                chainOffset      = (uint)(m_CurrentOffset + processed_bytes);
                m_CurrentOffset += (uint)(packet.Length - packet_offset);
                Monitor.Exit(m_libMutex);
                return(GetChainsListSize(chainChunkList));
            }
#endif
            foreach (long chunk in chunkList)
            {
                rc = m_chunkAndChainManager.ChainMatch(chunkList, idx, chainChunkList, m_SentChainList);

                if (rc < 0)
                {
                    lastNonMatchingChunk = idx;
                    if (firstNonMatchingChunk == chunkList.Count)
                    {
//                        LogUtility.LogUtility.LogFile("starting non-mactching range " + Convert.ToString(idx), ModuleLogLevel);
                        firstNonMatchingChunk       = idx;
                        firstNonMatchingChunkOffset = (int)offset;
                    }
                    else
                    {
//                        LogUtility.LogUtility.LogFile("updating non-matching range " + Convert.ToString(idx), ModuleLogLevel);
                    }
                }
                else if (lastNonMatchingChunk != chunkList.Count)
                {
//                    LogUtility.LogUtility.LogFile("end of non-matching range " + Convert.ToString(lastNonMatchingChunk), ModuleLogLevel);
                    Chains2Save chain2Save = new Chains2Save(chunkList, firstNonMatchingChunk, lastNonMatchingChunk, packet, firstNonMatchingChunkOffset, m_chunkAndChainManager);
                    AddChain2Save(chain2Save, m_Id);
                    firstNonMatchingChunk = chunkList.Count;
                    lastNonMatchingChunk  = chunkList.Count;
                }
                m_ChunksProcessed++;
                offset += (uint)PackChunking.chunkToLen(chunkList[idx]);
                idx++;
                LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + " AddUpdateChunk: offset=" + Convert.ToString(m_CurrentOffset + offset) + " chainChunkList " + Convert.ToString(chainChunkList.Count), ModuleLogLevel);
                if (rc > 0)
                {
                    m_SentChainList.Add(chainChunkList[0].chainId);
                    break;
                }
                else if (rc == 0)
                {
                    break;
                }
            }
            if (lastNonMatchingChunk != chunkList.Count)
            {
//                LogUtility.LogUtility.LogFile("end of non-matching range (last) " + Convert.ToString(lastNonMatchingChunk), ModuleLogLevel);
                Chains2Save chain2Save = new Chains2Save(chunkList, firstNonMatchingChunk, lastNonMatchingChunk, packet, firstNonMatchingChunkOffset, m_chunkAndChainManager);
                AddChain2Save(chain2Save, m_Id);
            }
            //Vadim 10/01/13 onDataReceived(packet, packet_offset, packet.Length - packet_offset);
            chainOffset      = (uint)(m_CurrentOffset + processed_bytes);
            m_CurrentOffset += (uint)(packet.Length - packet_offset);
            if (chainChunkList.Count == 0)
            {
                Monitor.Exit(m_libMutex);
                return(0);
            }
            Monitor.Exit(m_libMutex);
            return(GetChainsListSize(chainChunkList));
        }
Exemplo n.º 3
0
        static void WriteChunkChainsThreadProc()
        {
            int FlushSwTimer = -1;
            int time2wait;

            while (true)
            {
                try
                {
#if false
                    Monitor.Enter(m_Chains2SaveMutex);
                    Chains2Save[] chains2Save = new Chains2Save[m_Chains2Save.Count];
                    m_Chains2Save.CopyTo(chains2Save);
                    m_Chains2Save.Clear();
                    Monitor.Exit(m_Chains2SaveMutex);
#else
                    if (FlushSwTimer == -1)
                    {
                        time2wait = -1;
                    }
                    else
                    {
                        time2wait = (20 - FlushSwTimer) * 1000;
                    }
                    m_SaveEvent.WaitOne(time2wait);
                    LogUtility.LogUtility.LogFile("Got event", LogUtility.LogLevels.LEVEL_LOG_HIGH);
                    if (Queue.Synchronized(m_SaveQueue).Count == 0)
                    {
                        LogUtility.LogUtility.LogFile("Queue is empty", LogUtility.LogLevels.LEVEL_LOG_HIGH);
                        FlushSwTimer++;
                        if (FlushSwTimer == 20)
                        {
                            goto flush;
                        }
                        continue;
                    }
                    EndPoint           ipEndPoint = (EndPoint)Queue.Synchronized(m_SaveQueue).Dequeue();
                    List <Chains2Save> list;
                    Chains2Save[]      chains2Save;
                    Monitor.Enter(m_Chains2SaveMutex);
                    if (m_ChainsPerIpEndpoint.TryGetValue(ipEndPoint, out list))
                    {
                        chains2Save = new Chains2Save[list.Count];
                        list.CopyTo(chains2Save);
                        list.Clear();
                    }
                    else
                    {
                        chains2Save = null;
                    }
                    Monitor.Exit(m_Chains2SaveMutex);
#endif
                    if (chains2Save != null)
                    {
                        foreach (Chains2Save chain2Save in chains2Save)
                        {
                            if (PassesSaveCriteria(chain2Save))
                            {
                                ChunkAndChainManager.ChunkAndChainManager.SaveChain(chain2Save.GetChunkList(), chain2Save.GetFirstNonMatchingChunk(), chain2Save.GetLastNonMatchingChunk(), chain2Save.GetPacket(), chain2Save.GetFirstNonMatchingChunkOffset(), chain2Save.GetChunkAndChainManager());
                                LogUtility.LogUtility.LogFile("Saved chain " + Convert.ToString(chain2Save.GetChunkList().Count) + " " + Convert.ToString(chain2Save.GetFirstNonMatchingChunk()) + " " + Convert.ToString(chain2Save.GetLastNonMatchingChunk()) + " " + Convert.ToString(chain2Save.GetFirstNonMatchingChunkOffset()), LogUtility.LogLevels.LEVEL_LOG_HIGH);
                            }
                        }
                    }
                    FlushSwTimer++;
flush:
                    if (FlushSwTimer == 20)
                    {
                        FlushSwTimer = -1;
                        try
                        {
                            LogUtility.LogUtility.LogFile("FLUSHING ", LogUtility.LogLevels.LEVEL_LOG_HIGH);
                            Flush();
                        }
                        catch (Exception exc)
                        {
                            LogUtility.LogUtility.LogFile("EXCEPTION " + exc.Message + " " + exc.StackTrace, LogUtility.LogLevels.LEVEL_LOG_HIGH);
                        }
                    }
                }
                catch (Exception exc)
                {
                    LogUtility.LogUtility.LogFile("EXCEPTION " + exc.Message + " " + exc.StackTrace, LogUtility.LogLevels.LEVEL_LOG_HIGH);
                }
            }
        }
Exemplo n.º 4
0
 static bool PassesSaveCriteria(Chains2Save chain2Save)
 {
     return((GetChainLength(chain2Save.GetChunkList()) >= mc_MinChainLengthInBytes) && ((chain2Save.GetLastNonMatchingChunk() - chain2Save.GetFirstNonMatchingChunk() >= mc_MinChainLengthInChunks)));
 }