예제 #1
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");
        }
예제 #2
0
        public async Task Send(PiranhaMessage message)
        {
            try
            {
                var asyncEvent = GetArgs;

                await message.Encode();

                message.Encrypt();

                asyncEvent.SetBuffer(await message.BuildPacket(), 0, message.Length + 7);

                asyncEvent.AcceptSocket   = message.Device.Socket;
                asyncEvent.RemoteEndPoint = message.Device.Socket.RemoteEndPoint;
                asyncEvent.UserToken      = message.Device.Token;

                await StartSend(asyncEvent);

                message.Dispose();
            }
            catch (Exception exception)
            {
                Disconnect(message.Device.Token.EventArgs);
                Logger.Log(exception, Enums.LogType.Error);
            }
        }
예제 #3
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);
 }
        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));
        }
예제 #6
0
        /// <summary>
        ///     Forwards the specified <see cref="PiranhaMessage"/> to the service.
        /// </summary>
        public void  SendPiranhaMessage(int serviceNodeType, PiranhaMessage message)
        {
            NetSocket socket = this._serviceNodeSockets[serviceNodeType];

            if (socket != null)
            {
                if (message.GetEncodingLength() == 0)
                {
                    message.Encode();
                }

                ForwardPiranhaMessage forwardPiranhaMessage = new ForwardPiranhaMessage();
                forwardPiranhaMessage.SetPiranhaMessage(message);
                NetMessageManager.SendMessage(socket, this.SessionId, forwardPiranhaMessage);
            }
        }
        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);
        }