private void SetupServerAndGamelift()
    {
        // start the unet server

        //todo commented by dan
        //networkPort = LISTEN_PORT;
        //StartServer();
        //print($"Server listening on port {networkPort}");

        // initialize GameLift
        print("Starting GameLift initialization.");
        var initSDKOutcome = GameLiftServerAPI.InitSDK();

        if (initSDKOutcome.Success)
        {
            isGameliftServer = true;
            var processParams = new ProcessParameters(
                (gameSession) =>
            {
                // onStartGameSession callback
                GameLiftServerAPI.ActivateGameSession();
                // quit if no player joined within two minutes
                timer.Elapsed  += this.CheckPlayersJoined;
                timer.AutoReset = false;
                timer.Start();
            },
                (updateGameSession) =>
            {
            },
                () =>
            {
                // onProcessTerminate callback
                TerminateSession();
            },
                () =>
            {
                // healthCheck callback
                return(true);
            },
                LISTEN_PORT,
                new LogParameters(new List <string>()
            {
                "/local/game/logs/myserver.log"
            })
                );
            var processReadyOutcome = GameLiftServerAPI.ProcessReady(processParams);
            if (processReadyOutcome.Success)
            {
                print("GameLift process ready.");
            }
            else
            {
                print($"GameLift: Process ready failure - {processReadyOutcome.Error.ToString()}.");
            }
        }
        else
        {
            print($"GameLift: InitSDK failure - {initSDKOutcome.Error.ToString()}.");
        }
    }
    //This is an example of a simple integration with GameLift server SDK that makes game server
    //processes go active on Amazon GameLift
    public void Start()
    {
        //InitSDK establishes a local connection with the Amazon GameLift agent to enable
        //further communication.
        var initSDKOutcome = GameLiftServerAPI.InitSDK();

        if (initSDKOutcome.Success)
        {
            ProcessParameters processParameters = new ProcessParameters(
                (gameSession) => {
                //Respond to new game session activation request. GameLift sends activation request
                //to the game server along with a game session object containing game properties
                //and other settings. Once the game server is ready to receive player connections,
                //invoke GameLiftServerAPI.ActivateGameSession()
                GameLiftServerAPI.ActivateGameSession();
            },
                () => {
                //OnProcessTerminate callback. GameLift invokes this callback before shutting down
                //an instance hosting this game server. It gives this game server a chance to save
                //its state, communicate with services, etc., before being shut down.
                //In this case, we simply tell GameLift we are indeed going to shut down.
                GameLiftServerAPI.ProcessEnding();
            },
                () => {
                //This is the HealthCheck callback.
                //GameLift invokes this callback every 60 seconds or so.
                //Here, a game server might want to check the health of dependencies and such.
                //Simply return true if healthy, false otherwise.
                //The game server has 60 seconds to respond with its health status.
                //GameLift will default to 'false' if the game server doesn't respond in time.
                //In this case, we're always healthy!
                return(true);
            },
                //Here, the game server tells GameLift what port it is listening on for incoming player
                //connections. In this example, the port is hardcoded for simplicity. Active game
                //that are on the same instance must have unique ports.
                listeningPort,
                new LogParameters(new List <string>()
            {
                //Here, the game server tells GameLift what set of files to upload when the game session ends.
                //GameLift uploads everything specified here for the developers to fetch later.
                "/local/game/logs/myserver.log"
            }));

            //Calling ProcessReady tells GameLift this game server is ready to receive incoming game sessions!
            var processReadyOutcome = GameLiftServerAPI.ProcessReady(processParameters);
            if (processReadyOutcome.Success)
            {
                print("ProcessReady success.");
            }
            else
            {
                print("ProcessReady failure : " + processReadyOutcome.Error.ToString());
            }
        }
        else
        {
            print("InitSDK failure : " + initSDKOutcome.Error.ToString());
        }
    }
        private void ProcessReady()
        {
            //Set the port that your game service is listening on for incoming player connections
            //In this example, the port is hardcoded for simplicity. Active games
            //that are on the same instance must have unique ports.
            const int listeningPort = 7777; // TODO: port picking logic?

            //Here, the game server tells GameLift what set of files to upload when the game session ends.
            //GameLift uploads everything specified here for the developers to fetch later.
            // must be different for each server if multiple servers on instance
            var logParameters = new LogParameters(new List <string> {
                "/local/game/myserver.log"
            });

            var processParameters = new ProcessParameters(
                OnStartGameSession,
                OnProcessTerminate,
                OnHealthCheckDelegate,
                listeningPort,
                logParameters);

            //Notifies the GameLift service that the server process is ready to host game sessions.
            //Call this method after successfully invoking InitSDK() and completing setup tasks that are required
            //before the server process can host a game session. This method should be called only once per process.
            //Calling ProcessReady tells GameLift this game server is ready to receive incoming game sessions!
            var processReadyOutcome = GameLiftServerAPI.ProcessReady(processParameters);

            Debug.Log(processReadyOutcome.Success
        ? "ProcessReady success."
        : $"ProcessReady failure : {processReadyOutcome.Error}");
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            Console.WriteLine("Starting the server at port: " + PORT);
            GenericOutcome outcome = GameLiftServerAPI.InitSDK();

            if (outcome.Success)
            {
                /*
                 * short[] messageTypes = {
                 *  MsgType.Connect, MsgType.Disconnect, Messages.ACCEPT_PLAYER_SESSION, Messages.START_LOCAL_GAME,
                 *  Messages.START_GAME, Messages.SUBMIT_COMMANDS, Messages.END_GAME,
                 * };
                 * Util.ToList(messageTypes).ForEach(messageType => NetworkServer.RegisterHandler(messageType, GetHandler(messageType)));
                 */
                UdpClient udpClient = new UdpClient(PORT);
                udpClient.BeginReceive(DataReceived, udpClient);
                LogParameters paths = new LogParameters();
                paths.LogPaths.Add("C:\\Game\\logs");
                GameLiftServerAPI.ProcessReady(new ProcessParameters(
                                                   OnGameSession,
                                                   OnProcessTerminate,
                                                   OnHealthCheck,
                                                   PORT,
                                                   paths
                                                   ));
                Console.WriteLine("Listening on: " + PORT);
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine(outcome);
            }
        }
        private void InitializeServerProcess(int listeningPort)
        {
            //InitSDK will establish a local connection with GameLift's agent to enable further communication.
            var initSDKOutcome = GameLiftServerAPI.InitSDK();

            if (initSDKOutcome.Success)
            {
                ProcessParameters processParameters = new ProcessParameters(
                    (gameSession) => {
                    //When a game session is created, GameLift sends an activation request to the game server and passes along the game session object containing game properties and other settings.
                    //Here is where a game server should take action based on the game session object.
                    //Once the game server is ready to receive incoming player connections, it should invoke GameLiftServerAPI.ActivateGameSession()
                    GameLiftServerAPI.ActivateGameSession();
                },
                    (updateGameSession) => {
                    //When a game session is updated (e.g. by FlexMatch backfill), GameLiftsends a request to the game
                    //server containing the updated game session object.  The game server can then examine the provided
                    //matchmakerData and handle new incoming players appropriately.
                    //updateReason is the reason this update is being supplied.
                },
                    () => {
                    //OnProcessTerminate callback. GameLift will invoke this callback before shutting down an instance hosting this game server.
                    //It gives this game server a chance to save its state, communicate with services, etc., before being shut down.
                    //In this case, we simply tell GameLift we are indeed going to shutdown.
                    GameLiftServerAPI.ProcessEnding();
                },
                    () => {
                    //This is the HealthCheck callback.
                    //GameLift will invoke this callback every 60 seconds or so.
                    //Here, a game server might want to check the health of dependencies and such.
                    //Simply return true if healthy, false otherwise.
                    //The game server has 60 seconds to respond with its health status. GameLift will default to 'false' if the game server doesn't respond in time.
                    //In this case, we're always healthy!
                    return(true);
                },
                    listeningPort, //This game server tells GameLift that it will listen on port 7777 for incoming player connections.
                    new LogParameters(new List <string>()
                {
                    //Here, the game server tells GameLift what set of files to upload when the game session ends.
                    //GameLift will upload everything specified here for the developers to fetch later.
                    "/local/game/logs/myserver.log"
                }));

                //Calling ProcessReady tells GameLift this game server is ready to receive incoming game sessions!
                var processReadyOutcome = GameLiftServerAPI.ProcessReady(processParameters);
                if (processReadyOutcome.Success)
                {
                    print("ProcessReady success.");
                }
                else
                {
                    print("ProcessReady failure : " + processReadyOutcome.Error.ToString());
                }
            }
            else
            {
                print("InitSDK failure : " + initSDKOutcome.Error.ToString());
            }
        }
    void Start()
    {
        LogOutcome("init", GameLiftServerAPI.InitSDK());
        var logParams  = new LogParameters(new List <string>());
        var parameters = new ProcessParameters(OnStartGameSession, OnUpdateGameSession, OnProcessTerminated, OnHealthCheck, 1900, logParams);

        LogOutcome("ready", GameLiftServerAPI.ProcessReady(parameters));
    }
