Improved files download speed

This commit is contained in:
DrKLO 2014-06-20 23:34:16 +04:00
parent 70882c5d28
commit 458517e4a4
11 changed files with 398 additions and 404 deletions

View file

@ -81,7 +81,7 @@ android {
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 262
versionCode 264
versionName "1.5.7"
}
}

View file

@ -17,6 +17,7 @@ public class BuffersStorage {
private final ArrayList<ByteBufferDesc> freeBuffers4096;
private final ArrayList<ByteBufferDesc> freeBuffers16384;
private final ArrayList<ByteBufferDesc> freeBuffers32768;
private final ArrayList<ByteBufferDesc> freeBuffersBig;
private static volatile BuffersStorage Instance = null;
public static BuffersStorage getInstance() {
@ -38,6 +39,7 @@ public class BuffersStorage {
freeBuffers4096 = new ArrayList<ByteBufferDesc>();
freeBuffers16384 = new ArrayList<ByteBufferDesc>();
freeBuffers32768 = new ArrayList<ByteBufferDesc>();
freeBuffersBig = new ArrayList<ByteBufferDesc>();
for (int a = 0; a < 5; a++) {
freeBuffers128.add(new ByteBufferDesc(128));
@ -113,6 +115,17 @@ public class BuffersStorage {
buffer = new ByteBufferDesc(40000);
FileLog.e("tmessages", "create new 40000 buffer");
}
} else if (size <= 280000) {
synchronized (freeBuffersBig) {
if (freeBuffersBig.size() > 0) {
buffer = freeBuffersBig.get(0);
freeBuffersBig.remove(0);
}
}
if (buffer == null) {
buffer = new ByteBufferDesc(280000);
FileLog.e("tmessages", "create new big buffer");
}
} else {
buffer = new ByteBufferDesc(size);
}
@ -154,6 +167,12 @@ public class BuffersStorage {
freeBuffers32768.add(buffer);
}
}
} else if (buffer.buffer.capacity() == 280000) {
synchronized (freeBuffersBig) {
if (freeBuffersBig.size() < 4) {
freeBuffersBig.add(buffer);
}
}
}
}
}

View file

