/// <summary> /// Sends a block to the world /// </summary> /// <param name="blockRequest">The block to send</param> internal void UploadBlockRequest(BlockRequest blockRequest) { Block block = blockRequest.Block; if (block != null) { block.Upload(); blockRequest.HasBeenSent = true; Thread.Sleep(blockRequest.BlockThrottle); } }
/// <summary> /// Adds the block to the end of the list /// </summary> /// <param name="block">The block to be queued</param> /// <param name="blockThrottle">The speed of the block to be uploaded at</param> internal void QueueBlock(Block block, int blockThrottle) { BlockRequest request = new BlockRequest(block, blockThrottle); m_Queue.Add(request); if (IsUploadThreadDead()) { CreateUploadThread(); } }
/// <summary> /// The upload thread loop /// </summary> private void UploadThread() { try { while (m_Queue.Count > 0) { //Send the most tasked block BlockRequest send = GetNextInList(); if (send == null) { break; } else if (send.Missed) { bool removed = false; Block existing = m_WorldConnection.World[send.Block.X, send.Block.Y, send.Block.Layer]; if (existing.EqualsBlock(send.Block)) { for (int i = 0; i < m_Queue.Count; i++) { if (m_Queue[i].Block.EqualsBlock(send.Block)) { m_Queue.RemoveAt(i); removed = true; break; } } } if (removed) { continue; } } send.Request(); m_WorldConnection.UploadBlockRequest(send); } } catch (ThreadAbortException) { //Handled } if (m_ResetEvent != null) { m_ResetEvent.Set(); } }
/// <summary> /// Gets the next in the list /// </summary> /// <returns>The next block that should be sent</returns> private BlockRequest GetNextInList() { long oldestRequestTime = 0; BlockRequest oldestMissed = null; for (int i = 0; i < m_Queue.Count; i++) { if (m_Queue[i].Missed) { if (!m_Queue[i].Timestamp.HasValue) { return(m_Queue[i]); } long timePassed = m_Queue[i].GetTimePassed(); if (timePassed >= oldestRequestTime) { oldestRequestTime = timePassed; oldestMissed = m_Queue[i]; } } } if (oldestMissed != null) { return(oldestMissed); } //Check queue for the oldest unattempted send for (int i = 0; i < m_Queue.Count; i++) { if (!m_Queue[i].HasBeenSent) { return(m_Queue[i]); } } return(null); }