示例#1
0
        static async Task <string> CreateFleet(string name, string version, string buildId)
        {
            var config = new AmazonGameLiftConfig();

            config.RegionEndpoint = region;
            using (AmazonGameLiftClient aglc = new AmazonGameLiftClient(config))
            {
                // create launch configuration
                var serverProcess = new ServerProcess();
                serverProcess.ConcurrentExecutions = 1;
                serverProcess.LaunchPath           = @"C:\game\GameLiftUnity.exe"; // @"/local/game/ReproGLLinux.x86_64";
                serverProcess.Parameters           = "-batchmode -nographics";

                // create inbound IP permissions
                var permission1 = new IpPermission();
                permission1.FromPort = 1935;
                permission1.ToPort   = 1935;
                permission1.Protocol = IpProtocol.TCP;
                permission1.IpRange  = "0.0.0.0/0";

                // create inbound IP permissions
                var permission2 = new IpPermission();
                permission2.FromPort = 3389;
                permission2.ToPort   = 3389;
                permission2.Protocol = IpProtocol.TCP;
                permission2.IpRange  = "0.0.0.0/0";

                // create fleet
                var cfreq = new CreateFleetRequest();
                cfreq.Name            = name;
                cfreq.Description     = version;
                cfreq.BuildId         = buildId;
                cfreq.EC2InstanceType = EC2InstanceType.C4Large;
                cfreq.EC2InboundPermissions.Add(permission1);
                cfreq.EC2InboundPermissions.Add(permission2);
                cfreq.RuntimeConfiguration = new RuntimeConfiguration();
                cfreq.RuntimeConfiguration.ServerProcesses = new List <ServerProcess>();
                cfreq.RuntimeConfiguration.ServerProcesses.Add(serverProcess);
                cfreq.NewGameSessionProtectionPolicy = ProtectionPolicy.NoProtection;
                CreateFleetResponse cfres = await aglc.CreateFleetAsync(cfreq);

                // set fleet capacity
                var ufcreq = new UpdateFleetCapacityRequest();
                ufcreq.MinSize          = 0;
                ufcreq.DesiredInstances = 1;
                ufcreq.MaxSize          = 1;
                ufcreq.FleetId          = cfres.FleetAttributes.FleetId;
                UpdateFleetCapacityResponse ufcres = await aglc.UpdateFleetCapacityAsync(ufcreq);

                // set scaling rule (switch fleet off after 1 day of inactivity)
                // If [MetricName] is [ComparisonOperator] [Threshold] for [EvaluationPeriods] minutes, then [ScalingAdjustmentType] to/by [ScalingAdjustment].
                var pspreq = new PutScalingPolicyRequest();
                pspreq.Name                  = "Switch fleet off after 1 day of inactivity";
                pspreq.MetricName            = MetricName.ActiveGameSessions;
                pspreq.ComparisonOperator    = ComparisonOperatorType.LessThanOrEqualToThreshold;
                pspreq.Threshold             = 0.0;  // double (don't use int)
                pspreq.EvaluationPeriods     = 1435; // just under 1 day, 1435 appears to be the maximum permitted value now.
                pspreq.ScalingAdjustmentType = ScalingAdjustmentType.ExactCapacity;
                pspreq.ScalingAdjustment     = 0;
                pspreq.FleetId               = cfres.FleetAttributes.FleetId;
                PutScalingPolicyResponse pspres = await aglc.PutScalingPolicyAsync(pspreq);

                return(cfres.FleetAttributes.FleetId);
            }
        }