Exemplo n.º 1
0
            /// <summary>
            /// Creates instance of Active client state.
            /// </summary>
            /// <param name="gameClient"></param>
            /// <param name="snapshotId"></param>
            /// <param name="initialSnapshot"></param>
            public Active(ClientContext context, uint snapshotId, byte[] initialSnapshot, long svTicks) : base(context.GameClient, ClientState.Active)
            {
                this.context = context;

                queue = new SnapshotQueue(32);
                queue.Push(new Snapshot(snapshotId, initialSnapshot));

                lastServerTicks   = svTicks;
                lastSnapshotID    = snapshotId;
                lastSnapshotFrame = snapshotId;

                Message = "";


                                #if USE_DEJITTER
                jitter = new JitterBuffer(gameClient.Game, svTicks);
                                #endif


                stopwatch = new Stopwatch();
                stopwatch.Start();
                clientTicks = stopwatch.Elapsed.Ticks;


                context.Instance.FeedSnapshot(new GameTime(0, svTicks, 0L), initialSnapshot, 0);
            }
        /// <summary>
        /// Starts and returns a task that reads environmental data from Kafka.
        /// </summary>
        /// <returns>Returns the new task.</returns>
        private Task StartConsuming()
        {
            return(new TaskFactory().StartNew(() =>
            {
                using (IConsumer <Ignore, string> consumer = new ConsumerBuilder <Ignore, string>(ConsumerConfig).Build())
                {
                    Logger.LogInformation($" + Starting to read environment data from topic `{Topic}`.");
                    consumer.Subscribe(Topic);

                    CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
                    Console.CancelKeyPress += (_, @event) => {
                        @event.Cancel = true;
                        cancellationTokenSource.Cancel();
                    };

                    try
                    {
                        while (true)
                        {
                            try
                            {
                                ConsumeResult <Ignore, string> result = consumer.Consume(cancellationTokenSource.Token);
                                EnvironmentSnapshot snapshot = JsonConvert.DeserializeObject <EnvironmentSnapshot>(result.Message.Value);
                                snapshot.StorageSiteId = StorageSiteId;
                                SnapshotQueue.Enqueue(snapshot);
                                Logger.LogDebug($"Consumed message '{result.Message.Value}' at: '{result.TopicPartitionOffset}'.");
                            }
                            catch (ConsumeException exception)
                            {
                                Logger.LogError($"Error occured: {exception.Error.Reason}");
                            }
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        consumer.Close();
                    }
                    finally
                    {
                        Logger.LogInformation($"Kafka consumer for topic `{Topic}` shut down.");
                    }
                }
            }, TaskCreationOptions.LongRunning));
        }