Esempio n. 7
0
    public bool GameLiftStart(int listenPort)
    {
        //Debug.Log("GameLift Start with Port:" + listenPort);
        LogModule.WriteToLogFile("[GameLift] GameLift Start with Port:" + listenPort);
        var initSDKOutcome = GameLiftServerAPI.InitSDK();

        if (initSDKOutcome.Success)
        {
            ProcessParameters processParameters = new ProcessParameters(
                (gameSession) =>
            {
                //OnStartGameSession Callback
                LogModule.WriteToLogFile("[GameLift] OnStartGameSession with Parameter=" + gameSession);
                GameLiftServerAPI.ActivateGameSession();
            },
                (gameSession) =>
            {
                //OnUpdateGameSession Callback
                //You can implement custom Match update logics using Backfill Ticket, UpdateReason, GameSession data.
                LogModule.WriteToLogFile("[GameLift] OnUpdateGameSession with Backfill Ticket=" + gameSession.BackfillTicketId + ", UpdateReason=" + gameSession.UpdateReason);
            },
                () =>
            {
                //OnProcessTerminate Callback
                LogModule.WriteToLogFile("[GameLift] ProcessEnding");
                GameLiftServerAPI.ProcessEnding();
            },
                () =>
            {
                //OnHealthCheck Callback
                return(true);
            },
                listenPort,
                new LogParameters(new List <string>()
            {
                "./local/game/logs/myserver.log"
                //"C:\\game\\myserver.log"
            }
                                  ));
            var processReadyOutcome = GameLiftServerAPI.ProcessReady(processParameters);
            if (processReadyOutcome.Success)
            {
                LogModule.WriteToLogFile("[GameLift] ProcessReady Success");
                return(true);
            }
            else
            {
                LogModule.WriteToLogFile("[GameLift] ProcessReady Failure : " + processReadyOutcome.Error.ToString());
                return(false);
            }
        }
        else
        {
            LogModule.WriteToLogFile("[GameLift] InitSDK failure : " + initSDKOutcome.Error.ToString());
            return(false);
        }
    }
