public void SendMessage(PiranhaMessage message)
        {
            ClientConnectionState state = this.m_connection.State;

            if (state != ClientConnectionState.LOGGED)
            {
                if (state == ClientConnectionState.DISCONNECTED)
                {
                    return;
                }
                int messageType = message.GetMessageType();

                if (messageType != LoginFailedMessage.MESSAGE_TYPE &&
                    messageType != LoginOkMessage.MESSAGE_TYPE &&
                    messageType != UnlockAccountFailedMessage.MESSAGE_TYPE &&
                    messageType != UnlockAccountOkMessage.MESSAGE_TYPE)
                {
                    return;
                }

                if (messageType == LoginFailedMessage.MESSAGE_TYPE)
                {
                    this.m_connection.SetState(ClientConnectionState.LOGIN_FAILED);
                }
            }

            this.m_connection.Messaging.Send(message);
        }
        private bool IsRequestPiranhaMessage(PiranhaMessage message)
        {
            int messageType = message.GetMessageType();

            return(messageType == AskForAvatarProfileMessage.MESSAGE_TYPE ||
                   messageType == AskForAllianceDataMessage.MESSAGE_TYPE);
        }
Ejemplo n.º 3
0
        private static void OnForwardLogicRequestMessageReceived(ForwardLogicRequestMessage message)
        {
            PiranhaMessage logicMessage = LogicMagicMessageFactory.Instance.CreateMessageByType(message.MessageType);

            if (logicMessage == null)
            {
                throw new Exception("logicMessage should not be NULL!");
            }

            logicMessage.GetByteStream().SetByteArray(message.MessageBytes, message.MessageLength);
            logicMessage.SetMessageVersion(message.MessageVersion);
            logicMessage.Decode();

            if (!logicMessage.IsServerToClientMessage())
            {
                switch (logicMessage.GetMessageType())
                {
                case AskForJoinableAlliancesListMessage.MESSAGE_TYPE:
                    ServerRequestManager.Create(new AvatarRequestMessage
                    {
                        AccountId = message.AccountId
                    }, ServerManager.GetDocumentSocket(9, message.AccountId), 5).OnComplete = args =>
                    {
                        SearchMessageManager.OnAvatarResponseMessageReceivedForJoinableAlliancesList(message, args);
                    };

                    break;

                case SearchAlliancesMessage.MESSAGE_TYPE:
                    SearchMessageManager.OnSearchAlliancesMessageReceived((SearchAlliancesMessage)logicMessage, message);
                    break;
                }
            }
        }
