// MasterClientPeer.cs:
private
OperationResponse HandleAuthenticate(OperationRequest operationRequest)
{
OperationResponse response;
var
request =
new
AuthenticateRequest(
this
.Protocol, operationRequest);
if
(!OperationHelper.ValidateOperation(request, log,
out
response))
{
return
response;
}
this
.UserId = request.UserId;
// publish operation response
var
responseObject =
new
AuthenticateResponse { QueuePosition = 0 };
return
new
OperationResponse(operationRequest.OperationCode, responseObject);
}
// AppLobby.cs:
protected
virtual
OperationResponse HandleJoinLobby(MasterClientPeer peer, OperationRequest operationRequest, SendParameters sendParameters)
{
// validate operation
var
operation =
new
JoinLobbyRequest(peer.Protocol, operationRequest);
OperationResponse response;
if
(OperationHelper.ValidateOperation(operation, log,
out
response) ==
false
)
{
return
response;
}
peer.GameChannelSubscription =
null
;
var
subscription =
this
.GameList.AddSubscription(peer, operation.GameProperties, operation.GameListCount);
peer.GameChannelSubscription = subscription;
peer.SendOperationResponse(
new
OperationResponse(operationRequest.OperationCode), sendParameters);
// publish game list to peer after the response has been sent
var
gameList = subscription.GetGameList();
var
e =
new
GameListEvent { Data = gameList };
var
eventData =
new
EventData((
byte
)EventCode.GameList, e);
peer.SendEvent(eventData,
new
SendParameters());
return
null
;
}
// OutgoingMasterServerPeer.cs:
protected
virtual
void
HandleRegisterGameServerResponse(OperationResponse operationResponse)
{
// [...]
switch
(operationResponse.ReturnCode)
{
case
(
short
)ErrorCode.Ok:
{
log.InfoFormat(
"Successfully registered at master server: serverId={0}"
, GameApplication.ServerId);
this
.IsRegistered =
true
;
this
.UpdateAllGameStates();
this
.StartUpdateLoop();
break
;
}
}
}
// OutgoingMasterServerPeer.cs:
public
virtual
void
UpdateAllGameStates()
{
// [...]
foreach
(
var
gameId
in
GameCache.Instance.GetRoomNames())
{
Room room;
if
(GameCache.Instance.TryGetRoomWithoutReference(gameId,
out
room))
{
room.EnqueueMessage(
new
RoomMessage((
byte
)GameMessageCodes.ReinitializeGameStateOnMaster));
}
}
}
protected
virtual
void
UpdateGameStateOnMaster(
byte
? newMaxPlayer =
null
,
bool
? newIsOpen =
null
,
bool
? newIsVisble =
null
,
object
[] lobbyPropertyFilter =
null
,
Hashtable gameProperties =
null
,
string
newPeerId =
null
,
string
removedPeerId =
null
,
bool
reinitialize =
false
)
{
// [...]
var
e =
this
.CreateUpdateGameEvent();
e.Reinitialize = reinitialize;
e.MaxPlayers = newMaxPlayer;
// [ ... more event data is set here ... ]
var
eventData =
new
EventData((
byte
)ServerEventCode.UpdateGameState, e);
GameApplication.Instance.MasterPeer.SendEvent(eventData,
new
SendParameters());
}
}
See the LoadBalancing.LoadShedding namespace in the \src-server\Loadbalancing\Loadbalancing.sln solution for implementation details.
internal
enum
FeedbackName
{
CpuUsage,
Bandwidth,
TimeSpentInServer
}
public
enum
FeedbackLevel
{
Highest = 4,
High = 3,
Normal = 2,
Low = 1,
Lowest = 0
}
// DefaultConfiguration.cs:
internal
static
List<FeedbackController> GetDefaultControllers()
{
internal
static
List<FeedbackController> GetDefaultControllers()
{
var
cpuController =
new
FeedbackController(
FeedbackName.CpuUsage,
new
Dictionary<FeedbackLevel,
int
>
{
{ FeedbackLevel.Lowest, 20 },
{ FeedbackLevel.Low, 35 },
{ FeedbackLevel.Normal, 50 },
{ FeedbackLevel.High, 70 },
{ FeedbackLevel.Highest, 90 }
},
0,
FeedbackLevel.Lowest);
// [...]
}
private
void
Update()
{
FeedbackLevel oldValue =
this
.feedbackControlSystem.Output;
if
(
this
.cpuCounter.InstanceExists)
{
var
cpuUsage = (
int
)
this
.cpuCounter.GetNextAverage();
Counter.CpuAvg.RawValue = cpuUsage;
this
.feedbackControlSystem.SetCpuUsage(cpuUsage);
}
// [...]
if
(
this
.timeSpentInServerInCounter.InstanceExists &&
this
.timeSpentInServerOutCounter.InstanceExists)
{
var
timeSpentInServer = (
int
)
this
.timeSpentInServerInCounter.GetNextAverage() + (
int
)
this
.timeSpentInServerOutCounter.GetNextAverage();
Counter.TimeInServerInAndOutAvg.RawValue = timeSpentInServer;
this
.feedbackControlSystem.SetTimeSpentInServer(timeSpentInServer);
}
this
.FeedbackLevel =
this
.feedbackControlSystem.Output;
Counter.LoadLevel.RawValue = (
byte
)
this
.FeedbackLevel;
if
(oldValue !=
this
.FeedbackLevel)
{
if
(log.IsInfoEnabled)
{
log.InfoFormat(
"FeedbackLevel changed: old={0}, new={1}"
, oldValue,
this
.FeedbackLevel);
}
this
.RaiseFeedbacklevelChanged();
}
}
public
void
UpdateServerState()
{
// [...]
this
.UpdateServerState(
GameApplication.Instance.WorkloadController.FeedbackLevel,
GameApplication.Instance.PeerCount,
GameApplication.Instance.WorkloadController.ServerState);
}
See the LoadBalancing.LoadBalancer class in the \src-server\Loadbalancing\Loadbalancing.sln solution for implementation details.
<
Photon.LoadBalancing.GameServer.GameServerSettings
>
<
setting
name
=
"MasterIPAddress"
serializeAs
=
"String"
>
<
value
>127.0.0.1</
value
>
<
setting
>
<
setting
name
=
"PublicIPAddress"
serializeAs="String”>
<
value
>127.0.0.1</
value
>
<!-- use this to auto-detect the PublicIPAddress: -->
<!-- <value> </value> -->
</
setting
>
<!-- [...] -->
</
Photon.LoadBalancing.GameServer.GameServerSettings
>