Esempio n. 8
0
    // This is an example of a simple integration with GameLift server SDK that will make game server processes go active on GameLift!
    public void Start()
    {
        //InitSDK will establish a local connection with GameLift's agent to enable further communication.
        var initSDKOutcome = GameLiftServerAPI.InitSDK();

        _tcpServerPort = UnityEngine.Random.Range(7000, 8000);
        Debug.Log("TCP Port: " + _tcpServerPort);

        if (initSDKOutcome.Success)
        {
            ProcessParameters processParameters = new ProcessParameters(
                this.OnGameSession,
                this.OnGameSessionUpdate,
                this.OnProcessTerminate,
                this.OnHealthCheck,
                _tcpServerPort, // This game server tells GameLift the port it will listen on for incoming player connections.
                new LogParameters(new List <string>()
            {
                // Here, the game server tells GameLift what set of files to upload when the game session ends.
                // GameLift will upload everything specified here for the developers to fetch later.

                // When -isProd is NOT set, use a path relevant for local testing
                Startup.IsArgFlagPresent("-isProd") ? "/local/game/logs/server.log" : "~/Library/Logs/Unity/server.log"
            }
                                  ));

            // Calling ProcessReady tells GameLift this game server is ready to receive incoming game sessions!
            var processReadyOutcome = GameLiftServerAPI.ProcessReady(processParameters);
            if (processReadyOutcome.Success)
            {
                print("ProcessReady success.");

                _server = GetComponent <BADNetworkServer>();

                if (_server != null)
                {
                    Debug.Log("BADNetworkServer is good.");
                    _server.StartTCPServer(_tcpServerPort);
                }
                else
                {
                    Debug.Log("BADNetworkServer is null.");
                }
            }
            else
            {
                print("ProcessReady failure : " + processReadyOutcome.Error.ToString());
            }
        }
        else
        {
            print("InitSDK failure : " + initSDKOutcome.Error.ToString());
        }
    }