Ejemplo n.º 4
0
        private static void OnForwardLogicMessageRequestMessageReceived(ForwardLogicRequestMessage message)
        {
            PiranhaMessage logicMessage = LogicMagicMessageFactory.Instance.CreateMessageByType(message.MessageType);

            if (logicMessage == null)
            {
                throw new Exception("logicMessage should not be NULL!");
            }

            logicMessage.GetByteStream().SetByteArray(message.MessageBytes, message.MessageLength);
            logicMessage.SetMessageVersion(message.MessageVersion);
            logicMessage.Decode();

            if (!logicMessage.IsServerToClientMessage())
            {
                switch (logicMessage.GetMessageType())
                {
                case AskForAllianceDataMessage.MESSAGE_TYPE:
                    AskForAllianceDataMessage askForAllianceDataMessage = (AskForAllianceDataMessage)logicMessage;
                    LogicLong allianceId = askForAllianceDataMessage.RemoveAllianceId();

                    if (AllianceManager.TryGet(allianceId, out Alliance alliance))
                    {
                        ServerMessageManager.SendMessage(StreamMessageManager.CreateForwardLogicMessage(alliance.GetAllianceDataMessage(), message.SessionId),
                                                         ServerManager.GetProxySocket(message.SessionId));
                    }

                    break;
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     Receives the specified message.
        /// </summary>
        internal void ReceiveMessage(PiranhaMessage message)
        {
            int messageType = message.GetMessageType();

            switch (messageType)
            {
            case 20103:
            {
                this.LoginFailedMessageReceived((LoginFailedMessage)message);
                break;
            }

            case 20104:
            {
                this.LoginOkMessageReceived((LoginOkMessage)message);
                break;
            }

            case 20108:
            {
                this.KeepAliveServerReceived((KeepAliveServerMessage)message);
                break;
            }

            default:
            {
                // Logging.Warning(this, "MessageManager::receiveMessage no case for message type " + messageType);
                break;
            }
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 ///     Receives the specified <see cref="PiranhaMessage"/>.
 /// </summary>
 internal void ReceiveMessage(PiranhaMessage message)
 {
     switch (message.GetMessageType())
     {
     case 14715:
         this.SendGlobalChatLineMessageReceived((SendGlobalChatLineMessage)message);
         break;
     }
 }
 public void ReceiveMessage(PiranhaMessage message)
 {
     switch (message.GetMessageType())
     {
     case BattleEndClientTurnMessage.MESSAGE_TYPE:
         this.OnBattleEndClientTurnMessageReceived((BattleEndClientTurnMessage)message);
         break;
     }
 }
Ejemplo n.º 8
0
        public void SendMessage(PiranhaMessage message)
        {
            if (message.GetMessageType() == EndClientTurnMessage.MESSAGE_TYPE || message.GetMessageType() == BattleEndClientTurnMessage.MESSAGE_TYPE)
            {
                this.m_nextClientTurn = 5f;
            }
            else if (message.GetMessageType() == KeepAliveMessage.MESSAGE_TYPE)
            {
                this.m_nextKeepAlive = 0.1f;

                if (this.m_sendKeepAliveTime == DateTime.MinValue)
                {
                    this.m_sendKeepAliveTime = DateTime.UtcNow;
                }
            }

            this.m_messaging.Send(message);
        }
 public void ReceiveMessage(PiranhaMessage message)
 {
     switch (message.GetMessageType())
     {
     case SendGlobalChatLineMessage.MESSAGE_TYPE:
         this.OnSendGlobalChatLineMessageReceived((SendGlobalChatLineMessage)message);
         break;
     }
 }
Ejemplo n.º 10
0
        /// <summary>
        ///     Writes the message header
        /// </summary>
        private void WriteHeader(PiranhaMessage message, byte[] packet, int encodingLength)
        {
            int messageType    = message.GetMessageType();
            int messageVersion = message.GetMessageVersion();

            packet[1] = (byte)(messageType);
            packet[0] = (byte)(messageType >> 8);
            packet[4] = (byte)(encodingLength);
            packet[3] = (byte)(encodingLength >> 8);
            packet[2] = (byte)(encodingLength >> 16);
            packet[6] = (byte)(messageVersion);
            packet[5] = (byte)(messageVersion >> 8);
        }
Ejemplo n.º 11
0
        /// <summary>
        ///     Receives the specified <see cref="PiranhaMessage"/>.
        /// </summary>
        internal void ReceiveMessage(PiranhaMessage message)
        {
            switch (message.GetMessageType())
            {
            case 10212:
                this.ChangeAvatarNameMessageReceived((ChangeAvatarNameMessage)message);
                break;

            case 14325:
                this.AskForAvatarProfileMessageReceived((AskForAvatarProfileMessage)message);
                break;
            }
        }
Ejemplo n.º 12
0
 public void SendPiranhaMessage(PiranhaMessage message, int serverType)
 {
     if (message.GetByteStream().GetLength() == 0)
     {
         message.Encode();
     }
     this.SendMessage(new ForwardLogicMessage
     {
         MessageType    = message.GetMessageType(),
         MessageVersion = (short)message.GetMessageVersion(),
         MessageLength  = message.GetEncodingLength(),
         MessageBytes   = message.GetByteStream().GetByteArray()
     }, serverType);
 }
        public void ReceiveMessage(PiranhaMessage message)
        {
            switch (message.GetMessageType())
            {
            case LeaveAllianceMessage.MESSAGE_TYPE:
                this.OnLeaveAllianceMessageReceived((LeaveAllianceMessage)message);
                break;

            case ChatToAllianceStreamMessage.MESSAGE_TYPE:
                this.OnChatToAllianceStreamMessageReceived((ChatToAllianceStreamMessage)message);
                break;

            case ChangeAllianceSettingsMessage.MESSAGE_TYPE:
                this.OnChangeAllianceSettingsMessageReceived((ChangeAllianceSettingsMessage)message);
                break;

            case ChangeAllianceMemberRoleMessage.MESSAGE_TYPE:
                this.OnChangeAllianceMemberRoleMessageReceived((ChangeAllianceMemberRoleMessage)message);
                break;

            case DonateAllianceUnitMessage.MESSAGE_TYPE:
                this.OnDonateAllianceUnitMessageReceived((DonateAllianceUnitMessage)message);
                break;

            case CancelChallengeMessage.MESSAGE_TYPE:
                this.OnCancelChallengeMessageReceived((CancelChallengeMessage)message);
                break;

            case ScoutFriendlyBattleMessage.MESSAGE_TYPE:
                this.OnScoutFriendlyBattleMessageReceived((ScoutFriendlyBattleMessage)message);
                break;

            case AcceptFriendlyBattleMessage.MESSAGE_TYPE:
                this.OnAcceptFriendlyBattleMessageReceived((AcceptFriendlyBattleMessage)message);
                break;

            case StartFriendlyChallengeSpectateMessage.MESSAGE_TYPE:
                this.OnStartFriendlyChallengeSpectateMessageReceived((StartFriendlyChallengeSpectateMessage)message);
                break;

            case RespondToAllianceJoinRequestMessage.MESSAGE_TYPE:
                this.OnRespondToAllianceJoinRequestMessageReceived((RespondToAllianceJoinRequestMessage)message);
                break;

            case SendAllianceInvitationMessage.MESSAGE_TYPE:
                this.OnSendAllianceInvitationMessageReceived((SendAllianceInvitationMessage)message);
                break;
            }
        }
        private static ForwardLogicMessage CreateForwardLogicMessage(PiranhaMessage piranhaMessage, long sessionId)
        {
            if (piranhaMessage.GetEncodingLength() == 0)
            {
                piranhaMessage.Encode();
            }

            return(new ForwardLogicMessage
            {
                SessionId = sessionId,
                MessageType = piranhaMessage.GetMessageType(),
                MessageVersion = (short)piranhaMessage.GetMessageVersion(),
                MessageLength = piranhaMessage.GetEncodingLength(),
                MessageBytes = piranhaMessage.GetMessageBytes()
            });
        }
        public void SendPiranhaMessageToProxy(PiranhaMessage piranhaMessage)
        {
            if (piranhaMessage.GetEncodingLength() == 0)
            {
                piranhaMessage.Encode();
            }

            ServerMessageManager.SendMessage(new ForwardLogicMessage
            {
                SessionId      = this.SessionId,
                MessageType    = piranhaMessage.GetMessageType(),
                MessageVersion = (short)piranhaMessage.GetMessageVersion(),
                MessageLength  = piranhaMessage.GetEncodingLength(),
                MessageBytes   = piranhaMessage.GetMessageBytes()
            }, ServerManager.GetProxySocket(this.SessionId));
        }
        private static void OnForwardLogicMessageRequestMessageReceived(ForwardLogicRequestMessage message)
        {
            PiranhaMessage logicMessage = LogicMagicMessageFactory.Instance.CreateMessageByType(message.MessageType);

            if (logicMessage == null)
            {
                throw new Exception("logicMessage should not be NULL!");
            }

            logicMessage.GetByteStream().SetByteArray(message.MessageBytes, message.MessageLength);
            logicMessage.SetMessageVersion(message.MessageVersion);
            logicMessage.Decode();

            if (!logicMessage.IsServerToClientMessage())
            {
                switch (logicMessage.GetMessageType())
                {
                case AskForAvatarProfileMessage.MESSAGE_TYPE:
                {
                    AskForAvatarProfileMessage askForAvatarProfileMessage = (AskForAvatarProfileMessage)logicMessage;
                    LogicLong avatarId = askForAvatarProfileMessage.RemoveAvatarId();

                    if (GameAvatarManager.TryGet(avatarId, out GameAvatar document))
                    {
                        AvatarProfileMessage   avatarProfileMessage   = new AvatarProfileMessage();
                        AvatarProfileFullEntry avatarProfileFullEntry = new AvatarProfileFullEntry();

                        avatarProfileFullEntry.SetLogicClientAvatar(document.LogicClientAvatar);
                        avatarProfileFullEntry.SetCompressedHomeJSON(document.LogicClientHome.GetCompressibleHomeJSON().GetCompressed());

                        avatarProfileMessage.SetAvatarProfileFullEntry(avatarProfileFullEntry);

                        ServerMessageManager.SendMessage(GameMessageManager.CreateForwardLogicMessage(avatarProfileMessage, message.SessionId), ServerManager.GetProxySocket(message.SessionId));
                    }
                    else
                    {
                        AvatarProfileFailedMessage avatarProfileFailedMessage = new AvatarProfileFailedMessage();
                        avatarProfileFailedMessage.SetErrorType(AvatarProfileFailedMessage.ErrorType.NOT_FOUND);
                        ServerMessageManager.SendMessage(GameMessageManager.CreateForwardLogicMessage(avatarProfileFailedMessage, message.SessionId),
                                                         ServerManager.GetProxySocket(message.SessionId));
                    }

                    break;
                }
                }
            }
        }
Ejemplo n.º 17
0
        public void ReceiveMessage(PiranhaMessage piranhaMessage)
        {
            switch (piranhaMessage.GetMessageType())
            {
            case LoginFailedMessage.MESSAGE_TYPE:
                this.OnLoginFailedMessageReceived((LoginFailedMessage)piranhaMessage);
                break;

            case LoginOkMessage.MESSAGE_TYPE:
                this.OnLoginOkMessageReceived((LoginOkMessage)piranhaMessage);
                break;

            case KeepAliveServerMessage.MESSAGE_TYPE:
                this.OnKeepAliveServerMessageReceived((KeepAliveServerMessage)piranhaMessage);
                break;
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        ///     Writes the message header to buffer.
        /// </summary>
        internal static void WriteHeader(PiranhaMessage message, byte[] buffer, int length)
        {
            int messageType    = message.GetMessageType();
            int messageVersion = message.GetMessageVersion();

            buffer[1] = (byte)messageType;
            buffer[0] = (byte)(messageType >> 8);
            buffer[4] = (byte)length;
            buffer[3] = (byte)(length >> 8);
            buffer[2] = (byte)(length >> 16);
            buffer[6] = (byte)messageVersion;
            buffer[5] = (byte)(messageVersion >> 8);

            if (length > 0xFFFFFF)
            {
                Debugger.Error("Trying to send too big message, type " + messageType);
            }
        }
Ejemplo n.º 19
0
        private static void WriteHeader(PiranhaMessage message, byte[] stream, int length)
        {
            int messageType    = message.GetMessageType();
            int messageVersion = message.GetMessageVersion();

            stream[0] = (byte)(messageType >> 8);
            stream[1] = (byte)(messageType);
            stream[2] = (byte)(length >> 16);
            stream[3] = (byte)(length >> 8);
            stream[4] = (byte)(length);
            stream[5] = (byte)(messageVersion >> 8);
            stream[6] = (byte)(messageVersion);

            if (length > 0xFFFFFF)
            {
                Debugger.Error("NetworkMessaging::writeHeader trying to send too big message, type " + messageType);
            }
        }
        private void SendForwardLogicMessageRequestMessage(PiranhaMessage piranhaMessage, ServerSocket socket)
        {
            if (socket != null)
            {
                if (this.IsRequestPiranhaMessage(piranhaMessage) && !this.CanSendRequest(piranhaMessage.GetMessageType()))
                {
                    return;
                }

                ServerMessageManager.SendMessage(new ForwardLogicRequestMessage
                {
                    SessionId      = this.m_connection.Session.Id,
                    AccountId      = this.m_connection.Session.AccountId,
                    MessageType    = piranhaMessage.GetMessageType(),
                    MessageVersion = (short)piranhaMessage.GetMessageVersion(),
                    MessageLength  = piranhaMessage.GetEncodingLength(),
                    MessageBytes   = piranhaMessage.GetMessageBytes()
                }, socket);
            }
        }
Ejemplo n.º 21
0
        public void ReceiveMessage(PiranhaMessage message)
        {
            switch (message.GetMessageType())
            {
            case GoHomeMessage.MESSAGE_TYPE:
                this.OnGoHomeMessageReceived((GoHomeMessage)message);
                break;

            case EndClientTurnMessage.MESSAGE_TYPE:
                this.OnEndClientTurnMessageReceived((EndClientTurnMessage)message);
                break;

            case VisitHomeMessage.MESSAGE_TYPE:
                this.OnVisitHomeMessageReceived((VisitHomeMessage)message);
                break;

            case AttackNpcMessage.MESSAGE_TYPE:
                this.OnAttackNpcMessageReceived((AttackNpcMessage)message);
                break;

            case DuelNpcMessage.MESSAGE_TYPE:
                this.OnDuelNpcMessageReceived((DuelNpcMessage)message);
                break;

            case CreateAllianceMessage.MESSAGE_TYPE:
                this.OnCreateAllianceMessageReceived((CreateAllianceMessage)message);
                break;

            case JoinAllianceMessage.MESSAGE_TYPE:
                this.OnJoinAllianceMessageReceived((JoinAllianceMessage)message);
                break;

            case RequestJoinAllianceMessage.MESSAGE_TYPE:
                this.OnRequestJoinAllianceMessageReceived((RequestJoinAllianceMessage)message);
                break;

            case JoinAllianceUsingInvitationMessage.MESSAGE_TYPE:
                this.OnJoinAllianceUsingInvitationMessageReceived((JoinAllianceUsingInvitationMessage)message);
                break;
            }
        }
Ejemplo n.º 22
0
        /// <summary>
        ///     Receives the specified message.
        /// </summary>
        internal void ReceiveMessage(PiranhaMessage message)
        {
            int messageType = message.GetMessageType();

            if (this.ServerConnection.State != 6)
            {
                if (messageType != 20103 && messageType != 20104)
                {
                    return;
                }
            }

            switch (messageType)
            {
            case 20103:
            {
                this.LoginFailedMessageReceived((LoginFailedMessage)message);
                break;
            }
            }
        }
        private static void OnForwardLogicRequestMessageReceived(ForwardLogicRequestMessage message)
        {
            PiranhaMessage logicMessage = LogicMagicMessageFactory.Instance.CreateMessageByType(message.MessageType);

            if (logicMessage == null)
            {
                throw new Exception("logicMessage should not be NULL!");
            }

            logicMessage.GetByteStream().SetByteArray(message.MessageBytes, message.MessageLength);
            logicMessage.SetMessageVersion(message.MessageVersion);
            logicMessage.Decode();

            if (!logicMessage.IsServerToClientMessage())
            {
                switch (logicMessage.GetMessageType())
                {
                case AskForAllianceRankingListMessage.MESSAGE_TYPE:
                    ScoringMessageManager.OnAskForAllianceRankingListMessageReceived((AskForAllianceRankingListMessage)logicMessage, message);
                    break;

                case AskForAvatarRankingListMessage.MESSAGE_TYPE:
                    ScoringMessageManager.OnAskForAvatarRankingListMessageReceived((AskForAvatarRankingListMessage)logicMessage, message);
                    break;

                case AskForAvatarLocalRankingListMessage.MESSAGE_TYPE:
                    ScoringMessageManager.OnAskForLocalAvatarRankingListMessageReceived((AskForAvatarLocalRankingListMessage)logicMessage, message);
                    break;

                case AskForAvatarLastSeasonRankingListMessage.MESSAGE_TYPE:
                    ScoringMessageManager.OnAskForAvatarLastSeasonRankingListMessageReceived((AskForAvatarLastSeasonRankingListMessage)logicMessage, message);
                    break;

                case AskForAvatarDuelLastSeasonRankingListMessage.MESSAGE_TYPE:
                    ScoringMessageManager.OnAskForAvatarDuelLastSeasonRankingListMessageReceived((AskForAvatarDuelLastSeasonRankingListMessage)logicMessage, message);
                    break;
                }
            }
        }
        public void ReceiveMessage(PiranhaMessage message)
        {
            switch (message.GetMessageType())
            {
            case ChangeAvatarNameMessage.MESSAGE_TYPE:
                this.OnChangeAvatarNameMessageReceived((ChangeAvatarNameMessage)message);
                break;

            case AvatarNameCheckRequestMessage.MESSAGE_TYPE:
                this.OnAvatarNameCheckRequestMessageReceived((AvatarNameCheckRequestMessage)message);
                break;

            case CancelMatchmakingMessage.MESSAGE_TYPE:
                this.OnCancelMatchmakingMessageReceived((CancelMatchmakingMessage)message);
                break;

            case HomeBattleReplayMessage.MESSAGE_TYPE:
                this.OnHomeBattleReplayMessageReceived((HomeBattleReplayMessage)message);
                break;

            case AskForAllianceBookmarksFullDataMessage.MESSAGE_TYPE:
                this.OnAskForAllianceBookmarksFullDataMessageReceived((AskForAllianceBookmarksFullDataMessage)message);
                break;

            case AddAllianceBookmarkMessage.MESSAGE_TYPE:
                this.OnAddAllianceBookmarkMessageReceived((AddAllianceBookmarkMessage)message);
                break;

            case RemoveAllianceBookmarkMessage.MESSAGE_TYPE:
                this.OnRemoveAllianceBookmarkMessageReceived((RemoveAllianceBookmarkMessage)message);
                break;

            case RemoveAvatarStreamEntryMessage.MESSAGE_TYPE:
                this.OnRemoveAvatarStreamEntryMessageReceived((RemoveAvatarStreamEntryMessage)message);
                break;
            }
        }
Ejemplo n.º 25
0
        public void InternalSend(PiranhaMessage message)
        {
            if (message.GetEncodingLength() == 0)
            {
                message.Encode();
            }

            int encodingLength = message.GetEncodingLength();
            int encryptedLength;

            byte[] encodingBytes = message.GetMessageBytes();
            byte[] encryptedBytes;

            if (this.m_sendEncrypter != null)
            {
                if (!this.m_encryptionScrambled && message.GetMessageType() == LoginOkMessage.MESSAGE_TYPE)
                {
                    byte[] nonce = Messaging.CreateNonce();

                    ExtendedSetEncryptionMessage extendedSetEncryptionMessage = new ExtendedSetEncryptionMessage();

                    extendedSetEncryptionMessage.SetNonce(nonce);
                    extendedSetEncryptionMessage.SetNonceMethod(Messaging.NONCE_METHOD);

                    this.InternalSend(extendedSetEncryptionMessage);
                    this.SetEncryption(extendedSetEncryptionMessage);
                }

                encryptedLength = encodingLength + this.m_sendEncrypter.GetOverheadEncryption();
                encryptedBytes  = new byte[encryptedLength];

                this.EncryptUsingEncrypter(encodingBytes, encryptedBytes, encodingLength);
            }
            else if (this.m_pepperState != PepperState.DISABLED)
            {
                if (this.m_pepperState == PepperState.AUTHENTIFICATION)
                {
                    if (message.GetMessageType() == ServerHelloMessage.MESSAGE_TYPE)
                    {
                        this.m_pepperState = PepperState.AUTHENTIFICATION_SERVER;
                    }

                    encryptedLength = encodingLength;
                    encryptedBytes  = encodingBytes;
                }
                else if (this.m_pepperState == PepperState.LOGIN)
                {
                    throw new NotImplementedException();
                }
                else
                {
                    encryptedLength = encodingLength;
                    encryptedBytes  = encodingBytes;
                }
            }
            else
            {
                encryptedBytes  = encodingBytes;
                encryptedLength = encodingLength;
            }

            byte[] stream = new byte[encryptedLength + Messaging.HEADER_SIZE];

            Messaging.WriteHeader(message, stream, encryptedLength);
            Buffer.BlockCopy(encryptedBytes, 0, stream, Messaging.HEADER_SIZE, encryptedLength);

            this.m_clientConnection.Send(stream, encryptedLength + Messaging.HEADER_SIZE);
        }
        public void ReceiveMessage(PiranhaMessage message)
        {
            int messageType            = message.GetMessageType();
            int messageServiceNodeType = message.GetServiceNodeType();

            ClientConnectionState state = this.m_connection.State;

            if (state != ClientConnectionState.LOGGED)
            {
                if (state == ClientConnectionState.DISCONNECTED)
                {
                    return;
                }
                if (messageType != ClientHelloMessage.MESSAGE_TYPE && messageType != LoginMessage.MESSAGE_TYPE &&
                    messageType != KeepAliveMessage.MESSAGE_TYPE && messageType != UnlockAccountMessage.MESSAGE_TYPE)
                {
                    return;
                }
            }

            Logging.Print("MessageManager.receiveMessage: " + message.GetType().Name);

            if (messageServiceNodeType == 1)
            {
                switch (messageType)
                {
                case ClientHelloMessage.MESSAGE_TYPE:
                    this.ClientHelloMessageReceived((ClientHelloMessage)message);
                    break;

                case LoginMessage.MESSAGE_TYPE:
                    this.LoginMessageReceived((LoginMessage)message);
                    break;

                case KeepAliveMessage.MESSAGE_TYPE:
                    this.KeepAliveMessageReceived((KeepAliveMessage)message);
                    break;

                case UnlockAccountMessage.MESSAGE_TYPE:
                    this.UnlockAccountMessageReceived((UnlockAccountMessage)message);
                    break;

                case BindFacebookAccountMessage.MESSAGE_TYPE:
                    this.OnBindFacebookAccountMessageReceived((BindFacebookAccountMessage)message);
                    break;
                }
            }
            else
            {
                if (state != ClientConnectionState.LOGGED)
                {
                    return;
                }

                ProxySession session = this.m_connection.Session;

                if (session == null)
                {
                    return;
                }

                if (this.IsRequestPiranhaMessage(message))
                {
                    switch (messageType)
                    {
                    case AskForAvatarProfileMessage.MESSAGE_TYPE:
                        this.SendForwardLogicMessageRequestMessage(message, ServerManager.GetDocumentSocket(9, ((AskForAvatarProfileMessage)message).RemoveAvatarId()));
                        break;

                    case AskForAllianceDataMessage.MESSAGE_TYPE:
                        this.SendForwardLogicMessageRequestMessage(message, ServerManager.GetDocumentSocket(11, ((AskForAllianceDataMessage)message).RemoveAllianceId()));
                        break;
                    }
                }
                else if (messageServiceNodeType == 28 || messageServiceNodeType == 29)
                {
                    this.SendForwardLogicMessageRequestMessage(message, ServerManager.GetNextSocket(messageServiceNodeType));
                }
                else
                {
                    session.SendPiranhaMessage(message, messageServiceNodeType);
                }
            }
        }
Ejemplo n.º 27
0
        private int OnReceive(byte[] buffer, int length)
        {
            if (length >= Messaging.PACKET_HEADER_LENGTH)
            {
                Messaging.ReadHeader(buffer, out int messageType, out int messageLength, out int messageVersion);

                if (length >= Messaging.PACKET_HEADER_LENGTH + messageLength)
                {
                    byte[] encryptedBytes = new byte[messageLength];
                    byte[] encodingBytes;

                    Buffer.BlockCopy(buffer, Messaging.PACKET_HEADER_LENGTH, encryptedBytes, 0, messageLength);

                    int encodingLength;

                    if (this.m_receiveEncrypter != null)
                    {
                        encodingLength = messageLength - this.m_receiveEncrypter.GetOverheadEncryption();
                        encodingBytes  = new byte[encodingLength];

                        this.m_receiveEncrypter.Decrypt(encryptedBytes, encodingBytes, encodingLength);
                    }
                    else
                    {
                        encodingLength = messageLength;
                        encodingBytes  = encryptedBytes;
                    }

                    PiranhaMessage piranhaMessage = this.m_messageFactory.CreateMessageByType(messageType);

                    if (piranhaMessage != null)
                    {
                        piranhaMessage.SetMessageVersion(messageVersion);
                        piranhaMessage.GetByteStream().SetByteArray(encodingBytes, encodingLength);
                        piranhaMessage.Decode();

                        if (piranhaMessage.GetMessageType() != ExtendedSetEncryptionMessage.MESSAGE_TYPE)
                        {
                            this.m_receiveQueue.Enqueue(piranhaMessage);
                        }
                        else
                        {
                            ExtendedSetEncryptionMessage extendedSetEncryptionMessage = (ExtendedSetEncryptionMessage)piranhaMessage;
                            byte[] nonce = extendedSetEncryptionMessage.RemoveNonce();

                            switch (extendedSetEncryptionMessage.GetNonceMethod())
                            {
                            case 1:
                                this.ScrambleNonceUsingMersenneTwister(nonce);
                                break;

                            default:
                                this.ScrambleNonceUsingDefaultMethod(nonce);
                                break;
                            }

                            char[] nonceChars = new char[nonce.Length];
                            for (int i = 0; i < nonce.Length; i++)
                            {
                                nonceChars[i] = (char)nonce[i];
                            }
                            this.InitEncrypters(new string(nonceChars));
                        }
                    }
                    else
                    {
                        Debugger.Warning(string.Format("Messaging.onReceive: ignoring message of unknown type {0}", messageType));
                    }

                    return(Messaging.PACKET_HEADER_LENGTH + messageLength);
                }
            }

            return(0);
        }
Ejemplo n.º 28
0
        /// <summary>
        ///     Called when the gateway receive a packet.
        /// </summary>
        internal int OnReceive(byte[] packet, int length)
        {
            if (length >= 7)
            {
                length -= 7;

                int messageType    = packet[1] | packet[0] << 8;
                int messageLength  = packet[4] | packet[3] << 8 | packet[2] << 16;
                int messageVersion = packet[6] | packet[5] << 8;

                if (length >= messageLength)
                {
                    int    encodingLength    = messageLength;
                    byte[] encodingByteArray = new byte[messageLength];
                    Array.Copy(packet, 7, encodingByteArray, 0, messageLength);

                    if (this._receiveEncrypter != null)
                    {
                        byte[] encryptedByteArray = encodingByteArray;
                        byte[] decryptedByteArray = new byte[messageLength - this._receiveEncrypter.GetOverheadEncryption()];

                        this._receiveEncrypter.Decrypt(encryptedByteArray, decryptedByteArray, messageLength);

                        encodingByteArray = decryptedByteArray;
                        encodingLength   -= this._receiveEncrypter.GetOverheadEncryption();
                    }

                    PiranhaMessage message = LogicMagicMessageFactory.Instance.CreateMessageByType(messageType);

                    if (message != null)
                    {
                        message.SetMessageVersion((short)messageVersion);
                        message.GetByteStream().SetByteArray(encodingByteArray, encodingLength);

                        try
                        {
                            message.Decode();

                            if (message.GetMessageType() == 20000)
                            {
                                ExtendedSetEncryptionMessage extendedSetEncryptionMessage = (ExtendedSetEncryptionMessage)message;
                                LogicMersenneTwisterRandom   scrambler = new LogicMersenneTwisterRandom(0);

                                byte byte100 = 0;

                                for (int i = 0; i < 100; i++)
                                {
                                    byte100 = (byte)scrambler.NextInt();
                                }

                                byte[] nonce          = extendedSetEncryptionMessage.RemoveNonce();
                                string scrambledNonce = null;

                                for (int i = 0; i < nonce.Length; i++)
                                {
                                    scrambledNonce += (char)(nonce[i] ^ (byte)(scrambler.NextInt() & byte100));
                                }

                                if (this._receiveEncrypter != null)
                                {
                                    this._receiveEncrypter.Destruct();
                                    this._receiveEncrypter = null;
                                }

                                if (this._sendEncrypter != null)
                                {
                                    this._sendEncrypter.Destruct();
                                    this._sendEncrypter = null;
                                }

                                this._receiveEncrypter = new RC4Encrypter(LogicMessagingConstants.RC4_KEY, scrambledNonce);
                                this._sendEncrypter    = new RC4Encrypter(LogicMessagingConstants.RC4_KEY, scrambledNonce);
                            }
                            else
                            {
                                this.MessageManager.ReceiveMessage(message);
                            }
                        }
                        catch (Exception exception)
                        {
                            Logging.Error("Client::onReceive message decode exception, trace: " + exception);
                        }

                        Logging.Print("Client::sendMessage message " + message.GetType().Name + " received");
                    }
                    else
                    {
                        Logging.Warning("NetworkMessaging::onReceive Ignoring message of unknown type " + messageType);
                    }

                    return(messageLength + 7);
                }
            }

            return(0);
        }