NetPacketProvider.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using IFramework.Packets;
  2. using System.Collections.Generic;
  3. namespace IFramework.Net
  4. {
  5. internal class NetPacketProvider:INetPacketProvider
  6. {
  7. private PacketReader packetQueue = null;
  8. LockParam lockParam = null;
  9. public NetPacketProvider(int capacity)
  10. {
  11. if (capacity < 128) capacity = 128;
  12. capacity += 1;
  13. packetQueue = new PacketReader(capacity);
  14. lockParam = new LockParam();
  15. }
  16. public static NetPacketProvider CreateProvider(int capacity)
  17. {
  18. return new NetPacketProvider(capacity);
  19. }
  20. public int Count
  21. {
  22. get
  23. {
  24. return packetQueue.count;
  25. }
  26. }
  27. public bool SetBlocks(byte[] bufffer,int offset,int size)
  28. {
  29. using (LockWait lwait = new LockWait(ref lockParam))
  30. {
  31. return packetQueue.Set(bufffer, offset, size);
  32. }
  33. }
  34. public List<Packet> GetBlocks()
  35. {
  36. using (LockWait lwait = new LockWait(ref lockParam))
  37. {
  38. return packetQueue.Get();
  39. }
  40. }
  41. public void Clear()
  42. {
  43. using (LockWait lwait = new LockWait(ref lockParam))
  44. {
  45. packetQueue.Clear();
  46. }
  47. }
  48. }
  49. }