PacketBuffer.java 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package com.persagy.communication.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.util.concurrent.LinkedBlockingQueue;
  4. @Slf4j
  5. public class PacketBuffer<T> {
  6. private final LinkedBlockingQueue<T> buffer = new LinkedBlockingQueue<T>();
  7. public PacketBuffer() {
  8. }
  9. // put �����������������
  10. // add ��������������쳣
  11. // offer �����������������false
  12. public void offer(T packet, int size) {
  13. if (this.buffer.size() >= size) {
  14. return;
  15. }
  16. this.buffer.offer(packet);
  17. }
  18. // take ��������ѿգ�����
  19. // remove ��������ѿգ��쳣
  20. // poll ��������ѿգ�����null
  21. public T poll() {
  22. T MyPackage = null;
  23. try {
  24. MyPackage = this.buffer.take();
  25. } catch (InterruptedException e) {
  26. log.error(e.getMessage(), e);
  27. }
  28. return MyPackage;
  29. }
  30. public T take() throws InterruptedException {
  31. T MyPackage = this.buffer.take();
  32. return MyPackage;
  33. }
  34. public int BufferSize() {
  35. return this.buffer.size();
  36. }
  37. }