private void HandleCreateGameOperationInt(HivePeer peer, SendParameters sendParameters, JoinGameRequest createGameRequest, bool fromJoin = false) { var createOptions = createGameRequest.GetCreateGameSettings(this); peer.SetPrivateCustomTypeCache(this.customTypeCache); RequestHandler handler = () => { var oldValue = this.allowSetGameState; try { this.allowSetGameState = false; // since we allow to make changes to the original request sent by the client in a plugin // we should check op.IsValid() - if not report error createGameRequest.SetupRequest(); return this.ProcessCreateGame(peer, createGameRequest, sendParameters); } catch (Exception e) { Log.ErrorFormat("Exception: {0}", e); this.allowSetGameState = oldValue; throw; } }; var info = new CreateGameCallInfo(this.PendingPluginContinue, this.callEnv) { Request = createGameRequest, UserId = peer.UserId, Nickname = createGameRequest.GetNickname(), CreateOptions = createOptions, IsJoin = fromJoin, Handler = handler, Peer = peer, SendParams = sendParameters, OnFail = (onFailMsg, errorData) => { this.allowSetGameState = false; this.failureOnCreate = true; this.SendErrorResponse(peer, createGameRequest.OperationCode, ErrorCode.PluginReportedError, onFailMsg, sendParameters, errorData); peer.ReleaseRoomReference(); peer.ScheduleDisconnect(); // this gives the client a chance to get the reason } }; try { this.Plugin.OnCreateGame(info); } catch (Exception e) { this.Plugin.ReportError(Photon.Hive.Plugin.ErrorCodes.UnhandledException, e); } }
protected override void HandleJoinGameOperation(HivePeer peer, SendParameters sendParameters, JoinGameRequest joinGameRequest) { if (this.isClosed) { if (!this.CheckGameCanBeCreated(peer, joinGameRequest)) { return; } if (!this.ReinitGame()) { this.SendErrorResponse(peer, joinGameRequest.OperationCode, ErrorCode.InternalServerError, HiveErrorMessages.ReinitGameFailed, sendParameters); joinGameRequest.OnJoinFailed(ErrorCode.InternalServerError, HiveErrorMessages.ReinitGameFailed); this.JoinFailureHandler(LeaveReason.ServerDisconnect, peer, joinGameRequest); if (Log.IsWarnEnabled) { Log.WarnFormat("Game '{0}' userId '{1}' failed to join. msg:{2}", this.Name, peer.UserId, HiveErrorMessages.ReinitGameFailed); } return; } } //TBD do we still need this? string msg; if (!this.ValidatePlugin(joinGameRequest, out msg)) { this.SendErrorResponse(peer, joinGameRequest.OperationCode, ErrorCode.PluginMismatch, msg, sendParameters); joinGameRequest.OnJoinFailed(ErrorCode.InternalServerError, HiveErrorMessages.ReinitGameFailed); this.JoinFailureHandler(LeaveReason.ServerDisconnect, peer, joinGameRequest); if (Log.IsWarnEnabled) { Log.WarnFormat("HandleJoinGameOperation: Game '{0}' userId '{1}' failed to join. msg:{2} -- peer:{3}", this.Name, peer.UserId, msg, peer); } return; } if (this.ActorsManager.ActorNumberCounter == 0) // we were just beeing created { if (!this.CheckGameCanBeCreated(peer, joinGameRequest)) { return; } this.HandleCreateGameOperationInt(peer, sendParameters, joinGameRequest, true); } else { peer.SetPrivateCustomTypeCache(this.customTypeCache); RequestHandler handler = () => { try { return this.ProcessBeforeJoinGame(joinGameRequest, sendParameters, peer); } catch (Exception e) { Log.ErrorFormat("Exception: {0}", e); throw; } }; var info = new BeforeJoinGameCallInfo(this.PendingPluginContinue, this.callEnv) { Request = joinGameRequest, UserId = peer.UserId, Nickname = joinGameRequest.GetNickname(), Handler = handler, Peer = peer, SendParams = sendParameters, OnFail = (onFailMsg, errorData) => { this.allowSetGameState = false; joinGameRequest.OnJoinFailed(ErrorCode.PluginReportedError, onFailMsg); this.OnJoinFailHandler(LeaveReason.ServerDisconnect, onFailMsg, errorData, peer, sendParameters, joinGameRequest); } }; try { this.Plugin.BeforeJoin(info); } catch (Exception e) { this.Plugin.ReportError(Photon.Hive.Plugin.ErrorCodes.UnhandledException, e); } } }
private void CallPluginOnLeaveIfJoinFailed(byte reason, HivePeer peer, JoinGameRequest request) { if (peer.JoinStage == HivePeer.JoinStages.Connected || peer.JoinStage == Hive.HivePeer.JoinStages.CreatingOrLoadingGame) { if (Log.IsDebugEnabled) { Log.DebugFormat("Peer join stage is {0}. CallPluginOnLeaveIfJoinFailed will be skiped. p:{1}", peer.JoinStage, peer); } return; } if (Log.IsDebugEnabled) { Log.DebugFormat("Peer join stage is {0}. reason:{1}. CallPluginOnLeaveIfJoinFailed is called. p:{2}", peer.JoinStage, reason, peer); } var actor = this.GetActorByPeer(peer); RequestHandler handler = () => { try { this.RemovePeerFromGame(peer, false); } catch (Exception e) { Log.ErrorFormat("Exception: {0}", e); throw; } return true; }; var info = new LeaveGameCallInfo(this.PendingPluginContinue, this.callEnv) { ActorNr = actor != null ? actor.ActorNr : -1, UserId = actor != null ? actor.UserId : peer.UserId, Nickname = actor != null ? actor.Nickname : request.GetNickname(), IsInactive = false, Reason = reason, Request = null, Handler = handler, Peer = null, SendParams = new SendParameters(), }; try { this.Plugin.OnLeave(info); } catch (Exception e) { this.Plugin.ReportError(Photon.Hive.Plugin.ErrorCodes.UnhandledException, e); } }