Beispiel #1
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);
 }
Beispiel #2
0
        /// <summary>
        ///     Sends the specified message to server.
        /// </summary>
        internal void SendMessage(PiranhaMessage message)
        {
            message.Encode();

            byte[] encodingByteArray = message.GetByteStream().RemoveByteArray();
            int    encodingLength    = message.GetEncodingLength();

            if (this._sendEncrypter != null)
            {
                byte[] decryptedByteArray = encodingByteArray;
                byte[] encryptedByteArray = new byte[encodingLength + this._sendEncrypter.GetOverheadEncryption()];

                this._sendEncrypter.Encrypt(decryptedByteArray, encryptedByteArray, encodingLength);

                encodingLength   += this._sendEncrypter.GetOverheadEncryption();
                encodingByteArray = encryptedByteArray;
            }

            byte[] packet = new byte[7 + encodingLength];

            Array.Copy(encodingByteArray, 0, packet, 7, encodingLength);
            this.WriteHeader(message, packet, encodingLength);
            this._networkGateway.Send(packet, encodingLength + 7);

            Logging.Print("Client::sendMessage message " + message.GetType().Name + " sent");
        }
Beispiel #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;
                }
            }
        }
Beispiel #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;
                }
            }
        }
Beispiel #5
0
        private static void OnForwardLogicMessageReceived(ForwardLogicMessage message)
        {
            if (ProxySessionManager.TryGet(message.SessionId, out ProxySession session))
            {
                PiranhaMessage piranhaMessage = LogicMagicMessageFactory.Instance.CreateMessageByType(message.MessageType);

                if (piranhaMessage == null)
                {
                    Logging.Error("ProxyMessageManager.onForwardLogicMessageReceived: unknown logic message type: " + message.MessageType);
                    return;
                }

                piranhaMessage.SetMessageVersion(message.MessageVersion);
                piranhaMessage.GetByteStream().SetByteArray(message.MessageBytes, message.MessageLength);
                piranhaMessage.GetByteStream().SetOffset(message.MessageLength);

                session.ClientConnection.Messaging.Send(piranhaMessage);
            }
        }
        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;
                }
                }
            }
        }
        private static void OnForwardLogicMessageReceived(ForwardLogicMessage message)
        {
            if (ChatSessionManager.TryGet(message.SessionId, out ChatSession session))
            {
                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())
                {
                    session.LogicMessageManager.ReceiveMessage(logicMessage);
                }
            }
        }
        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;
                }
            }
        }
        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);
        }
Beispiel #10
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);
        }
Beispiel #11
0
        /// <summary>
        ///     Called when the connection receive a block of bytes.
        /// </summary>
        public void OnReceive(Connection connection)
        {
            byte[] buffer = this._connection.Buffer;
            int    length = buffer.Length;

            if (length >= 7)
            {
                length -= 7;

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

                if (length >= messageLength)
                {
                    this._connection.RemoveBlocking(messageLength + 7);

                    byte[] encodingByteArray = new byte[messageLength];
                    Array.Copy(buffer, 7, encodingByteArray, 0, messageLength);

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

                        Array.Copy(buffer, 7, encryptedByteArray, 0, messageLength);

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

                        encodingByteArray = decryptedByteArray;
                    }

                    PiranhaMessage message = LogicMagicMessageFactory.CreateMessageByType(messageType);

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

                        if (this._pepperState != 1)
                        {
                            if (this._pepperState == 2)
                            {
                                this._receiveMessageQueue.Enqueue(message);
                            }

                            message.Decode();

                            this._receiveMessageQueue.Enqueue(message);

                            Debugger.Log("Messaging::onReceive message " + message.GetType().Name + " received");
                        }
                    }
                    else
                    {
                        Debugger.Warning("Messaging::onReceive ignoring message of unknown type " + messageType);
                    }

                    if (length - messageLength >= 7)
                    {
                        this.OnReceive(connection);
                    }
                }
            }
        }
        public int OnReceive(byte[] buffer, int length)
        {
            if (length >= Messaging.HEADER_SIZE)
            {
                Messaging.ReadHeader(buffer, out int messageType, out int messageLength, out int messageVersion);

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

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

                    int encodingLength;

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

                        this.DecryptUsingEncrypter(encryptedBytes, encodingBytes, messageLength);
                    }
                    else
                    {
                        if (this.m_pepperState == PepperState.DEFAULT)
                        {
                            if (messageType == ClientHelloMessage.MESSAGE_TYPE)
                            {
                                this.m_pepperState = PepperState.AUTHENTIFICATION;

                                encodingLength = messageLength;
                                encodingBytes  = encryptedBytes;
                            }
                            else if (messageType == LoginMessage.MESSAGE_TYPE)
                            {
                                this.m_pepperState = PepperState.DISABLED;
                                this.InitRC4Encryption(this.GetDefaultNonce());

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

                                this.DecryptUsingEncrypter(encryptedBytes, encodingBytes, messageLength);
                            }
                            else
                            {
                                return(messageLength + Messaging.HEADER_SIZE);
                            }
                        }
                        else if (this.m_pepperState == PepperState.AUTHENTIFICATION_SERVER)
                        {
                            if (messageType != LoginMessage.MESSAGE_TYPE)
                            {
                                return(messageLength + Messaging.HEADER_SIZE);
                            }
                            throw new NotImplementedException();
                        }
                        else
                        {
                            return(messageLength + Messaging.HEADER_SIZE);
                        }
                    }

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

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

                        try
                        {
                            piranhaMessage.Decode();

                            if (!piranhaMessage.IsServerToClientMessage())
                            {
                                MessageHandler.EnqueueReceive(piranhaMessage, this.m_clientConnection);
                            }
                        }
                        catch (Exception exception)
                        {
                            Logging.Error(string.Format("Messaging::onReceive: error while the decoding of message type {0}, trace: {1}", messageType, exception));
                        }
                    }
                    else
                    {
                        Logging.Warning(string.Format("Messaging::onReceive: ignoring message of unknown type {0}", messageType));
                    }

                    return(messageLength + Messaging.HEADER_SIZE);
                }
                else
                {
                    int httpHeader = buffer[0] << 16 | buffer[1] << 8 | buffer[2];

                    if (httpHeader == 0x474554) // httpHeader == GET
                    {
                        return(-1);
                    }
                }
            }

            return(0);
        }