forked from jbruening/PNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlidgren_patch.patch
More file actions
508 lines (471 loc) · 18.7 KB
/
lidgren_patch.patch
File metadata and controls
508 lines (471 loc) · 18.7 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
Index: Lidgren.Network.csproj
===================================================================
--- Lidgren.Network.csproj (revision 357)
+++ Lidgren.Network.csproj (working copy)
@@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Lidgren.Network</RootNamespace>
<AssemblyName>Lidgren.Network</AssemblyName>
- <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
@@ -27,6 +27,7 @@
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
+ <TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -38,6 +39,7 @@
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<DocumentationFile>bin\Debug\Lidgren.Network.XML</DocumentationFile>
+ <PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@@ -47,11 +49,12 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
+ <PlatformTarget>AnyCPU</PlatformTarget>
+ <DocumentationFile>bin\Release\Lidgren.Network.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
- <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
Index: NetBuffer.cs
===================================================================
--- NetBuffer.cs (revision 357)
+++ NetBuffer.cs (working copy)
@@ -69,8 +69,39 @@
{
get { return (int)(m_readPosition / 8); }
}
-
- static NetBuffer()
+
+ /// <summary>
+ /// Gets the remaining number of bits in the buffer to be read
+ /// </summary>
+ public long RemainingBits
+ {
+ get { return (long)(m_bitLength - m_readPosition); }
+ }
+
+ /// <summary>
+ /// Clones this buffer into dest
+ /// </summary>
+ /// <param name="dest"></param>
+ public void Clone(NetBuffer dest)
+ {
+ dest.m_data = new byte[m_data.Length];
+
+ Buffer.BlockCopy(m_data, 0, dest.m_data, 0, m_data.Length);
+ //foreach loops are faster in release builds until about 32k of bytes. supposedly. probably has something to do with the method call taking time.
+ //dest.m_data = new byte[m_data.Length];
+ //var i = 0;
+ //foreach (var by in m_data)
+ //{
+ // dest.m_data[i] = by;
+ // ++i;
+ //}
+
+ //set other values...
+ dest.m_bitLength = m_bitLength;
+ dest.m_readPosition = 0;
+ }
+
+ static NetBuffer()
{
s_readMethods = new Dictionary<Type, MethodInfo>();
MethodInfo[] methods = typeof(NetIncomingMessage).GetMethods(BindingFlags.Instance | BindingFlags.Public);
Index: NetConnection.cs
===================================================================
--- NetConnection.cs (revision 357)
+++ NetConnection.cs (working copy)
@@ -19,6 +19,8 @@
internal NetConnectionStatus m_status;
internal NetConnectionStatus m_visibleStatus;
internal IPEndPoint m_remoteEndPoint;
+ internal int m_usedSendChannelsCount;
+ internal NetSenderChannelBase[] m_usedSendChannels;
internal NetSenderChannelBase[] m_sendChannels;
internal NetReceiverChannelBase[] m_receiveChannels;
internal NetOutgoingMessage m_localHailMessage;
@@ -85,6 +87,8 @@
m_status = NetConnectionStatus.None;
m_visibleStatus = NetConnectionStatus.None;
m_remoteEndPoint = remoteEndPoint;
+ m_usedSendChannelsCount = 0;
+ m_usedSendChannels = new NetSenderChannelBase[NetConstants.NumTotalChannels];
m_sendChannels = new NetSenderChannelBase[NetConstants.NumTotalChannels];
m_receiveChannels = new NetReceiverChannelBase[NetConstants.NumTotalChannels];
m_queuedOutgoingAcks = new NetQueue<NetTuple<NetMessageType, int>>(4);
@@ -195,7 +199,8 @@
m_sendBufferNumMessages++;
// write acks header
- sendBuffer[m_sendBufferWritePtr++] = (byte)NetMessageType.Acknowledge;
+ const byte ackByte = (byte) NetMessageType.Acknowledge;
+ sendBuffer[m_sendBufferWritePtr++] = ackByte;
sendBuffer[m_sendBufferWritePtr++] = 0; // no sequence number
sendBuffer[m_sendBufferWritePtr++] = 0; // no sequence number
int len = (acks * 3) * 8; // bits
@@ -243,31 +248,40 @@
//
// send queued messages
//
- if (m_peer.m_executeFlushSendQueue)
- {
- for (int i = m_sendChannels.Length - 1; i >= 0; i--) // Reverse order so reliable messages are sent first
- {
- var channel = m_sendChannels[i];
- NetException.Assert(m_sendBufferWritePtr < 1 || m_sendBufferNumMessages > 0);
- if (channel != null)
- channel.SendQueuedMessages(now);
- NetException.Assert(m_sendBufferWritePtr < 1 || m_sendBufferNumMessages > 0);
- }
- }
+ SendQueuedMessages(now);
//
// Put on wire data has been written to send buffer but not yet sent
//
- if (m_sendBufferWritePtr > 0)
- {
- m_peer.VerifyNetworkThread();
- NetException.Assert(m_sendBufferWritePtr > 0 && m_sendBufferNumMessages > 0);
- m_peer.SendPacket(m_sendBufferWritePtr, m_remoteEndPoint, m_sendBufferNumMessages, out connectionReset);
- m_statistics.PacketSent(m_sendBufferWritePtr, m_sendBufferNumMessages);
- m_sendBufferWritePtr = 0;
- m_sendBufferNumMessages = 0;
- }
+ SendPacket();
}
+
+ private void SendQueuedMessages(float now)
+ {
+ if (m_peer.m_executeFlushSendQueue)
+ {
+ for (int i = 0; i < m_usedSendChannelsCount; i++)
+ {
+ NetException.Assert(m_sendBufferWritePtr < 1 || m_sendBufferNumMessages > 0);
+ m_usedSendChannels[i].SendQueuedMessages(now);
+ NetException.Assert(m_sendBufferWritePtr < 1 || m_sendBufferNumMessages > 0);
+ }
+ }
+ }
+
+ private void SendPacket()
+ {
+ if (m_sendBufferWritePtr > 0)
+ {
+ m_peer.VerifyNetworkThread();
+ NetException.Assert(m_sendBufferWritePtr > 0 && m_sendBufferNumMessages > 0);
+ bool connectionReset;
+ m_peer.SendPacket(m_sendBufferWritePtr, m_remoteEndPoint, m_sendBufferNumMessages, out connectionReset);
+ m_statistics.PacketSent(m_sendBufferWritePtr, m_sendBufferNumMessages);
+ m_sendBufferWritePtr = 0;
+ m_sendBufferNumMessages = 0;
+ }
+ }
// Queue an item for immediate sending on the wire
// This method is called from the ISenderChannels
@@ -364,7 +378,16 @@
chan = new NetReliableSenderChannel(this, NetUtility.GetWindowSize(method));
break;
}
+
m_sendChannels[channelSlot] = chan;
+
+ // rebuild m_usedSendChannels
+ m_usedSendChannelsCount = 0;
+ for (int i = m_sendChannels.Length - 1; i >= 0; i--) // Reverse order so reliable messages are sent first
+ {
+ if (m_sendChannels[i] != null)
+ m_usedSendChannels[m_usedSendChannelsCount++] = m_sendChannels[i];
+ }
}
}
Index: NetConnectionStatistics.cs
===================================================================
--- NetConnectionStatistics.cs (revision 357)
+++ NetConnectionStatistics.cs (working copy)
@@ -177,7 +177,7 @@
if (relSendChan != null)
{
for (int i = 0; i < relSendChan.m_storedMessages.Length; i++)
- if (relSendChan.m_storedMessages[i].Message != null)
+ if ((relSendChan.m_usedStoredMessages & ((ulong)1 << i)) != 0)
numStored++;
}
}
Index: NetConstants.cs
===================================================================
--- NetConstants.cs (revision 357)
+++ NetConstants.cs (working copy)
@@ -25,18 +25,19 @@
/// </summary>
internal static class NetConstants
{
- internal const int NumTotalChannels = 99;
+ internal const int NumTotalChannels = 99; // Don't change!
- internal const int NetChannelsPerDeliveryMethod = 32;
+ internal const int NetChannelsPerDeliveryMethod = 32; // Don't change!
- internal const int NumSequenceNumbers = 1024;
+ internal const int NumSequenceNumbers = 1024; // Must be power of two
+ internal const int NumSequenceNumberMask = NumSequenceNumbers - 1; // Don't change!
internal const int HeaderByteSize = 5;
- internal const int UnreliableWindowSize = 128;
- internal const int ReliableOrderedWindowSize = 64;
- internal const int ReliableSequencedWindowSize = 64;
- internal const int DefaultWindowSize = 64;
+ internal const int UnreliableWindowSize = 1024;
+ internal const int ReliableOrderedWindowSize = 64; // MAX: 64
+ internal const int ReliableSequencedWindowSize = 64; // MAX: 64
+ internal const int DefaultWindowSize = 64; // MAX: 64
internal const int MaxFragmentationGroups = ushort.MaxValue - 1;
Index: NetPeer.Internal.cs
===================================================================
--- NetPeer.Internal.cs (revision 357)
+++ NetPeer.Internal.cs (working copy)
@@ -1,5 +1,5 @@
#if !__ANDROID__ && !IOS
-#define IS_MAC_AVAILABLE
+//#define IS_MAC_AVAILABLE
#endif
using System;
Index: NetPeer.MessagePools.cs
===================================================================
--- NetPeer.MessagePools.cs (revision 357)
+++ NetPeer.MessagePools.cs (working copy)
@@ -184,7 +184,7 @@
m_incomingMessagesPool.Enqueue(toRecycle);
}
- internal void Recycle(NetOutgoingMessage msg)
+ public void Recycle(NetOutgoingMessage msg)
{
if (m_outgoingMessagesPool == null)
return;
Index: NetReliableSenderChannel.cs
===================================================================
--- NetReliableSenderChannel.cs (revision 357)
+++ NetReliableSenderChannel.cs (working copy)
@@ -14,6 +14,7 @@
private int m_sendStart;
private NetBitVector m_receivedAcks;
+ internal ulong m_usedStoredMessages; // "used" bits for storedMessages
internal NetStoredReliableMessage[] m_storedMessages;
internal float m_resendDelay;
@@ -27,6 +28,8 @@
m_windowStart = 0;
m_sendStart = 0;
m_receivedAcks = new NetBitVector(NetConstants.NumSequenceNumbers);
+ NetException.Assert(m_windowSize <= 64); // we do only have sizeof(ulong)*8 "used" bits in m_usedStoredMessages
+ m_usedStoredMessages = 0;
m_storedMessages = new NetStoredReliableMessage[m_windowSize];
m_queuedSends = new NetQueue<NetOutgoingMessage>(8);
m_resendDelay = m_connection.GetResendDelay();
@@ -34,7 +37,7 @@
internal override int GetAllowedSends()
{
- int retval = m_windowSize - ((m_sendStart + NetConstants.NumSequenceNumbers) - m_windowStart) % NetConstants.NumSequenceNumbers;
+ int retval = m_windowSize - ((m_sendStart + NetConstants.NumSequenceNumbers) - m_windowStart) & NetConstants.NumSequenceNumberMask;
NetException.Assert(retval >= 0 && retval <= m_windowSize);
return retval;
}
@@ -63,48 +66,54 @@
//
// resends
//
- for (int i = 0; i < m_storedMessages.Length; i++)
- {
- NetOutgoingMessage om = m_storedMessages[i].Message;
- if (om == null)
- continue;
+ if (m_usedStoredMessages != 0)
+ {
+ for (int i = 0; i < m_storedMessages.Length; i++)
+ {
+ if ((m_usedStoredMessages & ((ulong)1 << i)) == 0)
+ continue;
- float t = m_storedMessages[i].LastSent;
- if (t > 0 && (now - t) > m_resendDelay)
- {
- // deduce sequence number
- /*
- int startSlot = m_windowStart % m_windowSize;
- int seqNr = m_windowStart;
- while (startSlot != i)
- {
- startSlot--;
- if (startSlot < 0)
- startSlot = m_windowSize - 1;
- seqNr--;
- }
- */
+ float t = m_storedMessages[i].LastSent;
+ if (t > 0 && (now - t) > m_resendDelay)
+ {
+ // deduce sequence number
+ /*
+ int startSlot = m_windowStart % m_windowSize;
+ int seqNr = m_windowStart;
+ while (startSlot != i)
+ {
+ startSlot--;
+ if (startSlot < 0)
+ startSlot = m_windowSize - 1;
+ seqNr--;
+ }
+ */
- //m_connection.m_peer.LogVerbose("Resending due to delay #" + m_storedMessages[i].SequenceNumber + " " + om.ToString());
- m_connection.m_statistics.MessageResent(MessageResendReason.Delay);
+ //m_connection.m_peer.LogVerbose("Resending due to delay #" + m_storedMessages[i].SequenceNumber + " " + om.ToString());
+ m_connection.m_statistics.MessageResent(MessageResendReason.Delay);
- m_connection.QueueSendMessage(om, m_storedMessages[i].SequenceNumber);
+ m_connection.QueueSendMessage(m_storedMessages[i].Message, m_storedMessages[i].SequenceNumber);
- m_storedMessages[i].LastSent = now;
- m_storedMessages[i].NumSent++;
- }
- }
+ m_storedMessages[i].LastSent = now;
+ m_storedMessages[i].NumSent++;
+ }
+ }
+ }
int num = GetAllowedSends();
- if (num < 1)
+ if (num == 0)
return;
// queued sends
- while (m_queuedSends.Count > 0 && num > 0)
+ int queued = m_queuedSends.Count;
+ while (queued > 0 && num > 0)
{
NetOutgoingMessage om;
- if (m_queuedSends.TryDequeue(out om))
- ExecuteSend(now, om);
+ if (m_queuedSends.TryDequeue(out om))
+ {
+ ExecuteSend(now, om);
+ queued--;
+ }
num--;
NetException.Assert(num == GetAllowedSends());
}
@@ -113,13 +122,14 @@
private void ExecuteSend(float now, NetOutgoingMessage message)
{
int seqNr = m_sendStart;
- m_sendStart = (m_sendStart + 1) % NetConstants.NumSequenceNumbers;
+ m_sendStart = (m_sendStart + 1) & NetConstants.NumSequenceNumberMask;
m_connection.QueueSendMessage(message, seqNr);
int storeIndex = seqNr % m_windowSize;
- NetException.Assert(m_storedMessages[storeIndex].Message == null);
+ NetException.Assert((m_usedStoredMessages & ((ulong)1 << storeIndex)) == 0);
+ m_usedStoredMessages |= (ulong)1 << storeIndex; // set used bit
m_storedMessages[storeIndex].NumSent++;
m_storedMessages[storeIndex].Message = message;
m_storedMessages[storeIndex].LastSent = now;
@@ -145,6 +155,7 @@
#if !DEBUG
}
#endif
+ m_usedStoredMessages &= ~((ulong)1 << storeIndex); // clear used bit
m_storedMessages[storeIndex] = new NetStoredReliableMessage();
}
@@ -170,7 +181,7 @@
m_receivedAcks[m_windowStart] = false;
DestoreMessage(m_windowStart % m_windowSize);
- m_windowStart = (m_windowStart + 1) % NetConstants.NumSequenceNumbers;
+ m_windowStart = (m_windowStart + 1) & NetConstants.NumSequenceNumberMask;
// advance window if we already have early acks
while (m_receivedAcks.Get(m_windowStart))
@@ -179,8 +190,8 @@
m_receivedAcks[m_windowStart] = false;
DestoreMessage(m_windowStart % m_windowSize);
- NetException.Assert(m_storedMessages[m_windowStart % m_windowSize].Message == null); // should already be destored
- m_windowStart = (m_windowStart + 1) % NetConstants.NumSequenceNumbers;
+ NetException.Assert((m_usedStoredMessages & ((ulong)1 << (m_windowStart % m_windowSize))) == 0); // should already be destored
+ m_windowStart = (m_windowStart + 1) & NetConstants.NumSequenceNumberMask;
//m_connection.m_peer.LogDebug("Advancing window to #" + m_windowStart);
}
Index: NetUnreliableSenderChannel.cs
===================================================================
--- NetUnreliableSenderChannel.cs (revision 357)
+++ NetUnreliableSenderChannel.cs (working copy)
@@ -29,7 +29,10 @@
internal override int GetAllowedSends()
{
- int retval = m_windowSize - ((m_sendStart + NetConstants.NumSequenceNumbers) - m_windowStart) % m_windowSize;
+ int subtract = (m_sendStart + NetConstants.NumSequenceNumbers) - m_windowStart;
+ while (subtract >= m_windowSize)
+ subtract -= m_windowSize;
+ int retval = m_windowSize - subtract;
NetException.Assert(retval >= 0 && retval <= m_windowSize);
return retval;
}
@@ -57,15 +60,19 @@
internal override void SendQueuedMessages(float now)
{
int num = GetAllowedSends();
- if (num < 1)
+ if (num == 0)
return;
// queued sends
- while (m_queuedSends.Count > 0 && num > 0)
+ int queued = m_queuedSends.Count;
+ while (queued > 0 && num > 0)
{
NetOutgoingMessage om;
- if (m_queuedSends.TryDequeue(out om))
- ExecuteSend(om);
+ if (m_queuedSends.TryDequeue(out om))
+ {
+ ExecuteSend(om);
+ queued--;
+ }
num--;
}
}
@@ -75,7 +82,7 @@
m_connection.m_peer.VerifyNetworkThread();
int seqNr = m_sendStart;
- m_sendStart = (m_sendStart + 1) % NetConstants.NumSequenceNumbers;
+ m_sendStart = (m_sendStart + 1) & NetConstants.NumSequenceNumberMask;
m_connection.QueueSendMessage(message, seqNr);
@@ -107,7 +114,7 @@
NetException.Assert(seqNr == m_windowStart);
m_receivedAcks[m_windowStart] = false;
- m_windowStart = (m_windowStart + 1) % NetConstants.NumSequenceNumbers;
+ m_windowStart = (m_windowStart + 1) & NetConstants.NumSequenceNumberMask;
return;
}
@@ -118,7 +125,7 @@
while (m_windowStart != seqNr)
{
m_receivedAcks[m_windowStart] = false;
- m_windowStart = (m_windowStart + 1) % NetConstants.NumSequenceNumbers;
+ m_windowStart = (m_windowStart + 1) & NetConstants.NumSequenceNumberMask;
}
}
}
Index: NetUtility.cs
===================================================================
--- NetUtility.cs (revision 357)
+++ NetUtility.cs (working copy)
@@ -17,7 +17,7 @@
USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#if !__ANDROID__ && !IOS
-#define IS_FULL_NET_AVAILABLE
+//#define IS_FULL_NET_AVAILABLE
#endif
using System;