public MatchStateMachine(EndPoint Id, LongestMatch longestMatch, StreamChunckingLib.PackChunking packChunking, byte[] data, List <List <ChunkMetaData> > predMsg) { m_LongestMatch = longestMatch; m_packChunking = packChunking; m_Id = Id; m_data = data; m_PredMsg = predMsg; m_SenderChunkList = new List <long>(); m_ProcessedBytes = m_packChunking.getChunks(m_SenderChunkList, m_data, 0, m_data.Length, true, false); m_SenderChunkListWithSha1 = new List <long>(); m_packChunking.getChunks(m_SenderChunkListWithSha1, m_data, (int)0, m_data.Length, true, true); LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + " processedBytes " + Convert.ToString(m_ProcessedBytes) + " chunk count " + Convert.ToString(m_SenderChunkList.Count), ModuleLogLevel); }
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); }