Beispiel #1
0
        public P2PBackrollSession(BackrollSessionConfig config)
        {
            _next_recommended_sleep = 0;

            _next_spectator_frame = 0;

            _callbacks = config.Callbacks;

            _players    = InitializeConnections(config.Players);
            _spectators = InitializeConnections(config.Spectators);

            SetupConnections(_players);
            SetupConnections(_spectators);

            _localConnectStatus = new BackrollConnectStatus[PlayerCount];
            for (int i = 0; i < _localConnectStatus.Length; i++)
            {
                _localConnectStatus[i].LastFrame = -1;
            }

            // Initialize the synchronziation layer
            _sync = new Sync(_localConnectStatus, new Sync.Config {
                NumPlayers          = config.Players.Length,
                InputSize           = InputSize,
                Callbacks           = _callbacks,
                NumPredictionFrames = BackrollConstants.kMaxPredictionFrames,
            });
        }
        public SyncTestsBackrollSession(BackrollSessionConfig config, int frames)
        {
            _callbacks      = config.Callbacks;
            _num_players    = config.Players.Length;
            _check_distance = frames;
            _last_verified  = 0;
            _rollingback    = false;
            _running        = false;
            _current_input.Clear();

            _sync = new Sync(null, new Sync.Config {
                NumPredictionFrames = BackrollConstants.kMaxPredictionFrames
            });
        }
 // Starts a new Backroll session.
 //
 // num_players - The number of players which will be in this game.  The number
 // of players per session is fixed.  If you need to change the number of
 // players or any player disconnects, you must start a new session.
 //
 // local_port - The port Backroll should bind to for UDP traffic.
 public static BackrollSession <T> StartSession <T>(BackrollSessionConfig config) where T : struct
 {
     return(new P2PBackrollSession <T>(config));
 }
        // Used to being a new Backroll.net sync test session.  During a sync test, every
        // frame of execution is run twice: once in prediction mode and once again to
        // verify the result of the prediction.  If the checksums of your save states
        // do not match, the test is aborted.
        //
        // cb - A BackrollSessionCallbacks structure which contains the callbacks you implement
        // to help Backroll.net synchronize the two games.  You must implement all functions in
        // cb, even if they do nothing but 'return true';
        //
        // game - The name of the game.  This is used internally for Backroll for logging purposes only.
        //
        // num_players - The number of players which will be in this game.  The number of players
        // per session is fixed.  If you need to change the number of players or any player
        // disconnects, you must start a new session.
        //
        // frames - The number of frames to run before verifying the prediction.  The
        // recommended value is 1.

        public static BackrollSession <T> StartSyncTest <T>(BackrollSessionConfig config, int frames) where T : struct
        {
            return(new SyncTestsBackrollSession <T>(config, frames));
        }
        public SpectatorBackrollSession(BackrollSessionConfig config)
        {
            _callbacks          = config.Callbacks;
            _num_players        = config.Players.Length;
            _next_input_to_send = 0;
            _synchronizing      = true;

            _inputs = new GameInput[kFrameBufferSize];
            for (var i = 0; i < _inputs.Length; i++)
            {
                _inputs[i].Frame = -1;
            }

            _host = new BackrollConnection(config.Players[0], 0, null);
            _host.Synchronize();

            _host.OnConnected += () => {
                _callbacks.OnConnected?.Invoke(new ConnectedEvent {
                    Player = new BackrollPlayerHandle {
                        Id = 0
                    }
                });
            };

            _host.OnSynchronizing += (count, total) => {
                _callbacks.OnPlayerSynchronizing?.Invoke(new PlayerSynchronizingEvent {
                    Player = new BackrollPlayerHandle {
                        Id = 0
                    },
                    Count = count,
                    Total = total
                });
            };

            _host.OnSynchronized += () => {
                if (!_synchronizing)
                {
                    return;
                }
                _callbacks.OnPlayerSynchronized?.Invoke(new PlayerSynchronizedEvent {
                    Player = new BackrollPlayerHandle {
                        Id = 0
                    },
                });
                _callbacks.OnReady?.Invoke();
                _synchronizing = false;
            };

            _host.OnNetworkInterrupted += (disconnectTimeout) => {
                _callbacks.OnConnectionInterrupted?.Invoke(new ConnectionInterruptedEvent {
                    Player = new BackrollPlayerHandle {
                        Id = 0
                    },
                    DisconnectTimeout = disconnectTimeout
                });
            };

            _host.OnNetworkResumed += () => {
                _callbacks.OnConnectionResumed?.Invoke(new ConnectionResumedEvent {
                    Player = new BackrollPlayerHandle {
                        Id = 0
                    },
                });
            };

            _host.OnDisconnected += () => {
                _callbacks.OnDisconnected?.Invoke(new DisconnectedEvent {
                    Player = new BackrollPlayerHandle {
                        Id = 0
                    },
                });
            };

            _host.OnInput += (input) => {
                _host.SetLocalFrameNumber(input.Frame);
                _host.SendInputAck();
                _inputs[input.Frame % _inputs.Length] = input;
            };
        }