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");
}
}
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;
}
}
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();
}
}
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();
}
}
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");
}
Aggregations