Esempio n. 9
0
    private void SetupServerAndGamelift()
    {
        // start the unet server
        networkPort = LISTEN_PORT;
        StartServer();
        print($"Server listening on port {networkPort}");

        // initialize GameLift
        print("Starting GameLift initialization.");
        var initSDKOutcome = GameLiftServerAPI.InitSDK();

        if (initSDKOutcome.Success)
        {
            isGameliftServer = true;
            var processParams = new ProcessParameters(
                (gameSession) =>
            {
                // onStartGameSession callback
                GameLiftServerAPI.ActivateGameSession();
            },
                (updateGameSession) =>
            {
            },
                () =>
            {
                // onProcessTerminate callback
                GameLiftServerAPI.ProcessEnding();
            },
                () =>
            {
                // healthCheck callback
                return(true);
            },
                LISTEN_PORT,
                new LogParameters(new List <string>()
            {
                "/local/game/logs/myserver.log"
            })
                );
            var processReadyOutcome = GameLiftServerAPI.ProcessReady(processParams);
            if (processReadyOutcome.Success)
            {
                print("GameLift process ready.");
            }
            else
            {
                print($"GameLift: Process ready failure - {processReadyOutcome.Error.ToString()}.");
            }
        }
        else
        {
            print($"GameLift: InitSDK failure - {initSDKOutcome.Error.ToString()}.");
        }
    }
Esempio n. 10
0
        private bool ProcessReady(int listenPort, out GameLiftError err)
        {
            var processParameters = new ProcessParameters
            {
                Port = listenPort,
                OnStartGameSession = session =>
                {
                    _logger.InfoFormat("start session: {0}", session.GameSessionId);
                    GameLiftServerAPI.ActivateGameSession();
                    _session = session;
                },
                OnProcessTerminate = () => { _isRunning = false; }
            };

            var outcome = GameLiftServerAPI.ProcessReady(processParameters);

            err = outcome.Error;
            return(outcome.Success);
        }
Esempio n. 11
0
        //private Thread keepAlive;

        public void start()
        {
            var initSDKOutcome = GameLiftServerAPI.InitSDK();

            if (initSDKOutcome.Success)
            {
                // Set parameters and call ProcessReady
                var processParams = new ProcessParameters(
                    (gameSession) =>
                {
                    Console.WriteLine("HERE");
                    GameLiftServerAPI.ActivateGameSession();
                },
                    this.OnProcessTerminate,
                    this.OnHealthCheck,
                    PORT,
                    new LogParameters(new List <string>()        // Examples of log and error files written by the game server
                {
                    "/local/game/log"
                })
                    );

                var processReadyOutcome = GameLiftServerAPI.ProcessReady(processParams);
                if (processReadyOutcome.Success)
                {
                    Console.WriteLine("ProcessReady success.");
                }
                else
                {
                    Console.WriteLine("ProcessReady failure : " + processReadyOutcome.Error.ToString());
                }
            }
            else
            {
                Console.WriteLine("InitSDK failure : " + initSDKOutcome.Error.ToString());
            }
        }
