Optimized client
This commit is contained in:
parent
319df71154
commit
a8800af2b6
|
@ -1,8 +1,7 @@
|
||||||
package it.tdlight.tdlight;
|
package it.tdlight.tdlight;
|
||||||
|
|
||||||
import it.tdlight.tdlib.TdApi.Object;
|
|
||||||
import it.tdlight.tdlib.NativeClient;
|
import it.tdlight.tdlib.NativeClient;
|
||||||
import java.util.ArrayList;
|
import it.tdlight.tdlib.TdApi.Object;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
@ -13,7 +12,8 @@ import java.util.concurrent.locks.StampedLock;
|
||||||
*/
|
*/
|
||||||
public class Client extends NativeClient implements TelegramClient {
|
public class Client extends NativeClient implements TelegramClient {
|
||||||
private long clientId;
|
private long clientId;
|
||||||
private final ReentrantLock receiveLock = new ReentrantLock();
|
private final ReentrantLock receiveResponsesLock = new ReentrantLock();
|
||||||
|
private final ReentrantLock receiveUpdatesLock = new ReentrantLock();
|
||||||
private final StampedLock executionLock = new StampedLock();
|
private final StampedLock executionLock = new StampedLock();
|
||||||
private volatile Long stampedLockValue;
|
private volatile Long stampedLockValue;
|
||||||
|
|
||||||
|
@ -39,43 +39,63 @@ public class Client extends NativeClient implements TelegramClient {
|
||||||
nativeClientSend(this.clientId, request.getId(), request.getFunction());
|
nativeClientSend(this.clientId, request.getId(), request.getFunction());
|
||||||
}
|
}
|
||||||
|
|
||||||
private long[] eventIds;
|
private final long[][] eventIds = new long[8][];
|
||||||
private Object[] events;
|
private final Object[][] events = new Object[8][];
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Response> receive(double timeout, int eventSize, boolean receiveResponses, boolean receiveUpdates) {
|
public List<Response> receive(double timeout, int eventsSize, boolean receiveResponses, boolean receiveUpdates) {
|
||||||
|
return Arrays.asList(this.receive(timeout, eventsSize, receiveResponses, receiveUpdates, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Response[] receive(double timeout, int eventsSize, boolean receiveResponses, boolean receiveUpdates, boolean singleResponse) {
|
||||||
if (this.executionLock.isWriteLocked()) {
|
if (this.executionLock.isWriteLocked()) {
|
||||||
throw new IllegalStateException("ClientActor is destroyed");
|
throw new IllegalStateException("ClientActor is destroyed");
|
||||||
}
|
}
|
||||||
|
int group = (singleResponse ? 0b100 : 0b000) | (receiveResponses ? 0b010 : 0b000) | (receiveUpdates ? 0b001 : 0b000);
|
||||||
|
|
||||||
ArrayList<Response> responseList = new ArrayList<>();
|
if (eventIds[group] == null) {
|
||||||
if (eventIds == null) {
|
eventIds[group] = new long[eventsSize];
|
||||||
eventIds = new long[eventSize];
|
|
||||||
events = new Object[eventSize];
|
|
||||||
} else if (eventIds.length != eventSize) {
|
|
||||||
throw new IllegalArgumentException("EventSize can't change! Previous value = " + eventIds.length + " New value = " + eventSize);
|
|
||||||
} else {
|
} else {
|
||||||
Arrays.fill(eventIds, 0);
|
Arrays.fill(eventIds[group], 0);
|
||||||
Arrays.fill(events, null);
|
}
|
||||||
|
if (events[group] == null) {
|
||||||
|
events[group] = new Object[eventsSize];
|
||||||
|
} else {
|
||||||
|
Arrays.fill(events[group], null);
|
||||||
|
}
|
||||||
|
if (eventIds[group].length != eventsSize || events[group].length != eventsSize) {
|
||||||
|
throw new IllegalArgumentException("EventSize can't change over time!"
|
||||||
|
+ " Previous: " + eventIds[group].length + " Current: " + eventsSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.receiveLock.isLocked()) {
|
if (receiveResponses && this.receiveResponsesLock.isLocked()) {
|
||||||
|
throw new IllegalThreadStateException("Thread: " + Thread.currentThread().getName() + " trying receive incoming responses but shouldn't be called simultaneously from two different threads!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (receiveUpdates && this.receiveUpdatesLock.isLocked()) {
|
||||||
throw new IllegalThreadStateException("Thread: " + Thread.currentThread().getName() + " trying receive incoming updates but shouldn't be called simultaneously from two different threads!");
|
throw new IllegalThreadStateException("Thread: " + Thread.currentThread().getName() + " trying receive incoming updates but shouldn't be called simultaneously from two different threads!");
|
||||||
}
|
}
|
||||||
|
|
||||||
int resultSize;
|
int resultSize;
|
||||||
this.receiveLock.lock();
|
if (receiveResponses) this.receiveResponsesLock.lock();
|
||||||
try {
|
try {
|
||||||
resultSize = nativeClientReceive(this.clientId, eventIds, events, timeout, receiveResponses, receiveUpdates);
|
if (receiveUpdates) this.receiveUpdatesLock.lock();
|
||||||
|
try {
|
||||||
|
resultSize = nativeClientReceive(this.clientId, eventIds[group], events[group], timeout, receiveResponses, receiveUpdates);
|
||||||
} finally {
|
} finally {
|
||||||
this.receiveLock.unlock();
|
if (receiveUpdates) this.receiveUpdatesLock.unlock();
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
if (receiveResponses) this.receiveResponsesLock.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
Response[] responses = new Response[resultSize];
|
||||||
|
|
||||||
for (int i = 0; i < resultSize; i++) {
|
for (int i = 0; i < resultSize; i++) {
|
||||||
responseList.add(new Response(eventIds[i], events[i]));
|
responses[i] = new Response(eventIds[group][i], events[group][i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return responseList;
|
return responses;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -84,13 +104,13 @@ public class Client extends NativeClient implements TelegramClient {
|
||||||
throw new IllegalStateException("ClientActor is destroyed");
|
throw new IllegalStateException("ClientActor is destroyed");
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Response> responseList = receive(timeout, 1, receiveResponses, receiveUpdates);
|
Response[] responses = receive(timeout, 1, receiveResponses, receiveUpdates, true);
|
||||||
|
|
||||||
if (responseList.size() < 1) {
|
if (responses.length != 0) {
|
||||||
return null;
|
return responses[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
return responseList.get(0);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue
Block a user