Search in sources :

Example 6 with DataInput

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

the class AbsContent method parse.

public static AbsContent parse(byte[] data) throws IOException {
    BserValues reader = new BserValues(BserParser.deserialize(new DataInput(data)));
    AbsContentContainer container;
    // Is New Layout
    if (reader.getBool(32, false)) {
        container = AbsContentContainer.loadContainer(reader.getBytes(33));
    } else {
        throw new RuntimeException("Unsupported obsolete format");
    }
    return convertData(container);
}
Also used : DataInput(im.actor.runtime.bser.DataInput) AbsContentContainer(im.actor.core.entity.content.internal.AbsContentContainer) BserValues(im.actor.runtime.bser.BserValues)

Example 7 with DataInput

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

the class AbsContentContainer method loadContainer.

public static AbsContentContainer 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 == TYPE_LOCAL) {
        return new ContentLocalContainer(AbsLocalContent.loadContainer(content));
    } else if (type == TYPE_REMOTE) {
        return new ContentRemoteContainer(ApiMessage.fromBytes(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 8 with DataInput

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

the class ContactsSyncActor method preStart.

@Override
public void preStart() {
    super.preStart();
    if (ENABLE_LOG) {
        Log.d(TAG, "Loading contacts ids from storage...");
    }
    byte[] data = preferences().getBytes("contact_list");
    if (data != null) {
        try {
            DataInput dataInput = new DataInput(data, 0, data.length);
            int count = dataInput.readInt();
            for (int i = 0; i < count; i++) {
                int uid = dataInput.readInt();
                if (!contacts.contains(uid)) {
                    contacts.add(uid);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    notifyState();
    self().send(new PerformSync());
}
Also used : DataInput(im.actor.runtime.bser.DataInput) IOException(java.io.IOException)

Example 9 with DataInput

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

the class ManagedConnection method onRawReceived.

private synchronized void onRawReceived(byte[] data) {
    if (isClosed) {
        return;
    }
    try {
        DataInput dataInput = new DataInput(data);
        int packageIndex = dataInput.readInt();
        if (receivedPackages != packageIndex) {
            Log.w(TAG, "Invalid package index. Expected: " + receivedPackages + ", got: " + packageIndex);
            throw new IOException("Invalid package index. Expected: " + receivedPackages + ", got: " + packageIndex);
        }
        receivedPackages++;
        int header = dataInput.readByte();
        int dataLength = dataInput.readInt();
        byte[] content = dataInput.readBytes(dataLength);
        int crc32 = dataInput.readInt();
        CRC32_ENGINE.reset();
        CRC32_ENGINE.update(content);
        if (((int) CRC32_ENGINE.getValue()) != crc32) {
            Log.w(TAG, "Incorrect CRC32");
            throw new IOException("Incorrect CRC32");
        }
        // Log.w(TAG, "Received package: " + header);
        if (header == HEADER_HANDSHAKE_RESPONSE) {
            if (isHandshakePerformed) {
                throw new IOException("Double Handshake");
            }
            onHandshakePackage(content);
        } else {
            if (!isHandshakePerformed) {
                throw new IOException("Package before Handshake");
            }
            if (header == HEADER_PROTO) {
                onProtoPackage(content);
                sendAckPackage(packageIndex);
            } else if (header == HEADER_PING) {
                onPingPackage(content);
            } else if (header == HEADER_PONG) {
                onPongPackage(content);
            } else if (header == HEADER_DROP) {
                onDropPackage(content);
            } else if (header == HEADER_ACK) {
                onAckPackage(content);
            } else {
                Log.w(TAG, "Received unknown package #" + header);
            }
        }
    } catch (IOException e) {
        Log.e(TAG, e);
        close();
    }
// Log.w(TAG, "onRawReceived:end");
}
Also used : DataInput(im.actor.runtime.bser.DataInput) IOException(java.io.IOException)

Example 10 with DataInput

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

the class ManagedConnection method onPongPackage.

private synchronized void onPongPackage(byte[] data) throws IOException {
    DataInput dataInput = new DataInput(data);
    int size = dataInput.readInt();
    if (size != 8) {
        Log.w(TAG, "Received incorrect pong");
        throw new IOException("Incorrect pong payload size");
    }
    long pingId = dataInput.readLong();
    // Log.d(TAG, "Received pong #" + pingId + "...");
    CommonTimer timeoutTask = schedulledPings.remove(pingId);
    if (timeoutTask == null) {
        return;
    }
    timeoutTask.cancel();
    refreshTimeouts();
}
Also used : DataInput(im.actor.runtime.bser.DataInput) CommonTimer(im.actor.runtime.threading.CommonTimer) 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