public void Init(string ip, ushort port, bool useLocalhost, float timeout, bool useSimulationPipeline) { SetEndpoint(ip, port, useLocalhost); _timeout = timeout; _averageServerTimeOffset = 0; if (_clientDriver.IsCreated) { _clientDriver.Dispose(); } _clientDriver = new NetworkDriver(new BaselibNetworkInterface(), new SimulatorUtility.Parameters { MaxPacketCount = 30, PacketDropPercentage = 5, MaxPacketSize = 256, PacketDelayMs = 50 }, new ReliableUtility.Parameters { WindowSize = 32 }); _unreliablePipeline = useSimulationPipeline ? _clientDriver.CreatePipeline(typeof(SimulatorPipelineStage)) : NetworkPipeline.Null; _reliablePipeline = useSimulationPipeline ? _clientDriver.CreatePipeline(typeof(ReliableSequencedPipelineStage), typeof(SimulatorPipelineStage)) : _clientDriver.CreatePipeline(typeof(ReliableSequencedPipelineStage)); _initialized = true; }
public void CreateServerDriver(World world, out NetworkDriver driver, out NetworkPipeline unreliablePipeline, out NetworkPipeline reliablePipeline, out NetworkPipeline unreliableFragmentedPipeline) { var reliabilityParams = new ReliableUtility.Parameters { WindowSize = 32 }; var fragmentationParams = new FragmentationUtility.Parameters { PayloadCapacity = 16 * 1024 }; #if UNITY_EDITOR || DEVELOPMENT_BUILD var netParams = new NetworkConfigParameter { maxConnectAttempts = NetworkParameterConstants.MaxConnectAttempts, connectTimeoutMS = NetworkParameterConstants.ConnectTimeoutMS, disconnectTimeoutMS = NetworkParameterConstants.DisconnectTimeoutMS, maxFrameTimeMS = 100 }; driver = NetworkDriver.Create(netParams, reliabilityParams, fragmentationParams); #else driver = NetworkDriver.Create(reliabilityParams); #endif unreliablePipeline = driver.CreatePipeline(typeof(NullPipelineStage)); reliablePipeline = driver.CreatePipeline(typeof(ReliableSequencedPipelineStage)); unreliableFragmentedPipeline = driver.CreatePipeline(typeof(FragmentationPipelineStage)); }
public void CreateClientDriver(World world, out NetworkDriver driver, out NetworkPipeline unreliablePipeline, out NetworkPipeline reliablePipeline) { var reliabilityParams = new ReliableUtility.Parameters { WindowSize = 32 }; var netParams = new NetworkConfigParameter { maxConnectAttempts = NetworkParameterConstants.MaxConnectAttempts, connectTimeoutMS = NetworkParameterConstants.ConnectTimeoutMS, disconnectTimeoutMS = NetworkParameterConstants.DisconnectTimeoutMS, maxFrameTimeMS = 100, fixedFrameTimeMS = DriverFixedTime }; var packetDelay = DriverSimulatedDelay; int networkRate = 60; // All 3 packet types every frame stored for maximum delay, doubled for safety margin int maxPackets = 2 * (networkRate * 3 * packetDelay + 999) / 1000; var fuzzFactor = 0; const int kStringLength = 10; // we name it ClientTest e.g. 10 bytes long. var worldId = int.Parse(world.Name.Substring(kStringLength, world.Name.Length - kStringLength)); if (DriverFuzzFactor?.Length >= worldId + 1) { fuzzFactor = DriverFuzzFactor[worldId]; } var simParams = new SimulatorUtility.Parameters { MaxPacketSize = NetworkParameterConstants.MTU, MaxPacketCount = maxPackets, PacketDelayMs = packetDelay, FuzzFactor = fuzzFactor, FuzzOffset = DriverFuzzOffset, RandomSeed = DriverRandomSeed }; driver = new NetworkDriver(new IPCNetworkInterface(), netParams, reliabilityParams, simParams); if (DriverSimulatedDelay + fuzzFactor > 0) { unreliablePipeline = driver.CreatePipeline(typeof(SimulatorPipelineStage), typeof(SimulatorPipelineStageInSend)); reliablePipeline = driver.CreatePipeline(typeof(SimulatorPipelineStageInSend), typeof(ReliableSequencedPipelineStage), typeof(SimulatorPipelineStage)); } else { unreliablePipeline = driver.CreatePipeline(typeof(NullPipelineStage)); reliablePipeline = driver.CreatePipeline(typeof(ReliableSequencedPipelineStage)); } }
public void Start(NetworkDriver serverDriver) { if (IsActive) { serverDriver.Dispose(); throw new InvalidOperationException("Cannot start server: is already active"); } if (!_initialized) { serverDriver.Dispose(); throw new InvalidOperationException("Cannot start server: not initialized. You must call Init() first"); } if (_serverDriver.IsCreated) { _serverDriver.Dispose(); } _serverDriver = serverDriver; _unreliablePipeline = _useSimulationPipeline ? _serverDriver.CreatePipeline(typeof(SimulatorPipelineStage)) : NetworkPipeline.Null; _reliablePipeline = _useSimulationPipeline ? _serverDriver.CreatePipeline(typeof(ReliableSequencedPipelineStage), typeof(SimulatorPipelineStage)) : _serverDriver.CreatePipeline(typeof(ReliableSequencedPipelineStage)); _connections = new NativeList <NetworkConnection>(16, Allocator.Persistent); foreach (ushort port in _ports) { _endPoint.Port = port; if (_serverDriver.Bind(_endPoint) != 0) { continue; } _serverDriver.Listen(); State = ServerState.Started; Debug.Log($"Started server on port {_endPoint.Port}"); break; } if (State != ServerState.Started) { _connections.Dispose(); _serverDriver.Dispose(); Application.Quit(-1); throw new NetException("Failed to bind to any port"); } }
public void CreateDriver(int minConnections, int maxConnections) { this.minConnections = minConnections; this.maxConnections = maxConnections; DestroyDriver(); // Create a new driver and listen for connections. driver = NetworkDriver.Create(new ReliableUtility.Parameters { WindowSize = 32 }); reliablePipeline = driver.CreatePipeline(typeof(ReliableSequencedPipelineStage)); var endPoint = NetworkEndPoint.AnyIpv4; endPoint.Port = 9000; var binding = driver.Bind(endPoint); Assert.AreEqual(0, binding); driver.Listen(); connections = new NativeList <NetworkConnection>(maxConnections, Allocator.Persistent); playerInfoByID = new NativeHashMap <int, PlayerInfo>(maxConnections, Allocator.Persistent); readMessage = ReadJoingingAreaMessages; }
void Start() { m_Connections = new NativeList <NetworkConnection>(16, Allocator.Persistent); m_Room = new NativeList <Room>(16, Allocator.Persistent); m_ConnectedClients = new NativeMultiHashMap <int, Client>(16, Allocator.Persistent); m_ConnectionIndexToRoomID = new NativeHashMap <int, int>(1, Allocator.Persistent); m_ServerAddressToRoomID = new NativeHashMap <FixedString32, int>(1, Allocator.Persistent); m_ReleasedRoomIds = new NativeList <Room>(Allocator.Persistent); // Driver can be used as normal m_Driver = NetworkDriver.Create(); // Driver now knows about this pipeline and can explicitly be asked to send packets through it (by default it sends directly) m_Pipeline = m_Driver.CreatePipeline(typeof(UnreliableSequencedPipelineStage)); var endpoint = NetworkEndPoint.AnyIpv4; endpoint.Port = Config.ListenPort; if (m_Driver.Bind(endpoint) != 0) { Console.WriteLine($"[ERROR]Failed to bind to port {Config.ListenPort}"); } else { m_Driver.Listen(); } }
public void CreateDriver() { DestroyDriver(); driver = NetworkDriver.Create(new ReliableUtility.Parameters { WindowSize = 32 }); reliablePipeline = driver.CreatePipeline(typeof(ReliableSequencedPipelineStage)); readMessage = ReadJoingingAreaMessage; }
public void CreateServerDriver(World world, out NetworkDriver driver, out NetworkPipeline unreliablePipeline, out NetworkPipeline reliablePipeline) { var reliabilityParams = new ReliableUtility.Parameters { WindowSize = 32 }; var netParams = new NetworkConfigParameter { maxConnectAttempts = NetworkParameterConstants.MaxConnectAttempts, connectTimeoutMS = NetworkParameterConstants.ConnectTimeoutMS, disconnectTimeoutMS = NetworkParameterConstants.DisconnectTimeoutMS, maxFrameTimeMS = 100, fixedFrameTimeMS = DriverFixedTime }; driver = new NetworkDriver(new IPCNetworkInterface(), netParams, reliabilityParams); unreliablePipeline = driver.CreatePipeline(typeof(NullPipelineStage)); reliablePipeline = driver.CreatePipeline(typeof(ReliableSequencedPipelineStage)); }
public void NetworkPipeline_Fragmentation_SendRecvOnce() { var clientPipe = m_ClientDriver.CreatePipeline(typeof(FragmentationPipelineStage)); var serverPipe = m_ServerDriver.CreatePipeline(typeof(FragmentationPipelineStage)); // Connect to server var clientToServer = m_ClientDriver.Connect(m_ServerDriver.LocalEndPoint()); Assert.AreNotEqual(default(NetworkConnection), clientToServer); m_ClientDriver.ScheduleUpdate().Complete(); // Handle incoming connection from client m_ServerDriver.ScheduleUpdate().Complete(); var serverToClient = m_ServerDriver.Accept(); Assert.AreNotEqual(default(NetworkConnection), serverToClient); // Send message to client if (m_ServerDriver.BeginSend(serverPipe, serverToClient, out var strm) == 0) { strm.WriteInt(42); m_ServerDriver.EndSend(strm); } m_ServerDriver.ScheduleUpdate().Complete(); // Receive incoming message from server m_ClientDriver.ScheduleUpdate().Complete(); DataStreamReader readStrm; Assert.AreEqual(NetworkEvent.Type.Connect, clientToServer.PopEvent(m_ClientDriver, out readStrm)); Assert.AreEqual(NetworkEvent.Type.Data, clientToServer.PopEvent(m_ClientDriver, out readStrm)); Assert.AreEqual(sizeof(int), readStrm.Length); Assert.AreEqual(42, readStrm.ReadInt()); }
public SoakClient(double sendInterval, int packetSize, int duration) { DriverHandle = NetworkDriver.Create( new SimulatorUtility.Parameters { MaxPacketSize = packetSize, MaxPacketCount = 30, PacketDelayMs = 25, PacketDropPercentage = 10 /*PacketDropInterval = 100*/ }, new ReliableUtility.Parameters { WindowSize = 32 }); //Pipeline = DriverHandle.CreatePipeline(typeof(UnreliableSequencedPipelineStage), typeof(SimulatorPipelineStage)); Pipeline = DriverHandle.CreatePipeline(typeof(ReliableSequencedPipelineStage), typeof(SimulatorPipelineStage)); ReliableStageId = NetworkPipelineStageCollection.GetStageId(typeof(ReliableSequencedPipelineStage)); SimulatorStageId = NetworkPipelineStageCollection.GetStageId(typeof(SimulatorPipelineStage)); if (packetSize > NetworkParameterConstants.MTU) { Debug.LogWarning("Truncating packet size to MTU"); packetSize = NetworkParameterConstants.MTU; } else if (packetSize < SoakMessage.HeaderLength) { Debug.LogWarning("Packet size was to small resizing to at least SoakMessage HeaderSize"); packetSize = SoakMessage.HeaderLength; } var payloadSize = packetSize - SoakMessage.HeaderLength; PendingSoakMessages = new NativeArray <SoakMessage>(64, Allocator.Persistent); ConnectionHandle = new NativeArray <NetworkConnection>(1, Allocator.Persistent); SoakJobDataPacket = new NativeArray <byte>(payloadSize, Allocator.Persistent); var random = new byte[payloadSize]; Random r = new Random(); r.NextBytes(random); SoakJobDataPacket.CopyFrom(random); SoakJobContextsHandle = new NativeArray <SoakJobContext>(1, Allocator.Persistent); var context = new SoakJobContext { Duration = duration, PacketSize = packetSize, SendInterval = sendInterval }; SoakJobContextsHandle[0] = context; SoakStatisticsHandle = new NativeArray <SoakStatisticsPoint>(2, Allocator.Persistent); SoakStatisticsHandle[0] = new SoakStatisticsPoint(); SoakStatisticsHandle[1] = new SoakStatisticsPoint(); }
void Start() { // Driver can be used as normal m_Driver = NetworkDriver.Create(); // Driver now knows about this pipeline and can explicitly be asked to send packets through it (by default it sends directly) m_Pipeline = m_Driver.CreatePipeline(typeof(UnreliableSequencedPipelineStage)); m_ConnectionIds = new NativeList <int>(1, Allocator.Persistent); m_ConnectionIds.Add(0); m_Connection = new NativeArray <NetworkConnection>(1, Allocator.Persistent); m_Done = new NativeArray <byte>(1, Allocator.Persistent); this.enabled = false; }
public void CreateClientDriver(World world, out NetworkDriver driver, out NetworkPipeline unreliablePipeline, out NetworkPipeline reliablePipeline) { var reliabilityParams = new ReliableUtility.Parameters { WindowSize = 32 }; #if UNITY_EDITOR || DEVELOPMENT_BUILD var netParams = new NetworkConfigParameter { maxConnectAttempts = NetworkParameterConstants.MaxConnectAttempts, connectTimeoutMS = NetworkParameterConstants.ConnectTimeoutMS, disconnectTimeoutMS = NetworkParameterConstants.DisconnectTimeoutMS, maxFrameTimeMS = 100 }; var simulatorParams = ClientSimulatorParameters; driver = NetworkDriver.Create(netParams, simulatorParams, reliabilityParams); #else driver = NetworkDriver.Create(reliabilityParams); #endif #if UNITY_EDITOR || DEVELOPMENT_BUILD if (simulatorParams.PacketDelayMs > 0 || simulatorParams.PacketDropInterval > 0) { unreliablePipeline = driver.CreatePipeline(typeof(SimulatorPipelineStage), typeof(SimulatorPipelineStageInSend)); reliablePipeline = driver.CreatePipeline(typeof(SimulatorPipelineStageInSend), typeof(ReliableSequencedPipelineStage), typeof(SimulatorPipelineStage)); } else #endif { unreliablePipeline = driver.CreatePipeline(typeof(NullPipelineStage)); reliablePipeline = driver.CreatePipeline(typeof(ReliableSequencedPipelineStage)); } }
void Start() { // Driver can be used as normal m_Driver = NetworkDriver.Create(); // Driver now knows about this pipeline and can explicitly be asked to send packets through it (by default it sends directly) m_Pipeline = m_Driver.CreatePipeline(typeof(UnreliableSequencedPipelineStage)); m_ConnectionIds = new NativeList <int>(1, Allocator.Persistent); m_ConnectionIds.Add(0); m_Connection = new NativeArray <NetworkConnection>(1, Allocator.Persistent); m_Done = new NativeArray <byte>(1, Allocator.Persistent); var endpoint = NetworkEndPoint.Parse("127.0.0.1", 9000); // Relay server IP&Port m_Connection[0] = m_Driver.Connect(endpoint); }
public void Start() { m_Connections = new NativeList <SoakClientCtx>(1, Allocator.Persistent); m_ServerDriver = NetworkDriver.Create(new ReliableUtility.Parameters { WindowSize = 32 }); //m_Pipeline = m_ServerDriver.CreatePipeline(typeof(UnreliableSequencedPipelineStage)); m_Pipeline = m_ServerDriver.CreatePipeline(typeof(ReliableSequencedPipelineStage)); m_ReliableStageId = NetworkPipelineStageCollection.GetStageId(typeof(ReliableSequencedPipelineStage)); var addr = NetworkEndPoint.AnyIpv4; addr.Port = 9000; if (m_ServerDriver.Bind(addr) != 0) { Debug.Log("Failed to bind to port 9000"); } else { m_ServerDriver.Listen(); } }
public void NetworkPipeline_CanStorePacketsForLaterDeliveryInReceiveLastStage() { var clientPipe1 = m_ClientDriver.CreatePipeline(typeof(TestEncryptPipelineStage), typeof(SimulatorPipelineStage)); var clientPipe2 = m_ClientDriver2.CreatePipeline(typeof(TestEncryptPipelineStage), typeof(SimulatorPipelineStage)); var serverPipe = m_ServerDriver.CreatePipeline(typeof(TestEncryptPipelineStage), typeof(SimulatorPipelineStage)); Assert.AreEqual(clientPipe1, serverPipe); Assert.AreEqual(clientPipe2, serverPipe); TestPipeline(30, serverPipe); }
public void NetworkPipeline_CreatePipelineIsSymetrical() { var clientPipe = m_ClientDriver.CreatePipeline(typeof(TestPipelineStageWithHeader)); var serverPipe = m_ServerDriver.CreatePipeline(typeof(TestPipelineStageWithHeader)); Assert.AreEqual(clientPipe, serverPipe); }