SequencedChannel.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #if DEBUG && !UNITY_WP_8_1 && !UNITY_WSA_8_1
  2. using System.Collections.Generic;
  3. namespace FlyingWormConsole3.LiteNetLib
  4. {
  5. internal sealed class SequencedChannel
  6. {
  7. private ushort _localSequence;
  8. private ushort _remoteSequence;
  9. private readonly Queue<NetPacket> _outgoingPackets;
  10. private readonly NetPeer _peer;
  11. public SequencedChannel(NetPeer peer)
  12. {
  13. _outgoingPackets = new Queue<NetPacket>();
  14. _peer = peer;
  15. }
  16. public void AddToQueue(NetPacket packet)
  17. {
  18. lock (_outgoingPackets)
  19. {
  20. _outgoingPackets.Enqueue(packet);
  21. }
  22. }
  23. public bool SendNextPacket()
  24. {
  25. NetPacket packet;
  26. lock (_outgoingPackets)
  27. {
  28. if (_outgoingPackets.Count == 0)
  29. return false;
  30. packet = _outgoingPackets.Dequeue();
  31. }
  32. _localSequence++;
  33. packet.Sequence = _localSequence;
  34. _peer.SendRawData(packet);
  35. _peer.Recycle(packet);
  36. return true;
  37. }
  38. public void ProcessPacket(NetPacket packet)
  39. {
  40. if (NetUtils.RelativeSequenceNumber(packet.Sequence, _remoteSequence) > 0)
  41. {
  42. _remoteSequence = packet.Sequence;
  43. _peer.AddIncomingPacket(packet);
  44. }
  45. }
  46. }
  47. }
  48. #endif