Search in sources :

Example 11 with DataOutput

use of im.actor.runtime.bser.DataOutput in project actor-platform by actorapp.

the class ManagerActor method onOutMessage.

private void onOutMessage(byte[] data, int offset, int len) {
    // Cleanup bad connection
    if (currentConnection != null && currentConnection.isClosed()) {
        currentConnection = null;
        // Log.d(TAG, "Set connection #" + 0);
        currentConnectionId = 0;
        outSeq = 0;
        inSeq = 0;
        checkConnection();
    }
    try {
        if (currentConnection != null) {
            if (authKey != null) {
                int seq = outSeq++;
                // long start = Runtime.getActorTime();
                byte[] ruIv = new byte[16];
                Crypto.nextBytes(ruIv);
                byte[] usIv = new byte[16];
                Crypto.nextBytes(usIv);
                byte[] ruCipherText = clientRUEncryptor.encryptPackage(ByteStrings.longToBytes(seq), ruIv, ByteStrings.substring(data, offset, len));
                byte[] ruPackage = new EncryptedCBCPackage(ruIv, ruCipherText).toByteArray();
                byte[] usCipherText = clientUSEncryptor.encryptPackage(ByteStrings.longToBytes(seq), usIv, ruPackage);
                byte[] usPackage = new EncryptedCBCPackage(usIv, usCipherText).toByteArray();
                EncryptedPackage encryptedPackage = new EncryptedPackage(seq, usPackage);
                byte[] cipherData = encryptedPackage.toByteArray();
                DataOutput bos = new DataOutput();
                bos.writeLong(authId);
                bos.writeLong(sessionId);
                bos.writeBytes(cipherData, 0, cipherData.length);
                byte[] pkg = bos.toByteArray();
                currentConnection.post(pkg, 0, pkg.length);
            // Log.d(TAG, "Package encrypted in " + (Runtime.getActorTime() - start) + " ms, size: " + len);
            } else {
                DataOutput bos = new DataOutput();
                bos.writeLong(authId);
                bos.writeLong(sessionId);
                bos.writeBytes(data, offset, len);
                byte[] pkg = bos.toByteArray();
                currentConnection.post(pkg, 0, pkg.length);
            }
        // Log.d(TAG, "Posted message to connection #" + currentConnectionId);
        }
    } catch (IOException e) {
        Log.w(TAG, "Closing connection: exception during push");
        Log.e(TAG, e);
        if (currentConnection != null) {
            try {
                currentConnection.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            currentConnection = null;
            currentConnectionId = 0;
            outSeq = 0;
            inSeq = 0;
        // Log.d(TAG, "Set connection #" + 0);
        }
        checkConnection();
    }
}
Also used : DataOutput(im.actor.runtime.bser.DataOutput) EncryptedPackage(im.actor.core.network.mtp.entity.EncryptedPackage) EncryptedCBCPackage(im.actor.core.network.mtp.entity.EncryptedCBCPackage) IOException(java.io.IOException) IOException(java.io.IOException)

Example 12 with DataOutput

use of im.actor.runtime.bser.DataOutput in project actor-platform by actorapp.

the class ManagedConnection method rawPost.

private synchronized void rawPost(int header, byte[] data, int offset, int len) {
    // Log.w(TAG, "rawPost");
    int packageId = sentPackages++;
    DataOutput dataOutput = new DataOutput();
    dataOutput.writeInt(packageId);
    dataOutput.writeByte(header);
    dataOutput.writeInt(data.length);
    dataOutput.writeBytes(data, offset, len);
    CRC32_ENGINE.reset();
    CRC32_ENGINE.update(data, offset, len);
    dataOutput.writeInt((int) CRC32_ENGINE.getValue());
    if (header == HEADER_PROTO) {
        CommonTimer timeoutTask = new CommonTimer(new TimeoutRunnable());
        packageTimers.put(packageId, timeoutTask);
        timeoutTask.schedule(RESPONSE_TIMEOUT);
    }
    rawConnection.doSend(dataOutput.toByteArray());
}
Also used : DataOutput(im.actor.runtime.bser.DataOutput) CommonTimer(im.actor.runtime.threading.CommonTimer)

Aggregations

DataOutput (im.actor.runtime.bser.DataOutput)12 BserWriter (im.actor.runtime.bser.BserWriter)4 IOException (java.io.IOException)3 CommonTimer (im.actor.runtime.threading.CommonTimer)2 EncryptedCBCPackage (im.actor.core.network.mtp.entity.EncryptedCBCPackage)1 EncryptedPackage (im.actor.core.network.mtp.entity.EncryptedPackage)1 Digest (im.actor.runtime.crypto.Digest)1