forked from RevenantX/LiteNetLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseChannel.cs
More file actions
48 lines (40 loc) · 1.31 KB
/
BaseChannel.cs
File metadata and controls
48 lines (40 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System.Collections.Generic;
using System.Threading;
namespace LiteNetLib
{
internal abstract class BaseChannel
{
protected readonly NetPeer Peer;
protected readonly Queue<NetPacket> OutgoingQueue = new Queue<NetPacket>(NetConstants.DefaultWindowSize);
private int _isAddedToPeerChannelSendQueue;
public int PacketsInQueue => OutgoingQueue.Count;
protected BaseChannel(NetPeer peer)
{
Peer = peer;
}
public void AddToQueue(NetPacket packet)
{
lock (OutgoingQueue)
{
OutgoingQueue.Enqueue(packet);
}
AddToPeerChannelSendQueue();
}
protected void AddToPeerChannelSendQueue()
{
if (Interlocked.CompareExchange(ref _isAddedToPeerChannelSendQueue, 1, 0) == 0)
{
Peer.AddToReliableChannelSendQueue(this);
}
}
public bool SendAndCheckQueue()
{
bool hasPacketsToSend = SendNextPackets();
if (!hasPacketsToSend)
Interlocked.Exchange(ref _isAddedToPeerChannelSendQueue, 0);
return hasPacketsToSend;
}
protected abstract bool SendNextPackets();
public abstract bool ProcessPacket(NetPacket packet);
}
}