1
+ // NetSim Implementation compilation boilerplate
2
+ // All references to UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED should be defined in the same way,
3
+ // as any discrepancies are likely to result in build failures
4
+ #if UNITY_EDITOR || ( DEVELOPMENT_BUILD && ! UNITY_MP_TOOLS_NETSIM_DISABLED_IN_DEVELOP ) || ( ! DEVELOPMENT_BUILD && UNITY_MP_TOOLS_NETSIM_ENABLED_IN_RELEASE )
5
+ #define UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
6
+ #endif
7
+
1
8
using System ;
2
9
using System . Collections . Generic ;
3
10
using UnityEngine ;
@@ -343,6 +350,9 @@ public struct SimulatorParameters
343
350
/// - packet jitter (variances in latency, see: https://en.wikipedia.org/wiki/Jitter)
344
351
/// - packet drop rate (packet loss)
345
352
/// </summary>
353
+ #if UTP_TRANSPORT_2_0_ABOVE
354
+ [ Obsolete ( "DebugSimulator is no longer supported and has no effect. Use Network Simulator from the Multiplayer Tools package." , false ) ]
355
+ #endif
346
356
public SimulatorParameters DebugSimulator = new SimulatorParameters
347
357
{
348
358
PacketDelayMS = 0 ,
@@ -359,6 +369,10 @@ private struct PacketLossCache
359
369
public float PacketLoss ;
360
370
} ;
361
371
372
+ internal static event Action < int , NetworkDriver > TransportInitialized ;
373
+ internal static event Action < int > TransportDisposed ;
374
+ internal NetworkDriver NetworkDriver => m_Driver ;
375
+
362
376
private PacketLossCache m_PacketLossCache = new PacketLossCache ( ) ;
363
377
364
378
private State m_State = State . Disconnected ;
@@ -402,6 +416,8 @@ private void InitDriver()
402
416
out m_UnreliableFragmentedPipeline ,
403
417
out m_UnreliableSequencedFragmentedPipeline ,
404
418
out m_ReliableSequencedPipeline ) ;
419
+
420
+ TransportInitialized ? . Invoke ( GetInstanceID ( ) , NetworkDriver ) ;
405
421
}
406
422
407
423
private void DisposeInternals ( )
@@ -419,6 +435,8 @@ private void DisposeInternals()
419
435
}
420
436
421
437
m_SendQueue . Clear ( ) ;
438
+
439
+ TransportDisposed ? . Invoke ( GetInstanceID ( ) ) ;
422
440
}
423
441
424
442
private NetworkPipeline SelectSendPipeline ( NetworkDelivery delivery )
@@ -652,6 +670,9 @@ public void SetConnectionData(NetworkEndpoint endPoint, NetworkEndpoint listenEn
652
670
/// <param name="packetDelay">Packet delay in milliseconds.</param>
653
671
/// <param name="packetJitter">Packet jitter in milliseconds.</param>
654
672
/// <param name="dropRate">Packet drop percentage.</param>
673
+ #if UTP_TRANSPORT_2_0_ABOVE
674
+ [ Obsolete ( "SetDebugSimulatorParameters is no longer supported and has no effect. Use Network Simulator from the Multiplayer Tools package." , false ) ]
675
+ #endif
655
676
public void SetDebugSimulatorParameters ( int packetDelay , int packetJitter , int dropRate )
656
677
{
657
678
if ( m_Driver . IsCreated )
@@ -1329,7 +1350,25 @@ public override void Shutdown()
1329
1350
m_ServerClientId = 0 ;
1330
1351
}
1331
1352
1332
- private void ConfigureSimulator ( )
1353
+ #if UTP_TRANSPORT_2_0_ABOVE
1354
+ private void ConfigureSimulatorForUtp2 ( )
1355
+ {
1356
+ // As DebugSimulator is deprecated, the 'packetDelayMs', 'packetJitterMs' and 'packetDropPercentage'
1357
+ // parameters are set to the default and are supposed to be changed using Network Simulator tool instead.
1358
+ m_NetworkSettings . WithSimulatorStageParameters (
1359
+ maxPacketCount : 300 , // TODO Is there any way to compute a better value?
1360
+ maxPacketSize : NetworkParameterConstants . MTU ,
1361
+ packetDelayMs : 0 ,
1362
+ packetJitterMs : 0 ,
1363
+ packetDropPercentage : 0 ,
1364
+ randomSeed : DebugSimulatorRandomSeed ?? ( uint ) System . Diagnostics . Stopwatch . GetTimestamp ( )
1365
+ , mode : ApplyMode . AllPackets
1366
+ ) ;
1367
+
1368
+ m_NetworkSettings . WithNetworkSimulatorParameters ( ) ;
1369
+ }
1370
+ #else
1371
+ private void ConfigureSimulatorForUtp1 ( )
1333
1372
{
1334
1373
m_NetworkSettings . WithSimulatorStageParameters (
1335
1374
maxPacketCount : 300 , // TODO Is there any way to compute a better value?
@@ -1338,11 +1377,9 @@ private void ConfigureSimulator()
1338
1377
packetJitterMs : DebugSimulator . PacketJitterMS ,
1339
1378
packetDropPercentage : DebugSimulator . PacketDropRate ,
1340
1379
randomSeed : DebugSimulatorRandomSeed ?? ( uint ) System . Diagnostics . Stopwatch . GetTimestamp ( )
1341
- #if UTP_TRANSPORT_2_0_ABOVE
1342
- , mode : ApplyMode . AllPackets
1343
- #endif
1344
1380
) ;
1345
1381
}
1382
+ #endif
1346
1383
1347
1384
/// <summary>
1348
1385
/// Creates the internal NetworkDriver
@@ -1357,14 +1394,14 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
1357
1394
out NetworkPipeline unreliableSequencedFragmentedPipeline ,
1358
1395
out NetworkPipeline reliableSequencedPipeline )
1359
1396
{
1360
- #if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1361
- #if ! UTP_TRANSPORT_2_0_ABOVE
1397
+ #if MULTIPLAYER_TOOLS_1_0_0_PRE_7 && ! UTP_TRANSPORT_2_0_ABOVE
1362
1398
NetworkPipelineStageCollection . RegisterPipelineStage ( new NetworkMetricsPipelineStage ( ) ) ;
1363
1399
#endif
1364
- #endif
1365
1400
1366
- #if UNITY_EDITOR || DEVELOPMENT_BUILD
1367
- ConfigureSimulator ( ) ;
1401
+ #if UTP_TRANSPORT_2_0_ABOVE && UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
1402
+ ConfigureSimulatorForUtp2 ( ) ;
1403
+ #elif ! UTP_TRANSPORT_2_0_ABOVE && ( UNITY_EDITOR || DEVELOPMENT_BUILD )
1404
+ ConfigureSimulatorForUtp1 ( ) ;
1368
1405
#endif
1369
1406
1370
1407
m_NetworkSettings . WithNetworkConfigParameters (
@@ -1395,42 +1432,53 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
1395
1432
driver = NetworkDriver . Create ( m_NetworkSettings ) ;
1396
1433
#endif
1397
1434
1398
- #if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1399
- #if UTP_TRANSPORT_2_0_ABOVE
1400
- driver . RegisterPipelineStage < NetworkMetricsPipelineStage > ( new NetworkMetricsPipelineStage ( ) ) ;
1435
+ #if MULTIPLAYER_TOOLS_1_0_0_PRE_7 && UTP_TRANSPORT_2_0_ABOVE
1436
+ driver . RegisterPipelineStage ( new NetworkMetricsPipelineStage ( ) ) ;
1401
1437
#endif
1438
+
1439
+ #if ! UTP_TRANSPORT_2_0_ABOVE
1440
+ SetupPipelinesForUtp1 ( driver ,
1441
+ out unreliableFragmentedPipeline ,
1442
+ out unreliableSequencedFragmentedPipeline ,
1443
+ out reliableSequencedPipeline ) ;
1444
+ #else
1445
+ SetupPipelinesForUtp2 ( driver ,
1446
+ out unreliableFragmentedPipeline ,
1447
+ out unreliableSequencedFragmentedPipeline ,
1448
+ out reliableSequencedPipeline ) ;
1402
1449
#endif
1450
+ }
1403
1451
1452
+ #if ! UTP_TRANSPORT_2_0_ABOVE
1453
+ private void SetupPipelinesForUtp1 ( NetworkDriver driver ,
1454
+ out NetworkPipeline unreliableFragmentedPipeline ,
1455
+ out NetworkPipeline unreliableSequencedFragmentedPipeline ,
1456
+ out NetworkPipeline reliableSequencedPipeline )
1457
+ {
1404
1458
#if UNITY_EDITOR || DEVELOPMENT_BUILD
1405
1459
if ( DebugSimulator . PacketDelayMS > 0 || DebugSimulator . PacketDropRate > 0 )
1406
1460
{
1407
1461
unreliableFragmentedPipeline = driver . CreatePipeline (
1408
1462
typeof ( FragmentationPipelineStage ) ,
1409
- typeof ( SimulatorPipelineStage )
1410
- #if ! UTP_TRANSPORT_2_0_ABOVE
1411
- , typeof ( SimulatorPipelineStageInSend )
1412
- #endif
1463
+ typeof ( SimulatorPipelineStage ) ,
1464
+ typeof ( SimulatorPipelineStageInSend )
1413
1465
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1414
1466
, typeof ( NetworkMetricsPipelineStage )
1415
1467
#endif
1416
1468
) ;
1417
1469
unreliableSequencedFragmentedPipeline = driver . CreatePipeline (
1418
1470
typeof ( FragmentationPipelineStage ) ,
1419
1471
typeof ( UnreliableSequencedPipelineStage ) ,
1420
- typeof ( SimulatorPipelineStage )
1421
- #if ! UTP_TRANSPORT_2_0_ABOVE
1422
- , typeof ( SimulatorPipelineStageInSend )
1423
- #endif
1472
+ typeof ( SimulatorPipelineStage ) ,
1473
+ typeof ( SimulatorPipelineStageInSend )
1424
1474
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1425
1475
, typeof ( NetworkMetricsPipelineStage )
1426
1476
#endif
1427
1477
) ;
1428
1478
reliableSequencedPipeline = driver . CreatePipeline (
1429
1479
typeof ( ReliableSequencedPipelineStage ) ,
1430
- typeof ( SimulatorPipelineStage )
1431
- #if ! UTP_TRANSPORT_2_0_ABOVE
1432
- , typeof ( SimulatorPipelineStageInSend )
1433
- #endif
1480
+ typeof ( SimulatorPipelineStage ) ,
1481
+ typeof ( SimulatorPipelineStageInSend )
1434
1482
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1435
1483
, typeof ( NetworkMetricsPipelineStage )
1436
1484
#endif
@@ -1460,7 +1508,45 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
1460
1508
) ;
1461
1509
}
1462
1510
}
1511
+ #else
1512
+ private void SetupPipelinesForUtp2 ( NetworkDriver driver ,
1513
+ out NetworkPipeline unreliableFragmentedPipeline ,
1514
+ out NetworkPipeline unreliableSequencedFragmentedPipeline ,
1515
+ out NetworkPipeline reliableSequencedPipeline )
1516
+ {
1463
1517
1518
+ unreliableFragmentedPipeline = driver . CreatePipeline (
1519
+ typeof ( FragmentationPipelineStage )
1520
+ #if UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
1521
+ , typeof ( SimulatorPipelineStage )
1522
+ #endif
1523
+ #if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1524
+ , typeof ( NetworkMetricsPipelineStage )
1525
+ #endif
1526
+ ) ;
1527
+
1528
+ unreliableSequencedFragmentedPipeline = driver . CreatePipeline (
1529
+ typeof ( FragmentationPipelineStage ) ,
1530
+ typeof ( UnreliableSequencedPipelineStage )
1531
+ #if UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
1532
+ , typeof ( SimulatorPipelineStage )
1533
+ #endif
1534
+ #if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1535
+ , typeof ( NetworkMetricsPipelineStage )
1536
+ #endif
1537
+ ) ;
1538
+
1539
+ reliableSequencedPipeline = driver . CreatePipeline (
1540
+ typeof ( ReliableSequencedPipelineStage )
1541
+ #if UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
1542
+ , typeof ( SimulatorPipelineStage )
1543
+ #endif
1544
+ #if MULTIPLAYER_TOOLS_1_0_0_PRE_7
1545
+ , typeof ( NetworkMetricsPipelineStage )
1546
+ #endif
1547
+ ) ;
1548
+ }
1549
+ #endif
1464
1550
// -------------- Utility Types -------------------------------------------------------------------------------
1465
1551
1466
1552
0 commit comments