Esempio n. 12
0
        public bool InitGameLift()
        {
            var initSDKOutcome = GameLiftServerAPI.InitSDK();

            if (initSDKOutcome.Success)
            {
                ProcessParameters m_processParameters = new ProcessParameters(
                    OnActiveGameSessionRequest,
                    OnTerminateProcessRequest,
                    OnHealthCheckRequest,
                    listening_port,
                    new LogParameters(new List <string>()
                {
                    //Here, the game server tells GameLift what set of files to upload when the game session ends.
                    //GameLift uploads everything specified here for the developers to fetch later.
                    "/local/game/logs/myserver.log"
                }
                                      )
                    );
                var processReadyOutcome = GameLiftServerAPI.ProcessReady(m_processParameters);
                if (processReadyOutcome.Success)
                {
                    Console.WriteLine("ProcessReady success.");
                }
                else
                {
                    Console.WriteLine("ProcessReady failure : " + processReadyOutcome.Error.ToString());
                }
            }
            else
            {
                Console.WriteLine("InitSDK failure : " + initSDKOutcome.Error.ToString());
                return(false);
            }

            return(true);
        }
Esempio n. 13
0
    public void DoStartStuff()
    {
        if (started == true)
        {
            LogToMyConsoleMainThread("GameLift Server Already Started");
            return;
        }

        started = true;

        LogToMyConsoleMainThread("GameLift Server Starting");



        Test1();
        Debug.Log("Port: " + listeningPort);

        //InitSDK establishes a local connection with the Amazon GameLift agent to enable
        //further communication.
        var initSDKOutcome = GameLiftServerAPI.InitSDK();

        if (initSDKOutcome.Success)
        {
            ProcessParameters processParameters = new ProcessParameters(
                (gameSession) =>
            {
                //Respond to new game session activation request. GameLift sends activation request
                //to the game server along with a game session object containing game properties
                //and other settings. Once the game server is ready to receive player connections,
                //invoke GameLiftServerAPI.ActivateGameSession()
                GameLiftServerAPI.ActivateGameSession();

                //TODO: We should call "ActivateGameSession" after Bolt is done starting
                myID        = gameSession.GameSessionId;
                SceneToLoad = gameSession.GameSessionData;



                NotAmazonUnityMainThreadDispatcher.Instance().Enqueue(Test0());
                //UnityMainThreadDispatcher.Instance().Enqueue(Test0());

                LogToMyConsoleMainThread(
                    "Starting game with " + gameSession.MaximumPlayerSessionCount + " players");
            },
                (updateGameSession) =>
            {
                LogToMyConsoleMainThread("updateGameSession");

                /*    updateGameSession.BackfillTicketId
                 *
                 *        MATCHMAKING_DATA_UPDATED = 0,
                 *        BACKFILL_FAILED = 1,
                 *        BACKFILL_TIMED_OUT = 2,
                 *        BACKFILL_CANCELLED = 3,
                 *        UNKNOWN = 4
                 */
            },
                () =>
            {
                //OnProcessTerminate callback. GameLift invokes this callback before shutting down
                //an instance hosting this game server. It gives this game server a chance to save
                //its state, communicate with services, etc., before being shut down.
                //In this case, we simply tell GameLift we are indeed going to shut down.
                GameLiftServerAPI.ProcessEnding();

                //TODO: We should save all data and shutdown Bolt before calling "ProcessEnding"
            },
                () =>
            {
                //This is the HealthCheck callback.
                //GameLift invokes this callback every 60 seconds or so.
                //Here, a game server might want to check the health of dependencies and such.
                //Simply return true if healthy, false otherwise.
                //The game server has 60 seconds to respond with its health status.
                //GameLift will default to 'false' if the game server doesn't respond in time.
                //In this case, we're always healthy!
                return(true);

                //TODO: maybe we should report unhealthy if performance is bad
            },
                //Here, the game server tells GameLift what port it is listening on for incoming player
                //connections. In this example, the port is hardcoded for simplicity. Active game
                //that are on the same instance must have unique ports.
                listeningPort,
                new LogParameters(new List <string>()
            {
                //Here, the game server tells GameLift what set of files to upload when the game session ends.
                //GameLift uploads everything specified here for the developers to fetch later.
                //TODO: put stuff in the log?
                "C:/Users/gl-user-server/AppData/LocalLow/DefaultCompany/GameLiftTest2"
                //"/local/game/logs/myserver.log"
            })
                );



            //Calling ProcessReady tells GameLift this game server is ready to receive incoming game sessions!
            var processReadyOutcome = GameLiftServerAPI.ProcessReady(processParameters);
            if (processReadyOutcome.Success)
            {
                LogToMyConsoleMainThread("ProcessReady success.");
            }
            else
            {
                LogToMyConsoleMainThread("ProcessReady failure : " + processReadyOutcome.Error.ToString());
            }
        }
        else
        {
            LogToMyConsoleMainThread("InitSDK failure : " + initSDKOutcome.Error.ToString());
            LogToMyConsoleMainThread("If testing locally, are you running the Server .jar?");
        }

        /*
         * https://docs.aws.amazon.com/gamelift/latest/developerguide/gamelift-sdk-server-api.html#gamelift-sdk-server-initialize
         * https://docs.aws.amazon.com/gamelift/latest/developerguide/integration-server-sdk-cpp-ref-actions.html#integration-server-sdk-cpp-ref-processreadyasync
         * GameLiftServerAPI:
         *
         * AcceptPlayerSession (used)
         * respond to  CreatePlayerSession() with an User ID and session ID
         *
         *
         * ActivateGameSession (used)
         *
         * DescribePlayerSessions
         *
         * GetGameSessionId
         *
         * GetSdkVersion
         *
         * GetTerminationTime
         * how much time is left to save data, move players to other game sessions
         *
         * InitSDK (used)
         *
         * ProcessEnding (used)
         *
         * ProcessReady (used)
         *
         * RemovePlayerSession (used)
         * kick someone out? or when someone left
         * "Notifies the Amazon GameLift service that a player with the specified
         * player session ID has disconnected from the server process. In response,
         * Amazon GameLift changes the player slot to available,
         * which allows it to be assigned to a new player."
         *
         *
         * StartMatchBackfill
         *
         * StopMatchBackfill
         *
         * TerminateGameSession
         * "call this at the end of game session shutdown process"
         * "After calling this action, the server process can call ProcessReady()
         * //to signal its availability to host a new game session.
         * //Alternatively it can call ProcessEnding() to shut down
         * //the server process and terminate the instance."
         *
         * UpdatePlayerSessionCreationPolicy
         *
         *
         * GetGameSessionLogUrl: Get logs from a game session, they are stored for 14 days
         *
         *
         */
    }
