Skip to content

Commit 0be12ed

Browse files
committedAug 19, 2016
replace slow reset events with Monitor.
1 parent 6462e7d commit 0be12ed

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed
 

‎LiteNetLib/ReliableChannel.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public NetPacket GetAndClear()
3838
private readonly double _resendDelay;
3939
private readonly bool _ordered;
4040
private readonly int _windowSize;
41-
private readonly AutoResetEvent _pendingPacketsAccess = new AutoResetEvent(true);
42-
private readonly AutoResetEvent _outgoingAcksAccess = new AutoResetEvent(true);
41+
private readonly object _pendingPacketsAccess = new object();
42+
private readonly object _outgoingAcksAccess = new object();
4343
private const int BitsInByte = 8;
4444

4545
private int _queueIndex;
@@ -97,7 +97,7 @@ public void ProcessAck(NetPacket packet)
9797
_peer.DebugWrite("[PA]AcksStart: {0}", ackWindowStart);
9898
int startByte = NetConstants.SequencedHeaderSize;
9999

100-
_pendingPacketsAccess.WaitOne();
100+
Monitor.Enter(_pendingPacketsAccess);
101101
for (int i = 0; i < _windowSize; i++)
102102
{
103103
int ackSequence = (ackWindowStart + i) % NetConstants.MaxSequence;
@@ -135,7 +135,7 @@ public void ProcessAck(NetPacket packet)
135135
_peer.DebugWrite("[PA]Removing reliableInOrder ack: {0} - false", ackSequence);
136136
}
137137
}
138-
_pendingPacketsAccess.Set();
138+
Monitor.Exit(_pendingPacketsAccess);
139139
}
140140

141141
public void AddToQueue(NetPacket packet)
@@ -176,7 +176,7 @@ public bool SendNextPacket()
176176
//check sending acks
177177
DateTime currentTime = DateTime.UtcNow;
178178

179-
_pendingPacketsAccess.WaitOne();
179+
Monitor.Enter(_pendingPacketsAccess);
180180
ProcessQueuedPackets();
181181

182182
//send
@@ -214,7 +214,7 @@ public bool SendNextPacket()
214214
sendResult = _peer.SendRawData(currentPacket.Packet.RawData);
215215
_peer.DebugWrite("[RR]Sended: {0}", sendResult);
216216
}
217-
_pendingPacketsAccess.Set();
217+
Monitor.Exit(_pendingPacketsAccess);
218218
return sendResult;
219219
}
220220

@@ -235,7 +235,7 @@ public void SendAcks()
235235
byte[] data = acksPacket.RawData; //window start + acks size
236236

237237
//Put window start
238-
_outgoingAcksAccess.WaitOne();
238+
Monitor.Enter(_outgoingAcksAccess);
239239
acksPacket.Sequence = (ushort)_remoteWindowStart;
240240

241241
//Put acks
@@ -258,7 +258,7 @@ public void SendAcks()
258258
}
259259
currentAckIndex = (currentAckIndex + 1) % _windowSize;
260260
} while (currentAckIndex != startAckIndex);
261-
_outgoingAcksAccess.Set();
261+
Monitor.Exit(_outgoingAcksAccess);
262262

263263
_peer.SendRawData(acksPacket.RawData);
264264
_peer.Recycle(acksPacket);
@@ -297,7 +297,7 @@ public void ProcessPacket(NetPacket packet)
297297
}
298298

299299
//If very new - move window
300-
_outgoingAcksAccess.WaitOne();
300+
Monitor.Enter(_outgoingAcksAccess);
301301
if (relate >= _windowSize)
302302
{
303303
//New window position
@@ -318,13 +318,13 @@ public void ProcessPacket(NetPacket packet)
318318
if (_outgoingAcks[packet.Sequence % _windowSize])
319319
{
320320
_peer.DebugWrite("[RR]ReliableInOrder duplicate");
321-
_outgoingAcksAccess.Set();
321+
Monitor.Exit(_outgoingAcksAccess);
322322
return;
323323
}
324324

325325
//save ack
326326
_outgoingAcks[packet.Sequence % _windowSize] = true;
327-
_outgoingAcksAccess.Set();
327+
Monitor.Exit(_outgoingAcksAccess);
328328

329329
//detailed check
330330
if (packet.Sequence == _remoteSequence)

0 commit comments

Comments
 (0)