Search in sources :

Example 1 with DataInput

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

the class AbsLocalContent method loadContainer.

public static AbsLocalContent loadContainer(byte[] data) throws IOException {
    BserValues values = new BserValues(BserParser.deserialize(new DataInput(data)));
    int type = values.getInt(1);
    byte[] content = values.getBytes(2);
    if (type == CONTENT_DOC) {
        return new LocalDocument(content);
    } else if (type == CONTENT_PHOTO) {
        return new LocalPhoto(content);
    } else if (type == CONTENT_VIDEO) {
        return new LocalVideo(content);
    } else if (type == CONTENT_VOICE) {
        return new LocalVoice(content);
    } else if (type == CONTENT_ANIMATION) {
        return new LocalAnimation(content);
    } else {
        throw new IOException("Unknown type");
    }
}
Also used : DataInput(im.actor.runtime.bser.DataInput) BserValues(im.actor.runtime.bser.BserValues) IOException(java.io.IOException)

Example 2 with DataInput

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

the class AbsSignal method fromBytes.

public static AbsSignal fromBytes(byte[] data) {
    try {
        BserValues values = new BserValues(BserParser.deserialize(new DataInput(data, 0, data.length)));
        String type = values.getString(1);
        AbsSignal res;
        if (OfferSignal.TYPE.equals(type)) {
            res = new OfferSignal();
        } else if (AnswerSignal.TYPE.equals(type)) {
            res = new AnswerSignal();
        } else if (CandidateSignal.TYPE.equals(type)) {
            res = new CandidateSignal();
        } else {
            throw new IOException("Unknown signal type " + type);
        }
        res.parse(values);
        return res;
    } catch (IOException e) {
        return null;
    }
}
Also used : DataInput(im.actor.runtime.bser.DataInput) BserValues(im.actor.runtime.bser.BserValues) IOException(java.io.IOException)

Example 3 with DataInput

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

the class AuthKeyActor method onMessage.

private void onMessage(int connectionId, byte[] data, int offset, int len) {
    if (connectionId != this.connectionId) {
        Log.d(TAG, "Too old: ignoring");
        return;
    }
    ProtoStruct protoStruct;
    try {
        DataInput dataInput = new DataInput(data, offset, len);
        ProtoPackage protoPackage = new ProtoPackage(dataInput);
        if (protoPackage.getAuthId() != 0) {
            throw new IOException("AuthId != 0");
        }
        if (protoPackage.getSessionId() != 0) {
            throw new IOException("Session != 0");
        }
        if (protoPackage.getPayload().getMessageId() != 0) {
            throw new IOException("MessageId != 0");
        }
        protoStruct = ProtoSerializer.readMessagePayload(protoPackage.getPayload().getPayload());
    } catch (IOException e) {
        e.printStackTrace();
        crashConnection();
        return;
    }
    try {
        if (currentState == null) {
            throw new IOException();
        }
        currentState.onMessage(protoStruct);
    } catch (Exception e) {
        e.printStackTrace();
        crashConnection();
    }
}
Also used : DataInput(im.actor.runtime.bser.DataInput) ProtoStruct(im.actor.core.network.mtp.entity.ProtoStruct) ProtoPackage(im.actor.core.network.mtp.entity.ProtoPackage) IOException(java.io.IOException) IOException(java.io.IOException)

Example 4 with DataInput

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

the class ManagerActor method onInMessage.

