private void HandleSendQueue() { QueuedPacket packet = null; lock (SyncLock) { packet = SendQueue.Dequeue(); } if (packet != null && packet.Data != null) { var se = SocketError.SocketError; NetworkSocket?.Send(packet.Data, 0, packet.Data.Length, SocketFlags.None, out se); if (se != SocketError.Success) { Disconnect(new SocketException()); return; } else { packet.HasBeenSent(this); } } }
public void Enqueue(QueuedPacket entry) { lock (_lock) { _queue.Add(entry); //InsertViaBinarySearch(entry); } }
private void InsertViaBinarySearch(QueuedPacket entry) { if (_queue.Count == 0) { _queue.Add(entry); return; } if (entry.timestamp > _queue[_queue.Count - 1].timestamp) { _queue.Add(entry); return; } if (entry.timestamp < _queue[0].timestamp) { _queue.Insert(0, entry); return; } double key = entry.timestamp; int minNum = 0; int maxNum = _queue.Count - 1; int mid = 0; while (minNum <= maxNum) { mid = (minNum + maxNum) / 2; if (System.Math.Abs(key - _queue[mid].timestamp) <= double.Epsilon) { _queue.Insert(mid + 1, entry); return; } else if (key < _queue[mid].timestamp) { maxNum = mid - 1; } else { minNum = mid + 1; } } throw new Exception("Failed to perform binary search for: " + key); }