@ -44,6 +44,11 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
private ArrayList<RPCRequest> runningRequests = new ArrayList<RPCRequest>();
private ArrayList<Action> actionQueue = new ArrayList<Action>();
private ArrayList<Integer> unknownDatacenterIds = new ArrayList<Integer>();
private ArrayList<Integer> neededDatacenterIds = new ArrayList<Integer>();
private ArrayList<Integer> unauthorizedDatacenterIds = new ArrayList<Integer>();
final HashMap<Integer, ArrayList<NetworkMessage>> genericMessagesToDatacenters = new HashMap<Integer, ArrayList<NetworkMessage>>();
private TLRPC.TL_auth_exportedAuthorization movingAuthorization;
public static final int DEFAULT_DATACENTER_ID = Integer.MAX_VALUE;
public static final int DC_UPDATE_TIME = 60 * 60;
@ -126,15 +131,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
if (!paused) {
FileLog.e("tmessages", "pausing network and timers by sleep time = " + nextSleepTimeout);
for (Datacenter datacenter : datacenters.values()) {
if (datacenter.connection != null) {
datacenter.connection.suspendConnection(true);
}
if (datacenter.uploadConnection != null) {
datacenter.uploadConnection.suspendConnection(true);
}
if (datacenter.downloadConnection != null) {
datacenter.downloadConnection.suspendConnection(true);
}
datacenter.suspendConnections();
}
}
try {
@ -472,15 +469,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
editor.putLong("pushSessionId", pushSessionId);
ArrayList<Long> sessions = new ArrayList<Long>();
if (currentDatacenter.connection != null) {
sessions.add(currentDatacenter.connection.getSissionId());
}
if (currentDatacenter.downloadConnection != null) {
sessions.add(currentDatacenter.downloadConnection.getSissionId());
}
if (currentDatacenter.uploadConnection != null) {
sessions.add(currentDatacenter.uploadConnection.getSissionId());
}
currentDatacenter.getSessions(sessions);
if (!sessions.isEmpty()) {
SerializedData data = new SerializedData(sessions.size() * 8 + 4);
@ -539,15 +528,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
@Override
public void run() {
Datacenter datacenter = datacenterWithId(currentDatacenterId);
if (datacenter.connection != null) {
datacenter.connection.recreateSession();
}
if (datacenter.downloadConnection != null) {
datacenter.downloadConnection.recreateSession();
}
if (datacenter.uploadConnection != null) {
datacenter.uploadConnection.recreateSession();
}
datacenter.recreateSessions();
clearRequestsForRequestClass(RPCRequest.RPCRequestClassGeneric, datacenter);
clearRequestsForRequestClass(RPCRequest.RPCRequestClassDownloadMedia, datacenter);
@ -632,15 +613,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
addresses.add(ip_address);
ports.put(ip_address, port);
exist.replaceAddressesAndPorts(addresses, ports);
if (exist.connection != null) {
exist.connection.suspendConnection(true);
}
if (exist.uploadConnection != null) {
exist.uploadConnection.suspendConnection(true);
}
if (exist.downloadConnection != null) {
exist.downloadConnection.suspendConnection(true);
}
exist.suspendConnections();
updateDcSettings(dc);
}
}
@ -681,15 +654,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
for (HashMap.Entry<Integer, Datacenter> entry : datacenters.entrySet()) {
Datacenter datacenter = entry.getValue();
datacenter.overridePort = 8888;
if (datacenter.connection != null) {
datacenter.connection.suspendConnection(true);
}
if (datacenter.uploadConnection != null) {
datacenter.uploadConnection.suspendConnection(true);
}
if (datacenter.downloadConnection != null) {
datacenter.downloadConnection.suspendConnection(true);
}
datacenter.suspendConnections();
}
} else {
for (HashMap.Entry<Integer, Datacenter> entry : datacenters.entrySet()) {
@ -852,6 +817,10 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
}
public void cancelRpc(final long token, final boolean notifyServer) {
cancelRpc(token, notifyServer, false);
}
public void cancelRpc(final long token, final boolean notifyServer, final boolean ifNotSent) {
if (token == 0) {
return;
}
@ -871,6 +840,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
}
}
if (!ifNotSent) {
for (int i = 0; i < runningRequests.size(); i++) {
RPCRequest request = runningRequests.get(i);
if (request.token == token) {
@ -897,6 +867,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
FileLog.d("tmessages", "***** Warning: cancelling unknown request");
}
}
}
});
}
@ -944,117 +915,18 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
}
private void processRequestQueue(int requestClass, int _datacenterId) {
final HashMap<Integer, Integer> activeTransportTokens = new HashMap<Integer, Integer>();
final ArrayList<Integer> transportsToResume = new ArrayList<Integer>();
boolean haveNetwork = true;//isNetworkOnline();
final HashMap<Integer, Integer> activeDownloadTransportTokens = new HashMap<Integer, Integer>();
final ArrayList<Integer> downloadTransportsToResume = new ArrayList<Integer>();
genericMessagesToDatacenters.clear();
unknownDatacenterIds.clear();
neededDatacenterIds.clear();
unauthorizedDatacenterIds.clear();
final HashMap<Integer, Integer> activeUploadTransportTokens = new HashMap<Integer, Integer>();
final ArrayList<Integer> uploadTransportsToResume = new ArrayList<Integer>();
for (Datacenter datacenter : datacenters.values()) {
if (datacenter.connection != null) {
int channelToken = datacenter.connection.channelToken;
if (channelToken != 0) {
activeTransportTokens.put(datacenter.datacenterId, channelToken);
TcpConnection genericConnection = null;
Datacenter defaultDatacenter = datacenterWithId(currentDatacenterId);
if (defaultDatacenter != null) {
genericConnection = defaultDatacenter.getGenericConnection(this);
}
}
if (datacenter.downloadConnection != null) {
int channelToken = datacenter.downloadConnection.channelToken;
if (channelToken != 0) {
activeDownloadTransportTokens.put(datacenter.datacenterId, channelToken);
}
}
if (datacenter.uploadConnection != null) {
int channelToken = datacenter.uploadConnection.channelToken;
if (channelToken != 0) {
activeUploadTransportTokens.put(datacenter.datacenterId, channelToken);
}
}
}
for (RPCRequest request : runningRequests) {
if ((request.flags & RPCRequest.RPCRequestClassGeneric) != 0) {
Datacenter requestDatacenter = datacenterWithId(request.runningDatacenterId);
if (requestDatacenter != null && !activeTransportTokens.containsKey(requestDatacenter.datacenterId) && !transportsToResume.contains(requestDatacenter.datacenterId)) {
transportsToResume.add(requestDatacenter.datacenterId);
}
} else if ((request.flags & RPCRequest.RPCRequestClassDownloadMedia) != 0) {
Datacenter requestDatacenter = datacenterWithId(request.runningDatacenterId);
if (requestDatacenter != null && !activeDownloadTransportTokens.containsKey(requestDatacenter.datacenterId) && !downloadTransportsToResume.contains(requestDatacenter.datacenterId)) {
downloadTransportsToResume.add(requestDatacenter.datacenterId);
}
} else if ((request.flags & RPCRequest.RPCRequestClassUploadMedia) != 0) {
Datacenter requestDatacenter = datacenterWithId(request.runningDatacenterId);
if (requestDatacenter != null && !activeUploadTransportTokens.containsKey(requestDatacenter.datacenterId) && !uploadTransportsToResume.contains(requestDatacenter.datacenterId)) {
uploadTransportsToResume.add(requestDatacenter.datacenterId);
}
}
}
for (RPCRequest request : requestQueue) {
if ((request.flags & RPCRequest.RPCRequestClassGeneric) != 0) {
Datacenter requestDatacenter = datacenterWithId(request.runningDatacenterId);
if (requestDatacenter != null && !activeTransportTokens.containsKey(requestDatacenter.datacenterId) && !transportsToResume.contains(requestDatacenter.datacenterId)) {
transportsToResume.add(requestDatacenter.datacenterId);
}
} else if ((request.flags & RPCRequest.RPCRequestClassDownloadMedia) != 0) {
Datacenter requestDatacenter = datacenterWithId(request.runningDatacenterId);
if (requestDatacenter != null && !activeDownloadTransportTokens.containsKey(requestDatacenter.datacenterId) && !downloadTransportsToResume.contains(requestDatacenter.datacenterId)) {
downloadTransportsToResume.add(requestDatacenter.datacenterId);
}
} else if ((request.flags & RPCRequest.RPCRequestClassUploadMedia) != 0) {
Datacenter requestDatacenter = datacenterWithId(request.runningDatacenterId);
if (requestDatacenter != null && !activeUploadTransportTokens.containsKey(requestDatacenter.datacenterId) && !uploadTransportsToResume.contains(requestDatacenter.datacenterId)) {
uploadTransportsToResume.add(requestDatacenter.datacenterId);
}
}
}
boolean haveNetwork = true;//activeTransportTokens.size() != 0 || isNetworkOnline();
if (!activeTransportTokens.containsKey(currentDatacenterId) && !transportsToResume.contains(currentDatacenterId)) {
transportsToResume.add(currentDatacenterId);
}
for (int it : transportsToResume) {
Datacenter datacenter = datacenterWithId(it);
if (datacenter.authKey != null) {
if (datacenter.connection == null) {
datacenter.connection = new TcpConnection(datacenter.datacenterId);
datacenter.connection.delegate = this;
datacenter.connection.transportRequestClass = RPCRequest.RPCRequestClassGeneric;
}
datacenter.connection.connect();
}
}
for (int it : downloadTransportsToResume) {
Datacenter datacenter = datacenterWithId(it);
if (datacenter.authKey != null) {
if (datacenter.downloadConnection == null) {
datacenter.downloadConnection = new TcpConnection(datacenter.datacenterId);
datacenter.downloadConnection.delegate = this;
datacenter.downloadConnection.transportRequestClass = RPCRequest.RPCRequestClassDownloadMedia;
}
datacenter.downloadConnection.connect();
}
}
for (int it : uploadTransportsToResume) {
Datacenter datacenter = datacenterWithId(it);
if (datacenter.authKey != null) {
if (datacenter.uploadConnection == null) {
datacenter.uploadConnection = new TcpConnection(datacenter.datacenterId);
datacenter.uploadConnection.delegate = this;
datacenter.uploadConnection.transportRequestClass = RPCRequest.RPCRequestClassUploadMedia;
}
datacenter.uploadConnection.connect();
}
}
final HashMap<Integer, ArrayList<NetworkMessage>> genericMessagesToDatacenters = new HashMap<Integer, ArrayList<NetworkMessage>>();
final ArrayList<Integer> unknownDatacenterIds = new ArrayList<Integer>();
final ArrayList<Integer> neededDatacenterIds = new ArrayList<Integer>();
final ArrayList<Integer> unauthorizedDatacenterIds = new ArrayList<Integer>();
int currentTime = (int)(System.currentTimeMillis() / 1000);
for (int i = 0; i < runningRequests.size(); i++) {
@ -1107,48 +979,27 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
continue;
}
Integer tokenIt = activeTransportTokens.get(requestDatacenter.datacenterId);
int datacenterTransportToken = tokenIt != null ? tokenIt : 0;
Integer uploadTokenIt = activeUploadTransportTokens.get(requestDatacenter.datacenterId);
int datacenterUploadTransportToken = uploadTokenIt != null ? uploadTokenIt : 0;
Integer downloadTokenIt = activeDownloadTransportTokens.get(requestDatacenter.datacenterId);
int datacenterDownloadTransportToken = downloadTokenIt != null ? downloadTokenIt : 0;
double maxTimeout = 8.0;
if ((request.flags & RPCRequest.RPCRequestClassGeneric) != 0) {
if (datacenterTransportToken == 0) {
continue;
}
} else if ((request.flags & RPCRequest.RPCRequestClassDownloadMedia) != 0) {
if (!haveNetwork) {
FileLog.d("tmessages", "Don't have any network connection, skipping download request");
continue;
}
if (datacenterDownloadTransportToken == 0) {
continue;
}
maxTimeout = 40.0;
} else if ((request.flags & RPCRequest.RPCRequestClassUploadMedia) != 0) {
if (!haveNetwork) {
FileLog.d("tmessages", "Don't have any network connection, skipping upload request");
continue;
}
if (datacenterUploadTransportToken == 0) {
continue;
}
maxTimeout = 30.0;
}
float maxTimeout = 8.0f;
TcpConnection connection = null;
if ((request.flags & RPCRequest.RPCRequestClassGeneric) != 0) {
connection = requestDatacenter.connection;
connection = requestDatacenter.getGenericConnection(this);
} else if ((request.flags & RPCRequest.RPCRequestClassDownloadMedia) != 0) {
connection = requestDatacenter.downloadConnection;
int num = (request.flags & RPCRequest.RPCRequestClassDownloadMedia2) != 0 ? 1 : 0;
connection = requestDatacenter.getDownloadConnection(num, this);
} else if ((request.flags & RPCRequest.RPCRequestClassUploadMedia) != 0 ) {
connection = requestDatacenter.uploadConnection;
connection = requestDatacenter.getUploadConnection(this);
}
if ((request.flags & RPCRequest.RPCRequestClassGeneric) != 0) {
if (connection.channelToken == 0) {
continue;
}
} else {
if (!haveNetwork || connection.channelToken == 0) {
continue;
}
maxTimeout = 30.0f;
}
boolean forceThisRequest = (request.flags & requestClass) != 0 && (_datacenterId == Integer.MIN_VALUE || requestDatacenter.datacenterId == _datacenterId);
@ -1165,19 +1016,14 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
if (((Math.abs(currentTime - request.runningStartTime) > maxTimeout) && (currentTime > request.runningMinStartTime || Math.abs(currentTime - request.runningMinStartTime) > 60.0)) || forceThisRequest) {
if (!forceThisRequest && request.transportChannelToken > 0) {
if ((request.flags & RPCRequest.RPCRequestClassGeneric) != 0 && datacenterTransportToken == request.transportChannelToken) {
if ((request.flags & RPCRequest.RPCRequestClassGeneric) != 0 && request.transportChannelToken == connection.channelToken) {
FileLog.d("tmessages", "Request token is valid, not retrying " + request.rawRequest);
continue;
} else if ((request.flags & RPCRequest.RPCRequestClassDownloadMedia) != 0) {
if (datacenterDownloadTransportToken != 0 && request.transportChannelToken == datacenterDownloadTransportToken) {
} else {
if (connection.channelToken != 0 && request.transportChannelToken == connection.channelToken) {
FileLog.d("tmessages", "Request download token is valid, not retrying " + request.rawRequest);
continue;
}
} else if ((request.flags & RPCRequest.RPCRequestClassUploadMedia) != 0) {
if (datacenterUploadTransportToken != 0 && request.transportChannelToken == datacenterUploadTransportToken) {
FileLog.d("tmessages", "Request upload token is valid, not retrying " + request.rawRequest);
continue;
}
}
}
@ -1199,15 +1045,15 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
request.runningStartTime = currentTime;
if ((request.flags & RPCRequest.RPCRequestClassGeneric) != 0) {
request.transportChannelToken = datacenterTransportToken;
addMessageToDatacenter(genericMessagesToDatacenters, requestDatacenter.datacenterId, networkMessage);
request.transportChannelToken = connection.channelToken;
addMessageToDatacenter(requestDatacenter.datacenterId, networkMessage);
} else if ((request.flags & RPCRequest.RPCRequestClassDownloadMedia) != 0) {
request.transportChannelToken = datacenterDownloadTransportToken;
request.transportChannelToken = connection.channelToken;
ArrayList<NetworkMessage> arr = new ArrayList<NetworkMessage>();
arr.add(networkMessage);
proceedToSendingMessages(arr, connection, false);
} else if ((request.flags & RPCRequest.RPCRequestClassUploadMedia) != 0) {
request.transportChannelToken = datacenterUploadTransportToken;
request.transportChannelToken = connection.channelToken;
ArrayList<NetworkMessage> arr = new ArrayList<NetworkMessage>();
arr.add(networkMessage);
proceedToSendingMessages(arr, connection, false);
@ -1217,7 +1063,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
boolean updatingState = MessagesController.getInstance().updatingState;
if (activeTransportTokens.get(currentDatacenterId) != null) {
if (genericConnection != null && genericConnection.channelToken != 0) {
if (!updatingState) {
Datacenter currentDatacenter = datacenterWithId(currentDatacenterId);
@ -1234,7 +1080,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
NetworkMessage networkMessage = new NetworkMessage();
networkMessage.protoMessage = wrapMessage(destroySession, currentDatacenter.connection, false);
if (networkMessage.protoMessage != null) {
addMessageToDatacenter(genericMessagesToDatacenters, currentDatacenter.datacenterId, networkMessage);
addMessageToDatacenter(currentDatacenter.datacenterId, networkMessage);
}
}
}
@ -1303,7 +1149,17 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
continue;
}
if ((request.flags & RPCRequest.RPCRequestClassGeneric) != 0 && activeTransportTokens.get(requestDatacenter.datacenterId) == null) {
TcpConnection connection = null;
if ((request.flags & RPCRequest.RPCRequestClassGeneric) != 0) {
connection = requestDatacenter.getGenericConnection(this);
} else if ((request.flags & RPCRequest.RPCRequestClassDownloadMedia) != 0) {
int num = (request.flags & RPCRequest.RPCRequestClassDownloadMedia2) != 0 ? 1 : 0;
connection = requestDatacenter.getDownloadConnection(num, this);
} else if ((request.flags & RPCRequest.RPCRequestClassUploadMedia) != 0) {
connection = requestDatacenter.getUploadConnection(this);
}
if ((request.flags & RPCRequest.RPCRequestClassGeneric) != 0 && connection.channelToken == 0) {
continue;
}
@ -1313,40 +1169,19 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
if (request.requiresCompletion) {
if ((request.flags & RPCRequest.RPCRequestClassGeneric) != 0) {
if (genericRunningRequestCount >= 60)
if (genericRunningRequestCount >= 60) {
continue;
}
genericRunningRequestCount++;
Integer tokenIt = activeTransportTokens.get(requestDatacenter.datacenterId);
request.transportChannelToken = tokenIt != null ? tokenIt : 0;
} else if ((request.flags & RPCRequest.RPCRequestClassUploadMedia) != 0) {
if (!haveNetwork) {
FileLog.d("tmessages", "Don't have any network connection, skipping upload request");
if (!haveNetwork || uploadRunningRequestCount >= 5) {
continue;
}
if (uploadRunningRequestCount >= 5) {
continue;
}
Integer uploadTokenIt = activeUploadTransportTokens.get(requestDatacenter.datacenterId);
request.transportChannelToken = uploadTokenIt != null ? uploadTokenIt : 0;
uploadRunningRequestCount++;
} else if ((request.flags & RPCRequest.RPCRequestClassDownloadMedia) != 0) {
if (!haveNetwork) {
FileLog.d("tmessages", "Don't have any network connection, skipping download request");
if (!haveNetwork || downloadRunningRequestCount >= 5) {
continue;
}
if (downloadRunningRequestCount >= 5) {
continue;
}
Integer downloadTokenIt = activeDownloadTransportTokens.get(requestDatacenter.datacenterId);
request.transportChannelToken = downloadTokenIt != null ? downloadTokenIt : 0;
downloadRunningRequestCount++;
}
}
@ -1360,15 +1195,6 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
int requestLength = os.length();
if (requestLength != 0) {
TcpConnection connection = null;
if ((request.flags & RPCRequest.RPCRequestClassGeneric) != 0) {
connection = requestDatacenter.connection;
} else if ((request.flags & RPCRequest.RPCRequestClassDownloadMedia) != 0) {
connection = requestDatacenter.downloadConnection;
} else if ((request.flags & RPCRequest.RPCRequestClassUploadMedia) != 0) {
connection = requestDatacenter.uploadConnection;
}
if (canCompress) {
try {
byte[] data = Utilities.compress(os.toByteArray());
@ -1398,22 +1224,17 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
request.runningMessageSeqNo = networkMessage.protoMessage.seqno;
request.serializedLength = requestLength;
request.runningStartTime = (int)(System.currentTimeMillis() / 1000);
request.transportChannelToken = connection.channelToken;
if (request.requiresCompletion) {
runningRequests.add(request);
}
if ((request.flags & RPCRequest.RPCRequestClassGeneric) != 0) {
addMessageToDatacenter(genericMessagesToDatacenters, requestDatacenter.datacenterId, networkMessage);
} else if ((request.flags & RPCRequest.RPCRequestClassDownloadMedia) != 0) {
ArrayList<NetworkMessage> arr = new ArrayList<NetworkMessage>();
arr.add(networkMessage);
proceedToSendingMessages(arr, requestDatacenter.downloadConnection, false);
} else if ((request.flags & RPCRequest.RPCRequestClassUploadMedia) != 0) {
ArrayList<NetworkMessage> arr = new ArrayList<NetworkMessage>();
arr.add(networkMessage);
proceedToSendingMessages(arr, requestDatacenter.uploadConnection, false);
addMessageToDatacenter(requestDatacenter.datacenterId, networkMessage);
} else {
FileLog.e("tmessages", "***** Error: request " + request.rawRequest + " has undefined session");
ArrayList<NetworkMessage> arr = new ArrayList<NetworkMessage>();
arr.add(networkMessage);
proceedToSendingMessages(arr, connection, false);
}
} else {
FileLog.e("tmessages", "***** Couldn't serialize " + request.rawRequest);
@ -1567,11 +1388,11 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
}
}
void addMessageToDatacenter(HashMap<Integer, ArrayList<NetworkMessage>> pMap, int datacenterId, NetworkMessage message) {
ArrayList<NetworkMessage> arr = pMap.get(datacenterId);
void addMessageToDatacenter(int datacenterId, NetworkMessage message) {
ArrayList<NetworkMessage> arr = genericMessagesToDatacenters.get(datacenterId);
if (arr == null) {
arr = new ArrayList<NetworkMessage>();
pMap.put(datacenterId, arr);
genericMessagesToDatacenters.put(datacenterId, arr);
}
arr.add(message);
}
@ -2278,16 +2099,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
timeDifference = (int)((time - currentTime) / 1000 - currentPingTime / 2.0);
}
if (datacenter.connection != null) {
datacenter.connection.recreateSession();
}
if (datacenter.downloadConnection != null) {
datacenter.downloadConnection.recreateSession();
}
if (datacenter.uploadConnection != null) {
datacenter.uploadConnection.recreateSession();
}
datacenter.recreateSessions();
saveSession();
lastOutgoingMessageId = 0;
@ -2831,15 +2643,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
return;
}
if (datacenter.connection != null) {
datacenter.connection.recreateSession();
}
if (datacenter.downloadConnection != null) {
datacenter.downloadConnection.recreateSession();
}
if (datacenter.uploadConnection != null) {
datacenter.uploadConnection.recreateSession();
}
datacenter.recreateSessions();
clearRequestsForRequestClass(RPCRequest.RPCRequestClassGeneric, datacenter);
clearRequestsForRequestClass(RPCRequest.RPCRequestClassDownloadMedia, datacenter);
@ -2904,16 +2708,7 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection.
if (eactor.datacenter.datacenterId == currentDatacenterId || eactor.datacenter.datacenterId == movingToDatacenterId) {
timeDifference = (Integer)params.get("timeDifference");
if (eactor.datacenter.connection != null) {
eactor.datacenter.connection.recreateSession();
}
if (eactor.datacenter.downloadConnection != null) {
eactor.datacenter.downloadConnection.recreateSession();
}
if (eactor.datacenter.uploadConnection != null) {
eactor.datacenter.uploadConnection.recreateSession();
}
eactor.datacenter.recreateSessions();
clearRequestsForRequestClass(RPCRequest.RPCRequestClassGeneric, eactor.datacenter);
clearRequestsForRequestClass(RPCRequest.RPCRequestClassDownloadMedia, eactor.datacenter);

View file

@ -35,8 +35,8 @@ public class Datacenter {
private volatile int currentAddressNum = 0;
public TcpConnection connection;
public TcpConnection downloadConnection;
public TcpConnection uploadConnection;
private ArrayList<TcpConnection> downloadConnections = new ArrayList<TcpConnection>();
private TcpConnection uploadConnection;
public TcpConnection pushConnection;
private ArrayList<ServerSalt> authServerSaltSet = new ArrayList<ServerSalt>();
@ -319,4 +319,81 @@ public class Datacenter {
}
return false;
}
public void suspendConnections() {
if (connection != null) {
connection.suspendConnection(true);
}
if (uploadConnection != null) {
uploadConnection.suspendConnection(true);
}
for (TcpConnection downloadConnection : downloadConnections) {
downloadConnection.suspendConnection(true);
}
}
public void getSessions(ArrayList<Long> sessions) {
if (connection != null) {
sessions.add(connection.getSissionId());
}
if (uploadConnection != null) {
sessions.add(uploadConnection.getSissionId());
}
for (TcpConnection downloadConnection : downloadConnections) {
sessions.add(downloadConnection.getSissionId());
}
}
public void recreateSessions() {
if (connection != null) {
connection.recreateSession();
}
if (uploadConnection != null) {
uploadConnection.recreateSession();
}
for (TcpConnection downloadConnection : downloadConnections) {
downloadConnection.recreateSession();
}
}
public TcpConnection getDownloadConnection(int num, TcpConnection.TcpConnectionDelegate delegate) {
if (num >= 0 && authKey != null) {
TcpConnection downloadConnection = null;
if (num < downloadConnections.size()) {
downloadConnection = downloadConnections.get(num);
} else {
downloadConnection = new TcpConnection(datacenterId);
downloadConnection.delegate = delegate;
downloadConnection.transportRequestClass = RPCRequest.RPCRequestClassDownloadMedia;
downloadConnections.add(downloadConnection);
}
downloadConnection.connect();
return downloadConnection;
}
return null;
}
public TcpConnection getUploadConnection(TcpConnection.TcpConnectionDelegate delegate) {
if (authKey != null) {
if (uploadConnection == null) {
uploadConnection = new TcpConnection(datacenterId);
uploadConnection.delegate = delegate;
uploadConnection.transportRequestClass = RPCRequest.RPCRequestClassUploadMedia;
}
uploadConnection.connect();
}
return uploadConnection;
}
public TcpConnection getGenericConnection(TcpConnection.TcpConnectionDelegate delegate) {
if (authKey != null) {
if (connection == null) {
connection = new TcpConnection(datacenterId);
connection.delegate = delegate;
connection.transportRequestClass = RPCRequest.RPCRequestClassGeneric;
}
connection.connect();
}
return connection;
}
}

View file

@ -25,7 +25,7 @@ import java.nio.channels.FileChannel;
import java.util.Scanner;
public class FileLoadOperation {
private int downloadChunkSize = 1024 * 32;
private int downloadChunkSize = 1024 * 256;
public int datacenter_id;
public TLRPC.InputFileLocation location;
@ -38,6 +38,13 @@ public class FileLoadOperation {
private byte[] key;
private byte[] iv;
private long requestToken = 0;
private long requestToken2 = 0;
private int requestProgress = 0;
private int requestProgress2 = 0;
private int nextDownloadOffset = 0;
private TLRPC.TL_upload_file delayedRes = null;
private int delayedResOffset = 0;
private int delayedResTokenNum = 0;
private File cacheFileTemp;
private File cacheFileFinal;
@ -335,7 +342,7 @@ public class FileLoadOperation {
cacheFileTemp = new File(Utilities.getCacheDir(), fileNameTemp);
if (cacheFileTemp.exists()) {
downloadedBytes = (int)cacheFileTemp.length();
downloadedBytes = downloadedBytes / 1024 * 1024;
nextDownloadOffset = downloadedBytes = downloadedBytes / 1024 * 1024;
}
if (fileNameIv != null) {
cacheIvTemp = new File(Utilities.getCacheDir(), fileNameIv);
@ -387,8 +394,9 @@ public class FileLoadOperation {
}
state = 2;
cleanup();
if (httpUrl == null && requestToken != 0) {
if (httpUrl == null) {
ConnectionsManager.getInstance().cancelRpc(requestToken, true);
ConnectionsManager.getInstance().cancelRpc(requestToken2, true);
}
delegate.didFailedLoadingFile(FileLoadOperation.this);
}
@ -422,6 +430,12 @@ public class FileLoadOperation {
} catch (Exception e) {
FileLog.e("tmessages", e);
}
if (delayedRes != null) {
delayedRes.disableFree = false;
delayedRes.freeResources();
delayedRes = null;
}
}
}
@ -593,26 +607,26 @@ public class FileLoadOperation {
}
}
private void startDownloadRequest() {
if (state != 1) {
return;
}
TLRPC.TL_upload_getFile req = new TLRPC.TL_upload_getFile();
req.location = location;
//if (totalBytesCount == -1) {
// req.offset = 0;
// req.limit = 0;
//} else {
req.offset = downloadedBytes;
req.limit = downloadChunkSize;
//}
requestToken = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
@Override
public void run(TLObject response, TLRPC.TL_error error) {
requestToken = 0;
private void processRequestResult(TLRPC.TL_upload_file res, TLRPC.TL_error error, int dowloadOffset, int tokenNum) {
if (error == null) {
TLRPC.TL_upload_file res = (TLRPC.TL_upload_file)response;
try {
if (downloadedBytes != dowloadOffset) {
if (delayedRes != null) {
FileLog.e("tmessages", "something went wrong!");
}
delayedRes = res;
delayedRes.disableFree = true;
delayedResOffset = dowloadOffset;
delayedResTokenNum = tokenNum;
return;
} else {
if (tokenNum == 0) {
requestToken = 0;
} else if (tokenNum == 1) {
requestToken2 = 0;
}
}
if (res.bytes.limit() == 0) {
onFinishLoadingFile();
return;
@ -629,9 +643,20 @@ public class FileLoadOperation {
fiv.write(iv);
}
downloadedBytes += res.bytes.limit();
if (totalBytesCount > 0) {
if (totalBytesCount > 0 && state == 1) {
delegate.didChangedLoadProgress(FileLoadOperation.this, Math.min(1.0f, (float)downloadedBytes / (float)totalBytesCount));
}
if(delayedRes != null && res != delayedRes) {
TLRPC.TL_upload_file temp = delayedRes;
processRequestResult(temp, null, delayedResOffset, delayedResTokenNum);
if (delayedRes != null) {
delayedRes.disableFree = false;
delayedRes.freeResources();
delayedRes = null;
}
}
if (downloadedBytes % downloadChunkSize == 0 || totalBytesCount > 0 && totalBytesCount != downloadedBytes) {
startDownloadRequest();
} else {
@ -658,6 +683,7 @@ public class FileLoadOperation {
delegate.didFailedLoadingFile(FileLoadOperation.this);
} else {
datacenter_id = val;
nextDownloadOffset = 0;
startDownloadRequest();
}
} else if (error.text.contains("OFFSET_INVALID")) {
@ -682,15 +708,70 @@ public class FileLoadOperation {
}
}
}
private void processRequestProgress() {
delegate.didChangedLoadProgress(FileLoadOperation.this, Math.min(1.0f, (float) (downloadedBytes + requestProgress + requestProgress2) / (float) totalBytesCount));
}
private void startDownloadRequest() {
if (state != 1) {
return;
}
if (requestToken == 0) {
requestProgress = 0;
if (totalBytesCount > 0 && nextDownloadOffset >= totalBytesCount) {
return;
}
final TLRPC.TL_upload_getFile req = new TLRPC.TL_upload_getFile();
req.location = location;
req.offset = nextDownloadOffset;
req.limit = downloadChunkSize;
nextDownloadOffset += downloadChunkSize;
final long time = System.currentTimeMillis();
requestToken = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
@Override
public void run(TLObject response, TLRPC.TL_error error) {
processRequestResult((TLRPC.TL_upload_file) response, error, req.offset, 0);
}
}, new RPCRequest.RPCProgressDelegate() {
@Override
public void progress(int length, int progress) {
if (totalBytesCount > 0) {
delegate.didChangedLoadProgress(FileLoadOperation.this, Math.min(1.0f, (float)(downloadedBytes + progress) / (float)totalBytesCount));
} else if (totalBytesCount == -1) {
delegate.didChangedLoadProgress(FileLoadOperation.this, Math.min(1.0f, (float)(progress) / (float)length));
if (state == 1) {
requestProgress = progress;
if (totalBytesCount == -1) {
delegate.didChangedLoadProgress(FileLoadOperation.this, Math.min(1.0f, (float) (progress) / (float) length));
} else if (totalBytesCount > 0) {
processRequestProgress();
}
}
}
}, null, true, RPCRequest.RPCRequestClassDownloadMedia, datacenter_id);
}
if (totalBytesCount > 0 && requestToken2 == 0) {
requestProgress2 = 0;
if (totalBytesCount > 0 && nextDownloadOffset >= totalBytesCount) {
return;
}
final long time = System.currentTimeMillis();
final TLRPC.TL_upload_getFile req = new TLRPC.TL_upload_getFile();
req.location = location;
req.offset = nextDownloadOffset;
req.limit = downloadChunkSize;
nextDownloadOffset += downloadChunkSize;
requestToken2 = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
@Override
public void run(TLObject response, TLRPC.TL_error error) {
processRequestResult((TLRPC.TL_upload_file) response, error, req.offset, 1);
}
}, new RPCRequest.RPCProgressDelegate() {
@Override
public void progress(int length, int progress) {
if (state == 1) {
requestProgress2 = progress;
processRequestProgress();
}
}
}, null, true, RPCRequest.RPCRequestClassDownloadMedia | RPCRequest.RPCRequestClassDownloadMedia2, datacenter_id);
}
}
}

View file

@ -52,9 +52,9 @@ public class HandshakeAction extends Action implements TcpConnection.TcpConnecti
void beginHandshake(boolean dropConnection) {
if (datacenter.connection == null) {
datacenter.connection = new TcpConnection(datacenter.datacenterId);
datacenter.connection.delegate = this;
datacenter.connection.transportRequestClass = RPCRequest.RPCRequestClassGeneric;
}
datacenter.connection.delegate = this;
processedMessageIds = new ArrayList<Long>();
authNonce = null;
@ -566,8 +566,14 @@ public class HandshakeAction extends Action implements TcpConnection.TcpConnecti
}
@Override
public void tcpConnectionClosed(TcpConnection connection) {
public void tcpConnectionClosed(final TcpConnection connection) {
wasDisconnect = true;
Utilities.stageQueue.postRunnable(new Runnable() {
@Override
public void run() {
connection.connect();
}
}, 1000);
}
@Override
@ -591,9 +597,7 @@ public class HandshakeAction extends Action implements TcpConnection.TcpConnecti
@Override
public void tcpConnectionReceivedData(TcpConnection connection, ByteBufferDesc data, int length) {
long keyId = data.readInt64();
if (keyId == 0) {
long messageId = data.readInt64();
if (processedMessageIds.contains(messageId)) {

View file

@ -29,6 +29,7 @@ public class RPCRequest {
public static int RPCRequestClassCanCompress = 32;
public static int RPCRequestClassPush = 64;
public static int RPCRequestClassWithoutLogin = 128;
public static int RPCRequestClassDownloadMedia2 = 256;
static int RPCRequestClassTransportMask = (RPCRequestClassGeneric | RPCRequestClassDownloadMedia | RPCRequestClassUploadMedia);

View file

@ -9,6 +9,8 @@
package org.telegram.messenger;
public class TLObject {
public boolean disableFree = false;
public TLObject () {
}

View file

@ -8136,6 +8136,9 @@ public class TLRPC {
@Override
public void freeResources() {
if (disableFree) {
return;
}
if (bytes != null) {
BuffersStorage.getInstance().reuseFreeBuffer(bytes);
bytes = null;
@ -8287,6 +8290,9 @@ public class TLRPC {
@Override
public void freeResources() {
if (disableFree) {
return;
}
if (result != null) {
result.freeResources();
}
@ -9171,6 +9177,9 @@ public class TLRPC {
@Override
public void freeResources() {
if (disableFree) {
return;
}
if (bytes != null) {
BuffersStorage.getInstance().reuseFreeBuffer(bytes);
bytes = null;
@ -9198,6 +9207,9 @@ public class TLRPC {
@Override
public void freeResources() {
if (disableFree) {
return;
}
if (bytes != null) {
BuffersStorage.getInstance().reuseFreeBuffer(bytes);
bytes = null;

View file

@ -40,7 +40,6 @@ import org.telegram.messenger.ScreenReceiver;
import org.telegram.messenger.UserConfig;
import org.telegram.messenger.Utilities;
import java.util.Calendar;
import java.util.concurrent.atomic.AtomicInteger;
public class ApplicationLoader extends Application {
@ -151,10 +150,14 @@ public class ApplicationLoader extends Application {
applicationContext.startService(new Intent(applicationContext, NotificationsService.class));
if (android.os.Build.VERSION.SDK_INT >= 19) {
Calendar cal = Calendar.getInstance();
// Calendar cal = Calendar.getInstance();
// PendingIntent pintent = PendingIntent.getService(applicationContext, 0, new Intent(applicationContext, NotificationsService.class), 0);
// AlarmManager alarm = (AlarmManager) applicationContext.getSystemService(Context.ALARM_SERVICE);
// alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30000, pintent);
PendingIntent pintent = PendingIntent.getService(applicationContext, 0, new Intent(applicationContext, NotificationsService.class), 0);
AlarmManager alarm = (AlarmManager) applicationContext.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30000, pintent);
AlarmManager alarm = (AlarmManager)applicationContext.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pintent);
}
} else {
stopPushService();

View file

@ -1624,7 +1624,7 @@ public class PhotoViewer implements NotificationCenter.NotificationCenterDelegat
currentPathObject = null;
currentThumb = null;
centerImage.setImageBitmap((Bitmap)null);
leftImage.setImageBitmap((Bitmap) null);
leftImage.setImageBitmap((Bitmap)null);
rightImage.setImageBitmap((Bitmap)null);
if (android.os.Build.VERSION.SDK_INT >= 11 && object != null) {
object.imageReceiver.setVisible(true, true);