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 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);
            }
        }
        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);
        }