Пример #1
0
        public bool AddData(byte[] data, bool invokedOnTransmit)
        {
            int  offset;
            bool ret = true;

            byte[] msg;
            Monitor.Enter(m_libMutex);
            uint offset_in_stream = m_TotalDataSent + m_TotalSavedData;

            m_TotalDataReceived += (uint)data.Length;
            LogUtility.LogUtility.LogFile("AddData: " + Convert.ToString(data.Length) + " bytes received, isInvokedOnTx " + Convert.ToString(invokedOnTransmit), ModuleLogLevel);

            LongestMatch longestMatch             = new LongestMatch();
            List <List <ChunkMetaData> > predMsgs = GetPredMsg();

            if (predMsgs != null)
            {
                LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + " AddData: process " + Convert.ToString(predMsgs.Count) + " chains total received " + Convert.ToString(m_TotalDataReceived) + " total sent " + Convert.ToString(m_TotalDataSent) + " total saved " + Convert.ToString(m_TotalSavedData) + " data len " + Convert.ToString(data.Length), ModuleLogLevel);
                MatchStateMachine matchStateMachine = new MatchStateMachine(m_Id, longestMatch, packChunking, data, predMsgs);
                matchStateMachine.Run();
            }

            if (!longestMatch.IsMatchFound())
            {
                LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + " no match at all", ModuleLogLevel);

                if (!invokedOnTransmit)
                {
                    ForwardData(data);
                }
                else
                {
                    LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + " not submitting - tx case", ModuleLogLevel);
                    ret = false;
                }
                Monitor.Exit(m_libMutex);
                return(ret);
            }
            if (invokedOnTransmit)
            {
                m_TotalDataSent -= (uint)data.Length;
                //m_TotalPreSaved -= (uint)count;
                m_DataMsgSent--;
                int remainder   = longestMatch.GetRemainder(data.Length);
                int alreadySent = (int)longestMatch.GetPrecedingBytesCount() + (int)longestMatch.GetLongestChainLen();
                if ((alreadySent + remainder) != data.Length)
                {
                    LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + " something wrong: prec+chainLen+remainder " + Convert.ToString(longestMatch.GetPrecedingBytesCount()) + " " + Convert.ToString(longestMatch.GetLongestChainLen()) + " " + Convert.ToString(remainder) + " does not equal data.length " + Convert.ToString(data.Length), ModuleLogLevel);
                }

                if (remainder > 0)
                {
                    LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + " sending pos-PredAck data " + Convert.ToString(remainder), ModuleLogLevel);
                    msg = PackMsg.PackMsg.AllocateMsgAndBuildHeader((uint)remainder, 0, (byte)PackMsg.PackMsg.MsgKind_e.PACK_FINALLY_PROCESSED_DATA_MSG_KIND, out offset);
                    for (int i = alreadySent; i < data.Length; i++)
                    {
                        msg[(i - alreadySent) + offset] = data[i];
                    }
                    onMessageReadyToTx(onTxMessageParam, msg, invokedOnTransmit);
                    m_TotalDataSent += (uint)remainder;
                    //  m_TotalPostSaved += (uint)remainder;
                    m_DataMsgSent++;
                }
                byte[] buff = PackMsg.PackMsg.AllocateMsgAndBuildHeader(GetPredictionAckMessageSize((uint)longestMatch.GetLongestChunkCount()), 0, (byte)PackMsg.PackMsg.MsgKind_e.PACK_PRED_ACK_MSG_KIND, out offset);
                EncodePredictionAckMessage(buff, offset, longestMatch.GetPredMsg(), (uint)longestMatch.GetLongestChunkCount(), longestMatch.GetFirstReceiverChunkIdx());
                onMessageReadyToTx(onTxMessageParam, buff, invokedOnTransmit);
                m_PredAckMsgSent++;
                if (longestMatch.GetPrecedingBytesCount() > 0)
                {
                    SendChunksData(data, (int)longestMatch.GetPrecedingBytesCount(), invokedOnTransmit);
                }
            }
            else
            {
                if (longestMatch.GetPrecedingBytesCount() > 0)
                {
                    SendChunksData(data, (int)longestMatch.GetPrecedingBytesCount(), invokedOnTransmit);
                }

                byte[] buff = PackMsg.PackMsg.AllocateMsgAndBuildHeader(GetPredictionAckMessageSize((uint)longestMatch.GetLongestChunkCount()), 0, (byte)PackMsg.PackMsg.MsgKind_e.PACK_PRED_ACK_MSG_KIND, out offset);
                EncodePredictionAckMessage(buff, offset, longestMatch.GetPredMsg(), (uint)longestMatch.GetLongestChunkCount(), longestMatch.GetFirstReceiverChunkIdx());
                onMessageReadyToTx(onTxMessageParam, buff, invokedOnTransmit);
                m_PredAckMsgSent++;
                int remainder   = longestMatch.GetRemainder(data.Length);
                int alreadySent = (int)longestMatch.GetPrecedingBytesCount() + (int)longestMatch.GetLongestChainLen();
                if ((alreadySent + remainder) != data.Length)
                {
                    LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + " something wrong: prec+chainLen+remainder " + Convert.ToString(longestMatch.GetPrecedingBytesCount()) + " " + Convert.ToString(longestMatch.GetLongestChainLen()) + " " + Convert.ToString(remainder) + " does not equal data.length " + Convert.ToString(data.Length), ModuleLogLevel);
                }

                if (remainder > 0)
                {
                    LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + " sending pos-PredAck data " + Convert.ToString(remainder), ModuleLogLevel);
                    msg = PackMsg.PackMsg.AllocateMsgAndBuildHeader((uint)remainder, 0, (byte)PackMsg.PackMsg.MsgKind_e.PACK_DATA_MSG_KIND, out offset);
                    for (int i = alreadySent; i < data.Length; i++)
                    {
                        msg[(i - alreadySent) + offset] = data[i];
                    }
                    onMessageReadyToTx(onTxMessageParam, msg, invokedOnTransmit);
                    m_TotalDataSent += (uint)remainder;
                    //m_TotalPostSaved += (uint)remainder;
                    m_DataMsgSent++;
                }
            }
            longestMatch.GetPredMsg().RemoveRange((int)longestMatch.GetFirstReceiverChunkIdx(), (int)longestMatch.GetLongestChunkCount());
            Monitor.Exit(m_libMutex);
            LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) /*+ " PreSaved " + Convert.ToString(m_TotalPreSaved)*/ + " Saved " + Convert.ToString(m_TotalSavedData) /*+ " PostSaved " + Convert.ToString(m_TotalPostSaved)*/ + " Received from server " + Convert.ToString(m_TotalDataReceived) + " Total sent to client " + Convert.ToString(m_TotalDataSent) /*+ " Sent raw " + Convert.ToString(m_TotalRawSent)*/, ModuleLogLevel);
            return(ret);
        }
Пример #2
0
 void OnEndOfMatch(List <ChunkMetaData> predMsg, uint matchLen, uint matchChunkCount, uint firstSenderIdx, uint firstReceiverIdx, int offset)
 {
     if (m_LongestMatch.IsLonger(matchLen))
     {
         /*matchLen*/
         matchChunkCount = IsSha1Match(firstSenderIdx, predMsg, firstReceiverIdx, /*matchLen*/ matchChunkCount);
         if (matchChunkCount > 0)
         {
             matchLen = GetMatchLen(firstSenderIdx, matchChunkCount);
             LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + " chain longer " + Convert.ToString(m_LongestMatch.GetLongestChainLen()), ModuleLogLevel);
             m_LongestMatch.UpdateFields(predMsg, offset, matchChunkCount, matchLen, (uint)m_ProcessedBytes, m_SenderChunkList, firstSenderIdx, firstReceiverIdx);
         }
     }
 }