public override void _PhysicsProcess(float delta) { if (network != default && world != default) { network.PollEvents(); world.Update(); UpdateBandwidth(); // if Input.GetKey... // NetConfig.LatencySimulation = true; void UpdateBandwidth() { bandwidthWindow[framesActive % BANDWIDTH_WINDOW_SIZE] = receivedThisFrame; framesActive++; var sum = 0; foreach (var bytes in bandwidthWindow) { sum += bytes; } var average = sum / (float)BANDWIDTH_WINDOW_SIZE; var kbps = average / delta / 1024.0f; //logger.LogInformation($"framesActive: {framesActive} receivedThisFrame: {receivedThisFrame} KBps: {kbps}"); receivedThisFrame = 0; } } }
public async Task RunAsync() { await Task.CompletedTask; var host = new ServerWorldHost(); var world = host.ServiceProvider.GetRequiredService <ServerWorld>(); var network = new NetCore("NetDemo1.0", true); // Responsible for interpreting events from the socket and communicating them to the server. network.PeerConnected += (peer, token) => { var wrapper = new MiniUDPConnection(peer); peer.UserData = wrapper; world.AddConnection(wrapper); }; network.PeerClosed += (peer, reason, userKickReason, error) => { var wrapper = (MiniUDPConnection)peer.UserData; world.RemovePeer(wrapper); }; var clock = new Clock(0.02f); clock.OnFixedUpdate += () => { network.PollEvents(); world.Update(); var evnt = (GameActionEvent)world._pools.CreateEvent(typeof(GameActionEvent)); evnt.Key = (int)(clock.Time); world.QueueEventBroadcast(evnt); }; var loggerFactory = host.ServiceProvider.GetService <ILoggerFactory>(); var logger = loggerFactory.CreateLogger <Program>(); logger.LogInformation("Server Starting..."); network.Host(44325); logger.LogInformation("Server Started."); clock.Start(); while (true) { clock.Tick(); } network.Stop(); logger.LogInformation("Server Stopped."); }