public virtual void OnCustomAuthenticationError(Photon.Common.ErrorCode errorCode, string debugMessage,
                                                        IAuthenticateRequest authenticateRequest, SendParameters sendParameters,
                                                        object state)
        {
            try
            {
                if (this.Connected == false)
                {
                    return;
                }

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("Client custom authentication failed: appId={0}, result={1}, msg={2}", authenticateRequest.ApplicationId, errorCode, debugMessage);
                }

                var operationResponse = new OperationResponse((byte)Hive.Operations.OperationCode.Authenticate)
                {
                    ReturnCode   = (short)errorCode,
                    DebugMessage = debugMessage,
                };

                this.SendOperationResponse(operationResponse, sendParameters);
                this.SetCurrentOperationHandler(OperationHandlerInitial.Instance);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                var errorResponse = new OperationResponse((byte)Hive.Operations.OperationCode.Authenticate)
                {
                    ReturnCode = (short)Photon.Common.ErrorCode.InternalServerError
                };
                this.SendOperationResponse(errorResponse, sendParameters);
            }
        }
Example #2
0
        /// <summary>
        /// Tries to add a <see cref="HivePeer"/> to this game instance.
        /// </summary>
        /// <param name="game"></param>
        /// <param name="peer">
        /// The peer to add.
        /// </param>
        /// <param name="actorNr">
        /// The actor Nr.
        /// </param>
        /// <param name="actor">
        /// When this method returns this out param contains the <see cref="Actor"/> associated with the <paramref name="peer"/>.
        /// </param>
        /// <param name="isNewActor">indicates that new actor was created</param>
        /// <param name="reason">
        /// reason why player can not be added
        /// </param>
        /// <param name="joinRequest"></param>
        /// <returns>
        /// Returns true if no actor exists for the specified peer and a new actor for the peer has been successfully added.
        ///   The actor parameter is set to the newly created <see cref="Actor"/> instance.
        ///   Returns false if an actor for the specified peer already exists.
        ///   The actor parameter is set to the existing <see cref="Actor"/> for the specified peer.
        /// </returns>
        public bool TryAddPeerToGame(HiveGame game, HivePeer peer, int actorNr, out Actor actor, out bool isNewActor,
                                     out Photon.Common.ErrorCode errorcode, out string reason, JoinGameRequest joinRequest)
        {
            isNewActor = false;
            errorcode  = Photon.Common.ErrorCode.InternalServerError;

            if (!this.VerifyCanJoin(peer, actorNr, game.PlayerTTL, out actor, out errorcode, out reason, ref isNewActor, game.CheckUserOnJoin, joinRequest))
            {
                return(false);
            }

            if (isNewActor)
            {
                actor = new Actor(peer);
                this.actorNumberCounter++;
                actor.ActorNr = this.actorNumberCounter;

                this.SanityChecks(peer, actorNr, actor, ref reason, game.CheckUserOnJoin);

                this.allActors.Add(actor);
            }
            else
            {
                actor.Reactivate(peer);
            }
            ++this.activeActorsCount;

            if (Log.IsDebugEnabled)
            {
                Log.DebugFormat("Actor {0}: {1} {2} to game: {3} -- peer:{4}", isNewActor ? "added" : "reactivated", actor.ActorNr, actor.UserId, game.Name, peer.ToString());
            }

            reason = "";
            return(true);
        }
