/// <summary> /// Corutine that waits until a given condition or until the maximum /// waiting time is reached. It is meant to be used for waiting for the /// arrival of the chunks associated to a header. /// </summary> /// <param name="header">Header message.</param> /// <param name="Condition">Method that defines the condition for which the corutine will have to keep waiting.</param> /// <param name="maxWaitingTime">Maximum time that the corutine can wait.</param> /// <returns></returns> public IEnumerator WaitTillReceiveAllTheStream(headerMsg header, WaitingCondition Condition, float maxWaitingTime) { uint waitingId = header.id; float elapsedTime = 0; while (ThereIsDataFor(waitingId) && Condition(waitingId)) { yield return(new WaitForSecondsRealtime(0.01f)); elapsedTime += 0.01f; if (elapsedTime > maxWaitingTime) { RemoveStream(waitingId); yield break; } } if (ThereIsDataFor(waitingId) && elapsedTime <= maxWaitingTime) { StreamIsFullyReceived(waitingId); } else { RemoveStream(waitingId); } }
/// <summary> /// Saves the media associated to a given header message and looks for /// unheaded chunks of this header to save their data on the media structure. /// </summary> /// <param name="header">Header message.</param> /// <param name="streamS">Media associated to the header message.</param> /// <param name="SaveChunk">Methodd that takes a chunk message and saves its data.</param> public void RecoverEarlyChunks(headerMsg header, struc streamS, StreamChunkHandler <chunkMsg> SaveChunk) { streamData[header.id] = streamS; streamWasFullyReceived[header.id] = false; if (amountOfEarlyChunks.ContainsKey(header.id)) { int i = 0; int count = 0; while (i < amountOfEarlyChunks[header.id] && i < unheadedChunks.Count) { var row = unheadedChunks[i].Item1; if (row.id == header.id) { SaveChunk(row); unheadedChunks.RemoveAt(i); count += 1; } i += 1; } if (amountOfEarlyChunks[header.id] != count) { RemoveStream(header.id); } amountOfEarlyChunks.Remove(header.id); } }
/// <summary> /// Checks if a given header has arrived late. If its timestamp is lower or /// equal than the first timestamp in the registered sequence then the information /// saved for this header message is discarded. If not, then the header is added. /// </summary> /// <param name="header">Header message.</param> /// <returns></returns> public bool CheckTimestamp(headerMsg header) { if (streamIdsReceived.Count > 0 && streamIdsReceived.KeyAt(0) >= header.timeStamp) { RemoveStream(header.id); return(false); } else { streamIdsReceived.Add(header.id, header.timeStamp); return(true); } }