Esempio n. 14
0
        static int Main(string[] args)
        {
            var listeningPort = 7777;
            var waitHandle    = new AutoResetEvent(false);

            var initSDKOutcome = GameLiftServerAPI.InitSDK();

            if (initSDKOutcome.Success)
            {
                ProcessParameters processParameters = new ProcessParameters(
                    // OnGameSession callback
                    (gameSession) =>
                {
                    Console.WriteLine("OnGameSession received.");
                    var activateGameSessionOutcome = GameLiftServerAPI.ActivateGameSession();
                    if (activateGameSessionOutcome.Success)
                    {
                        Console.WriteLine("ActivateGameSession success.");
                    }
                    else
                    {
                        Console.WriteLine("ActivateGameSession failure : " + activateGameSessionOutcome.Error.ToString());
                    }
                },

                    // OnProcessTerminate callback
                    () =>
                {
                    Console.WriteLine("OnProcessTerminate received.");
                    waitHandle.Set();
                },

                    // OnHealthCheck
                    () =>
                {
                    Console.WriteLine("OnHealthCheck received.");
                    return(true);
                },

                    listeningPort,

                    new LogParameters(new List <string>()
                {
                    "/local/game/logs/myserver.log"
                })
                    );

                var processReadyOutcome = GameLiftServerAPI.ProcessReady(processParameters);
                if (processReadyOutcome.Success)
                {
                    Console.WriteLine("ProcessReady success.");
                }
                else
                {
                    Console.WriteLine("ProcessReady failure : " + processReadyOutcome.Error.ToString());
                }
            }
            else
            {
                Console.WriteLine("InitSDK failure : " + initSDKOutcome.Error.ToString());
            }

            Console.CancelKeyPress += new ConsoleCancelEventHandler(
                (object sender, ConsoleCancelEventArgs eventArgs) =>
            {
                Console.WriteLine("The read operation has been interrupted.");
                Console.WriteLine($"  Key pressed: {eventArgs.SpecialKey}");
                Console.WriteLine($"  Cancel property: {eventArgs.Cancel}");

                Console.WriteLine("Setting the Cancel property to true...");
                eventArgs.Cancel = true;

                Console.WriteLine($"  Cancel property: {eventArgs.Cancel}");
                Console.WriteLine("The read operation will resume...");
                waitHandle.Set();
            }
                );

            waitHandle.WaitOne();

            var processEndingOutcome = GameLiftServerAPI.ProcessEnding();

            if (processEndingOutcome.Success)
            {
                Console.WriteLine("ProcessEnding success.");
            }
            else
            {
                Console.WriteLine("ProcessEnding failure : " + processEndingOutcome.Error.ToString());
            }

            GameLiftServerAPI.Destroy();

            return(0);
        }
    // Called when the monobehaviour is created
    public void Awake()
    {
        //Initiate the simple statsD client
        this.statsdClient = new SimpleStatsdClient("localhost", 8125);

        //Get the port from command line args
        listeningPort = this.GetPortFromArgs();

        System.Console.WriteLine("Will be running in port: " + this.listeningPort);

        //InitSDK establishes a local connection with the Amazon GameLift agent to enable
        //further communication.
        var initSDKOutcome = GameLiftServerAPI.InitSDK();

        if (initSDKOutcome.Success)
        {
            ProcessParameters processParameters = new ProcessParameters(
                (gameSession) => {
                //Respond to new game session activation request. GameLift sends activation request
                //to the game server along with a game session object containing game properties
                //and other settings.

                // Activate the session
                GameLiftServerAPI.ActivateGameSession();

                //Start waiting for players
                this.gameSessionInfoReceived = true;
                this.gameSessionId           = gameSession.GameSessionId;

                //Set the game session tag (CloudWatch dimension) for custom metrics
                string justSessionId = this.gameSessionId.Split('/')[2];
                this.statsdClient.SetCommonTagString("#gamesession:" + justSessionId);

                //Send session started to CloudWatch just for testing
                this.statsdClient.SendCounter("game.SessionStarted", 1);

                System.Console.WriteLine("Matchmaker data New session:" + gameSession.MatchmakerData);
                this.matchmakerData   = MatchmakerData.FromJson(gameSession.MatchmakerData);
                this.backfillTicketID = this.matchmakerData.AutoBackfillTicketId;
            },
                (gameSession) => {
                //Respond to game session updates

                System.Console.WriteLine("backfill ticked ID update session:" + gameSession.BackfillTicketId);

                if (gameSession.BackfillTicketId != null)
                {
                    System.Console.WriteLine("Updating backfill ticked ID: " + gameSession.BackfillTicketId);
                    this.backfillTicketID = gameSession.BackfillTicketId;
                }
            },
                () => {
                //OnProcessTerminate callback. GameLift invokes this callback before shutting down
                //an instance hosting this game server. It gives this game server a chance to save
                //its state, communicate with services, etc., before being shut down.
                //In this case, we simply tell GameLift we are indeed going to shut down.
                GameLiftServerAPI.ProcessEnding();
                Application.Quit();
            },
                () => {
                //This is the HealthCheck callback.
                //GameLift invokes this callback every 60 seconds or so.
                //Here, a game server might want to check the health of dependencies and such.
                //Simply return true if healthy, false otherwise.
                //The game server has 60 seconds to respond with its health status.
                //GameLift will default to 'false' if the game server doesn't respond in time.
                //In this case, we're always healthy!
                return(true);
            },
                //Here, the game server tells GameLift what port it is listening on for incoming player
                //connections. We will use the port received from command line arguments
                listeningPort,
                new LogParameters(new List <string>()
            {
                //Let GameLift know where our logs are stored. We are expecting the command line args to specify the server with the port in log file
                "/local/game/logs/myserver" + listeningPort + ".log"
            }));

            //Calling ProcessReady tells GameLift this game server is ready to receive incoming game sessions
            var processReadyOutcome = GameLiftServerAPI.ProcessReady(processParameters);

            if (processReadyOutcome.Success)
            {
                print("ProcessReady success.");
            }
            else
            {
                print("ProcessReady failure : " + processReadyOutcome.Error.ToString());
            }
        }
        else
        {
            print("InitSDK failure : " + initSDKOutcome.Error.ToString());
        }
    }
