public void McpePlayStatus_parallel_encode_perf_test() { byte[] result = new byte[0]; void Body(int i) { var input = new McpeMovePlayer(); result = input.Encode(); } long totalAllocatedBytes = GC.GetTotalMemory(true); var watch = Stopwatch.StartNew(); Parallel.For(0, _loops, i => { var input = new McpeMovePlayer(); result = input.Encode(); }); if (result.Length < 0) { return; } long watchElapsedMilliseconds = watch.ElapsedMilliseconds; Console.WriteLine($"Time: {watchElapsedMilliseconds:N}ms, mem: {(GC.GetTotalMemory(true) - totalAllocatedBytes) / 1024f:N}KB"); }
public void SendMcpeMovePlayer() { //Log.Debug($"Sending move {EntityId}: {CurrentLocation}"); McpeMovePlayer movePlayerPacket = McpeMovePlayer.CreateObject(); movePlayerPacket.runtimeEntityId = EntityId; movePlayerPacket.x = CurrentLocation.X; movePlayerPacket.y = CurrentLocation.Y; movePlayerPacket.z = CurrentLocation.Z; movePlayerPacket.yaw = 91; movePlayerPacket.pitch = 28; movePlayerPacket.headYaw = 91; _toNodeHandler.WriteBytes(movePlayerPacket.Encode()); movePlayerPacket.PutPool(); }
public void McpePlayStatus_encode_perf_test() { long totalAllocatedBytes = GC.GetTotalMemory(true); var watch = Stopwatch.StartNew(); byte[] result = new byte[0]; for (int i = 0; i < _loops; i++) { var input = new McpeMovePlayer(); result = input.Encode(); input.Reset(); } if (result.Length < 0) { return; } long watchElapsedMilliseconds = watch.ElapsedMilliseconds; Console.WriteLine($"Time: {watchElapsedMilliseconds:N}ms, mem: {(GC.GetTotalMemory(true) - totalAllocatedBytes) / 1024f:N}KB"); }
protected virtual void BroadCastMovement(Player[] players, Entity[] entities) { DateTime now = DateTime.UtcNow; if (players.Length == 0) { return; } if (players.Length <= 1 && entities.Length == 0) { return; } if (now - _lastBroadcast < TimeSpan.FromMilliseconds(50)) { return; } DateTime tickTime = _lastSendTime; _lastSendTime = DateTime.UtcNow; using (MemoryStream stream = MiNetServer.MemoryStreamManager.GetStream()) { int playerMoveCount = 0; int entiyMoveCount = 0; foreach (var player in players) { if (now - player.LastUpdatedTime <= now - tickTime) { PlayerLocation knownPosition = player.KnownPosition; McpeMovePlayer move = McpeMovePlayer.CreateObject(); move.entityId = player.EntityId; move.x = knownPosition.X; move.y = knownPosition.Y + 1.62f; move.z = knownPosition.Z; move.yaw = knownPosition.Yaw; move.pitch = knownPosition.Pitch; move.headYaw = knownPosition.HeadYaw; move.mode = 0; byte[] bytes = move.Encode(); BatchUtils.WriteLength(stream, bytes.Length); stream.Write(bytes, 0, bytes.Length); move.PutPool(); playerMoveCount++; } } foreach (var entity in entities) { if (now - entity.LastUpdatedTime <= now - tickTime) { { McpeMoveEntity moveEntity = McpeMoveEntity.CreateObject(); moveEntity.entityId = entity.EntityId; moveEntity.position = (PlayerLocation)entity.KnownPosition.Clone(); moveEntity.position.Y += entity.PositionOffset; byte[] bytes = moveEntity.Encode(); BatchUtils.WriteLength(stream, bytes.Length); stream.Write(bytes, 0, bytes.Length); moveEntity.PutPool(); } { McpeSetEntityMotion entityMotion = McpeSetEntityMotion.CreateObject(); entityMotion.entityId = entity.EntityId; entityMotion.velocity = entity.Velocity; byte[] bytes = entityMotion.Encode(); BatchUtils.WriteLength(stream, bytes.Length); stream.Write(bytes, 0, bytes.Length); entityMotion.PutPool(); } entiyMoveCount++; } } if (playerMoveCount == 0 && entiyMoveCount == 0) { return; } if (players.Length == 1 && entiyMoveCount == 0) { return; } McpeBatch batch = BatchUtils.CreateBatchPacket(stream.GetBuffer(), 0, (int)stream.Length, CompressionLevel.Optimal, false); batch.AddReferences(players.Length - 1); batch.Encode(); //batch.ValidUntil = now + TimeSpan.FromMilliseconds(50); foreach (var player in players) { MiNetServer.FastThreadPool.QueueUserWorkItem(() => player.SendPackage(batch)); } _lastBroadcast = DateTime.UtcNow; } }
protected virtual void BroadCastMovement(Player[] players, Entity[] entities) { if (players.Length == 0) { return; } DateTime tickTime = _lastSendTime; _lastSendTime = DateTime.UtcNow; DateTime now = DateTime.UtcNow; MemoryStream stream = MiNetServer.MemoryStreamManager.GetStream(); int count = 0; McpeMovePlayer move = McpeMovePlayer.CreateObject(); foreach (var player in players) { if (((now - player.LastUpdatedTime) <= now - tickTime)) { PlayerLocation knownPosition = player.KnownPosition; move.entityId = player.EntityId; move.x = knownPosition.X; move.y = knownPosition.Y + 1.62f; move.z = knownPosition.Z; move.yaw = knownPosition.Yaw; move.pitch = knownPosition.Pitch; move.headYaw = knownPosition.HeadYaw; move.mode = 0; byte[] bytes = move.Encode(); stream.Write(BitConverter.GetBytes(Endian.SwapInt32(bytes.Length)), 0, 4); stream.Write(bytes, 0, bytes.Length); move.Reset(); count++; } } move.PutPool(); McpeMoveEntity moveEntity = McpeMoveEntity.CreateObject(); moveEntity.entities = new EntityLocations(); McpeSetEntityMotion entityMotion = McpeSetEntityMotion.CreateObject(); entityMotion.entities = new EntityMotions(); foreach (var entity in entities) { if (((now - entity.LastUpdatedTime) <= now - tickTime)) { moveEntity.entities.Add(entity.EntityId, entity.KnownPosition); entityMotion.entities.Add(entity.EntityId, entity.Velocity); count++; } } if (moveEntity.entities.Count > 0) { byte[] bytes = moveEntity.Encode(); stream.Write(BitConverter.GetBytes(Endian.SwapInt32(bytes.Length)), 0, 4); stream.Write(bytes, 0, bytes.Length); } moveEntity.PutPool(); if (moveEntity.entities.Count > 0) { byte[] bytes = entityMotion.Encode(); stream.Write(BitConverter.GetBytes(Endian.SwapInt32(bytes.Length)), 0, 4); stream.Write(bytes, 0, bytes.Length); } entityMotion.PutPool(); if (count == 0) { return; } McpeBatch batch = McpeBatch.CreateObject(players.Length); byte[] buffer = Player.CompressBytes(stream.ToArray(), CompressionLevel.Optimal); batch.payloadSize = buffer.Length; batch.payload = buffer; batch.Encode(true); foreach (var player in players) { Task sendTask = new Task(obj => ((Player)obj).SendMoveList(batch, now), player); sendTask.Start(); } }
protected virtual void BroadCastMovement(Player[] players, Player[] updatedPlayers) { if (updatedPlayers.Length == 0) { return; } List <McpeMovePlayer> moves = new List <McpeMovePlayer>(); foreach (var player in updatedPlayers) { Player updatedPlayer = player; var knownPosition = updatedPlayer.KnownPosition; { McpeMovePlayer move = McpeMovePlayer.CreateObject(players.Length); move.entityId = updatedPlayer.EntityId; move.x = knownPosition.X; move.y = knownPosition.Y + 1.62f; move.z = knownPosition.Z; move.yaw = knownPosition.Yaw; move.pitch = knownPosition.Pitch; move.headYaw = knownPosition.HeadYaw; move.teleport = 0; move.Encode(); // Optmized moves.Add(move); } } foreach (var p in players) { Player player = p; Task sendTask = new Task(delegate { player.SendMoveList(moves); }); sendTask.Start(); } //MemoryStream stream = new MemoryStream(); //int messageCount = 0; //foreach (var movePlayer in moves) //{ // { // messageCount++; // byte[] bytes = movePlayer.Encode(); // stream.Write(bytes, 0, bytes.Length); // } // movePlayer.PutPool(); //} //if (messageCount > 0) //{ // McpeBatch batch = McpeBatch.CreateObject(players.Length); // byte[] buffer = CompressBytes(stream.ToArray()); // batch.payloadSize = buffer.Length; // batch.payload = buffer; // batch.Encode(); // foreach (var p in players) // { // Player player = p; // Task sendTask = new Task(delegate { player.SendPackage(batch, true); }); // sendTask.Start(); // } //} }