@AutoreleasePool
private void onInMessage(byte[] data, int offset, int len) {
    // Log.d(TAG, "Received package");
    DataInput bis = new DataInput(data, offset, len);
    try {
        long authId = bis.readLong();
        long sessionId = bis.readLong();
        if (authId != this.authId || sessionId != this.sessionId) {
            throw new IOException("Incorrect header");
        }
        if (authKey != null) {
            EncryptedPackage encryptedPackage = new EncryptedPackage(bis);
            int seq = (int) encryptedPackage.getSeqNumber();
            if (seq != inSeq) {
                throw new IOException("Expected " + inSeq + ", got: " + seq);
            }
            inSeq++;
            // long start = Runtime.getActorTime();
            EncryptedCBCPackage usEncryptedPackage = new EncryptedCBCPackage(new DataInput(encryptedPackage.getEncryptedPackage()));
            byte[] ruPackage = serverUSDecryptor.decryptPackage(ByteStrings.longToBytes(seq), usEncryptedPackage.getIv(), usEncryptedPackage.getEncryptedContent());
            EncryptedCBCPackage ruEncryptedPackage = new EncryptedCBCPackage(new DataInput(ruPackage));
            byte[] plainText = serverRUDecryptor.decryptPackage(ByteStrings.longToBytes(seq), ruEncryptedPackage.getIv(), ruEncryptedPackage.getEncryptedContent());
            // Log.d(TAG, "Package decrypted in " + (Runtime.getActorTime() - start) + " ms, size: " + len);
            DataInput ptInput = new DataInput(plainText);
            long messageId = ptInput.readLong();
            byte[] ptPayload = ptInput.readProtoBytes();
            receiver.send(new ProtoMessage(messageId, ptPayload));
        } else {
            long messageId = bis.readLong();
            byte[] payload = bis.readProtoBytes();
            receiver.send(new ProtoMessage(messageId, payload));
        }
    } catch (IOException e) {
        Log.w(TAG, "Closing connection: incorrect package");
        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 : DataInput(im.actor.runtime.bser.DataInput) EncryptedPackage(im.actor.core.network.mtp.entity.EncryptedPackage) ProtoMessage(im.actor.core.network.mtp.entity.ProtoMessage) EncryptedCBCPackage(im.actor.core.network.mtp.entity.EncryptedCBCPackage) IOException(java.io.IOException) IOException(java.io.IOException) AutoreleasePool(com.google.j2objc.annotations.AutoreleasePool)

Example 5 with DataInput

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

the class ProtoSerializer method readRpcResponsePayload.

public static ProtoStruct readRpcResponsePayload(byte[] data) throws IOException {
    DataInput bs = new DataInput(data, 0, data.length);
    final int header = bs.readByte();
    switch(header) {
        case RpcOk.HEADER:
            return new RpcOk(bs);
        case RpcError.HEADER:
            return new RpcError(bs);
        case RpcFloodWait.HEADER:
            return new RpcFloodWait(bs);
        case RpcInternalError.HEADER:
            return new RpcInternalError(bs);
    }
    throw new IOException("Unable to read proto object");
}
Also used : DataInput(im.actor.runtime.bser.DataInput) RpcOk(im.actor.core.network.mtp.entity.rpc.RpcOk) RpcFloodWait(im.actor.core.network.mtp.entity.rpc.RpcFloodWait) RpcError(im.actor.core.network.mtp.entity.rpc.RpcError) RpcInternalError(im.actor.core.network.mtp.entity.rpc.RpcInternalError) IOException(java.io.IOException)

Aggregations

DataInput (im.actor.runtime.bser.DataInput)14 IOException (java.io.IOException)12 BserValues (im.actor.runtime.bser.BserValues)5 CommonTimer (im.actor.runtime.threading.CommonTimer)2 AutoreleasePool (com.google.j2objc.annotations.AutoreleasePool)1 AbsContentContainer (im.actor.core.entity.content.internal.AbsContentContainer)1 EncryptedCBCPackage (im.actor.core.network.mtp.entity.EncryptedCBCPackage)1 EncryptedPackage (im.actor.core.network.mtp.entity.EncryptedPackage)1 ProtoMessage (im.actor.core.network.mtp.entity.ProtoMessage)1 ProtoPackage (im.actor.core.network.mtp.entity.ProtoPackage)1 ProtoStruct (im.actor.core.network.mtp.entity.ProtoStruct)1 RpcError (im.actor.core.network.mtp.entity.rpc.RpcError)1 RpcFloodWait (im.actor.core.network.mtp.entity.rpc.RpcFloodWait)1 RpcInternalError (im.actor.core.network.mtp.entity.rpc.RpcInternalError)1 RpcOk (im.actor.core.network.mtp.entity.rpc.RpcOk)1 ListEngineRecord (im.actor.runtime.storage.ListEngineRecord)1