Example #3
0
        private bool VerifyCanJoin(HivePeer peer, int actorNr, int playerTtl,
                                   out Actor actor, out Photon.Common.ErrorCode errorcode, out string reason, ref bool isNewActor, bool checkUserIdOnJoin, JoinGameRequest joinRequest)
        {
            // check if the peer already is linked to this game
            actor = this.ActorsGetActorByPeer(peer);
            if (actor != null)
            {
                reason    = "Join failed: Peer already joined the specified game.";
                errorcode = Photon.Common.ErrorCode.JoinFailedPeerAlreadyJoined;
                return(false);
            }

            reason = string.Empty;

            var joinMode = joinRequest.JoinMode;

            if (checkUserIdOnJoin)
            {
                if (string.IsNullOrEmpty(peer.UserId))
                {
                    // ERROR: should never happen with auto-generated userid's
                    //        without autogen-userid's its not supported
                    reason    = string.Format("Join failed: UserId is not set, checkUserIdOnJoin=true expects a UserId.");
                    errorcode = Photon.Common.ErrorCode.OperationInvalid;
                    return(false);
                }
                if (!this.CheckExcludeList(peer.UserId, out reason))
                {
                    errorcode = Photon.Common.ErrorCode.JoinFailedFoundExcludedUserId;
                    return(false);
                }

                // check if the userId already joined this game
                actor = GetActorByUserId(this, peer.UserId);
                if (actor != null)
                {
                    if (actor.IsActive)
                    {
                        if (!this.TrySameSessionRejoin(peer, playerTtl, actor, joinMode, joinRequest.ForceRejoin))
                        {
                            reason    = string.Format("Join failed: UserId '{0}' already joined the specified game (JoinMode={1}).", peer.UserId, joinMode);
                            errorcode = Photon.Common.ErrorCode.JoinFailedFoundActiveJoiner;
                            return(false);
                        }

                        errorcode = 0;
                        return(true);
                    }

                    // actor.IsInactive
                    // verify is not inactive actor is not expired, since actorlist could contain expired actors after reload
                    if (canPlayerTtlExpire(playerTtl) && actor.DeactivationTime.HasValue)
                    {
                        var scheduleTime = (int)((DateTime)actor.DeactivationTime - DateTime.Now).TotalMilliseconds + playerTtl;
                        if (scheduleTime <= 0)
                        {
                            reason    = string.Format("Join failed: UserId '{0}' is expired (JoinMode={1}).", peer.UserId, joinMode);
                            errorcode = Photon.Common.ErrorCode.JoinFailedWithRejoinerNotFound;
                            return(false);
                        }
                    }
#if !AutoRejoin
                    if (joinMode != JoinModes.RejoinOnly && joinMode != JoinModes.RejoinOrJoin)
                    {
                        reason    = string.Format("Found inactive UserId '{0}', but not rejoining (JoinMode={1}).", peer.UserId, joinMode);
                        errorcode = Photon.Common.ErrorCode.JoinFailedFoundInactiveJoiner;
                        return(false);
                    }
#endif
                    errorcode = 0;
                    return(true);
                }
            }
            else
            {
                // actorNr > 0 => re-joing with actornr & without userid
                if (actorNr > 0)
                {
                    actor = GetActorByNumber(this, actorNr);
                    if (actor == null)
                    {
                        // not finding the actor was allways an error - so NO JoinOrRejoin (NOR RejoinOrJoin)!
                        reason    = string.Format("Rejoin failed: actor nr={0} not found.", actorNr);
                        errorcode = Photon.Common.ErrorCode.JoinFailedWithRejoinerNotFound;
                        return(false);
                    }

                    if (actor.IsActive)
                    {
                        if (!this.TrySameSessionRejoin(peer, playerTtl, actor, joinMode, false))
                        {
                            reason    = string.Format(HiveErrorMessages.UserAlreadyJoined, actorNr, joinMode);
                            errorcode = Photon.Common.ErrorCode.JoinFailedFoundActiveJoiner;
                            return(false);
                        }

                        errorcode = 0;
                        return(true);
                    }

                    // TODO: comment above stays that we re-join without userid.
                    // why we check it here? it also may be null or empty
                    // this may happen if checkUserOnJoin is false
                    if (actor.IsInactive && actor.UserId != peer.UserId)
                    {
                        Guid tmpGuid;
                        if (!(Guid.TryParse(actor.UserId, out tmpGuid) && Guid.TryParse(peer.UserId, out tmpGuid)))
                        {
                            reason = "Rejoin failed: userId of peer doesn't match userid of actor.";

                            errorcode = errorcode = Photon.Common.ErrorCode.InternalServerError;
                            return(false);
                        }

                        Log.Debug("Rejoin: userId of peer doesn't match userid of actor. But are GUId's - assuming its AutoUserIds");
                    }

                    // actor is inActive
                    if (canPlayerTtlExpire(playerTtl) && actor.DeactivationTime.HasValue)
                    {
                        var sceduleTime = (int)((DateTime)actor.DeactivationTime - DateTime.Now).TotalMilliseconds + playerTtl;
                        if (sceduleTime <= 0)
                        {
                            reason    = string.Format("Join failed: ActorNr '{0}' is expired.", actorNr);
                            errorcode = Photon.Common.ErrorCode.JoinFailedWithRejoinerNotFound;
                            return(false);
                        }
                    }
#if !AutoRejoin
                    if (joinMode != JoinModes.RejoinOnly && joinMode != JoinModes.RejoinOrJoin)
                    {
                        // should never happen, since actorn>0 means rejoinmode is set / see joingamerequest.actornr
                        reason    = string.Format("Found inactive ActorNr '{0}', but not rejoining (JoinMode={1}).", actorNr, joinMode);
                        errorcode = Photon.Common.ErrorCode.InternalServerError;
                        return(false);
                    }
#endif
                    errorcode = 0;
                    return(true);
                }
            }

            if (joinMode == JoinModes.RejoinOnly)
            {
                reason    = string.Format("Rejoin failed: userid={0} not found, actor nr={1}.", peer.UserId, actorNr);
                errorcode = Photon.Common.ErrorCode.JoinFailedWithRejoinerNotFound;
                return(false);
            }

            // create new actor instance
            isNewActor = true;
            errorcode  = 0;
            return(true);
        }