Esempio n. 16
0
    private void ProcessReady()
    {
        try
        {
            ProcessParameters prParams = new ProcessParameters(
                /* onStartGameSession */ (gameSession) => {
                Debug.Log(":) GAMELIFT SESSION REQUESTED");  //And then do stuff with it maybe.
                try
                {
                    var outcome = GameLiftServerAPI.ActivateGameSession();
                    if (outcome.Success)
                    {
                        Debug.Log(":) GAME SESSION ACTIVATED");
                    }
                    else
                    {
                        Debug.Log(":( GAME SESSION ACTIVATION FAILED. ActivateGameSession() returned " + outcome.Error.ToString());
                    }
                }
                catch (Exception e)
                {
                    Debug.Log(":( GAME SESSION ACTIVATION FAILED. ActivateGameSession() exception " + Environment.NewLine + e.Message);
                }
            },
                /* onProcessTerminate */ () => {
                Debug.Log(":| GAMELIFT PROCESS TERMINATION REQUESTED (OK BYE)");
                GameLiftRequestedTermination = true;
                gl.TerminateServer();
            },
                /* onHealthCheck */ () => {
                Debug.Log(":) GAMELIFT HEALTH CHECK REQUESTED (HEALTHY)");
                return(true);
            },
                /* port */ port, // tell the GameLift service which port to connect to this process on.
                // unless we manage this there can only be one process per server.
                new LogParameters(new List <string>()
            {
                mLogFilePath
                //@"C:\game\GameLiftUnity_Data\output_log.txt" // must be different for each server if multiple servers on instance
            }));

            var processReadyOutcome = GameLiftServerAPI.ProcessReady(prParams);
            if (processReadyOutcome.Success)
            {
                if (gl != null)
                {
                    gl.gameliftStatus = true;
                }
                Debug.Log(":) PROCESSREADY SUCCESS.");
            }
            else
            {
                if (gl != null)
                {
                    gl.gameliftStatus = false;
                }
                Debug.Log(":( PROCESSREADY FAILED. ProcessReady() returned " + processReadyOutcome.Error.ToString());
            }
        }
        catch (Exception e)
        {
            Debug.Log(":( PROCESSREADY FAILED. ProcessReady() exception " + Environment.NewLine + e.Message);
        }
    }