//internal void OnDisconnect(Func<Exception, Task> onDisconnect) //{ //} //IAsyncEnumerable does not really make sense here public void ApplyInputs(Dictionary <Guid, PlayerInputs> inputs) { gameState.Handle(gameStateTracker.UpdateGameState()); PlayerInputApplyer.Apply(gameState, inputs); //foreach (var (p1, p2) in PhysicsEngine2.PlayerPairs(gameState)) //{ // PhysicsMath2.TryPushBallBall(p1, p2); //} gameState.Simulate(gameStateTracker); // clear out effects after a few frames var nextCollisions = new ConcurrentLinkedList <GameState.Collision>(); foreach (var item in gameState.collisions.Where(x => x.Frame + 3 > gameState.Frame)) { nextCollisions.Add(item); } gameState.collisions = nextCollisions; var nextGoalsScored = new ConcurrentLinkedList <GameState.GoalScored>(); foreach (var item in gameState.GoalsScored.Where(x => x.Frame + 3 > gameState.Frame)) { nextGoalsScored.Add(item); } gameState.GoalsScored = nextGoalsScored; }
public void AddRemoveAndCount() { for (int i = 0; i < 10000; i++) { var random = new Random(); var removed = 0; var subject = new ConcurrentLinkedList <int>(); var toAdd = random.Next(0, 1000); var toRemove = random.Next(0, 1000); var actions = new List <Action>(); for (int j = 0; j < toAdd; j++) { actions.Add(() => subject.Add(0)); } for (int j = 0; j < toRemove; j++) { actions.Add(() => { if (subject.RemoveStart()) { Interlocked.Increment(ref removed); } }); } Parallel.Invoke(actions.OrderBy(x => random.NextDouble()).ToArray()); Assert.Equal(toAdd - removed, subject.Count); Assert.Equal(toAdd - removed, subject.Count()); } }
public static void Handle(this GameState gameState, GameStateUpdate gameStateUpdate) { gameState.Frame = gameStateUpdate.Frame; gameState.GameBall = gameStateUpdate.Ball; var nextCollisions = new ConcurrentLinkedList <Collision>(); foreach (var item in gameStateUpdate.Collisions) { nextCollisions.Add(item); } gameState.collisions = nextCollisions; gameState.CountDownState = gameStateUpdate.CountDownState; var nextGoalsScores = new ConcurrentLinkedList <GoalScored>(); foreach (var item in gameStateUpdate.GoalsScored) { nextGoalsScores.Add(item); } gameState.GoalsScored = nextGoalsScores; var nextPlayers = new RawConcurrentIndexed <Guid, Player>(); foreach (var item in gameStateUpdate.Players) { nextPlayers.TryAdd(item.Id, item); } gameState.players = nextPlayers; gameState.RightScore = gameStateUpdate.RightScore; gameState.LeftScore = gameStateUpdate.LeftScore; }
public ChunkedArray(int chunkSize) { Contract.Requires(chunkSize > 0); this.chunkSize = chunkSize; chunkList.Add(new T[chunkSize]); }
internal void SendTransfer(Delivery delivery) { delivery.DeliveryId = nextOutgoingId++; var transfer = new Transfer() { Handle = delivery.Link.LocalHandle, DeliveryId = delivery.DeliveryId, DeliveryTag = delivery.DeliveryTag, MessageFormat = 0, Settled = delivery.Settled, More = false, }; if (!delivery.Settled) { outgoingUnsettledMap.Add(delivery); } while (delivery.PayloadBuffer.LengthAvailableToRead > 0) { var buffer = new ByteBuffer((int)Connection.MaxFrameSize, false); var bufferStartOffset = buffer.WriteOffset; transfer.More = false; AmqpCodec.EncodeFrame(buffer, transfer, ChannelNumber); // encode to get space available for payload int frameSize = buffer.LengthAvailableToRead; int payloadBufferSpaceAvailable = (int)Connection.MaxFrameSize - frameSize; int payloadSize = delivery.PayloadBuffer.LengthAvailableToRead; // payload is too big, need to split into multiple transfers if (payloadSize > payloadBufferSpaceAvailable) { transfer.More = true; // payloadBufferSpaceAvailable should not change after encoding again buffer.ResetReadWrite(); AmqpCodec.EncodeFrame(buffer, transfer, ChannelNumber); // re-encode with correct value. TODO: is there a way to estimate instead of encoding, testing, and reencoding? frameSize = buffer.LengthAvailableToRead; payloadSize = payloadBufferSpaceAvailable; // max size } // copy payload to buffer to write AmqpBitConverter.WriteBytes(buffer, delivery.PayloadBuffer.Buffer, delivery.PayloadBuffer.ReadOffset, payloadSize); delivery.PayloadBuffer.CompleteRead(payloadSize); // rewrite frame size AmqpBitConverter.WriteInt(buffer.Buffer, bufferStartOffset, frameSize + payloadSize); if (Trace.IsDebugEnabled) { trace.Debug("SEND CH({0}) {1} Payload {2} Bytes", ChannelNumber.ToString(), transfer.ToString(), payloadSize.ToString()); } Connection.SendBuffer(buffer); // following fields may be null on subsequent transfers transfer.DeliveryId = null; transfer.DeliveryTag = null; transfer.MessageFormat = null; transfer.Settled = null; } }
internal void NotifyUnsettledIncomingDelivery(AmqpLink link, Delivery delivery) { incomingUnsettledMap.Add(delivery); }